SwfdecAsFunction

SwfdecAsFunction — script objects that can be executed

Synopsis


#include <swfdec/swfdec.h>


                    SwfdecAsFunction;
void                (*SwfdecAsNative)                   (SwfdecAsContext *context,
                                                         SwfdecAsObject *thisp,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         SwfdecAsValue *retval);
                    SwfdecAsNativeFunction;
void                swfdec_as_function_call             (SwfdecAsFunction *function,
                                                         SwfdecAsObject *thisp,
                                                         guint n_args,
                                                         const SwfdecAsValue *args,
                                                         SwfdecAsValue *return_value);
SwfdecAsFunction*   swfdec_as_native_function_new       (SwfdecAsContext *context,
                                                         const char *name,
                                                         SwfdecAsNative native,
                                                         guint min_args,
                                                         SwfdecAsObject *prototype);
void                swfdec_as_native_function_set_construct_type
                                                        (SwfdecAsNativeFunction *function,
                                                         GType type);
void                swfdec_as_native_function_set_object_type
                                                        (SwfdecAsNativeFunction *function,
                                                         GType type);
gboolean            swfdec_as_native_function_check     (SwfdecAsContext *cx,
                                                         SwfdecAsObject *object,
                                                         GType type,
                                                         gpointer *result,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         const char *args,
                                                         ...);
gboolean            swfdec_as_native_function_checkv    (SwfdecAsContext *cx,
                                                         SwfdecAsObject *object,
                                                         GType type,
                                                         gpointer *result,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         const char *args,
                                                         va_list varargs);
#define             SWFDEC_AS_CHECK                     (type,result,...)

Object Hierarchy


  GObject
   +----SwfdecAsObject
         +----SwfdecAsFunction
               +----SwfdecAsNativeFunction

  GObject
   +----SwfdecAsObject
         +----SwfdecAsFunction
               +----SwfdecAsNativeFunction

Description

Functions is the basic object for executing code in the Swfdec script engine. There is multiple different variants of functions, such as script-created ones and native functions.

If you want to create your own functions, you should create native functions. The easiest way to do this is with swfdec_as_object_add_function() or swfdec_as_native_function_new().

In Actionscript, every function can be used as a constructor. If you want to make a native function be used as a constructor for your own SwfdecAsObject subclass, have a look at swfdec_as_native_function_set_construct_type().

Details

SwfdecAsFunction

typedef struct _SwfdecAsFunction SwfdecAsFunction;

This is the base executable object in Swfdec. It is an abstract object. If you want to create functions yourself, use SwfdecAsNativeFunction.


SwfdecAsNative ()

void                (*SwfdecAsNative)                   (SwfdecAsContext *context,
                                                         SwfdecAsObject *thisp,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         SwfdecAsValue *retval);

This is the prototype for all native functions.

context : SwfdecAsContext
thisp : the this object.

Warning

Can be NULL.
argc : number of arguments passed to this function
argv : the argc arguments passed to this function
retval : set to the return value. Initialized to undefined by default

SwfdecAsNativeFunction

typedef struct _SwfdecAsNativeFunction SwfdecAsNativeFunction;

This is the object type for native functions.


swfdec_as_function_call ()

void                swfdec_as_function_call             (SwfdecAsFunction *function,
                                                         SwfdecAsObject *thisp,
                                                         guint n_args,
                                                         const SwfdecAsValue *args,
                                                         SwfdecAsValue *return_value);

Calls the given function. This means a SwfdecAsFrame is created for the function and pushed on top of the execution stack. The function is however not executed. Call swfdec_as_context_run() to execute it.

function : the SwfdecAsFunction to call
thisp : this argument to use for the call or NULL for none
n_args : number of arguments to pass to the function
args : the arguments to pass or NULL to read the last n_args stack elements. The memory must be unchanged until the function call has completed. This is after the call to swfdec_as_context_run() has finished.
return_value : pointer for return value or NULL to push the return value to the stack

swfdec_as_native_function_new ()

SwfdecAsFunction*   swfdec_as_native_function_new       (SwfdecAsContext *context,
                                                         const char *name,
                                                         SwfdecAsNative native,
                                                         guint min_args,
                                                         SwfdecAsObject *prototype);

