Value#
Superclasses: Object
JSCValue represents a reference to a value in a Context. The JSCValue
protects the referenced value from being garbage collected.
Constructors#
- class Value
- classmethod new_array_buffer(context: Context, data: Any, size: int, destroy_notify: Callable[[Any], None] | None = None, user_data: Any = None) Value | None#
Creates a new %ArrayBuffer from existing
datain memory.The
datais not copied: while this allows sharing data with JavaScript efficiently, the caller must ensure that the memory region remains valid until the newly created object is released by JSC.Optionally, a
destroy_notifycallback can be provided, which will be invoked withuser_dataas parameter when the %ArrayBuffer object is released. This is intended to be used for freeing resources related to the memory region which contains the data:Note that the
user_datacan be the same value asdata:Added in version 2.38.
- Parameters:
context – A
Contextdata – Pointer to a region of memory.
size – Size in bytes of the memory region.
destroy_notify – destroy notifier for
user_data.user_data – user data.
- classmethod new_array_from_garray(context: Context, array: list[Value] | None = None) Value#
Create a new
Valuereferencing an array with the items fromarray. IfarrayisNoneor empty a new empty array will be created. Elements ofarrayshould be pointers to aValue.- Parameters:
context – a
Contextarray – a
GPtrArray
- classmethod new_array_from_strv(context: Context, strv: list[str]) Value#
Create a new
Valuereferencing an array of strings with the items fromstrv. IfarrayisNoneor empty a new empty array will be created.- Parameters:
context – a
Contextstrv – a
None-terminated array of strings
- classmethod new_boolean(context: Context, value: bool) Value#
Create a new
Valuefromvalue- Parameters:
context – a
Contextvalue – a
gboolean
- classmethod new_from_json(context: Context, json: str) Value#
Create a new
Valuereferencing a new value created by parsingjson.Added in version 2.28.
- Parameters:
context – a
Contextjson – the JSON string to be parsed
- classmethod new_function(context: Context, name: str | None, callback: Callable[[], None], user_data: Any, return_type: GType, parameter_types: list[GType] | None = None) Value#
Create a function in
context. IfnameisNonean anonymous function will be created. When the function is called by JavaScript orfunction_call(),callbackis called receiving the function parameters and thenuser_dataas last parameter. When the function is cleared incontext,destroy_notifyis called withuser_dataas parameter.Note that the value returned by
callbackmust be fully transferred. In case of boxed types, you could use %G_TYPE_POINTER instead of the actual boxedTypeto ensure that the instance owned byClassis used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUEand return aValuecreated withnew_object()that receives the copy as instance parameter.- Parameters:
context – a
Context:name – the function name or
Nonecallback – a
Callback.user_data – user data to pass to
callback.return_type – the
Typeof the function return value, or %G_TYPE_NONE if the function is void.parameter_types
- classmethod new_function_variadic(context: Context, name: str | None, callback: Callable[[], None], user_data: Any, return_type: GType) Value#
Create a function in
context. IfnameisNonean anonymous function will be created. When the function is called by JavaScript orfunction_call(),callbackis called receiving anGPtrArrayofValue<!– –>s with the arguments and thenuser_dataas last parameter. When the function is cleared incontext,destroy_notifyis called withuser_dataas parameter.Note that the value returned by
callbackmust be fully transferred. In case of boxed types, you could use %G_TYPE_POINTER instead of the actual boxedTypeto ensure that the instance owned byClassis used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUEand return aValuecreated withnew_object()that receives the copy as instance parameter.- Parameters:
context – a
Contextname – the function name or
Nonecallback – a
Callback.user_data – user data to pass to
callback.return_type – the
Typeof the function return value, or %G_TYPE_NONE if the function is void.
- classmethod new_null(context: Context) Value#
Create a new
Valuereferencing <function>null</function> incontext.- Parameters:
context – a
Context
- classmethod new_number(context: Context, number: float) Value#
Create a new
Valuefromnumber.- Parameters:
context – a
Contextnumber – a number
- classmethod new_object(context: Context, instance: Any = None, jsc_class: Class | None = None) Value#
Create a new
Valuefrominstance. IfinstanceisNonea new empty object is created. Wheninstanceis provided,jsc_classmust be provided too.jsc_classtakes ownership ofinstancethat will be freed by theDestroyNotifypassed toregister_class().
- classmethod new_promise(context: Context, executor: Callable[[Value, Value, Any], None], user_data: Any = None) Value#
Creates a new Promise.
executorwill be invoked during promise initialization and it receives theresolveandrejectobjects than can be called to resolve or reject the promise. It is called like a JavaScript function, so exceptions raised during the executor invocation will not be propagated to the context, but handled by the promise causing a rejection.Added in version 2.48.
- Parameters:
context – a
Contextexecutor – an initialization callback
user_data – user data passed in
executor
- classmethod new_string(context: Context, string: str | None = None) Value#
Create a new
Valuefromstring. If you need to create aValuefrom a string containing null characters, usenew_string_from_bytes()instead.- Parameters:
context – a
Contextstring – a null-terminated string
- classmethod new_string_from_bytes(context: Context, bytes: Bytes | None = None) Value#
Create a new
Valuefrombytes.
- classmethod new_typed_array(context: Context, type: TypedArrayType, length: int) Value#
Create a new typed array containing a given amount of elements.
Create a
Valuereferencing a new typed array with space forlengthelements of a giventype. As all typed arrays must have an associatedArrayBuffer, a new one of suitable size will be allocated to store the elements, which will be initialized to zero.The
typemust not beNONE.Added in version 2.38.
- Parameters:
context – a
Contexttype – the type of array elements
length – number of elements in the array
Methods#
- class Value
- array_buffer_get_data(size: int | None = None) Any | None#
Gets a pointer to memory that contains the array buffer data.
Obtains a pointer to the memory region that holds the contents of the %ArrayBuffer; modifications done to the data will be visible to JavaScript code. If
sizeis notNone, the size in bytes of the memory region will also be stored in the pointed location.Note that the pointer returned by this function is not guaranteed to remain the same after calls to other JSC API functions. If you plan to access the data of the %ArrayBuffer later, you can keep a reference to the
valueand obtain the data pointer at a later point. Keep in mind that if JavaScript code has a chance to run, for example due to main loop events that result in JSC being called, the contents of the memory region might be modified in the meantime. Consider taking a copy of the data and using the copy instead in asynchronous code.Added in version 2.38.
- Parameters:
size – location where to store the size of the memory region.
- array_buffer_get_size() int#
Gets the size in bytes of the array buffer.
Obtains the size in bytes of the memory region that holds the contents of an %ArrayBuffer.
Added in version 2.38.
- constructor_call(parameters: list[Value] | None = None) Value#
Invoke <function>new</function> with constructor referenced by
value. Iffirst_parameter_typeis %G_TYPE_NONE no parameters will be passed to the constructor.- Parameters:
parameters
- function_call(parameters: list[Value] | None = None) Value#
Call function referenced by
value, passing the given parameters. Iffirst_parameter_typeis %G_TYPE_NONE no parameters will be passed to the function.This function always returns a
Value, in case of void functions aValuereferencing <function>undefined</function> is returned- Parameters:
parameters
- new_typed_array_with_buffer(type: TypedArrayType, offset: int, length: int) Value#
Create a new typed array value with elements from an array buffer.
Create a
Valuereferencing a new typed array value containing elements of the giventype, where the elements are stored at the memory region represented by thearray_buffer.The
typemust not beNONE.The
offsetandlengthparameters can be used to indicate which part of the array buffer can be accessed through the typed array. If both are omitted (passing zero asoffset, and-1aslength), the wholearray_bufferis exposed through the typed array. Omitting thelengthwith a non-zerooffsetwill expose the remainder of thearray_bufferstarting at the indicated offset.Added in version 2.38.
- Parameters:
type – type of array elements.
offset – offset, in bytes.
length – number of array elements, or
-1.
- object_define_property_accessor(property_name: str, flags: ValuePropertyFlags, property_type: GType, getter: Callable[[], None] | None = None, setter: Callable[[], None] | None = None, user_data: Any = None) None#
Define or modify a property with
property_namein object referenced byvalue. When the property value needs to be getted or set,getterandsettercallbacks will be called. When the property is cleared in theClasscontext,destroy_notifyis called withuser_dataas parameter. This is equivalent to JavaScript <function>Object.defineProperty()</function> when used with an accessor descriptor.Note that the value returned by
gettermust be fully transferred. In case of boxed types, you could use %G_TYPE_POINTER instead of the actual boxedTypeto ensure that the instance owned byClassis used. If you really want to return a new copy of the boxed type, useJSC_TYPE_VALUEand return aValuecreated withnew_object()that receives the copy as instance parameter.Note that
getterandsetterare called as functions and not methods, so they don’t receive an instance as first parameter. Useadd_property()if you want to add property accessor invoked as a method.- Parameters:
property_name – the name of the property to define
flags –
ValuePropertyFlagsproperty_type – the
Typeof the propertygetter – a
Callbackto be called to get the property valuesetter – a
Callbackto be called to set the property valueuser_data – user data to pass to
getterandsetter
- object_define_property_data(property_name: str, flags: ValuePropertyFlags, property_value: Value | None = None) None#
Define or modify a property with
property_namein object referenced byvalue. This is equivalent to JavaScript <function>Object.defineProperty()</function> when used with a data descriptor.- Parameters:
property_name – the name of the property to define
flags –
ValuePropertyFlagsproperty_value – the default property value
- object_delete_property(name: str) bool#
Try to delete property with
namefromvalue. This function will returnFalseif the property was defined withoutCONFIGURABLEflag.- Parameters:
name – the property name
- object_enumerate_properties() list[str] | None#
Get the list of property names of
value. Only properties defined withENUMERABLEflag will be collected.
- object_get_property(name: str) Value#
Get property with
namefromvalue.- Parameters:
name – the property name
- object_get_property_at_index(index: int) Value#
Get property at
indexfromvalue.- Parameters:
index – the property index
- object_has_property(name: str) bool#
Get whether
valuehas property withname.- Parameters:
name – the property name
- object_invoke_method(name: str, parameters: list[Value] | None = None) Value#
Invoke method with
nameon object referenced byvalue, passing the given parameters. Iffirst_parameter_typeis %G_TYPE_NONE no parameters will be passed to the method. The object instance will be handled automatically even when the method is a custom one registered withadd_method(), so it should never be passed explicitly as parameter of this function.This function always returns a
Value, in case of void methods aValuereferencing <function>undefined</function> is returned.- Parameters:
name – the method name
parameters
- object_is_instance_of(name: str) bool#
Get whether the value referenced by
valueis an instance of classname.- Parameters:
name – a class name
- object_set_property(name: str, property: Value) None#
Set
propertywithnameonvalue.- Parameters:
name – the property name
property – the
Valueto set
- object_set_property_at_index(index: int, property: Value) None#
Set
propertyatindexonvalue.- Parameters:
index – the property index
property – the
Valueto set
- to_json(indent: int) str#
Create a JSON string of
valueserialization. Ifindentis 0, the resulting JSON will not contain newlines. The size of the indent is clamped to 10 spaces.Added in version 2.28.
- Parameters:
indent – The number of spaces to indent when nesting.
- to_string() str#
Convert
valueto a string. Useto_string_as_bytes()instead, if you need to handle strings containing null characters.
- to_string_as_bytes() Bytes#
Convert
valueto a string and return the results asBytes. This is needed to handle strings with null characters.
- typed_array_get_buffer() Value#
Obtain the %ArrayBuffer for the memory region of the typed array elements.
Added in version 2.38.
- typed_array_get_data() tuple[Any | None, int | None]#
Obtains a pointer to the memory region that holds the elements of the typed array; modifications done to them will be visible to JavaScript code. If
lengthis notNone, the number of elements contained in the typed array are also stored in the pointed location.The returned pointer needs to be casted to the appropriate type (see
TypedArrayType), and has theoffsetover the underlying array buffer data applied—that is, points to the first element of the typed array:if (jsc_value_typed_array_get_type(value) != JSC_TYPED_ARRAY_UINT32) g_error ("Only arrays of uint32_t are supported"); gsize count = 0; uint32_t *elements = jsc_value_typed_array_get_contents (value, &count); for (gsize i = 0; i < count; i++) g_print ("index %zu, value %" PRIu32 "\n", i, elements[i]);
Note that the pointer returned by this function is not guaranteed to remain the same after calls to other JSC API functions. See
array_buffer_get_data()for details.Added in version 2.38.
- typed_array_get_offset() int#
Gets the offset over the underlying array buffer data.
Added in version 2.38.
- typed_array_get_type() TypedArrayType#
Gets the type of elements contained in a typed array.
Added in version 2.38.