SpiderMonkey Fundamentals
SpiderMonkey
Mozilla's
JavaScript
engine written in C/C++Compiles and executes scripts containing
JavaScript
statements and functions.Handles memory allocation for the objects needed to execute scripts, and cleans up—garbage collects—objects no longer needs.
Runtimes
JSRuntime
The space in which the JavaScript variables, objects, scripts, and contexts allocated.
JSContext
and object lives within aJSRuntime
and cannot travel to other runtimes or be shared across runtimes.Most applications only need one runtime.
C++
Cocos
Contexts
JSContext
Do things involving JavaScript code and objects.
Compile and execute scripts
Get and set object properties
Call JavaScript functions
Convert JavaScript data from one type to another
Create objects
JSAPI functions often require a
JSContext *
as the argumentRepresents the execution of JS code.
Contains a JS stack and is associated with a runtime.
C++
Cocos
Global Objects
mozilla::Maybe<JS::PersistentRootedObject>
Contains all the classes, functions, and variables available for JavaScript code to use.
JSAPI applications have full control over what global properties scripts can see.
Application starts out by creating an object and populating it with the standard JavaScript classes, like
Array
andObject
. Then adds custom classes, functions, and variables.
C++
Cocos
Compartments
JSCompartments
JSContexts
are control,JSCompartments
are data.JSCompartment
is a memory space thatobjects
and otherGCthings
are stored within.JSContext
is associated with a singleJSCompartment
at all times.JSContext
is "running inside" the associatedJSCompartment
.Any
object
created with theJSContext
will be physically stored within the context's currentJSCompartment
.
Cross-Compartment Call
To access data in another JSCompartment
.
JSContext
must first "enter" that otherJSCompartment
.A
JSContext
has a fieldcx->compartment
that gives the current compartment.To make a
cross-compartment call
,cx->compartment
is updated to the new compartment.
Only objects in the current compartment can be accessed, so to access an object in a different compartment, this containing compartment has to be entered first. Compartments have to be entered and left in LIFO order.
JSAutoCompartment
guarantees that by automatically entering the given compartment and leaving it upon getting out of scope:
C++
Last updated
Was this helpful?