|
Ultralight C API 2.0.0
|
JavaScript context and value handles for interacting with page script in C.
#include <Ultralight/CAPI/CAPI_JSValue.h>
This header provides the core types and functions of the JavaScript C API. You'll use its context and value handles to call page functions and exchange data with scripts running in a View.
The rules documented in this header hold across every JavaScript C header in the library.
This example calls a function on the page and destroys every handle afterward:
The JavaScript C API organizes script execution around these handle types:
Handle ownership determines who destroys a handle:
A handle never keeps its underlying page alive. When a page navigates away, its frame is removed, or its View is destroyed, every handle from that page enters the gone state.
Calls on NULL or on a handle whose page is gone fail safely without crashing. You'll still need to destroy those handles to prevent memory leaks.
To check whether a handle can still interact with its page, call ulJSValueIsAlive() or ulJSContextIsAlive(). Both functions return false for NULL handles as well as handles whose page is gone, so check whether a handle is NULL first if you need to distinguish between the two states.
Conversion functions like ulJSValueToNumber(), ulJSValueToString(), and ulJSValueToBoolean() follow JavaScript's coercion rules rather than strict type checks. Converting an object can run page script by calling its valueOf() or toString() method.
To enforce strict conversions without coercion, call ulJSValueGetType() first to check the value's type.
This function shows how to perform a strict type check before reading a number versus relying on coercion:
Functions that can encounter JavaScript exceptions accept a ULJSValue* exception out-parameter. Pass NULL if you don't need exception details. When an operation throws, the library populates the parameter with an owned ULJSValue handle that you must destroy.
Passing a NULL handle or calling a function on a handle whose page is gone leaves exception untouched. Check whether the handle is alive to distinguish a missing page from a script exception.
To inspect an error, pass the exception handle to ulJSValueGetErrorDetails(). It populates a ULJSErrorDetails struct with the error's line and column numbers, along with owned string handles for the message, source URL, and stack trace. Release the string handles with ulJSErrorDetailsRelease().
For errors created by the library, inspect the code property on the error object to check what went wrong without parsing the message text. Standard error codes are defined in <Ultralight/js/Error.h>.
You must call functions in this header on the Renderer's thread, unless a function's documentation states otherwise.
You can perform these operations from any thread, including a finalizer thread:
Call ulCreateJSFunction() to create a JavaScript function that invokes a native ULJSFunctionCallback.
You can pass application state in user_data along with a destroy_user_data cleanup callback. The library runs destroy_user_data exactly once on the Renderer's thread when the function is garbage-collected or if function creation fails. Inside destroy_user_data, you must not call any library functions other than destroying handles.
Classes | |
| struct | ULJSErrorDetails |
| Details of a JavaScript error, filled by ulJSValueGetErrorDetails(). More... | |
Functions | |
| ULJSContext | ulCreateJSContextRef (ULJSContext ctx) |
| Create a new handle to the same context. | |
| void | ulDestroyJSContext (ULJSContext ctx) |
| Destroy a context handle (NULL-safe). | |
| bool | ulJSContextIsAlive (ULJSContext ctx) |
| Whether or not the context is still alive. | |
| ULJSValue | ulJSContextGetGlobalObject (ULJSContext ctx) |
| Get the context's global object. | |
| ULJSValue | ulJSContextEvaluate (ULJSContext ctx, ULString script, const char *source_url, ULJSValue *exception) |
| Run a script in the context. | |
| JSContextRef | ulJSContextGetJSContextRef (ULJSContext ctx) |
| Get the JavaScriptCore JSContextRef, for use with the JavaScriptCore C API. | |
| ULJSValue | ulCreateJSValueUndefined (ULJSContext ctx) |
| Create a JavaScript undefined value. | |
| ULJSValue | ulCreateJSValueNull (ULJSContext ctx) |
| Create a JavaScript null value. | |
| ULJSValue | ulCreateJSValueBoolean (ULJSContext ctx, bool value) |
| Create a JavaScript boolean value. | |
| ULJSValue | ulCreateJSValueNumber (ULJSContext ctx, double value) |
| Create a JavaScript number value. | |
| ULJSValue | ulCreateJSValueString (ULJSContext ctx, ULString value) |
| Create a JavaScript string value from a ULString. | |
| ULJSValue | ulCreateJSValueStringFromCString (ULJSContext ctx, const char *value) |
| Create a JavaScript string value from a null-terminated UTF-8 string. | |
| ULJSValue | ulCreateJSValueStringUTF8 (ULJSContext ctx, const char *bytes, size_t length) |
| Create a JavaScript string value from UTF-8 bytes with an explicit length. | |
| ULJSValue | ulCreateJSValueFromJSON (ULJSContext ctx, ULString json, ULJSValue *exception) |
| Create a JavaScript value by parsing JSON text. | |
| ULJSValue | ulCreateJSError (ULJSContext ctx, ULJSErrorType type, const char *message) |
| Create a JavaScript error object of the given type. | |
| ULJSValue | ulCreateJSMarshalError (ULJSContext ctx, const char *binding_path, size_t arg_number, const char *value_path, const char *expected, ULJSValue got) |
| Create a JavaScript TypeError for a value that didn't convert to the type a binding expected. | |
| ULJSValue | ulCreateJSValueRef (ULJSValue value) |
| Create a new handle to the same JavaScript value. | |
| void | ulDestroyJSValue (ULJSValue value) |
| Destroy a value handle (NULL-safe). | |
| ULJSType | ulJSValueGetType (ULJSValue value) |
| Get the type of a JavaScript value. | |
| bool | ulJSValueIsArray (ULJSValue value) |
| Whether or not the value is an Array. | |
| bool | ulJSValueIsCallable (ULJSValue value) |
| Whether or not the value can be called (see ulJSFunctionCall()). | |
| bool | ulJSValueIsFunction (ULJSValue value) |
| Whether or not the value is a function. | |
| bool | ulJSValueIsPromise (ULJSValue value) |
| Whether or not the value is a Promise. | |
| bool | ulJSValueIsAlive (ULJSValue value) |
| Whether or not the value's page is still alive. | |
| void | ulJSValueReportUnobservedException (ULJSValue exception) |
| Log a JavaScript exception that your code received but never read. | |
| ULJSContext | ulJSValueGetContext (ULJSValue value) |
| Get the context a value belongs to. | |
| bool | ulJSValueToBoolean (ULJSValue value, bool *result) |
| Convert to a boolean using JavaScript's rules (this never runs script). | |
| bool | ulJSValueToNumber (ULJSValue value, double *result, ULJSValue *exception) |
| Convert to a number using JavaScript's rules. | |
| ULString | ulJSValueToString (ULJSValue value, ULJSValue *exception) |
| Convert to a string using JavaScript's rules. | |
| bool | ulJSValueGetUTF8 (ULJSValue value, char *buffer, size_t capacity, size_t *out_length, ULJSValue *exception) |
| Convert to a string using JavaScript's rules and write it as UTF-8 into your buffer. | |
| ULString | ulJSValueToJSON (ULJSValue value, unsigned indent, ULJSValue *exception) |
| Convert to JSON text like JSON.stringify() does. | |
| ULJSValue | ulCreateJSObject (ULJSContext ctx) |
| Create a new empty JavaScript object. | |
| ULJSValue | ulCreateJSArray (ULJSContext ctx, const ULJSValue *elements, size_t count) |
| Create a new JavaScript Array from a list of values. | |
| ULJSValue | ulCreateJSObjectWithProperties (ULJSContext ctx, const char *const *names, const ULJSValue *values, size_t count, ULJSValue *exception) |
| Create a new JavaScript object with a set of properties. | |
| ULJSValue | ulJSObjectGetProperty (ULJSValue object, const char *name, ULJSValue *exception) |
| Get a property of an object (own or inherited). | |
| bool | ulJSObjectHasProperty (ULJSValue object, const char *name, ULJSValue *exception) |
| Whether or not an object has a property (own or inherited). | |
| bool | ulJSObjectGetProperties (ULJSValue object, const char *const *names, size_t count, ULJSValue *out_values, ULJSValue *exception) |
| Get several properties of an object in one call. | |
| bool | ulJSObjectSetProperty (ULJSValue object, const char *name, ULJSValue value, unsigned attributes, ULJSValue *exception) |
| Set a property of an object. | |
| ULJSValue | ulJSObjectGetPropertyAtIndex (ULJSValue object, size_t index, ULJSValue *exception) |
| Get a property by numeric index (see ulJSObjectGetProperty()). | |
| bool | ulJSObjectSetPropertyAtIndex (ULJSValue object, size_t index, ULJSValue value, ULJSValue *exception) |
| Set a property by numeric index (see ulJSObjectSetProperty()). | |
| bool | ulJSObjectGetOwnEntries (ULJSValue object, ULString *out_names, ULJSValue *out_values, size_t capacity, size_t *out_count, ULJSValue *exception) |
| Get the names and values of an object's own enumerable properties in one call. | |
| ULJSValue | ulJSFunctionCall (ULJSValue function, ULJSValue this_value, const ULJSValue *args, size_t argc, ULJSValue *exception) |
| Call a JavaScript function. | |
| ULJSValue | ulCreateJSFunction (ULJSContext ctx, const char *name, ULJSFunctionCallback callback, void *user_data, ULUserDataDestroyCallback destroy_user_data) |
| Create a JavaScript function that calls a native callback. | |
| bool | ulJSValueGetErrorDetails (ULJSValue error, ULJSErrorDetails *details) |
| Get the type, message, source location, and stack trace of an error in one call. | |
| void | ulJSErrorDetailsRelease (ULJSErrorDetails *details) |
| Destroy the strings a ulJSValueGetErrorDetails() call stored in details and reset them to NULL. | |
| size_t | ulJSTypedArrayGetLength (ULJSValue typed_array) |
| Get the number of elements in a typed array. | |
| size_t | ulJSArrayBufferGetByteLength (ULJSValue array_buffer) |
| Get the length of an ArrayBuffer in bytes. | |
Macros | |
| #define | ULJS_HANDLE_TYPES_DEFINED |
Typedefs | |
| typedef struct C_JSContext * | ULJSContext |
| Opaque handle to a JavaScript context (the script environment of the page in one frame). | |
| typedef struct C_JSValue * | ULJSValue |
| Opaque handle to a JavaScript value. | |
| typedef ULJSValue(*) | ULJSFunctionCallback(void *user_data, ULJSContext ctx, ULJSValue this_value, const ULJSValue *args, size_t argc, ULJSValue *exception) |
| Callback invoked when JavaScript calls a function created with ulCreateJSFunction(). | |
Enumerations | |
| enum | ULJSType { kULJSType_Invalid = 0 , kULJSType_Undefined , kULJSType_Null , kULJSType_Boolean , kULJSType_Number , kULJSType_String , kULJSType_Symbol , kULJSType_Object , kULJSType_BigInt = 8 , kULJSType_Count } |
| The type of a JavaScript value. More... | |
| enum | ULJSPropertyAttributes { kULJSPropertyAttributes_None = 0 , kULJSPropertyAttributes_ReadOnly = 1 << 1 , kULJSPropertyAttributes_DontEnum = 1 << 2 , kULJSPropertyAttributes_DontDelete = 1 << 3 } |
| Attribute flags for a new property (see ulJSObjectSetProperty() and ulJSClassAddMethod()). More... | |
| enum | ULJSErrorType { kULJSErrorType_Error = 0 , kULJSErrorType_TypeError , kULJSErrorType_RangeError , kULJSErrorType_SyntaxError , kULJSErrorType_ReferenceError } |
| The type of a JavaScript error, matching the standard error constructors. More... | |
| ULJSValue ulCreateJSArray | ( | ULJSContext | ctx, |
| const ULJSValue * | elements, | ||
| size_t | count ) |
Create a new JavaScript Array from a list of values.
| ctx | The context to create the array in. |
| elements | The element handles (they remain yours; a NULL entry becomes undefined). May be NULL when count is 0. |
| count | The number of entries in elements. |
| ULJSContext ulCreateJSContextRef | ( | ULJSContext | ctx | ) |
Create a new handle to the same context.
| ctx | The context handle to reference. |
| ULJSValue ulCreateJSError | ( | ULJSContext | ctx, |
| ULJSErrorType | type, | ||
| const char * | message ) |
Create a JavaScript error object of the given type.
Use this to throw from a callback (through its exception out-parameter) or to reject a promise.
| ctx | The context to create the error in. |
| type | The error type (TypeError, RangeError, and so on). |
| message | The error's message as a null-terminated UTF-8 string. |
| ULJSValue ulCreateJSFunction | ( | ULJSContext | ctx, |
| const char * | name, | ||
| ULJSFunctionCallback | callback, | ||
| void * | user_data, | ||
| ULUserDataDestroyCallback | destroy_user_data ) |
Create a JavaScript function that calls a native callback.
The function is a real function object (it inherits from Function.prototype, so bind, call, and apply work on it).
| ctx | The context to create the function in. |
| name | The function's name as a null-terminated UTF-8 string (NULL for "anonymous"). |
| callback | The callback to invoke when the function is called. |
| user_data | Opaque user data passed to every call of callback. |
| destroy_user_data | Callback invoked exactly once to destroy user_data on the Renderer's thread after the function is garbage collected, or right away if the function can't be created (may be NULL). |
| ULJSValue ulCreateJSMarshalError | ( | ULJSContext | ctx, |
| const char * | binding_path, | ||
| size_t | arg_number, | ||
| const char * | value_path, | ||
| const char * | expected, | ||
| ULJSValue | got ) |
Create a JavaScript TypeError for a value that didn't convert to the type a binding expected.
The message has the same form as the library's own conversion errors:
The error's code property is "ULJS_BAD_ARG", so page script can check the kind of error without parsing the message.
| ctx | The context to create the error in. |
| binding_path | The binding's full dot-separated path, root included (eg, "myApp.fs.readFile"). |
| arg_number | The 1-based position of the argument that failed, or 0 when the failure isn't tied to an argument (eg, a property assignment). |
| value_path | Where the failing value is inside the argument (eg, "volume" or "items[2].name"), or NULL when the whole argument failed. |
| expected | A short description of the expected type (eg, "string", "number[]", or "'Small' | 'Large'"). |
| got | The value that failed to convert (NULL reads as undefined). It remains yours. A string is quoted in the message (shortened if it's long); any other value is described by its type. |
| ULJSValue ulCreateJSObject | ( | ULJSContext | ctx | ) |
Create a new empty JavaScript object.
| ctx | The context to create the object in. |
| ULJSValue ulCreateJSObjectWithProperties | ( | ULJSContext | ctx, |
| const char *const * | names, | ||
| const ULJSValue * | values, | ||
| size_t | count, | ||
| ULJSValue * | exception ) |
Create a new JavaScript object with a set of properties.
The properties are defined like an object literal's, so a name like __proto__ becomes an ordinary property instead of setting the prototype.
| ctx | The context to create the object in. |
| names | The property names as null-terminated UTF-8 strings (a NULL entry makes the call fail and return NULL). |
| values | The property values (they remain yours; a NULL entry becomes undefined). |
| count | The number of entries in names and values. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| ULJSValue ulCreateJSValueBoolean | ( | ULJSContext | ctx, |
| bool | value ) |
Create a JavaScript boolean value.
| ctx | The context to create the value in. |
| value | The boolean to store. |
| ULJSValue ulCreateJSValueFromJSON | ( | ULJSContext | ctx, |
| ULString | json, | ||
| ULJSValue * | exception ) |
Create a JavaScript value by parsing JSON text.
| ctx | The context to create the value in. |
| json | The JSON text. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| ULJSValue ulCreateJSValueNull | ( | ULJSContext | ctx | ) |
Create a JavaScript null value.
| ctx | The context to create the value in. |
| ULJSValue ulCreateJSValueNumber | ( | ULJSContext | ctx, |
| double | value ) |
Create a JavaScript number value.
| ctx | The context to create the value in. |
| value | The number to store. |
Create a new handle to the same JavaScript value.
Use this to keep a value you received in a callback after the callback returns.
| value | The value handle to reference. |
| ULJSValue ulCreateJSValueString | ( | ULJSContext | ctx, |
| ULString | value ) |
Create a JavaScript string value from a ULString.
| ctx | The context to create the value in. |
| value | The string to store. |
| ULJSValue ulCreateJSValueStringFromCString | ( | ULJSContext | ctx, |
| const char * | value ) |
Create a JavaScript string value from a null-terminated UTF-8 string.
| ctx | The context to create the value in. |
| value | The null-terminated UTF-8 string to store. |
| ULJSValue ulCreateJSValueStringUTF8 | ( | ULJSContext | ctx, |
| const char * | bytes, | ||
| size_t | length ) |
Create a JavaScript string value from UTF-8 bytes with an explicit length.
The bytes don't need a null terminator and may contain null characters. Invalid UTF-8 becomes U+FFFD.
| ctx | The context to create the value in. |
| bytes | The UTF-8 bytes (may be NULL when length is 0). |
| length | The number of bytes. |
| ULJSValue ulCreateJSValueUndefined | ( | ULJSContext | ctx | ) |
Create a JavaScript undefined value.
| ctx | The context to create the value in. |
| void ulDestroyJSContext | ( | ULJSContext | ctx | ) |
Destroy a context handle (NULL-safe).
This doesn't affect the context itself.
| ctx | The context handle to destroy. |
| void ulDestroyJSValue | ( | ULJSValue | value | ) |
Destroy a value handle (NULL-safe).
| value | The value handle to destroy. |
| size_t ulJSArrayBufferGetByteLength | ( | ULJSValue | array_buffer | ) |
Get the length of an ArrayBuffer in bytes.
The ArrayBuffer can still be detached afterwards.
Unlike reading the ArrayBuffer's byteLength property, this never runs script.
| array_buffer | The ArrayBuffer. |
| ULJSValue ulJSContextEvaluate | ( | ULJSContext | ctx, |
| ULString | script, | ||
| const char * | source_url, | ||
| ULJSValue * | exception ) |
Run a script in the context.
| ctx | The context handle. |
| script | The script. |
| source_url | A URL for the script as a null-terminated UTF-8 string, used in error reports (may be NULL). |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| ULJSValue ulJSContextGetGlobalObject | ( | ULJSContext | ctx | ) |
Get the context's global object.
| ctx | The context handle. |
| JSContextRef ulJSContextGetJSContextRef | ( | ULJSContext | ctx | ) |
Get the JavaScriptCore JSContextRef, for use with the JavaScriptCore C API.
| ctx | The context handle. |
| bool ulJSContextIsAlive | ( | ULJSContext | ctx | ) |
Whether or not the context is still alive.
A context goes away when its page navigates away, its frame is removed, or its View or Renderer is destroyed.
| ctx | The context handle. |
| void ulJSErrorDetailsRelease | ( | ULJSErrorDetails * | details | ) |
Destroy the strings a ulJSValueGetErrorDetails() call stored in details and reset them to NULL.
| details | The details to release (NULL does nothing). |
| ULJSValue ulJSFunctionCall | ( | ULJSValue | function, |
| ULJSValue | this_value, | ||
| const ULJSValue * | args, | ||
| size_t | argc, | ||
| ULJSValue * | exception ) |
Call a JavaScript function.
| function | The function to call. |
| this_value | The this value for the call (NULL uses the global object). |
| args | The arguments (they remain yours; a NULL entry passes undefined). |
| argc | The number of entries in args. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| bool ulJSObjectGetOwnEntries | ( | ULJSValue | object, |
| ULString * | out_names, | ||
| ULJSValue * | out_values, | ||
| size_t | capacity, | ||
| size_t * | out_count, | ||
| ULJSValue * | exception ) |
Get the names and values of an object's own enumerable properties in one call.
Inherited properties aren't included. Call it twice: first with capacity 0 to get the count, then with buffers of that size. If the object gains properties between the calls (a getter can run script), the second call reports a larger count, so repeat with more room.
| object | The object handle. |
| out_names | An array of capacity slots that receives the property names. You must destroy each one with ulDestroyString(). |
| out_values | An array of capacity slots that receives the property values. You must destroy each one with ulDestroyJSValue(). |
| capacity | The number of slots in out_names and out_values (0 to query the count). |
| out_count | Receives the total number of own enumerable properties even when it exceeds capacity (required). |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| bool ulJSObjectGetProperties | ( | ULJSValue | object, |
| const char *const * | names, | ||
| size_t | count, | ||
| ULJSValue * | out_values, | ||
| ULJSValue * | exception ) |
Get several properties of an object in one call.
| object | The object handle. |
| names | The property names as null-terminated UTF-8 strings (a NULL entry makes the call fail). |
| count | The number of entries in names and out_values. |
| out_values | An array of count slots that receives the values (undefined for a missing property). On success you must destroy each one with ulDestroyJSValue(). On failure nothing is stored. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
Get a property of an object (own or inherited).
| object | The object handle. |
| name | The property name as a null-terminated UTF-8 string. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
Get a property by numeric index (see ulJSObjectGetProperty()).
| object | The object handle. |
| index | The property index. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
Whether or not an object has a property (own or inherited).
Unlike ulJSObjectGetProperty(), this returns true for a property set to undefined and false for a missing one. It doesn't run getters.
| object | The object handle. |
| name | The property name as a null-terminated UTF-8 string. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| bool ulJSObjectSetProperty | ( | ULJSValue | object, |
| const char * | name, | ||
| ULJSValue | value, | ||
| unsigned | attributes, | ||
| ULJSValue * | exception ) |
Set a property of an object.
| object | The object handle. |
| name | The property name as a null-terminated UTF-8 string. |
| value | The value to assign (it remains yours; NULL assigns undefined). |
| attributes | ULJSPropertyAttributes flags combined with |. They only apply if this creates the property; pass kULJSPropertyAttributes_None for a normal assignment. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| bool ulJSObjectSetPropertyAtIndex | ( | ULJSValue | object, |
| size_t | index, | ||
| ULJSValue | value, | ||
| ULJSValue * | exception ) |
Set a property by numeric index (see ulJSObjectSetProperty()).
| object | The object handle. |
| index | The property index. |
| value | The value to assign (it remains yours; NULL assigns undefined). |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| size_t ulJSTypedArrayGetLength | ( | ULJSValue | typed_array | ) |
Get the number of elements in a typed array.
The ArrayBuffer can still be detached afterwards.
Unlike reading the array's length property, this never runs script.
| typed_array | The typed array. |
| ULJSContext ulJSValueGetContext | ( | ULJSValue | value | ) |
Get the context a value belongs to.
The context may already be gone (see ulJSContextIsAlive()).
| value | The value handle. |
| bool ulJSValueGetErrorDetails | ( | ULJSValue | error, |
| ULJSErrorDetails * | details ) |
Get the type, message, source location, and stack trace of an error in one call.
Use this on an exception from any function with an exception out-parameter. A thrown value that isn't an error object (eg, a thrown string) reports kULJSErrorType_Error, its string form as the message, and no source location or stack.
| error | The error value. |
| details | Receives the details. Every field is reset first, so it's safe to pass to ulJSErrorDetailsRelease() whatever this returns. |
Get the type of a JavaScript value.
| value | The value handle. |
| bool ulJSValueGetUTF8 | ( | ULJSValue | value, |
| char * | buffer, | ||
| size_t | capacity, | ||
| size_t * | out_length, | ||
| ULJSValue * | exception ) |
Convert to a string using JavaScript's rules and write it as UTF-8 into your buffer.
For a string value, a good pattern is a small stack buffer first, then a heap buffer of *out_length bytes if the text didn't fit. For any other value, each call converts it again (running any toString() method again), so convert once with ulJSValueToString() instead.
| value | The value handle. |
| buffer | The destination buffer (may be NULL when capacity is 0, to query the length). |
| capacity | The buffer size in bytes. |
| out_length | Receives the full UTF-8 length in bytes even when it exceeds capacity (no null terminator is written or counted). |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| bool ulJSValueIsAlive | ( | ULJSValue | value | ) |
Whether or not the value's page is still alive.
| value | The value handle. |
| bool ulJSValueIsArray | ( | ULJSValue | value | ) |
Whether or not the value is an Array.
| value | The value handle. |
| bool ulJSValueIsCallable | ( | ULJSValue | value | ) |
Whether or not the value can be called (see ulJSFunctionCall()).
| value | The value handle. |
| bool ulJSValueIsFunction | ( | ULJSValue | value | ) |
Whether or not the value is a function.
| value | The value handle. |
| bool ulJSValueIsPromise | ( | ULJSValue | value | ) |
Whether or not the value is a Promise.
This has no side effects: the promise isn't marked as handled.
| value | The value handle. |
| void ulJSValueReportUnobservedException | ( | ULJSValue | exception | ) |
Log a JavaScript exception that your code received but never read.
When JavaScript diagnostics are on (see ulJSAPISetDiagnostics()), this logs the exception's type, message, and stack to the Logger and, if the page is still alive, to the page's console. Otherwise it does nothing. A language binding built on this API can call it when a dropped result held an exception (the C++ js::Error does this in its destructor).
| exception | The exception (NULL for no report). The handle isn't consumed. |
| bool ulJSValueToBoolean | ( | ULJSValue | value, |
| bool * | result ) |
Convert to a boolean using JavaScript's rules (this never runs script).
| value | The value handle. |
| result | Receives the boolean. |
Convert to JSON text like JSON.stringify() does.
| value | The value handle. |
| indent | The number of spaces to indent each level by (0 for compact output, at most 10). |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
Convert to a number using JavaScript's rules.
| value | The value handle. |
| result | Receives the number. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
Convert to a string using JavaScript's rules.
| value | The value handle. |
| exception | A pointer to a ULJSValue in which to store an exception, if any. Pass NULL if you don't care about exceptions. If set, you own the stored handle. |
| #define ULJS_HANDLE_TYPES_DEFINED |
| typedef struct C_JSContext* ULJSContext |
Opaque handle to a JavaScript context (the script environment of the page in one frame).
| typedef ULJSValue(*) ULJSFunctionCallback(void *user_data, ULJSContext ctx, ULJSValue this_value, const ULJSValue *args, size_t argc, ULJSValue *exception) |
Callback invoked when JavaScript calls a function created with ulCreateJSFunction().
The callback can call back into JavaScript (eg, evaluate scripts or call functions).
| user_data | The user data passed to ulCreateJSFunction(). |
| ctx | The calling context (owned by the library, valid only during the callback; call ulCreateJSContextRef() to keep it). |
| this_value | The call's this value (owned by the library, valid only during the callback). |
| args | The arguments (owned by the library, valid only during the callback; call ulCreateJSValueRef() to keep one). |
| argc | The number of entries in args. |
| exception | To throw an exception into JavaScript, set *exception to a handle you own. The library takes ownership and ignores the return value. |
| typedef struct C_JSValue* ULJSValue |
Opaque handle to a JavaScript value.
While a handle exists, its value can't be garbage collected.
| enum ULJSErrorType |
Attribute flags for a new property (see ulJSObjectSetProperty() and ulJSClassAddMethod()).
| enum ULJSType |
The type of a JavaScript value.
The values stay the same across releases. Later versions may add types, so handle a value you don't know (eg, with a default case).