|
Ultralight C API 2.0.0
|
JavaScript runtime functions for task posting, background work, and weak references.
#include <Ultralight/CAPI/CAPI_JSRuntime.h>
JavaScript contexts and values must stay on the Renderer's thread, so background threads can't touch them directly. This header provides the runtime functions that coordinate work between background threads and a page's JavaScript environment.
This example sets a page property from a background thread with a posted task:
Deferred work follows these rules:
Call ulJSRunOnWorker() to perform blocking operations without stalling the Renderer's thread. The call schedules work across two callbacks:
You should avoid long-running operations that occupy a worker thread for an extended time because the worker pool is shared across the entire library.
Unlike a ULJSValue handle, a weak reference doesn't keep its JavaScript object alive, allowing the garbage collector to reclaim it. Use weak references for caches and observers where native code needs to watch an object without preventing collection.
Classes | |
| struct | ULJSHandleStats |
| Counts of the live handles on one context, for finding leaks. More... | |
Functions | |
| void | ulJSContextPostTask (ULJSContext ctx, ULJSTaskCallback task, void *user_data, ULUserDataDestroyCallback destroy_user_data) |
| Post a task to run with a live context during a later ulUpdate(). | |
| void | ulJSRunOnWorker (ULJSWorkerCallback work, ULJSWorkerCallback complete, void *user_data, ULUserDataDestroyCallback destroy_user_data) |
| Run blocking work on a background thread, then a completion callback on the Renderer's thread. | |
| ULJSWeakRef | ulCreateJSWeakRef (ULJSValue object) |
| Create a weak reference to a JavaScript object. | |
| ULJSValue | ulJSWeakRefLock (ULJSWeakRef weak) |
| Get a handle to the object a weak reference observes. | |
| void | ulJSWeakRefSetCollectedCallback (ULJSWeakRef weak, ULJSTaskCallback callback, void *user_data, ULUserDataDestroyCallback destroy_user_data) |
| Set a callback to run after the object is garbage collected. | |
| void | ulDestroyJSWeakRef (ULJSWeakRef weak) |
| Destroy a weak reference (NULL-safe). | |
| void | ulJSContextAddDestroyedCallback (ULJSContext ctx, ULJSContextDestroyedCallback callback, void *user_data, ULUserDataDestroyCallback destroy_user_data) |
| Add a callback that runs on the Renderer's thread when a context goes away. | |
| bool | ulJSContextGetHandleStats (ULJSContext ctx, ULJSHandleStats *stats) |
| Count the live handles on a context. | |
Typedefs | |
| typedef struct C_JSWeakRef * | ULJSWeakRef |
| Opaque handle to a weak reference to a JavaScript object. | |
| typedef void(*) | ULJSTaskCallback(void *user_data, ULJSContext ctx) |
| Callback invoked on the Renderer's thread with a live context (for a posted task or a collected callback). | |
| typedef void(*) | ULJSContextDestroyedCallback(void *user_data, ULJSContext ctx) |
| Callback invoked on the Renderer's thread when a context goes away. | |
| typedef void(*) | ULJSWorkerCallback(void *user_data) |
| Callback for ulJSRunOnWorker(): work runs on a background thread and complete runs on the Renderer's thread. | |
| ULJSWeakRef ulCreateJSWeakRef | ( | ULJSValue | object | ) |
Create a weak reference to a JavaScript object.
Unlike a ULJSValue handle, a weak reference doesn't keep its object alive, so the garbage collector can reclaim the object. Use this for caches and observers.
| object | The object to observe. |
| void ulDestroyJSWeakRef | ( | ULJSWeakRef | weak | ) |
Destroy a weak reference (NULL-safe).
A pending collected callback is skipped (its user data is still destroyed).
| weak | The weak reference to destroy. |
| void ulJSContextAddDestroyedCallback | ( | ULJSContext | ctx, |
| ULJSContextDestroyedCallback | callback, | ||
| void * | user_data, | ||
| ULUserDataDestroyCallback | destroy_user_data ) |
Add a callback that runs on the Renderer's thread when a context goes away.
A context goes away when its page navigates away (including into the back-forward cache), its frame is removed, or its View or Renderer is destroyed. Language bindings can use this to clear their caches right away. If the context is already gone, the callback is skipped and destroy_user_data runs right away.
| ctx | The context. |
| callback | The callback to invoke when the context goes away. |
| user_data | Opaque user data passed to callback. |
| destroy_user_data | Callback invoked exactly once to destroy user_data after the callback runs or is skipped (may be NULL). |
| bool ulJSContextGetHandleStats | ( | ULJSContext | ctx, |
| ULJSHandleStats * | stats ) |
Count the live handles on a context.
Value handles that are never destroyed keep counting after the page navigates away, which is how a leak shows up.
| ctx | The context. |
| stats | Receives the counts. |
| void ulJSContextPostTask | ( | ULJSContext | ctx, |
| ULJSTaskCallback | task, | ||
| void * | user_data, | ||
| ULUserDataDestroyCallback | destroy_user_data ) |
Post a task to run with a live context during a later ulUpdate().
Use this to touch JavaScript from another thread. Tasks run in the order they were posted. If the context goes away before the task runs, the task is skipped.
| ctx | The context (you can destroy your handle right after posting). |
| task | The callback to invoke on the Renderer's thread. |
| user_data | Opaque user data passed to task. |
| destroy_user_data | Callback invoked exactly once to destroy user_data after the task runs or is skipped (may be NULL). |
| void ulJSRunOnWorker | ( | ULJSWorkerCallback | work, |
| ULJSWorkerCallback | complete, | ||
| void * | user_data, | ||
| ULUserDataDestroyCallback | destroy_user_data ) |
Run blocking work on a background thread, then a completion callback on the Renderer's thread.
Use this for slow work like file reads or database queries. Do the work in work without touching JavaScript, then use the result in complete (eg, to settle a promise).
The work runs on a small pool of background threads shared by the library (up to 4 threads, fewer on machines with few cores), so work that blocks for a long time delays the work queued behind it. complete runs during a later ulUpdate() after work returns, even if pages navigate or Views are destroyed in between.
| work | The callback to invoke on a background thread. |
| complete | The callback to invoke on the Renderer's thread after work returns (may be NULL). |
| user_data | Opaque user data passed to work and complete. |
| destroy_user_data | Callback invoked exactly once to destroy user_data after complete runs or the work is discarded (may be NULL). |
| ULJSValue ulJSWeakRefLock | ( | ULJSWeakRef | weak | ) |
Get a handle to the object a weak reference observes.
| weak | The weak reference. |
| void ulJSWeakRefSetCollectedCallback | ( | ULJSWeakRef | weak, |
| ULJSTaskCallback | callback, | ||
| void * | user_data, | ||
| ULUserDataDestroyCallback | destroy_user_data ) |
Set a callback to run after the object is garbage collected.
The callback runs once on the Renderer's thread during a later ulUpdate(). Setting a new callback replaces the old one (its user data is destroyed right away). If the page goes away or the weak reference is destroyed before the callback runs, it's skipped.
| weak | The weak reference. |
| callback | The callback to invoke after collection (NULL to clear). |
| user_data | Opaque user data passed to callback. |
| destroy_user_data | Callback invoked exactly once to destroy user_data after the callback runs or is skipped (may be NULL). |
| typedef void(*) ULJSContextDestroyedCallback(void *user_data, ULJSContext ctx) |
Callback invoked on the Renderer's thread when a context goes away.
The context is already gone. Use this to clear caches and destroy handles, not to run script.
| user_data | The user data you passed to ulJSContextAddDestroyedCallback(). |
| ctx | The context that went away (owned by the library). |
| typedef void(*) ULJSTaskCallback(void *user_data, ULJSContext ctx) |
Callback invoked on the Renderer's thread with a live context (for a posted task or a collected callback).
| user_data | The user data you passed along with the callback. |
| ctx | The context (owned by the library, live during the callback). |
| typedef struct C_JSWeakRef* ULJSWeakRef |
Opaque handle to a weak reference to a JavaScript object.
| typedef void(*) ULJSWorkerCallback(void *user_data) |
Callback for ulJSRunOnWorker(): work runs on a background thread and complete runs on the Renderer's thread.
| user_data | The user data you passed to ulJSRunOnWorker(). |
Don't touch JavaScript in work (values can't be created or read off the Renderer's thread). Compute a result there and use it in complete.
Don't let C++ exceptions escape either callback, since nothing catches them across the C boundary.