This article covers features introduced in SpiderMonkey 24

Note: the preferred way of changing a context's current compartment is using JSAutoCompartment.

Enter a different compartment on the given context, so that objects in that compartment can be accessed.


JSCompartment *
JS_EnterCompartment(JSContext *cx, JSObject *target);
Name Type Description
cx JSContext * The context on which a cross-compartment call is needed.
target JSObject *

The object in a different compartment to be accessed. This implicitly identifies the compartment to be entered.


Every JSContext has a current compartment. Any access to an object in a different compartment must be bracketed by calling JS_EnterCompartment and JS_LeaveCompartment.

On success, JS_EnterCompartment returns a pointer to the previously entered compartment. To return to that compartment, pass it to JS_LeaveCompartment. JS_EnterCompartment is infallible, so a NULL return value doesn't indicate failure.

See Also