Creates a new native function, that will execute native when called. The min_args parameter sets a requirement for the minimum number of arguments to pass to native. If the function gets called with less arguments, it will just redurn undefined. You might want to use swfdec_as_object_add_function() instead of this function.

context : a SwfdecAsContext
name : name of the function
native : function to call when executed
min_args : minimum number of arguments required
prototype : The object to be used as "prototype" property for the created function or NULL for none.
Returns : a new SwfdecAsFunction or NULL on OOM

swfdec_as_native_function_set_construct_type ()

void                swfdec_as_native_function_set_construct_type
                                                        (SwfdecAsNativeFunction *function,
                                                         GType type);

Sets the type to be used when using function as a constructor. If this is not set, using function as a constructor will create a SwfdecAsObject.

function : a SwfdecAsNativeFunction
type : GType used when constructing an object with function

swfdec_as_native_function_set_object_type ()

void                swfdec_as_native_function_set_object_type
                                                        (SwfdecAsNativeFunction *function,
                                                         GType type);

Sets the required type for the this object to type. If the this object isn't of the required type, the function will not be called and its return value will be undefined.

function : a SwfdecAsNativeFunction
type : required GType for the this object

swfdec_as_native_function_check ()

gboolean            swfdec_as_native_function_check     (SwfdecAsContext *cx,
                                                         SwfdecAsObject *object,
                                                         GType type,
                                                         gpointer *result,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         const char *args,
                                                         ...);

This function is a convenience function to validate and convert arguments to a native function while avoiding common pitfalls. You typically want to call it at the beginning of every native function you write. Or you can use the SWFDEC_AS_CHECK() macro instead which calls this function. The cx, object, argc and argv paramters should be passed verbatim from the function call to your native function. If type is not 0, object is then checked to be of that type and cast to result. After that the args string is used to convert the arguments. Every character in args describes the conversion of one argument. For that argument, you have to pass a pointer that takes the value. For the conversion, the default conversion functions like swfdec_as_value_to_string() are used. If not enough arguments are available, the function stops converting and returns NULL. The following conversion characters are allowed:

  • "b": convert to boolean. Requires a gboolean pointer

  • "i": convert to integer. Requires an integer pointer

  • "n": convert to number. Requires a double pointer

  • "o": convert to object. Requires a SwfdecAsObject pointer. If the conversion fails, this function immediately return % FALSE.

  • "O": convert to object or NULL. Requires a SwfdecAsObject pointer.

  • "s": convert to garbage-collected string. Requires a const char pointer

  • "v": copy the value. The given argument must be a pointer to a SwfdecAsValue

  • "|": optional arguments follow. Optional arguments will be initialized to the empty value for their type. This conversion character is only allowed once in the conversion string.

cx : a SwfdecAsContext
object : this object passed to the native function
type : expected type of object or 0 for any
result : pointer to variable taking cast result of object
argc : count of arguments passed to the function
argv : arguments passed to the function
args : argument conversion string
... : pointers to variables taking converted arguments
Returns : TRUE if the conversion succeeded, FALSE otherwise

swfdec_as_native_function_checkv ()

gboolean            swfdec_as_native_function_checkv    (SwfdecAsContext *cx,
                                                         SwfdecAsObject *object,
                                                         GType type,
                                                         gpointer *result,
                                                         guint argc,
                                                         SwfdecAsValue *argv,
                                                         const char *args,
                                                         va_list varargs);

This is the valist version of swfdec_as_native_function_check(). See that function for details.

cx : a SwfdecAsContext
object : this object passed to the native function
type : expected type of object
result : pointer to variable taking cast result of object
argc : count of arguments passed to the function
argv : arguments passed to the function
args : argument conversion string
varargs : pointers to variables taking converted arguments
Returns : TRUE if the conversion succeeded, FALSE otherwise

SWFDEC_AS_CHECK()

#define             SWFDEC_AS_CHECK(type,result,...)

This is a shortcut macro for calling swfdec_as_native_function_check() at the beginning of a native function. See that function for details. It requires the native function parameters to have the default name. So your function must be declared like this:

static void
my_function (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval);

type : required type of this object or 0 for ignoring
result : converted this object
... : conversion string and pointers taking converted values