Mozilla wants to hear from developers like you.

JSAPI reference Redirect 1


The JSAPI is the C API for the SpiderMonkey JavaScript engine. To learn how to use the JSAPI, see the JSAPI User Guide and the JSAPI Cookbook.

Note: The FOSS wiki page contains a few links to other libraries and programs that can make life easier when using SpiderMonkey and the JSAPI.
A note on versioning: Up until the release of Firefox 4, SpiderMonkey, and thus the JSAPI, was versioned in an ad-hoc way, with releases happening at times that roughly, but not really, corresponded to Firefox releases. In the fall of 2012, the SpiderMonkey team decided to align releases with those of Firefox. The reference applies this versioning scheme retroactively starting with Firefox 5. Firefox 4 is the last release that has a corresponding SpiderMonkey release with the old scheme, where the JSAPI has the version 1.8.5.

Runtimes and contexts


Locale callbacks:

Locale callback types:


Just running some JavaScript code is straightforward:

You can instead compile JavaScript code into a JSScript which you can then execute multiple times.

You can also compile JavaScript code into a function:

Error handling

The following functions allow C/C++ functions to throw and catch JavaScript exceptions:

These functions translate errors into exceptions and vice versa:

Values and types

jsval constants:

Function and macros for checking the type of a jsval:

High-level type-conversion routines for packing and unpacking function arguments.

The following functions convert JS values to various types. They can be safely applied to jsvals of any type. They may return new objects. For example, JS_ValueToObject(cx, s) where s is a string creates a new String wrapper object. These functions may call JavaScript methods. For example, JS_ValueToString(cx, obj) may call obj.toString().

Fast, unchecked type-casting macros. These macros must not be applied to values that are not known to be the right type. Like C casts, they may cause crashes if applied to incorrect values. They never create new objects or call into JavaScript code.

A function that behaves like typeof:

And functions that behave like the equality operators:

Memory management

These functions act like the Standard C malloc family of functions, except that errors are reported using the SpiderMonkey error APIs rather than errno. These functions also allow SpiderMonkey to account the number of bytes allocated:

JavaScript objects, strings, and floating-point numbers are garbage collected. These functions provide access to the garbage collector:

The rest of these APIs help protect objects from being destroyed by the garbage collector before the application is done using them.

If a variable is a root, then anything it points to will not be freed by the garbage collector. Failure to root objects is a very common cause of mysterious crashes.

Local root scopes were another way of protecting objects from the garbage collector. They have been removed in JS 1.8.5, though

Added in SpiderMonkey 1.8 If an object contains references to other GC things that are not stored in SpiderMonkey data structures ("slots"), it must implement the JSTraceOp hook to enable the garbage collector to traverse those references. Otherwise the garbage collector will not find all reachable objects and may collect objects that are still reachable, leading to a crash. (In SpiderMonkey 1.7 and earlier, the JSMarkOp hook was used instead. This has since been removed.)

The tracing APIs are used by the garbage collector and JSTraceOp hooks. JSAPI applications may also use them to examine the object graph. (For example, these APIs support very smooth integration between the JS garbage collector and other garbage collectors.)

Miscellaneous GC APIs:



Interning strings tells the SpiderMonkey engine to reuse existing string objects when possible.

The character data for external strings is stored in memory provided by the application. Applications can use this to avoid copying data back and forth between SpiderMonkey's heap and application memory.




These functions correspond directly to the ways scripts access object properties:

The following functions are lower-level, allowing the JSAPI application more access to details of how properties are implemented. "Define" is a lower-level version of "set" that provides access to extra settings and does not call setters. Similarly, "lookup" is a lower-level version of "get" that offers extra options and does not call getters.

The following functions behave like JS_GetProperty and JS_GetPropertyById except when operating on E4X XML objects.

A SpiderMonkey extension allows a native function to return an lvalue—that is, a reference to a property of an object:

A jsid is kind of like a jsval, only different. Some APIs use jsids instead of jsvals for property names: JS_CheckAccess, JS_Enumerate, JS_NextProperty and all functions with names ending in "ById".

jsid constants:

Function and macros for checking the type of a jsid:


These API features are used to define custom classes—object types that are implemented in C/C++ code but accessible from JavaScript.

Adding native properties and methods to classes:

The behavior of a JSClass and its instances can be customized in many ways using callback functions.

JSClass method types:

JSExtendedClass method types:

In JS 1.8.5, JSExtendedClass has made private.

JSObjectOps method types:

In JS 1.8.5, JSObjectOps has been made private.

JSXMLObjectOps method types:

In JS 1.8.5, JSXMLObjectOps has been made private.

These stub functions can be used when creating a custom JSClass:

The behavior of a JSClass can be customized using these flags:



Calling a function or a method of an object:

Function accessors:

Creating functions:



  • struct JSStructuredCloneCallbacks, JS_SetStructuredCloneCallbacks
  • JS_ReadStructuredClone, JS_WriteStructuredClone
  • JS_StructuredClone
  • JS_ReadUint32Pair, JS_ReadBytes, JS_WriteUint32Pair, JS_WriteBytes


Added in SpiderMonkey 1.8.1 Security callbacks are set per-runtime.


The following functions support the SpiderMonkey threading model.

Note: JS_THREADSAFE is now permanently on and the JSRuntime is single-threaded. To execute more than one JS script at once, use multiple JSRuntimes.

JSAPI 1.7 and earlier They are only available in JS_THREADSAFE builds.

Added in SpiderMonkey 1.8 These functions are always available, but in non-JS_THREADSAFE builds, they do nothing.

The following functions are always available, but in non-JS_THREADSAFE builds, they do nothing:


Callback Types

Native function types:

Other callback types:

See also Classes, above.


C++ features

Tracing and debugging

Document Tags and Contributors

 Contributors to this page: Sheppy
 Last updated by: Sheppy,