Specifies a callback function that is automatically called whenever a JSContext is created or destroyed.


JS_SetContextCallback(JSRuntime *rt, JSContextCallback cxCallback, void *data);
Name Type Description
rt JSRuntime * Pointer to a JS runtime.
cxCallback JSContextCallback Pointer to the callback function, described below.
data void * Any data passed as 3rd argument to the callback function. Added in SpiderMonkey 31

Callback syntax

typedef bool
(* JSContextCallback)(JSContext *cx, unsigned contextOp, void *data);
Name Type Description
cx JSContext * Pointer to a JSContext which the callback may use to call into JSAPI functions. This is the context which was just created, or is about to be destroyed. For example, the callback my call JS_GetContextPrivate(cx) in order to free up resources previously allocated by JS_SetContextPrivate.
contextOp unsigned See JSContextOp. For future compatibility the callback must do nothing and return true if any other value is passed.
data void * data specified in JS_SetContextCallback. Added in SpiderMonkey 31
typedef enum JSContextOp {
} JSContextOp;
Enumeration Meaning
JSCONTEXT_NEW JS_NewContext successfully created a new JSContext instance. The callback can initialize the instance as required. If the callback returns JS_FALSE, the instance will be destroyed and JS_NewContext returns NULL. In this case the callback is not called again.
JSCONTEXT_DESTROY One of JS_DestroyContext, JS_DestroyContextNoGC, or JS_DestroyContextMaybeGC was called. The callback may perform its own cleanup and must always return true. In a DEBUG build, if the callback returns false, the JS engine will halt with an assertion.


JS_SetContextCallback specifies a callback function that is automatically called when JSContexts are created or destroyed. Only one callback function may be specified per JS runtime. This function returns the previous context callback, if any, else NULL.

See Also

Document Tags and Contributors

 Contributors to this page: arai, fscholz, Jorend, Wesgarland
 Last updated by: arai,