JSCClass

JSCClass — JavaScript custom class

Functions

Properties

JSCContext * context Read / Write / Construct Only
gchar * name Read / Write / Construct Only
JSCClass * parent Read / Write / Construct Only

Types and Values

Object Hierarchy

    GObject
    ╰── JSCClass

Description

A JSSClass represents a custom JavaScript class registered by the user in a JSCContext. It allows to create new JavaScripts objects whose instances are created by the user using this API. It's possible to add constructors, properties and methods for a JSSClass by providing GCallbacks to implement them.

Functions

JSCClassGetPropertyFunction ()

JSCValue *
(*JSCClassGetPropertyFunction) (JSCClass *jsc_class,
                                JSCContext *context,
                                gpointer instance,
                                const char *name);

The type of get_property in JSCClassVTable. This is only required when you need to handle external properties not added to the prototype.

Parameters

jsc_class

a JSCClass

 

context

a JSCContext

 

instance

the jsc_class instance

 

name

the property name

 

Returns

a JSCValue or NULL to forward the request to the parent class or prototype chain.

[transfer full][nullable]


JSCClassSetPropertyFunction ()

gboolean
(*JSCClassSetPropertyFunction) (JSCClass *jsc_class,
                                JSCContext *context,
                                gpointer instance,
                                const char *name,
                                JSCValue *value);

The type of set_property in JSCClassVTable. This is only required when you need to handle external properties not added to the prototype.

Parameters

jsc_class

a JSCClass

 

context

a JSCContext

 

instance

the jsc_class instance

 

name

the property name

 

value

the JSCValue to set

 

Returns

TRUE if handled or FALSE to forward the request to the parent class or prototype chain.


JSCClassHasPropertyFunction ()

gboolean
(*JSCClassHasPropertyFunction) (JSCClass *jsc_class,
                                JSCContext *context,
                                gpointer instance,
                                const char *name);

The type of has_property in JSCClassVTable. This is only required when you need to handle external properties not added to the prototype.

Parameters

jsc_class

a JSCClass

 

context

a JSCContext

 

instance

the jsc_class instance

 

name

the property name

 

Returns

TRUE if instance has a property with name or FALSE to forward the request to the parent class or prototype chain.


JSCClassDeletePropertyFunction ()

gboolean
(*JSCClassDeletePropertyFunction) (JSCClass *jsc_class,
                                   JSCContext *context,
                                   gpointer instance,
                                   const char *name);

The type of delete_property in JSCClassVTable. This is only required when you need to handle external properties not added to the prototype.

Parameters

jsc_class

a JSCClass

 

context

a JSCContext

 

instance

the jsc_class instance

 

name

the property name

 

Returns

TRUE if handled or FALSE to to forward the request to the parent class or prototype chain.


JSCClassEnumeratePropertiesFunction ()

gchar **
(*JSCClassEnumeratePropertiesFunction)
                               (JSCClass *jsc_class,
                                JSCContext *context,
                                gpointer instance);

The type of enumerate_properties in JSCClassVTable. This is only required when you need to handle external properties not added to the prototype.

Parameters

jsc_class

a JSCClass

 

context

a JSCContext

 

instance

the jsc_class instance

 

Returns

a NULL-terminated array of strings containing the property names, or NULL if instance doesn't have enumerable properties.

[array zero-terminated=1][transfer full][nullable]


jsc_class_get_name ()

const char *
jsc_class_get_name (JSCClass *jsc_class);

Get the class name of jsc_class

Parameters

jsc_class

a JSCClass

 

Returns

the name of jsc_class .

[transfer none]


jsc_class_get_parent ()

JSCClass *
jsc_class_get_parent (JSCClass *jsc_class);

Get the parent class of jsc_class

Parameters

jsc_class

a JSCClass

 

Returns

the parent class of jsc_class .

[transfer none]


jsc_class_add_constructor ()

JSCValue *
jsc_class_add_constructor (JSCClass *jsc_class,
                           const char *name,
                           GCallback callback,
                           gpointer user_data,
                           GDestroyNotify destroy_notify,
                           GType return_type,
                           guint n_params,
                           ...);

Add a constructor to jsc_class . If name is NULL, the class name will be used. When new is used with the constructor or jsc_value_constructor_call() is called, callback is invoked receiving the parameters and user_data as the last parameter. When the constructor object is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use jsc_context_set_value() to make the constructor available in the global object.

Note that the value returned by callback is adopted by jsc_class , and the GDestroyNotify passed to jsc_context_register_class() is responsible for disposing of it.

