:right-sidebar: True
Class
===================================================================
.. currentmodule:: gi.repository.JavaScriptCore
.. class:: Class(**properties: ~typing.Any)
:no-contents-entry:
Superclasses: :class:`~gi.repository.GObject.Object`
A JSSClass represents a custom JavaScript class registered by the user in a :obj:`~gi.repository.JavaScriptCore.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
:obj:`~gi.repository.GObject.Callback`\s to implement them.
Methods
-------
.. rst-class:: interim-class
.. class:: Class
:no-index:
.. method:: add_constructor(name: str | None, callback: ~typing.Callable[[], None], user_data: ~typing.Any, return_type: ~gobject.GType, parameter_types: list[~gobject.GType] | None = None) -> ~gi.repository.JavaScriptCore.Value
Add a constructor to ``jsc_class``\. If ``name`` is :const:`None`, the class name will be used. When new
is used with the constructor or :func:`~gi.repository.JavaScriptCore.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 :obj:`~gi.repository.JavaScriptCore.Class` 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
:func:`~gi.repository.JavaScriptCore.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 :obj:`~gi.repository.GLib.DestroyNotify` passed to
:func:`~gi.repository.JavaScriptCore.Context.register_class` is responsible for disposing of it.
:param name: the constructor name or :const:`None`
:param callback: a :obj:`~gi.repository.GObject.Callback` to be called to create an instance of ``jsc_class``
:param user_data: user data to pass to ``callback``
:param return_type: the :obj:`~gi.repository.GObject.Type` of the constructor return value
:param parameter_types:
.. method:: add_constructor_variadic(name: str | None, callback: ~typing.Callable[[], None], user_data: ~typing.Any, return_type: ~gobject.GType) -> ~gi.repository.JavaScriptCore.Value
Add a constructor to ``jsc_class``\. If ``name`` is :const:`None`, the class name will be used. When new
is used with the constructor or :func:`~gi.repository.JavaScriptCore.Value.constructor_call` is called, ``callback`` is invoked receiving
a ``GPtrArray`` of :obj:`~gi.repository.JavaScriptCore.Value`\s as arguments and ``user_data`` as the last parameter. When the constructor object
is cleared in the :obj:`~gi.repository.JavaScriptCore.Class` 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
:func:`~gi.repository.JavaScriptCore.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 :obj:`~gi.repository.GLib.DestroyNotify` passed to
:func:`~gi.repository.JavaScriptCore.Context.register_class` is responsible for disposing of it.
:param name: the constructor name or :const:`None`
:param callback: a :obj:`~gi.repository.GObject.Callback` to be called to create an instance of ``jsc_class``
:param user_data: user data to pass to ``callback``
:param return_type: the :obj:`~gi.repository.GObject.Type` of the constructor return value
.. method:: add_method(name: str, callback: ~typing.Callable[[], None], user_data: ~typing.Any, return_type: ~gobject.GType, parameter_types: list[~gobject.GType] | None = None) -> None
Add method with ``name`` to ``jsc_class``\. When the method is called by JavaScript or :func:`~gi.repository.JavaScriptCore.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 :obj:`~gi.repository.JavaScriptCore.Class` 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 :obj:`~gi.repository.GObject.Type` to ensure that the instance owned by :obj:`~gi.repository.JavaScriptCore.Class` is used.
If you really want to return a new copy of the boxed type, use ``JSC_TYPE_VALUE`` and return a :obj:`~gi.repository.JavaScriptCore.Value` created
with :func:`~gi.repository.JavaScriptCore.Value.new_object` that receives the copy as the instance parameter.
:param name: the method name
:param callback: a :obj:`~gi.repository.GObject.Callback` to be called to invoke method ``name`` of ``jsc_class``
:param user_data: user data to pass to ``callback``
:param return_type: the :obj:`~gi.repository.GObject.Type` of the method return value, or %G_TYPE_NONE if the method is void.
:param parameter_types:
.. method:: add_method_variadic(name: str, callback: ~typing.Callable[[], None], user_data: ~typing.Any, return_type: ~gobject.GType) -> None
Add method with ``name`` to ``jsc_class``\. When the method is called by JavaScript or :func:`~gi.repository.JavaScriptCore.Value.object_invoke_method`,
``callback`` is called receiving the class instance as first parameter, followed by a ``GPtrArray`` of :obj:`~gi.repository.JavaScriptCore.Value`\s
with the method arguments and then ``user_data`` as last parameter. When the method is cleared in the :obj:`~gi.repository.JavaScriptCore.Class` 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 :obj:`~gi.repository.GObject.Type` to ensure that the instance owned by :obj:`~gi.repository.JavaScriptCore.Class` is used.
If you really want to return a new copy of the boxed type, use ``JSC_TYPE_VALUE`` and return a :obj:`~gi.repository.JavaScriptCore.Value` created
with :func:`~gi.repository.JavaScriptCore.Value.new_object` that receives the copy as the instance parameter.
:param name: the method name
:param callback: a :obj:`~gi.repository.GObject.Callback` to be called to invoke method ``name`` of ``jsc_class``
:param user_data: user data to pass to ``callback``
:param return_type: the :obj:`~gi.repository.GObject.Type` of the method return value, or %G_TYPE_NONE if the method is void.
.. method:: add_property(name: str, property_type: ~gobject.GType, getter: ~typing.Callable[[], None] | None = None, setter: ~typing.Callable[[], None] | None = None, user_data: ~typing.Any = None) -> None
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
:obj:`~gi.repository.JavaScriptCore.Class` 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 :obj:`~gi.repository.GObject.Type` to ensure that the instance owned by :obj:`~gi.repository.JavaScriptCore.Class` is used.
If you really want to return a new copy of the boxed type, use ``JSC_TYPE_VALUE`` and return a :obj:`~gi.repository.JavaScriptCore.Value` created
with :func:`~gi.repository.JavaScriptCore.Value.new_object` that receives the copy as the instance parameter.
:param name: the property name
:param property_type: the :obj:`~gi.repository.GObject.Type` of the property value
:param getter: a :obj:`~gi.repository.GObject.Callback` to be called to get the property value
:param setter: a :obj:`~gi.repository.GObject.Callback` to be called to set the property value
:param user_data: user data to pass to ``getter`` and ``setter``
.. method:: get_name() -> str
Get the class name of ``jsc_class``
.. method:: get_parent() -> ~gi.repository.JavaScriptCore.Class
Get the parent class of ``jsc_class``
Properties
----------
.. rst-class:: interim-class
.. class:: Class
:no-index:
.. attribute:: props.context
:type: ~gi.repository.JavaScriptCore.Context
The type of the None singleton.
.. attribute:: props.name
:type: str
The type of the None singleton.
.. attribute:: props.parent
:type: ~gi.repository.JavaScriptCore.Class
The type of the None singleton.