JSAPI Reference
From MDC
The JSAPI is the C API for the SpiderMonkey JavaScript engine. To learn how to use the JSAPI, see Embedding SpiderMonkey and the JSAPI Phrasebook.
Contents |
[edit] Runtimes and contexts
- typedef JSRuntime
- JS_NewRuntime
- JS_DestroyRuntime
- JS_ShutDown
- JS_GetRuntimePrivate
- JS_SetRuntimePrivate
- JS_Init Deprecated
- JS_Finish Deprecated
- JS_ContextIterator
- typedef JSContext
- JS_NewContext
- JS_DestroyContext
- JS_DestroyContextMaybeGC
- JS_DestroyContextNoGC
- JS_GetRuntime
- JS_GetContextPrivate
- JS_SetContextPrivate
- JS_GetOptions
- JS_SetOptions – JSOPTION_ATLINE, JSOPTION_COMPILE_N_GO, JSOPTION_DONT_REPORT_UNCAUGHT, JSOPTION_NATIVE_BRANCH_CALLBACK, JSOPTION_RELIMIT, JSOPTION_STRICT, JSOPTION_VAROBJFIX, JSOPTION_WERROR, JSOPTION_XML
- JS_ToggleOptions
- JS_SetBranchCallback
- JS_SetOperationCallback, JS_ClearOperationCallback, JS_GetOperationCallback, JS_GetOperationLimit, JS_SetOperationLimit, JS_MAX_OPERATION_LIMIT, JS_OPERATION_WEIGHT_BASE New in SpiderMonkey 1.8 (not yet released)
- JS_SetThreadStackLimit
- JS_SetScriptStackQuota
- JS_GetGlobalObject
- JS_SetGlobalObject
- JS_InitClass
- JS_InitStandardClasses
- JS_ResolveStandardClass
- JS_EnumerateStandardClasses
- JS_EnumerateResolvedStandardClasses
- JS_IsAssigning
- JS_IsConstructing
- JS_IsRunning
- JS_GetScopeChain
- JS_SaveFrameChain, JS_RestoreFrameChain
Locale callbacks:
Locale callback types:
[edit] Scripts
Just running some JavaScript code is straightforward:
- JS_EvaluateScript
- JS_EvaluateUCScript
- JS_EvaluateScriptForPrincipals
- JS_EvaluateUCScriptForPrincipals
You can instead compile JavaScript code into a JSScript which you can then execute multiple times.
- typedef JSScript
- JS_CompileFile
- JS_CompileFileHandle
- JS_CompileFileHandleForPrincipals
- JS_CompileScript
- JS_CompileUCScript
- JS_CompileScriptForPrincipals
- JS_CompileUCScriptForPrincipals
- JS_BufferIsCompilableUnit
- JS_GetScriptObject
- JS_NewScriptObject
- JS_ExecuteScript
- JS_ExecuteScriptPart
- JS_DecompileScript
- JS_DestroyScript
You can also compile JavaScript code into a function:
- typedef JSFunction
- JS_CompileFunction
- JS_CompileFunctionForPrincipals
- JS_CompileUCFunction
- JS_CompileUCFunctionForPrincipals
- JS_DecompileFunction
- JS_DecompileFunctionBody
[edit] Error handling
- struct JSErrorReport
- JS_ReportError
- JS_ReportWarning
- JS_ReportErrorNumber, JS_ReportErrorNumberUC, JS_ReportErrorFlagsAndNumber, JS_ReportErrorFlagsAndNumberUC
- JS_ReportOutOfMemory
- JS_SetErrorReporter
- JSREPORT_IS_EXCEPTION
- JSREPORT_IS_STRICT
- JSREPORT_IS_WARNING
The following functions allow C/C++ functions to throw and catch JavaScript exceptions:
- JS_IsExceptionPending
- JS_GetPendingException
- JS_SetPendingException
- JS_ClearPendingException
- JS_ThrowStopIteration
These functions translate errors into exceptions and vice versa:
[edit] Values and types
- typedef jsval
jsval constants:
Function and macros for checking the type of a jsval:
- JS_TypeOfValue
- JSVAL_IS_NULL
- JSVAL_IS_VOID
- JSVAL_IS_BOOLEAN
- JSVAL_IS_NUMBER
- JSVAL_IS_INT
- JSVAL_IS_DOUBLE
- JSVAL_IS_STRING
- JSVAL_IS_OBJECT
- JSVAL_IS_PRIMITIVE
- JSVAL_IS_GCTHING
An enumeration of all the JavaScript types:
- enum JSType
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().
- JS_ValueToBoolean
- JS_ValueToConstructor
- JS_ValueToECMAInt32
- JS_ValueToECMAUint32
- JS_ValueToFunction
- JS_ValueToInt32
- JS_ValueToNumber
- JS_ValueToObject
- JS_ValueToString
- JS_ValueToUint16
- JS_ConvertValue
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.
- JSVAL_TO_BOOLEAN, BOOLEAN_TO_JSVAL
- JSVAL_TO_INT, INT_TO_JSVAL, INT_FITS_IN_JSVAL
- JSVAL_TO_DOUBLE, DOUBLE_TO_JSVAL
- JSVAL_TO_OBJECT, OBJECT_TO_JSVAL
- JSVAL_TO_STRING, STRING_TO_JSVAL
- JSVAL_TO_GCTHING
- JSVAL_TO_PRIVATE, PRIVATE_TO_JSVAL
An enumeration of JavaScript language versions:
And:
[edit] 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:
JavaScript objects, strings, and floating-point numbers are garbage collected. These functions provide access to the garbage collector:
- JS_GC
- JS_MaybeGC
- JS_SetGCParameter, enum JSGCParamKey, JSGC_MAX_BYTES, JSGC_MAX_MALLOC_BYTES, JSGC_STACKPOOL_LIFESPAN
- JS_SetGCCallback, JS_SetGCCallbackRT, enum JSGCStatus, JSGC_BEGIN, JSGC_MARK_END, JSGC_FINALIZE_END, JSGC_END
- JS_MarkGCThing
- JS_IsAboutToBeFinalized
- JS_ClearNewbornRoots
- JS_SetGCZeal
- JS_DumpHeap New in SpiderMonkey 1.8 (not yet released)
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.
- JS_AddRoot
- JS_AddNamedRoot
- JS_AddNamedRootRT
- JS_RemoveRoot
- JS_RemoveRootRT
- JS_MapGCRoots, JSGCMapRootFun – JS_MAP_GCROOT_NEXT, JS_MAP_GCROOT_REMOVE, JS_MAP_GCROOT_STOP
- JS_DumpNamedRoots
Local root scopes are another way of protecting objects from the garbage collector.
New in SpiderMonkey 1.8 (not yet released) 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 is used instead. This will be deprecated when SpiderMonkey 1.8 is released.)
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.)
- JSVAL_IS_TRACEABLE New in SpiderMonkey 1.8 (not yet released)
- JSVAL_TO_TRACEABLE New in SpiderMonkey 1.8 (not yet released)
- JSVAL_TRACE_KIND New in SpiderMonkey 1.8 (not yet released)
- struct JSTracer New in SpiderMonkey 1.8 (not yet released)
- JS_TRACER_INIT New in SpiderMonkey 1.8 (not yet released)
- JS_CallTracer New in SpiderMonkey 1.8 (not yet released)
- JS_SET_TRACING_DETAILS New in SpiderMonkey 1.8 (not yet released)
- JS_SET_TRACING_INDEX New in SpiderMonkey 1.8 (not yet released)
- JS_SET_TRACING_NAME New in SpiderMonkey 1.8 (not yet released)
- JS_CALL_TRACER New in SpiderMonkey 1.8 (not yet released)
- JS_CALL_VALUE_TRACER New in SpiderMonkey 1.8 (not yet released)
- JS_CALL_OBJECT_TRACER New in SpiderMonkey 1.8 (not yet released)
- JS_CALL_STRING_TRACER New in SpiderMonkey 1.8 (not yet released)
- JS_CALL_DOUBLE_TRACER New in SpiderMonkey 1.8 (not yet released)
- JS_TraceChildren New in SpiderMonkey 1.8 (not yet released)
- JS_TraceRuntime New in SpiderMonkey 1.8 (not yet released)
- JS_PrintTraceThingInfo (DEBUG-only) New in SpiderMonkey 1.8 (not yet released)
Miscellaneous GC APIs:
- JSVAL_LOCK Deprecated
- JSVAL_UNLOCK Deprecated
- JS_LockGCThing Deprecated
- JS_LockGCThingRT
- JS_UnlockGCThing Deprecated
- JS_UnlockGCThingRT
[edit] Numbers
- JS_NewNumberValue, JS_NewDoubleValue, JS_NewDouble
- struct JSConstDoubleSpec
- JS_DefineConstDoubles
- JS_GetNaNValue
- JS_GetNegativeInfinityValue
- JS_GetPositiveInfinityValue
[edit] Strings
- typedef JSString
- typedef jschar
- JS_NewString
- JS_NewStringCopyN
- JS_NewStringCopyZ
- JS_NewUCString
- JS_NewUCStringCopyN
- JS_NewUCStringCopyZ
- JS_NewGrowableString
- JS_NewDependentString
- JS_GetEmptyStringValue
- JS_CompareStrings
- JS_ConcatStrings
- JS_GetStringBytes
- JS_GetStringChars
- JS_GetStringLength
- JS_MakeStringImmutable
- JS_UndependString
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.
- JS_AddExternalStringFinalizer
- JS_RemoveExternalStringFinalizer
- JS_GetExternalStringGCType
- JS_NewExternalString
[edit] Objects
- typedef JSObject
- JS_ConstructObject
- JS_ConstructObjectWithArguments
- JS_DefineObject
- JS_NewObject
- JS_NewObjectWithGivenProto
- JS_GET_CLASS
- JS_GetClass
- JS_GetClassObject
- JS_GetConstructor
- JS_GetGlobalForObject
- JS_GetInstancePrivate
- JS_GetParent, JS_SetParent
- JS_GetPrivate, JS_SetPrivate
- JS_GetPrototype, JS_SetPrototype
- JS_GetReservedSlot, JS_SetReservedSlot
- JS_HasInstance
- JS_InstanceOf
- JS_SealObject
[edit] Properties
- JS_AlreadyHasOwnElement, JS_AlreadyHasOwnProperty, JS_AlreadyHasOwnUCProperty New in SpiderMonkey 1.8 (not yet released)
- JS_ClearScope
- JS_DefineProperties
- JS_DefineProperty, JS_DefineUCProperty
- JS_DefinePropertyWithTinyId, JS_DefineUCPropertyWithTinyId
- JS_DeleteProperty
- JS_DeleteProperty2, JS_DeleteUCProperty2
- JS_Enumerate
- JS_GetProperty, JS_GetUCProperty
- JS_GetPropertyAttributes, JS_GetUCPropertyAttributes – JSPROP_ENUMERATE, JSPROP_EXPORTED, JSPROP_GETTER, JSPROP_INDEX, JSPROP_PERMANENT, JSPROP_READONLY, JSPROP_SETTER, JSPROP_SHARED
- JS_GetPropertyAttrsGetterAndSetter, JS_GetUCPropertyAttrsGetterAndSetter
- JS_HasProperty, JS_HasUCProperty
- JS_LookupProperty, JS_LookupUCProperty
- JS_LookupPropertyWithFlags
- JS_NewPropertyIterator
- JS_NextProperty
- JS_SetProperty, JS_SetUCProperty
- JS_SetPropertyAttributes, JS_SetUCPropertyAttributes
- JS_AliasProperty Deprecated
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. A handful of APIs use jsids instead of jsvals for property names: JS_CheckAccess, JS_Enumerate, JS_GetMethodById, and JS_NextProperty.
[edit] Classes
These API features are used to define custom classes—object types that are implemented in C/C++ code but accessible from JavaScript.
- struct JSClass
- struct JSExtendedClass
- struct JSObjectOps
- struct JSXMLObjectOps
- struct JSFunctionSpec
- struct JSProperty
- struct JSPropertySpec
- JS_InitClass
Adding native properties and methods to classes:
- JSNative
- JSFastNative
- struct JSFunctionSpec
- JS_FS
- JS_FN
- JS_FS_END
- struct JSPropertySpec
JSFastNative methods use these macros:
The behavior of a JSClass and its instances can be customized in many ways using callback functions.
JSClass method types:
- JSPropertyOp
- JSEnumerateOp
- JSNewEnumerateOp
- JSResolveOp
- JSNewResolveOp – JSRESOLVE_ASSIGNING, JSRESOLVE_CLASSNAME, JSRESOLVE_DECLARING, JSRESOLVE_DETECTING, JSRESOLVE_QUALIFIED
- JSConvertOp
- JSFinalizeOp
- JSGetObjectOps
- JSCheckAccessOp
- JSXDRObjectOp
- JSHasInstanceOp
- JSMarkOp
- JSTraceOp
- JSReserveSlotsOp
JSExtendedClass method types:
JSObjectOps method types:
- JSNewObjectMapOp
- JSObjectMapOp
- JSLookupPropOp
- JSDefinePropOp
- JSPropertyIdOp
- JSAttributesOp
- JSConvertOp
- JSNewEnumerateOp
- JSCheckAccessIdOp
- JSObjectOp
- JSPropertyRefOp
- JSXDRObjectOp
- JSHasInstanceOp
- JSSetObjectSlotOp
- JSTraceOp
- JSFinalizeOp
- JSGetRequiredSlotOp
- JSSetRequiredSlotOp
JSXMLObjectOps method types:
These stub functions can be used when creating a custom JSClass:
The behavior of a JSClass can be customized using these flags:
- JSClass.flags – JSCLASS_CONSTUCT_PROTOTYPE, JSCLASS_GLOBAL_FLAGS, JSCLASS_HAS_PRIVATE, JSCLASS_HAS_RESERVED_SLOTS, JSCLASS_IS_EXTENDED, JSCLASS_MARK_IS_TRACE, JSCLASS_NEW_ENUMERATE, JSCLASS_NEW_RESOLVE, JSCLASS_NEW_RESOLVE_GETS_START, JSCLASS_PRIVATE_IS_NSISUPPORTS, JSCLASS_SHARE_ALL_PROPERTIES
[edit] Arrays
- JS_AliasElement Deprecated
- JS_DefineElement
- JS_DeleteElement
- JS_DeleteElement2
- JS_GetElement
- JS_LookupElement
- JS_SetElement
[edit] Functions
Calling a function or a method of an object:
Function accessors:
- JS_ObjectIsFunction
- JS_GetFunctionArity
- JS_GetFunctionFlags
- JS_GetFunctionId
- JS_GetFunctionName Deprecated
- JS_GetFunctionObject
Creating functions:
[edit] RegExps
[edit] Security
- struct JSPrincipals
- JSPRINCIPALS_HOLD, JSPRINCIPALS_DROP
- JS_SetObjectPrincipalsFinder
- JS_SetPrincipalsTranscoder
- enum JSAccessMode – JSACC_PROTO, JSACC_PARENT, JSACC_IMPORT, JSACC_WATCH, JSACC_READ, JSACC_WRITE
- JS_CheckAccess
- JSObjectOps.checkAccess
- JSClass.checkAccess
- JS_SetCheckObjectAccessCallback
[edit] Threading
The following functions support the SpiderMonkey threading model. They are only available in JS_THREADSAFE builds.
- JS_BeginRequest
- JS_EndRequest
- JS_YieldRequest
- JS_SuspendRequest
- JS_ResumeRequest
- JS_GetContextThread
- JS_SetContextThread
- JS_ClearContextThread
The following functions exist in all builds, but in non-JS_THREADSAFE builds, they do nothing:
- JS_LockRuntime
- JS_UnlockRuntime
- JS_Lock Deprecated
- JS_Unlock Deprecated
[edit] Time
[edit] Callback Types
Native function types:
Other callback types:
- JSStringFinalizeOp - used by JS_AddExternalStringFinalizer
- JSTraceCallback - used by JS_TRACER_INIT
- JSTraceNamePrinter - used by JS_SET_TRACING_DETAILS
- JSContextCallback - used by JS_SetContextCallback
- JSGCCallback - used by JS_SetGCCallback
- JSTraceDataOp - used by JS_SetExtraGCRoots
- JSBranchCallback - used by JS_SetBranchCallback
- JSErrorReporter - used by JS_SetErrorReporter
- JSErrorCallback - used by JS_ReportErrorNumber and friends
- JSArgumentFormatter - used by JS_AddArgumentFormatter
- JSPrincipalsTranscoder - used by JS_SetPrincipalsTranscoder
- JSObjectPrincipalsFinder - used by JS_SetObjectPrincipalsFinder
- JSGCRootMapFun - used by JS_MapGCRoots
See also Classes, above.
[edit] Macros
- JS_DEFAULT_XML_NAMESPACE_ID
- JSFUN_BOUND_METHOD Deprecated
- JSFUN_GETTER
- JSFUN_GLOBAL_PARENT Deprecated
- JSFUN_HEAVYWEIGHT
- JSFUN_LAMBDA
- JSFUN_SETTER
- JSREG_GLOB
- JSREG_FOLD
- JSREG_MULTILINE
[edit] Preprocessor conditionals
[edit] C++ features
- class JSAutoRequest
- class JSAutoLocalRootScope