[skip]

Parameters

jsc_class

a JSCClass

 

name

the constructor name or NULL.

[nullable]

callback

a GCallback to be called to create an instance of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the constructor return value

 

n_params

the number of parameter types to follow or 0 if constructor doesn't receive parameters.

 

...

a list of GTypes, one for each parameter.

 

Returns

a JSCValue representing the class constructor.

[transfer full]


jsc_class_add_constructorv ()

JSCValue *
jsc_class_add_constructorv (JSCClass *jsc_class,
                            const char *name,
                            GCallback callback,
                            gpointer user_data,
                            GDestroyNotify destroy_notify,
                            GType return_type,
                            guint n_parameters,
                            GType *parameter_types);

Add a constructor to jsc_class . If name is NULL, the class name will be used. When new is used with the constructor or jsc_value_constructor_call() is called, callback is invoked receiving the parameters and user_data as the last parameter. When the constructor object is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use jsc_context_set_value() to make the constructor available in the global object.

Note that the value returned by callback is adopted by jsc_class , and the GDestroyNotify passed to jsc_context_register_class() is responsible for disposing of it.

[rename-to jsc_class_add_constructor]

Parameters

jsc_class

a JSCClass

 

name

the constructor name or NULL.

[nullable]

callback

a GCallback to be called to create an instance of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the constructor return value

 

n_parameters

the number of parameters

 

parameter_types

a list of GTypes, one for each parameter, or NULL.

[nullable][array length=n_parameters][element-type GType]

Returns

a JSCValue representing the class constructor.

[transfer full]


jsc_class_add_constructor_variadic ()

JSCValue *
jsc_class_add_constructor_variadic (JSCClass *jsc_class,
                                    const char *name,
                                    GCallback callback,
                                    gpointer user_data,
                                    GDestroyNotify destroy_notify,
                                    GType return_type);

Add a constructor to jsc_class . If name is NULL, the class name will be used. When new is used with the constructor or jsc_value_constructor_call() is called, callback is invoked receiving a GPtrArray of JSCValues as arguments and user_data as the last parameter. When the constructor object is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use jsc_context_set_value() to make the constructor available in the global object.

Note that the value returned by callback is adopted by jsc_class , and the GDestroyNotify passed to jsc_context_register_class() is responsible for disposing of it.

Parameters

jsc_class

a JSCClass

 

name

the constructor name or NULL.

[nullable]

callback

a GCallback to be called to create an instance of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the constructor return value

 

Returns

a JSCValue representing the class constructor.

[transfer full]


jsc_class_add_method ()

void
jsc_class_add_method (JSCClass *jsc_class,
                      const char *name,
                      GCallback callback,
                      gpointer user_data,
                      GDestroyNotify destroy_notify,
                      GType return_type,
                      guint n_params,
                      ...);

Add method with name to jsc_class . When the method is called by JavaScript or jsc_value_object_invoke_method(), callback is called receiving the class instance as first parameter, followed by the method parameters and then user_data as last parameter. When the method is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

Note that the value returned by callback must be transfer full. In case of non-refcounted boxed types, you should use G_TYPE_POINTER instead of the actual boxed GType to ensure that the instance owned by JSCClass is used. If you really want to return a new copy of the boxed type, use JSC_TYPE_VALUE and return a JSCValue created with jsc_value_new_object() that receives the copy as the instance parameter.

[skip]

Parameters

jsc_class

a JSCClass

 

name

the method name

 

callback

a GCallback to be called to invoke method name of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the method return value, or G_TYPE_NONE if the method is void.

 

n_params

the number of parameter types to follow or 0 if the method doesn't receive parameters.

 

...

a list of GTypes, one for each parameter.

 

jsc_class_add_methodv ()

void
jsc_class_add_methodv (JSCClass *jsc_class,
                       const char *name,
                       GCallback callback,
                       gpointer user_data,
                       GDestroyNotify destroy_notify,
                       GType return_type,
                       guint n_parameters,
                       GType *parameter_types);

Add method with name to jsc_class . When the method is called by JavaScript or jsc_value_object_invoke_method(), callback is called receiving the class instance as first parameter, followed by the method parameters and then user_data as last parameter. When the method is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

Note that the value returned by callback must be transfer full. In case of non-refcounted boxed types, you should use G_TYPE_POINTER instead of the actual boxed GType to ensure that the instance owned by JSCClass is used. If you really want to return a new copy of the boxed type, use JSC_TYPE_VALUE and return a JSCValue created with jsc_value_new_object() that receives the copy as the instance parameter.

