Class#
Superclasses: Object
A JSSClass represents a custom JavaScript class registered by the user in a Context
.
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
Callback
<!– –>s to implement them.
Methods#
- class Class
- add_constructor(name: str | None, callback: Callable[[], None], user_data: Any, return_type: GType, parameter_types: list[GType] | None = None) Value #
Add a constructor to
jsc_class
. Ifname
isNone
, the class name will be used. When <function>new</function> is used with the constructor orconstructor_call()
is called,callback
is invoked receiving the parameters anduser_data
as the last parameter. When the constructor object is cleared in theClass
context,destroy_notify
is called withuser_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
set_value()
to make the constructor available in the global object.Note that the value returned by
callback
is adopted byjsc_class
, and theDestroyNotify
passed toregister_class()
is responsible for disposing of it.- Parameters:
name – the constructor name or
None
callback – a
Callback
to be called to create an instance ofjsc_class
user_data – user data to pass to
callback
return_type – the
Type
of the constructor return valueparameter_types
- add_constructor_variadic(name: str | None, callback: Callable[[], None], user_data: Any, return_type: GType) Value #
Add a constructor to
jsc_class
. Ifname
isNone
, the class name will be used. When <function>new</function> is used with the constructor orconstructor_call()
is called,callback
is invoked receiving aGPtrArray
ofValue
<!– –>s as arguments anduser_data
as the last parameter. When the constructor object is cleared in theClass
context,destroy_notify
is called withuser_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
set_value()
to make the constructor available in the global object.Note that the value returned by
callback
is adopted byjsc_class
, and theDestroyNotify
passed toregister_class()
is responsible for disposing of it.- Parameters:
name – the constructor name or
None
callback – a
Callback
to be called to create an instance ofjsc_class
user_data – user data to pass to
callback
return_type – the
Type
of the constructor return value
- add_method(name: str, callback: Callable[[], None], user_data: Any, return_type: GType, parameter_types: list[GType] | None = None) None #
Add method with
name
tojsc_class
. When the method is called by JavaScript orobject_invoke_method()
,callback
is called receiving the class instance as first parameter, followed by the method parameters and thenuser_data
as last parameter. When the method is cleared in theClass
context,destroy_notify
is called withuser_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 boxedType
to ensure that the instance owned byClass
is used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUE
and return aValue
created withnew_object()
that receives the copy as the instance parameter.- Parameters:
name – the method name
callback – a
Callback
to be called to invoke methodname
ofjsc_class
user_data – user data to pass to
callback
return_type – the
Type
of the method return value, or %G_TYPE_NONE if the method is void.parameter_types
- add_method_variadic(name: str, callback: Callable[[], None], user_data: Any, return_type: GType) None #
Add method with
name
tojsc_class
. When the method is called by JavaScript orobject_invoke_method()
,callback
is called receiving the class instance as first parameter, followed by aGPtrArray
ofValue
<!– –>s with the method arguments and thenuser_data
as last parameter. When the method is cleared in theClass
context,destroy_notify
is called withuser_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 boxedType
to ensure that the instance owned byClass
is used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUE
and return aValue
created withnew_object()
that receives the copy as the instance parameter.- Parameters:
name – the method name
callback – a
Callback
to be called to invoke methodname
ofjsc_class
user_data – user data to pass to
callback
return_type – the
Type
of the method return value, or %G_TYPE_NONE if the method is void.
- add_property(name: str, property_type: GType, getter: Callable[[], None] | None = None, setter: Callable[[], None] | None = None, user_data: Any = None) None #
Add a property with
name
tojsc_class
. When the property value needs to be getted,getter
is called receiving the the class instance as first parameter anduser_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 thenuser_data
as the last parameter. When the property is cleared in theClass
context,destroy_notify
is called withuser_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 boxedType
to ensure that the instance owned byClass
is used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUE
and return aValue
created withnew_object()
that receives the copy as the instance parameter.- Parameters:
name – the property name
property_type – the
Type
of the property valuegetter – a
Callback
to be called to get the property valuesetter – a
Callback
to be called to set the property valueuser_data – user data to pass to
getter
andsetter