[rename-to jsc_class_add_method]

Parameters

jsc_class

a JSCClass

 

name

the method name

 

callback

a GCallback to be called to invoke method name of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the method return value, or G_TYPE_NONE if the method is void.

 

n_parameters

the number of parameter types to follow or 0 if the method doesn't receive parameters.

 

parameter_types

a list of GTypes, one for each parameter, or NULL.

[nullable][array length=n_parameters][element-type GType]

jsc_class_add_method_variadic ()

void
jsc_class_add_method_variadic (JSCClass *jsc_class,
                               const char *name,
                               GCallback callback,
                               gpointer user_data,
                               GDestroyNotify destroy_notify,
                               GType return_type);

Add method with name to jsc_class . When the method is called by JavaScript or jsc_value_object_invoke_method(), callback is called receiving the class instance as first parameter, followed by a GPtrArray of JSCValues with the method arguments and then user_data as last parameter. When the method is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

Note that the value returned by callback must be transfer full. In case of non-refcounted boxed types, you should use G_TYPE_POINTER instead of the actual boxed GType to ensure that the instance owned by JSCClass is used. If you really want to return a new copy of the boxed type, use JSC_TYPE_VALUE and return a JSCValue created with jsc_value_new_object() that receives the copy as the instance parameter.

Parameters

jsc_class

a JSCClass

 

name

the method name

 

callback

a GCallback to be called to invoke method name of jsc_class .

[scope async]

user_data

user data to pass to callback .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

return_type

the GType of the method return value, or G_TYPE_NONE if the method is void.

 

jsc_class_add_property ()

void
jsc_class_add_property (JSCClass *jsc_class,
                        const char *name,
                        GType property_type,
                        GCallback getter,
                        GCallback setter,
                        gpointer user_data,
                        GDestroyNotify destroy_notify);

Add a property with name to jsc_class . When the property value needs to be getted, getter is called receiving the the class instance as first parameter and user_data as last parameter. When the property value needs to be set, setter is called receiving the the class instance as first parameter, followed by the value to be set and then user_data as the last parameter. When the property is cleared in the JSCClass context, destroy_notify is called with user_data as parameter.

Note that the value returned by getter must be transfer full. In case of non-refcounted boxed types, you should use G_TYPE_POINTER instead of the actual boxed GType to ensure that the instance owned by JSCClass is used. If you really want to return a new copy of the boxed type, use JSC_TYPE_VALUE and return a JSCValue created with jsc_value_new_object() that receives the copy as the instance parameter.

Parameters

jsc_class

a JSCClass

 

name

the property name

 

property_type

the GType of the property value

 

getter

a GCallback to be called to get the property value.

[scope async][nullable]

setter

a GCallback to be called to set the property value.

[scope async][nullable]

user_data

user data to pass to getter and setter .

[closure]

destroy_notify

destroy notifier for user_data .

[nullable]

Types and Values

JSCClass

typedef struct _JSCClass JSCClass;

JSCClassVTable

typedef struct {
    JSCClassGetPropertyFunction get_property;
    JSCClassSetPropertyFunction set_property;
    JSCClassHasPropertyFunction has_property;
    JSCClassDeletePropertyFunction delete_property;
    JSCClassEnumeratePropertiesFunction enumerate_properties;
} JSCClassVTable;

Virtual table for a JSCClass. This can be optionally used when registering a JSCClass in a JSCContext to provide a custom implementation for the class. All virtual functions are optional and can be set to NULL to fallback to the default implementation.

Members

JSCClassGetPropertyFunction get_property;

a JSCClassGetPropertyFunction for getting a property.

 

JSCClassSetPropertyFunction set_property;

a JSCClassSetPropertyFunction for setting a property.

 

JSCClassHasPropertyFunction has_property;

a JSCClassHasPropertyFunction for querying a property.

 

JSCClassDeletePropertyFunction delete_property;

a JSCClassDeletePropertyFunction for deleting a property.

 

JSCClassEnumeratePropertiesFunction enumerate_properties;

a JSCClassEnumeratePropertiesFunction for enumerating properties.

 

Property Details

The “context” property

  “context”                  JSCContext *

The JSCContext in which the class was registered.

Flags: Read / Write / Construct Only


The “name” property

  “name”                     gchar *

The name of the class.

Flags: Read / Write / Construct Only

Default value: NULL


The “parent” property

  “parent”                   JSCClass *

The parent class or NULL in case of final classes.

Flags: Read / Write / Construct Only

See Also

JSCContext