1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
48 #include "js-config.h"
55 * Type tags stored in the low bits of a jsval.
57 #define JSVAL_OBJECT 0x0 /* untagged reference to object */
58 #define JSVAL_INT 0x1 /* tagged 31-bit integer value */
59 #define JSVAL_DOUBLE 0x2 /* tagged reference to double */
60 #define JSVAL_STRING 0x4 /* tagged reference to string */
61 #define JSVAL_BOOLEAN 0x6 /* tagged boolean value */
63 /* Type tag bitfield length and derived macros. */
64 #define JSVAL_TAGBITS 3
65 #define JSVAL_TAGMASK JS_BITMASK(JSVAL_TAGBITS)
66 #define JSVAL_TAG(v) ((v) & JSVAL_TAGMASK)
67 #define JSVAL_SETTAG(v,t) ((v) | (t))
68 #define JSVAL_CLRTAG(v) ((v) & ~(jsval)JSVAL_TAGMASK)
69 #define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
71 /* Predicates for type testing. */
72 #define JSVAL_IS_OBJECT(v) (JSVAL_TAG(v) == JSVAL_OBJECT)
73 #define JSVAL_IS_NUMBER(v) (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))
74 #define JSVAL_IS_INT(v) ((v) & JSVAL_INT)
75 #define JSVAL_IS_DOUBLE(v) (JSVAL_TAG(v) == JSVAL_DOUBLE)
76 #define JSVAL_IS_STRING(v) (JSVAL_TAG(v) == JSVAL_STRING)
77 #define JSVAL_IS_BOOLEAN(v) (((v) & ~((jsval)1 << JSVAL_TAGBITS)) == \
79 #define JSVAL_IS_NULL(v) ((v) == JSVAL_NULL)
80 #define JSVAL_IS_VOID(v) ((v) == JSVAL_VOID)
81 #define JSVAL_IS_PRIMITIVE(v) (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))
83 /* Objects, strings, and doubles are GC'ed. */
84 #define JSVAL_IS_GCTHING(v) (!((v) & JSVAL_INT) && \
85 JSVAL_TAG(v) != JSVAL_BOOLEAN)
86 #define JSVAL_TO_GCTHING(v) ((void *)JSVAL_CLRTAG(v))
87 #define JSVAL_TO_OBJECT(v) ((JSObject *)JSVAL_TO_GCTHING(v))
88 #define JSVAL_TO_DOUBLE(v) ((jsdouble *)JSVAL_TO_GCTHING(v))
89 #define JSVAL_TO_STRING(v) ((JSString *)JSVAL_TO_GCTHING(v))
90 #define OBJECT_TO_JSVAL(obj) ((jsval)(obj))
91 #define DOUBLE_TO_JSVAL(dp) JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)
92 #define STRING_TO_JSVAL(str) JSVAL_SETTAG((jsval)(str), JSVAL_STRING)
94 /* Lock and unlock the GC thing held by a jsval. */
95 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
96 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
98 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
99 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
102 /* Domain limits for the jsval int type. */
103 #define JSVAL_INT_BITS 31
104 #define JSVAL_INT_POW2(n) ((jsval)1 << (n))
105 #define JSVAL_INT_MIN (-JSVAL_INT_POW2(30))
106 #define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
107 #define INT_FITS_IN_JSVAL(i) ((jsuint)(i) - (jsuint)JSVAL_INT_MIN <= \
108 (jsuint)(JSVAL_INT_MAX - JSVAL_INT_MIN))
109 #define JSVAL_TO_INT(v) ((jsint)(v) >> 1)
110 #define INT_TO_JSVAL(i) (((jsval)(i) << 1) | JSVAL_INT)
112 /* Convert between boolean and jsval. */
113 #define JSVAL_TO_BOOLEAN(v) ((JSBool)((v) >> JSVAL_TAGBITS))
114 #define BOOLEAN_TO_JSVAL(b) JSVAL_SETTAG((jsval)(b) << JSVAL_TAGBITS, \
117 /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
118 #define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
119 #define PRIVATE_TO_JSVAL(p) ((jsval)(p) | JSVAL_INT)
121 /* Property attributes, set in JSPropertySpec and passed to API functions. */
122 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
123 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
124 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
125 #define JSPROP_GETTER 0x10 /* property holds getter function */
126 #define JSPROP_SETTER 0x20 /* property holds setter function */
127 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
128 property; don't copy the property on
129 set of the same-named property in an
130 object that delegates to a prototype
131 containing this property */
132 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
134 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
135 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
136 #define JSFUN_GETTER JSPROP_GETTER
137 #define JSFUN_SETTER JSPROP_SETTER
138 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
139 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
141 #define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
142 #define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
144 #define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
145 #define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
146 #define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
147 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
149 #define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
151 #define JSFUN_THISP_FLAGS(f) (f)
152 #define JSFUN_THISP_TEST(f,t) ((f) & t)
154 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
155 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
156 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
157 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
159 #define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
161 #define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
162 note that bit #15 is used internally
163 to flag interpreted functions */
165 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
166 instead of defaulting to class gsops
167 for property holding function */
170 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
171 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
172 * methods of a class prototype that are exposed as static methods taking an
173 * extra leading argument: the generic |this| parameter.
175 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
176 * that struct must live at least as long as the native static method object
177 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
178 * JSFunctionSpec structs are allocated in static arrays.
180 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
183 * Well-known JS values. The extern'd variables are initialized when the
184 * first JSContext is created by JS_NewContext (see below).
186 #define JSVAL_VOID BOOLEAN_TO_JSVAL(2)
187 #define JSVAL_NULL OBJECT_TO_JSVAL(0)
188 #define JSVAL_ZERO INT_TO_JSVAL(0)
189 #define JSVAL_ONE INT_TO_JSVAL(1)
190 #define JSVAL_FALSE BOOLEAN_TO_JSVAL(JS_FALSE)
191 #define JSVAL_TRUE BOOLEAN_TO_JSVAL(JS_TRUE)
194 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
195 * comment in jstypes.h regarding safe int64 usage.
197 extern JS_PUBLIC_API(int64
)
200 /* Don't want to export data, so provide accessors for non-inline jsvals. */
201 extern JS_PUBLIC_API(jsval
)
202 JS_GetNaNValue(JSContext
*cx
);
204 extern JS_PUBLIC_API(jsval
)
205 JS_GetNegativeInfinityValue(JSContext
*cx
);
207 extern JS_PUBLIC_API(jsval
)
208 JS_GetPositiveInfinityValue(JSContext
*cx
);
210 extern JS_PUBLIC_API(jsval
)
211 JS_GetEmptyStringValue(JSContext
*cx
);
214 * Format is a string of the following characters (spaces are insignificant),
215 * specifying the tabulated type conversions:
218 * c uint16/jschar ECMA uint16, Unicode char
220 * u uint32 ECMA uint32
221 * j int32 Rounded int32 (coordinate)
222 * d jsdouble IEEE double
223 * I jsdouble Integral IEEE double
225 * S JSString * Unicode string, accessed by a JSString pointer
226 * W jschar * Unicode character vector, 0-terminated (W for wide)
227 * o JSObject * Object reference
228 * f JSFunction * Function private
229 * v jsval Argument value (no conversion)
230 * * N/A Skip this argument (no vararg)
231 * / N/A End of required arguments
233 * The variable argument list after format must consist of &b, &c, &s, e.g.,
234 * where those variables have the types given above. For the pointer types
235 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
236 * to the JS runtime, not to the calling native code. The runtime promises
237 * to keep this memory valid so long as argv refers to allocated stack space
238 * (so long as the native function is active).
240 * Fewer arguments than format specifies may be passed only if there is a /
241 * in format after the last required argument specifier and argc is at least
242 * the number of required arguments. More arguments than format specifies
243 * may be passed without error; it is up to the caller to deal with trailing
244 * unconverted arguments.
246 extern JS_PUBLIC_API(JSBool
)
247 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
251 extern JS_PUBLIC_API(JSBool
)
252 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
253 const char *format
, va_list ap
);
257 * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
258 * into jsvals, GC-rooted if necessary by the JS stack. Return null on error,
259 * and a pointer to the new argument vector on success. Also return a stack
260 * mark on success via *markp, in which case the caller must eventually clean
261 * up by calling JS_PopArguments.
263 * Note that the number of actual arguments supplied is specified exclusively
264 * by format, so there is no argc parameter.
266 extern JS_PUBLIC_API(jsval
*)
267 JS_PushArguments(JSContext
*cx
, void **markp
, const char *format
, ...);
270 extern JS_PUBLIC_API(jsval
*)
271 JS_PushArgumentsVA(JSContext
*cx
, void **markp
, const char *format
, va_list ap
);
274 extern JS_PUBLIC_API(void)
275 JS_PopArguments(JSContext
*cx
, void *mark
);
277 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
280 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
281 * The handler function has this signature (see jspubtd.h):
283 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
284 * JSBool fromJS, jsval **vpp, va_list *app);
286 * It should return true on success, and return false after reporting an error
287 * or detecting an already-reported error.
289 * For a given format string, for example "AA", the formatter is called from
290 * JS_ConvertArgumentsVA like so:
292 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
294 * sp points into the arguments array on the JS stack, while ap points into
295 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
296 * the formatter to convert zero or more jsvals at sp to zero or more C values
297 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
298 * (via *app) to point past the converted arguments and their result pointers
301 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
303 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
305 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
306 * format specifier and store them at sp, updating ap and sp appropriately.
308 * The "..." after "AA" is the rest of the format string that was passed into
309 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
310 * in each Convert or PushArguments call is passed to the formatter, so that
311 * one such function may implement several formats, in order to share code.
313 * Remove just forgets about any handler associated with format. Add does not
314 * copy format, it points at the string storage allocated by the caller, which
315 * is typically a string constant. If format is in dynamic storage, it is up
316 * to the caller to keep the string alive until Remove is called.
318 extern JS_PUBLIC_API(JSBool
)
319 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
320 JSArgumentFormatter formatter
);
322 extern JS_PUBLIC_API(void)
323 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
325 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
327 extern JS_PUBLIC_API(JSBool
)
328 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
330 extern JS_PUBLIC_API(JSBool
)
331 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
333 extern JS_PUBLIC_API(JSFunction
*)
334 JS_ValueToFunction(JSContext
*cx
, jsval v
);
336 extern JS_PUBLIC_API(JSFunction
*)
337 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
339 extern JS_PUBLIC_API(JSString
*)
340 JS_ValueToString(JSContext
*cx
, jsval v
);
342 extern JS_PUBLIC_API(JSString
*)
343 JS_ValueToSource(JSContext
*cx
, jsval v
);
345 extern JS_PUBLIC_API(JSBool
)
346 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
349 * Convert a value to a number, then to an int32, according to the ECMA rules
352 extern JS_PUBLIC_API(JSBool
)
353 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
356 * Convert a value to a number, then to a uint32, according to the ECMA rules
359 extern JS_PUBLIC_API(JSBool
)
360 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
363 * Convert a value to a number, then to an int32 if it fits by rounding to
364 * nearest; but failing with an error report if the double is out of range
367 extern JS_PUBLIC_API(JSBool
)
368 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
371 * ECMA ToUint16, for mapping a jsval to a Unicode point.
373 extern JS_PUBLIC_API(JSBool
)
374 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
376 extern JS_PUBLIC_API(JSBool
)
377 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
379 extern JS_PUBLIC_API(JSType
)
380 JS_TypeOfValue(JSContext
*cx
, jsval v
);
382 extern JS_PUBLIC_API(const char *)
383 JS_GetTypeName(JSContext
*cx
, JSType type
);
385 /************************************************************************/
388 * Initialization, locking, contexts, and memory allocation.
390 * It is important that the first runtime and first context be created in a
391 * single-threaded fashion, otherwise the behavior of the library is undefined.
392 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
394 #define JS_NewRuntime JS_Init
395 #define JS_DestroyRuntime JS_Finish
396 #define JS_LockRuntime JS_Lock
397 #define JS_UnlockRuntime JS_Unlock
399 extern JS_PUBLIC_API(JSRuntime
*)
400 JS_NewRuntime(uint32 maxbytes
);
402 extern JS_PUBLIC_API(void)
403 JS_DestroyRuntime(JSRuntime
*rt
);
405 extern JS_PUBLIC_API(void)
408 JS_PUBLIC_API(void *)
409 JS_GetRuntimePrivate(JSRuntime
*rt
);
412 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
414 extern JS_PUBLIC_API(void)
415 JS_BeginRequest(JSContext
*cx
);
417 extern JS_PUBLIC_API(void)
418 JS_EndRequest(JSContext
*cx
);
420 /* Yield to pending GC operations, regardless of request depth */
421 extern JS_PUBLIC_API(void)
422 JS_YieldRequest(JSContext
*cx
);
424 extern JS_PUBLIC_API(jsrefcount
)
425 JS_SuspendRequest(JSContext
*cx
);
427 extern JS_PUBLIC_API(void)
428 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
433 class JSAutoRequest
{
435 JSAutoRequest(JSContext
*cx
) : mContext(cx
), mSaveDepth(0) {
436 JS_BeginRequest(mContext
);
439 JS_EndRequest(mContext
);
443 mSaveDepth
= JS_SuspendRequest(mContext
);
446 JS_ResumeRequest(mContext
, mSaveDepth
);
451 jsrefcount mSaveDepth
;
455 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
456 static void operator delete(void *, size_t) { };
460 class JSAutoSuspendRequest
{
462 JSAutoSuspendRequest(JSContext
*cx
) : mContext(cx
), mSaveDepth(0) {
464 mSaveDepth
= JS_SuspendRequest(mContext
);
467 ~JSAutoSuspendRequest() {
473 JS_ResumeRequest(mContext
, mSaveDepth
);
480 jsrefcount mSaveDepth
;
484 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
485 static void operator delete(void *, size_t) { };
492 extern JS_PUBLIC_API(void)
493 JS_Lock(JSRuntime
*rt
);
495 extern JS_PUBLIC_API(void)
496 JS_Unlock(JSRuntime
*rt
);
498 extern JS_PUBLIC_API(JSContextCallback
)
499 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
501 extern JS_PUBLIC_API(JSContext
*)
502 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
504 extern JS_PUBLIC_API(void)
505 JS_DestroyContext(JSContext
*cx
);
507 extern JS_PUBLIC_API(void)
508 JS_DestroyContextNoGC(JSContext
*cx
);
510 extern JS_PUBLIC_API(void)
511 JS_DestroyContextMaybeGC(JSContext
*cx
);
513 extern JS_PUBLIC_API(void *)
514 JS_GetContextPrivate(JSContext
*cx
);
516 extern JS_PUBLIC_API(void)
517 JS_SetContextPrivate(JSContext
*cx
, void *data
);
519 extern JS_PUBLIC_API(JSRuntime
*)
520 JS_GetRuntime(JSContext
*cx
);
522 extern JS_PUBLIC_API(JSContext
*)
523 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
525 extern JS_PUBLIC_API(JSVersion
)
526 JS_GetVersion(JSContext
*cx
);
528 extern JS_PUBLIC_API(JSVersion
)
529 JS_SetVersion(JSContext
*cx
, JSVersion version
);
531 extern JS_PUBLIC_API(const char *)
532 JS_VersionToString(JSVersion version
);
534 extern JS_PUBLIC_API(JSVersion
)
535 JS_StringToVersion(const char *string
);
538 * JS options are orthogonal to version, and may be freely composed with one
539 * another as well as with version.
541 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
542 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
544 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
545 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
546 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
547 the last object on its 'obj'
548 param's scope chain as the
549 ECMA 'variables object' */
550 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
551 JS_BIT(3) /* context private data points
552 to an nsISupports subclass */
553 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
554 promises to execute compiled
555 script once only; enables
556 compile-time scope chain
557 resolution of consts. */
558 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
559 option supported for the
560 XUL preprocessor and kindred
562 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
563 parse <!-- --> as a token,
564 not backward compatible with
565 the comment-hiding hack used
566 in HTML script tags. */
567 #define JSOPTION_NATIVE_BRANCH_CALLBACK \
568 JS_BIT(7) /* the branch callback set by
569 JS_SetBranchCallback may be
570 called with a null script
571 parameter, by native code
572 that loops intensively.
574 JS_SetOperationCallback
576 #define JSOPTION_DONT_REPORT_UNCAUGHT \
577 JS_BIT(8) /* When returning from the
578 outermost API call, prevent
579 uncaught exceptions from
580 being converted to error
583 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
584 regular expression which
585 backtracks more than n^3
586 times, where n is length
587 of the input string */
588 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
589 statement context per
590 ECMA-262 Edition 3. */
592 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
594 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
595 that a null rval out-param
596 will be passed to each call
597 to JS_ExecuteScript. */
598 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
599 global objects leaving
600 that up to the embedding. */
602 extern JS_PUBLIC_API(uint32
)
603 JS_GetOptions(JSContext
*cx
);
605 extern JS_PUBLIC_API(uint32
)
606 JS_SetOptions(JSContext
*cx
, uint32 options
);
608 extern JS_PUBLIC_API(uint32
)
609 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
611 extern JS_PUBLIC_API(const char *)
612 JS_GetImplementationVersion(void);
614 extern JS_PUBLIC_API(JSObject
*)
615 JS_GetGlobalObject(JSContext
*cx
);
617 extern JS_PUBLIC_API(void)
618 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
621 * Initialize standard JS class constructors, prototypes, and any top-level
622 * functions and constants associated with the standard classes (e.g. isNaN
625 * NB: This sets cx's global object to obj if it was null.
627 extern JS_PUBLIC_API(JSBool
)
628 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
631 * Resolve id, which must contain either a string or an int, to a standard
632 * class name in obj if possible, defining the class's constructor and/or
633 * prototype and storing true in *resolved. If id does not name a standard
634 * class or a top-level property induced by initializing a standard class,
635 * store false in *resolved and just return true. Return false on error,
636 * as usual for JSBool result-typed API entry points.
638 * This API can be called directly from a global object class's resolve op,
639 * to define standard classes lazily. The class's enumerate op should call
640 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
641 * loops any classes not yet resolved lazily.
643 extern JS_PUBLIC_API(JSBool
)
644 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsval id
,
647 extern JS_PUBLIC_API(JSBool
)
648 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
651 * Enumerate any already-resolved standard class ids into ida, or into a new
652 * JSIdArray if ida is null. Return the augmented array on success, null on
653 * failure with ida (if it was non-null on entry) destroyed.
655 extern JS_PUBLIC_API(JSIdArray
*)
656 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
659 extern JS_PUBLIC_API(JSBool
)
660 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
663 extern JS_PUBLIC_API(JSObject
*)
664 JS_GetScopeChain(JSContext
*cx
);
666 extern JS_PUBLIC_API(JSObject
*)
667 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
670 * Macros to hide interpreter stack layout details from a JSFastNative using
671 * its jsval *vp parameter. The stack layout underlying invocation can't change
672 * without breaking source and binary compatibility (argv[-2] is well-known to
673 * be the callee jsval, and argv[-1] is as well known to be |this|).
675 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
676 * is the global object, so embeddings implementing fast natives *must* call
677 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
678 * which should propagate as a false return from native functions and hooks.
680 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
681 * handle a null obj parameter by returning false (throwing a TypeError if
682 * given non-null argv), so most native functions that type-check their |this|
683 * parameter need not add null checking.
685 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
686 * methods that may inspect their callee must defer setting their return value
687 * until after any such possible inspection. Otherwise the return value will be
688 * inspected instead of the callee function object.
690 * WARNING: These are not (yet) mandatory macros, but new code outside of the
691 * engine should use them. In the Mozilla 2.0 milestone their definitions may
692 * change incompatibly.
694 #define JS_CALLEE(cx,vp) ((vp)[0])
695 #define JS_ARGV_CALLEE(argv) ((argv)[-2])
696 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
697 #define JS_THIS_OBJECT(cx,vp) ((JSObject *) JS_THIS(cx,vp))
698 #define JS_ARGV(cx,vp) ((vp) + 2)
699 #define JS_RVAL(cx,vp) (*(vp))
700 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
702 extern JS_PUBLIC_API(jsval
)
703 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
705 extern JS_PUBLIC_API(void *)
706 JS_malloc(JSContext
*cx
, size_t nbytes
);
708 extern JS_PUBLIC_API(void *)
709 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
711 extern JS_PUBLIC_API(void)
712 JS_free(JSContext
*cx
, void *p
);
714 extern JS_PUBLIC_API(char *)
715 JS_strdup(JSContext
*cx
, const char *s
);
717 extern JS_PUBLIC_API(jsdouble
*)
718 JS_NewDouble(JSContext
*cx
, jsdouble d
);
720 extern JS_PUBLIC_API(JSBool
)
721 JS_NewDoubleValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
723 extern JS_PUBLIC_API(JSBool
)
724 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
727 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
728 * itself points into the GC heap (more recently, we support this extension:
729 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
731 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
732 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
733 * or the structure it is embedded within goes out of scope or is freed, you
734 * must call JS_RemoveRoot(cx, &obj).
736 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
737 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
738 * roots by their source callsites. This way, you can find the callsite while
739 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
740 * before freeing structPtr's memory.
742 extern JS_PUBLIC_API(JSBool
)
743 JS_AddRoot(JSContext
*cx
, void *rp
);
745 #ifdef NAME_ALL_GC_ROOTS
746 #define JS_DEFINE_TO_TOKEN(def) #def
747 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
748 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
751 extern JS_PUBLIC_API(JSBool
)
752 JS_AddNamedRoot(JSContext
*cx
, void *rp
, const char *name
);
754 extern JS_PUBLIC_API(JSBool
)
755 JS_AddNamedRootRT(JSRuntime
*rt
, void *rp
, const char *name
);
757 extern JS_PUBLIC_API(JSBool
)
758 JS_RemoveRoot(JSContext
*cx
, void *rp
);
760 extern JS_PUBLIC_API(JSBool
)
761 JS_RemoveRootRT(JSRuntime
*rt
, void *rp
);
764 * The last GC thing of each type (object, string, double, external string
765 * types) created on a given context is kept alive until another thing of the
766 * same type is created, using a newborn root in the context. These newborn
767 * roots help native code protect newly-created GC-things from GC invocations
768 * activated before those things can be rooted using local or global roots.
770 * However, the newborn roots can also entrain great gobs of garbage, so the
771 * JS_GC entry point clears them for the context on which GC is being forced.
772 * Embeddings may need to do likewise for all contexts.
774 * See the scoped local root API immediately below for a better way to manage
775 * newborns in cases where native hooks (functions, getters, setters, etc.)
776 * create many GC-things, potentially without connecting them to predefined
777 * local roots such as *rval or argv[i] in an active native function. Using
778 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
779 * newborn roots, until control flow unwinds and leaves the outermost nesting
782 extern JS_PUBLIC_API(void)
783 JS_ClearNewbornRoots(JSContext
*cx
);
786 * Scoped local root management allows native functions, getter/setters, etc.
787 * to avoid worrying about the newborn root pigeon-holes, overloading local
788 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
789 * and JS_RemoveRoot to manage global roots temporarily.
791 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
792 * the body of the native hook causes the engine to allocate a local root for
793 * each newborn created in between the two API calls, using a local root stack
794 * associated with cx. For example:
797 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
801 * if (!JS_EnterLocalRootScope(cx))
803 * ok = my_GetPropertyBody(cx, obj, id, vp);
804 * JS_LeaveLocalRootScope(cx);
808 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
809 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
810 * not make the matching JS_LeaveLocalRootScope call.
812 * JS_LeaveLocalRootScopeWithResult(cx, rval) is an alternative way to leave
813 * a local root scope that protects a result or return value, by effectively
814 * pushing it in the caller's local root scope.
816 * In case a native hook allocates many objects or other GC-things, but the
817 * native protects some of those GC-things by storing them as property values
818 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
819 * to free the local root automatically pushed for the now-protected GC-thing.
821 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
822 * root scope, but it's more time-efficient when called on references to more
823 * recently created GC-things. Calling it successively on other than the most
824 * recently allocated GC-thing will tend to average the time inefficiency, and
825 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
826 * many local roots if you can root as you go (build a tree of objects from
827 * the top down, forgetting each latest-allocated GC-thing immediately upon
828 * linking it to its parent).
830 extern JS_PUBLIC_API(JSBool
)
831 JS_EnterLocalRootScope(JSContext
*cx
);
833 extern JS_PUBLIC_API(void)
834 JS_LeaveLocalRootScope(JSContext
*cx
);
836 extern JS_PUBLIC_API(void)
837 JS_LeaveLocalRootScopeWithResult(JSContext
*cx
, jsval rval
);
839 extern JS_PUBLIC_API(void)
840 JS_ForgetLocalRoot(JSContext
*cx
, void *thing
);
845 class JSAutoLocalRootScope
{
847 JSAutoLocalRootScope(JSContext
*cx
) : mContext(cx
) {
848 JS_EnterLocalRootScope(mContext
);
850 ~JSAutoLocalRootScope() {
851 JS_LeaveLocalRootScope(mContext
);
854 void forget(void *thing
) {
855 JS_ForgetLocalRoot(mContext
, thing
);
863 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
864 static void operator delete(void *, size_t) { };
872 extern JS_PUBLIC_API(void)
873 JS_DumpNamedRoots(JSRuntime
*rt
,
874 void (*dump
)(const char *name
, void *rp
, void *data
),
879 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
880 * The root is pointed at by rp; if the root is unnamed, name is null; data is
881 * supplied from the third parameter to JS_MapGCRoots.
883 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
884 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
885 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
886 * constants are flags; you can OR them together.
888 * This function acquires and releases rt's GC lock around the mapping of the
889 * roots table, so the map function should run to completion in as few cycles
890 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
891 * or any JS API entry point that acquires locks, without double-tripping or
892 * deadlocking on the GC lock.
894 * JS_MapGCRoots returns the count of roots that were successfully mapped.
896 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
897 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
898 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
901 (* JSGCRootMapFun
)(void *rp
, const char *name
, void *data
);
903 extern JS_PUBLIC_API(uint32
)
904 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
906 extern JS_PUBLIC_API(JSBool
)
907 JS_LockGCThing(JSContext
*cx
, void *thing
);
909 extern JS_PUBLIC_API(JSBool
)
910 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
912 extern JS_PUBLIC_API(JSBool
)
913 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
915 extern JS_PUBLIC_API(JSBool
)
916 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
919 * Register externally maintained GC roots.
921 * traceOp: the trace operation. For each root the implementation should call
922 * JS_CallTracer whenever the root contains a traceable thing.
923 * data: the data argument to pass to each invocation of traceOp.
925 extern JS_PUBLIC_API(void)
926 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
929 * For implementors of JSMarkOp. All new code should implement JSTraceOp
932 extern JS_PUBLIC_API(void)
933 JS_MarkGCThing(JSContext
*cx
, void *thing
, const char *name
, void *arg
);
936 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
937 * enumerate all references to traceable things reachable via a property or
938 * other strong ref identified for debugging purposes by name or index or
941 * By definition references to traceable things include non-null pointers
942 * to JSObject, JSString and jsdouble and corresponding jsvals.
944 * See the JSTraceOp typedef in jspubtd.h.
947 /* Trace kinds to pass to JS_Tracing. */
948 #define JSTRACE_OBJECT 0
949 #define JSTRACE_DOUBLE 1
950 #define JSTRACE_STRING 2
953 * Use the following macros to check if a particular jsval is a traceable
954 * thing and to extract the thing and its kind to pass to JS_CallTracer.
956 #define JSVAL_IS_TRACEABLE(v) (JSVAL_IS_GCTHING(v) && !JSVAL_IS_NULL(v))
957 #define JSVAL_TO_TRACEABLE(v) (JSVAL_TO_GCTHING(v))
958 #define JSVAL_TRACE_KIND(v) (JSVAL_TAG(v) >> 1)
960 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_OBJECT
) == JSTRACE_OBJECT
);
961 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_DOUBLE
) == JSTRACE_DOUBLE
);
962 JS_STATIC_ASSERT(JSVAL_TRACE_KIND(JSVAL_STRING
) == JSTRACE_STRING
);
966 JSTraceCallback callback
;
968 JSTraceNamePrinter debugPrinter
;
969 const void *debugPrintArg
;
970 size_t debugPrintIndex
;
975 * The method to call on each reference to a traceable thing stored in a
976 * particular JSObject or other runtime structure. With DEBUG defined the
977 * caller before calling JS_CallTracer must initialize JSTracer fields
978 * describing the reference using the macros below.
980 extern JS_PUBLIC_API(void)
981 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
984 * Set debugging information about a reference to a traceable thing to prepare
985 * for the following call to JS_CallTracer.
987 * When printer is null, arg must be const char * or char * C string naming
988 * the reference and index must be either (size_t)-1 indicating that the name
989 * alone describes the reference or it must be an index into some array vector
990 * that stores the reference.
992 * When printer callback is not null, the arg and index arguments are
993 * available to the callback as debugPrinterArg and debugPrintIndex fields
996 * The storage for name or callback's arguments needs to live only until
997 * the following call to JS_CallTracer returns.
1000 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1002 (trc)->debugPrinter = (printer); \
1003 (trc)->debugPrintArg = (arg); \
1004 (trc)->debugPrintIndex = (index); \
1007 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1013 * Convenience macro to describe the argument of JS_CallTracer using C string
1016 # define JS_SET_TRACING_INDEX(trc, name, index) \
1017 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1020 * Convenience macro to describe the argument of JS_CallTracer using C string.
1022 # define JS_SET_TRACING_NAME(trc, name) \
1023 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1026 * Convenience macro to invoke JS_CallTracer using C string as the name for
1027 * the reference to a traceable thing.
1029 # define JS_CALL_TRACER(trc, thing, kind, name) \
1031 JS_SET_TRACING_NAME(trc, name); \
1032 JS_CallTracer((trc), (thing), (kind)); \
1036 * Convenience macros to invoke JS_CallTracer when jsval represents a
1037 * reference to a traceable thing.
1039 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1041 if (JSVAL_IS_TRACEABLE(val)) { \
1042 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1043 JSVAL_TRACE_KIND(val), name); \
1047 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1049 JSObject *obj_ = (object); \
1051 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1054 #define JS_CALL_STRING_TRACER(trc, string, name) \
1056 JSString *str_ = (string); \
1058 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1061 #define JS_CALL_DOUBLE_TRACER(trc, number, name) \
1063 jsdouble *num_ = (number); \
1065 JS_CALL_TRACER((trc), num_, JSTRACE_DOUBLE, name); \
1069 * API for JSTraceCallback implementations.
1071 # define JS_TRACER_INIT(trc, cx_, callback_) \
1073 (trc)->context = (cx_); \
1074 (trc)->callback = (callback_); \
1075 JS_SET_TRACING_DETAILS(trc, NULL, NULL, (size_t)-1); \
1078 extern JS_PUBLIC_API(void)
1079 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1081 extern JS_PUBLIC_API(void)
1082 JS_TraceRuntime(JSTracer
*trc
);
1086 extern JS_PUBLIC_API(void)
1087 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1088 void *thing
, uint32 kind
, JSBool includeDetails
);
1091 * DEBUG-only method to dump the object graph of heap-allocated things.
1093 * fp: file for the dump output.
1094 * start: when non-null, dump only things reachable from start
1095 * thing. Otherwise dump all things reachable from the
1097 * startKind: trace kind of start if start is not null. Must be 0 when
1099 * thingToFind: dump only paths in the object graph leading to thingToFind
1101 * maxDepth: the upper bound on the number of edges to descend from the
1103 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1105 extern JS_PUBLIC_API(JSBool
)
1106 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1107 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1112 * Garbage collector API.
1114 extern JS_PUBLIC_API(void)
1115 JS_GC(JSContext
*cx
);
1117 extern JS_PUBLIC_API(void)
1118 JS_MaybeGC(JSContext
*cx
);
1120 extern JS_PUBLIC_API(JSGCCallback
)
1121 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1123 extern JS_PUBLIC_API(JSGCCallback
)
1124 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1126 extern JS_PUBLIC_API(JSBool
)
1127 JS_IsGCMarkingTracer(JSTracer
*trc
);
1129 extern JS_PUBLIC_API(JSBool
)
1130 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1132 typedef enum JSGCParamKey
{
1133 /* Maximum nominal heap before last ditch GC. */
1136 /* Number of JS_malloc bytes before last ditch GC. */
1137 JSGC_MAX_MALLOC_BYTES
= 1,
1139 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1140 JSGC_STACKPOOL_LIFESPAN
= 2
1143 extern JS_PUBLIC_API(void)
1144 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1147 * Add a finalizer for external strings created by JS_NewExternalString (see
1148 * below) using a type-code returned from this function, and that understands
1149 * how to free or release the memory pointed at by JS_GetStringChars(str).
1151 * Return a nonnegative type index if there is room for finalizer in the
1152 * global GC finalizers table, else return -1. If the engine is compiled
1153 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1154 * be invoked on the primordial thread only, at startup -- or else the entire
1155 * program must single-thread itself while loading a module that calls this
1158 extern JS_PUBLIC_API(intN
)
1159 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1162 * Remove finalizer from the global GC finalizers table, returning its type
1163 * code if found, -1 if not found.
1165 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1166 * if you compile the engine JS_THREADSAFE: this function may be called for a
1167 * given finalizer pointer on only one thread; different threads may call to
1168 * remove distinct finalizers safely.
1170 * You must ensure that all strings with finalizer's type have been collected
1171 * before calling this function. Otherwise, string data will be leaked by the
1172 * GC, for want of a finalizer to call.
1174 extern JS_PUBLIC_API(intN
)
1175 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1178 * Create a new JSString whose chars member refers to external memory, i.e.,
1179 * memory requiring special, type-specific finalization. The type code must
1180 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1182 extern JS_PUBLIC_API(JSString
*)
1183 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1186 * Returns the external-string finalizer index for this string, or -1 if it is
1187 * an "internal" (native to JS engine) string.
1189 extern JS_PUBLIC_API(intN
)
1190 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1193 * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
1194 * address in limitAddr for the thread or process stack used by cx. To disable
1195 * stack size checking, pass 0 for limitAddr.
1197 extern JS_PUBLIC_API(void)
1198 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1201 * Set the quota on the number of bytes that stack-like data structures can
1202 * use when the runtime compiles and executes scripts. These structures
1203 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1204 * The default quota is 32MB which is quite generous.
1206 * The function must be called before any script compilation or execution API
1207 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1210 extern JS_PUBLIC_API(void)
1211 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1213 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1215 /************************************************************************/
1218 * Classes, objects, and properties.
1221 /* For detailed comments on the function pointer types, see jspubtd.h. */
1226 /* Mandatory non-null function pointer members. */
1227 JSPropertyOp addProperty
;
1228 JSPropertyOp delProperty
;
1229 JSPropertyOp getProperty
;
1230 JSPropertyOp setProperty
;
1231 JSEnumerateOp enumerate
;
1232 JSResolveOp resolve
;
1233 JSConvertOp convert
;
1234 JSFinalizeOp finalize
;
1236 /* Optionally non-null members start here. */
1237 JSGetObjectOps getObjectOps
;
1238 JSCheckAccessOp checkAccess
;
1241 JSXDRObjectOp xdrObject
;
1242 JSHasInstanceOp hasInstance
;
1244 JSReserveSlotsOp reserveSlots
;
1247 struct JSExtendedClass
{
1249 JSEqualityOp equality
;
1250 JSObjectOp outerObject
;
1251 JSObjectOp innerObject
;
1252 JSIteratorOp iteratorObject
;
1253 JSObjectOp wrappedObject
; /* NB: infallible, null
1254 returns are treated as
1255 the original object */
1256 void (*reserved0
)(void);
1257 void (*reserved1
)(void);
1258 void (*reserved2
)(void);
1261 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1262 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1263 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1264 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1265 #define JSCLASS_SHARE_ALL_PROPERTIES (1<<4) /* all properties are SHARED */
1266 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1267 object in prototype chain
1268 passed in via *objp in/out
1270 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1272 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1275 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1276 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1277 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1279 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1280 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1281 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1282 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1283 << JSCLASS_RESERVED_SLOTS_SHIFT)
1284 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1285 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1286 & JSCLASS_RESERVED_SLOTS_MASK)
1288 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1289 JSCLASS_RESERVED_SLOTS_WIDTH)
1291 /* True if JSClass is really a JSExtendedClass. */
1292 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1293 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1294 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1296 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1297 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1300 * ECMA-262 requires that most constructors used internally create objects
1301 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1302 * member initial value. The "original ... value" verbiage is there because
1303 * in ECMA-262, global properties naming class objects are read/write and
1304 * deleteable, for the most part.
1306 * Implementing this efficiently requires that global objects have classes
1307 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS won't break
1308 * anything except the ECMA-262 "original prototype value" behavior, which was
1309 * broken for years in SpiderMonkey. In other words, without these flags you
1310 * get backward compatibility.
1312 #define JSCLASS_GLOBAL_FLAGS \
1313 (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT))
1315 /* Fast access to the original value of each standard class's prototype. */
1316 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1317 #define JSCLASS_CACHED_PROTO_WIDTH 8
1318 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1319 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1320 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1322 >> JSCLASS_CACHED_PROTO_SHIFT) \
1323 & JSCLASS_CACHED_PROTO_MASK))
1325 /* Initializer for unused members of statically initialized JSClass structs. */
1326 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
1327 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
1329 /* For detailed comments on these function pointer types, see jspubtd.h. */
1330 struct JSObjectOps
{
1331 /* Mandatory non-null function pointer members. */
1332 JSNewObjectMapOp newObjectMap
;
1333 JSObjectMapOp destroyObjectMap
;
1334 JSLookupPropOp lookupProperty
;
1335 JSDefinePropOp defineProperty
;
1336 JSPropertyIdOp getProperty
;
1337 JSPropertyIdOp setProperty
;
1338 JSAttributesOp getAttributes
;
1339 JSAttributesOp setAttributes
;
1340 JSPropertyIdOp deleteProperty
;
1341 JSConvertOp defaultValue
;
1342 JSNewEnumerateOp enumerate
;
1343 JSCheckAccessIdOp checkAccess
;
1345 /* Optionally non-null members start here. */
1346 JSObjectOp thisObject
;
1347 JSPropertyRefOp dropProperty
;
1350 JSXDRObjectOp xdrObject
;
1351 JSHasInstanceOp hasInstance
;
1352 JSSetObjectSlotOp setProto
;
1353 JSSetObjectSlotOp setParent
;
1356 JSGetRequiredSlotOp getRequiredSlot
;
1357 JSSetRequiredSlotOp setRequiredSlot
;
1360 struct JSXMLObjectOps
{
1362 JSGetMethodOp getMethod
;
1363 JSSetMethodOp setMethod
;
1364 JSEnumerateValuesOp enumerateValues
;
1365 JSEqualityOp equality
;
1366 JSConcatenateOp concatenate
;
1370 * Classes that expose JSObjectOps via a non-null getObjectOps class hook may
1371 * derive a property structure from this struct, return a pointer to it from
1372 * lookupProperty and defineProperty, and use the pointer to avoid rehashing
1373 * in getAttributes and setAttributes.
1375 * The jsid type contains either an int jsval (see JSVAL_IS_INT above), or an
1376 * internal pointer that is opaque to users of this API, but which users may
1377 * convert from and to a jsval using JS_ValueToId and JS_IdToValue.
1385 jsid vector
[1]; /* actually, length jsid words */
1388 extern JS_PUBLIC_API(void)
1389 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
1391 extern JS_PUBLIC_API(JSBool
)
1392 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
1394 extern JS_PUBLIC_API(JSBool
)
1395 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
1398 * The magic XML namespace id is int-tagged, but not a valid integer jsval.
1399 * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
1400 * should handle this id specially before converting id via JSVAL_TO_INT.
1402 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
1405 * JSNewResolveOp flag bits.
1407 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1408 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1409 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1410 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1411 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1413 extern JS_PUBLIC_API(JSBool
)
1414 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsval id
, jsval
*vp
);
1416 extern JS_PUBLIC_API(JSBool
)
1417 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
1419 extern JS_PUBLIC_API(JSBool
)
1420 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsval id
);
1422 extern JS_PUBLIC_API(JSBool
)
1423 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
1425 extern JS_PUBLIC_API(void)
1426 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
1428 struct JSConstDoubleSpec
{
1436 * To define an array element rather than a named property member, cast the
1437 * element's index to (const char *) and initialize name with it, and set the
1438 * JSPROP_INDEX bit in flags.
1440 struct JSPropertySpec
{
1444 JSPropertyOp getter
;
1445 JSPropertyOp setter
;
1448 struct JSFunctionSpec
{
1455 * extra & 0xFFFF: Number of extra argument slots for local GC roots.
1456 * If fast native, must be zero.
1457 * extra >> 16: Reserved for future use (must be 0).
1463 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1464 * that's passed to JS_DefineFunctions or JS_InitClass.
1466 #define JS_FS_END JS_FS(NULL,NULL,0,0,0)
1469 * Initializer macro for a JSFunctionSpec array element. This is the original
1470 * kind of native function specifier initializer. Use JS_FN ("fast native", see
1471 * JSFastNative in jspubtd.h) for all functions that do not need a stack frame
1474 #define JS_FS(name,call,nargs,flags,extra) \
1475 {name, call, nargs, flags, extra}
1478 * "Fast native" initializer macro for a JSFunctionSpec array element. Use this
1479 * in preference to JS_FS if the native in question does not need its own stack
1480 * frame when activated.
1482 #define JS_FN(name,fastcall,nargs,flags) \
1483 {name, (JSNative)(fastcall), nargs, \
1484 (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0}
1486 extern JS_PUBLIC_API(JSObject
*)
1487 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
1488 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
1489 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
1490 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
1492 #ifdef JS_THREADSAFE
1493 extern JS_PUBLIC_API(JSClass
*)
1494 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
1496 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1498 extern JS_PUBLIC_API(JSClass
*)
1499 JS_GetClass(JSObject
*obj
);
1501 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1504 extern JS_PUBLIC_API(JSBool
)
1505 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
1507 extern JS_PUBLIC_API(JSBool
)
1508 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
1510 extern JS_PUBLIC_API(void *)
1511 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
1513 extern JS_PUBLIC_API(JSBool
)
1514 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
1516 extern JS_PUBLIC_API(void *)
1517 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
1520 extern JS_PUBLIC_API(JSObject
*)
1521 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
1523 extern JS_PUBLIC_API(JSBool
)
1524 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
1526 extern JS_PUBLIC_API(JSObject
*)
1527 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
1529 extern JS_PUBLIC_API(JSBool
)
1530 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
1532 extern JS_PUBLIC_API(JSObject
*)
1533 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
1536 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1537 * is moved by a copying GC). Return false on failure (likely out of memory),
1538 * and true with *idp containing the unique id on success.
1540 extern JS_PUBLIC_API(JSBool
)
1541 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
1543 extern JS_PUBLIC_API(JSObject
*)
1544 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
1547 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1548 * proto if proto's actual parameter value is null.
1550 extern JS_PUBLIC_API(JSObject
*)
1551 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1554 extern JS_PUBLIC_API(JSBool
)
1555 JS_SealObject(JSContext
*cx
, JSObject
*obj
, JSBool deep
);
1557 extern JS_PUBLIC_API(JSObject
*)
1558 JS_ConstructObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1561 extern JS_PUBLIC_API(JSObject
*)
1562 JS_ConstructObjectWithArguments(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1563 JSObject
*parent
, uintN argc
, jsval
*argv
);
1565 extern JS_PUBLIC_API(JSObject
*)
1566 JS_DefineObject(JSContext
*cx
, JSObject
*obj
, const char *name
, JSClass
*clasp
,
1567 JSObject
*proto
, uintN attrs
);
1569 extern JS_PUBLIC_API(JSBool
)
1570 JS_DefineConstDoubles(JSContext
*cx
, JSObject
*obj
, JSConstDoubleSpec
*cds
);
1572 extern JS_PUBLIC_API(JSBool
)
1573 JS_DefineProperties(JSContext
*cx
, JSObject
*obj
, JSPropertySpec
*ps
);
1575 extern JS_PUBLIC_API(JSBool
)
1576 JS_DefineProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval value
,
1577 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1579 extern JS_PUBLIC_API(JSBool
)
1580 JS_DefinePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval value
,
1581 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1584 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1586 * If the object does not have a property by that name, *foundp will be
1587 * JS_FALSE and the value of *attrsp is undefined.
1589 extern JS_PUBLIC_API(JSBool
)
1590 JS_GetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1591 uintN
*attrsp
, JSBool
*foundp
);
1594 * The same, but if the property is native, return its getter and setter via
1595 * *getterp and *setterp, respectively (and only if the out parameter pointer
1598 extern JS_PUBLIC_API(JSBool
)
1599 JS_GetPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1601 uintN
*attrsp
, JSBool
*foundp
,
1602 JSPropertyOp
*getterp
,
1603 JSPropertyOp
*setterp
);
1606 * Set the attributes of a property on a given object.
1608 * If the object does not have a property by that name, *foundp will be
1609 * JS_FALSE and nothing will be altered.
1611 extern JS_PUBLIC_API(JSBool
)
1612 JS_SetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1613 uintN attrs
, JSBool
*foundp
);
1615 extern JS_PUBLIC_API(JSBool
)
1616 JS_DefinePropertyWithTinyId(JSContext
*cx
, JSObject
*obj
, const char *name
,
1617 int8 tinyid
, jsval value
,
1618 JSPropertyOp getter
, JSPropertyOp setter
,
1621 extern JS_PUBLIC_API(JSBool
)
1622 JS_AliasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1625 extern JS_PUBLIC_API(JSBool
)
1626 JS_AlreadyHasOwnProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1629 extern JS_PUBLIC_API(JSBool
)
1630 JS_AlreadyHasOwnPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1633 extern JS_PUBLIC_API(JSBool
)
1634 JS_HasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, JSBool
*foundp
);
1636 extern JS_PUBLIC_API(JSBool
)
1637 JS_HasPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool
*foundp
);
1639 extern JS_PUBLIC_API(JSBool
)
1640 JS_LookupProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1642 extern JS_PUBLIC_API(JSBool
)
1643 JS_LookupPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1645 extern JS_PUBLIC_API(JSBool
)
1646 JS_LookupPropertyWithFlags(JSContext
*cx
, JSObject
*obj
, const char *name
,
1647 uintN flags
, jsval
*vp
);
1649 extern JS_PUBLIC_API(JSBool
)
1650 JS_LookupPropertyWithFlagsById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1651 uintN flags
, JSObject
**objp
, jsval
*vp
);
1653 extern JS_PUBLIC_API(JSBool
)
1654 JS_GetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1656 extern JS_PUBLIC_API(JSBool
)
1657 JS_GetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1659 extern JS_PUBLIC_API(JSBool
)
1660 JS_GetMethodById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSObject
**objp
,
1663 extern JS_PUBLIC_API(JSBool
)
1664 JS_GetMethod(JSContext
*cx
, JSObject
*obj
, const char *name
, JSObject
**objp
,
1667 extern JS_PUBLIC_API(JSBool
)
1668 JS_SetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1670 extern JS_PUBLIC_API(JSBool
)
1671 JS_SetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1673 extern JS_PUBLIC_API(JSBool
)
1674 JS_DeleteProperty(JSContext
*cx
, JSObject
*obj
, const char *name
);
1676 extern JS_PUBLIC_API(JSBool
)
1677 JS_DeleteProperty2(JSContext
*cx
, JSObject
*obj
, const char *name
,
1680 extern JS_PUBLIC_API(JSBool
)
1681 JS_DeletePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
);
1683 extern JS_PUBLIC_API(JSBool
)
1684 JS_DeletePropertyById2(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*rval
);
1686 extern JS_PUBLIC_API(JSBool
)
1687 JS_DefineUCProperty(JSContext
*cx
, JSObject
*obj
,
1688 const jschar
*name
, size_t namelen
, jsval value
,
1689 JSPropertyOp getter
, JSPropertyOp setter
,
1693 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1695 * If the object does not have a property by that name, *foundp will be
1696 * JS_FALSE and the value of *attrsp is undefined.
1698 extern JS_PUBLIC_API(JSBool
)
1699 JS_GetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1700 const jschar
*name
, size_t namelen
,
1701 uintN
*attrsp
, JSBool
*foundp
);
1704 * The same, but if the property is native, return its getter and setter via
1705 * *getterp and *setterp, respectively (and only if the out parameter pointer
1708 extern JS_PUBLIC_API(JSBool
)
1709 JS_GetUCPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1710 const jschar
*name
, size_t namelen
,
1711 uintN
*attrsp
, JSBool
*foundp
,
1712 JSPropertyOp
*getterp
,
1713 JSPropertyOp
*setterp
);
1716 * Set the attributes of a property on a given object.
1718 * If the object does not have a property by that name, *foundp will be
1719 * JS_FALSE and nothing will be altered.
1721 extern JS_PUBLIC_API(JSBool
)
1722 JS_SetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1723 const jschar
*name
, size_t namelen
,
1724 uintN attrs
, JSBool
*foundp
);
1727 extern JS_PUBLIC_API(JSBool
)
1728 JS_DefineUCPropertyWithTinyId(JSContext
*cx
, JSObject
*obj
,
1729 const jschar
*name
, size_t namelen
,
1730 int8 tinyid
, jsval value
,
1731 JSPropertyOp getter
, JSPropertyOp setter
,
1734 extern JS_PUBLIC_API(JSBool
)
1735 JS_AlreadyHasOwnUCProperty(JSContext
*cx
, JSObject
*obj
, const jschar
*name
,
1736 size_t namelen
, JSBool
*foundp
);
1738 extern JS_PUBLIC_API(JSBool
)
1739 JS_HasUCProperty(JSContext
*cx
, JSObject
*obj
,
1740 const jschar
*name
, size_t namelen
,
1743 extern JS_PUBLIC_API(JSBool
)
1744 JS_LookupUCProperty(JSContext
*cx
, JSObject
*obj
,
1745 const jschar
*name
, size_t namelen
,
1748 extern JS_PUBLIC_API(JSBool
)
1749 JS_GetUCProperty(JSContext
*cx
, JSObject
*obj
,
1750 const jschar
*name
, size_t namelen
,
1753 extern JS_PUBLIC_API(JSBool
)
1754 JS_SetUCProperty(JSContext
*cx
, JSObject
*obj
,
1755 const jschar
*name
, size_t namelen
,
1758 extern JS_PUBLIC_API(JSBool
)
1759 JS_DeleteUCProperty2(JSContext
*cx
, JSObject
*obj
,
1760 const jschar
*name
, size_t namelen
,
1763 extern JS_PUBLIC_API(JSObject
*)
1764 JS_NewArrayObject(JSContext
*cx
, jsint length
, jsval
*vector
);
1766 extern JS_PUBLIC_API(JSBool
)
1767 JS_IsArrayObject(JSContext
*cx
, JSObject
*obj
);
1769 extern JS_PUBLIC_API(JSBool
)
1770 JS_GetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1772 extern JS_PUBLIC_API(JSBool
)
1773 JS_SetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint length
);
1775 extern JS_PUBLIC_API(JSBool
)
1776 JS_HasArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1778 extern JS_PUBLIC_API(JSBool
)
1779 JS_DefineElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval value
,
1780 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1782 extern JS_PUBLIC_API(JSBool
)
1783 JS_AliasElement(JSContext
*cx
, JSObject
*obj
, const char *name
, jsint alias
);
1785 extern JS_PUBLIC_API(JSBool
)
1786 JS_AlreadyHasOwnElement(JSContext
*cx
, JSObject
*obj
, jsint index
,
1789 extern JS_PUBLIC_API(JSBool
)
1790 JS_HasElement(JSContext
*cx
, JSObject
*obj
, jsint index
, JSBool
*foundp
);
1792 extern JS_PUBLIC_API(JSBool
)
1793 JS_LookupElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1795 extern JS_PUBLIC_API(JSBool
)
1796 JS_GetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1798 extern JS_PUBLIC_API(JSBool
)
1799 JS_SetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1801 extern JS_PUBLIC_API(JSBool
)
1802 JS_DeleteElement(JSContext
*cx
, JSObject
*obj
, jsint index
);
1804 extern JS_PUBLIC_API(JSBool
)
1805 JS_DeleteElement2(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*rval
);
1807 extern JS_PUBLIC_API(void)
1808 JS_ClearScope(JSContext
*cx
, JSObject
*obj
);
1810 extern JS_PUBLIC_API(JSIdArray
*)
1811 JS_Enumerate(JSContext
*cx
, JSObject
*obj
);
1814 * Create an object to iterate over enumerable properties of obj, in arbitrary
1815 * property definition order. NB: This differs from longstanding for..in loop
1816 * order, which uses order of property definition in obj.
1818 extern JS_PUBLIC_API(JSObject
*)
1819 JS_NewPropertyIterator(JSContext
*cx
, JSObject
*obj
);
1822 * Return true on success with *idp containing the id of the next enumerable
1823 * property to visit using iterobj, or JSVAL_VOID if there is no such property
1824 * left to visit. Return false on error.
1826 extern JS_PUBLIC_API(JSBool
)
1827 JS_NextProperty(JSContext
*cx
, JSObject
*iterobj
, jsid
*idp
);
1829 extern JS_PUBLIC_API(JSBool
)
1830 JS_CheckAccess(JSContext
*cx
, JSObject
*obj
, jsid id
, JSAccessMode mode
,
1831 jsval
*vp
, uintN
*attrsp
);
1833 extern JS_PUBLIC_API(JSBool
)
1834 JS_GetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval
*vp
);
1836 extern JS_PUBLIC_API(JSBool
)
1837 JS_SetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval v
);
1839 /************************************************************************/
1842 * Security protocol.
1844 struct JSPrincipals
{
1847 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
1848 void * (* getPrincipalArray
)(JSContext
*cx
, JSPrincipals
*);
1849 JSBool (* globalPrivilegesEnabled
)(JSContext
*cx
, JSPrincipals
*);
1851 /* Don't call "destroy"; use reference counting macros below. */
1852 jsrefcount refcount
;
1854 void (* destroy
)(JSContext
*cx
, JSPrincipals
*);
1855 JSBool (* subsume
)(JSPrincipals
*, JSPrincipals
*);
1858 #ifdef JS_THREADSAFE
1859 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
1860 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
1862 extern JS_PUBLIC_API(jsrefcount
)
1863 JS_HoldPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
1865 extern JS_PUBLIC_API(jsrefcount
)
1866 JS_DropPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
1869 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
1870 #define JSPRINCIPALS_DROP(cx, principals) \
1871 ((--(principals)->refcount == 0) \
1872 ? ((*(principals)->destroy)((cx), (principals)), 0) \
1873 : (principals)->refcount)
1877 struct JSSecurityCallbacks
{
1878 JSCheckAccessOp checkObjectAccess
;
1879 JSPrincipalsTranscoder principalsTranscoder
;
1880 JSObjectPrincipalsFinder findObjectPrincipals
;
1883 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
1884 JS_SetRuntimeSecurityCallbacks(JSRuntime
*rt
, JSSecurityCallbacks
*callbacks
);
1886 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
1887 JS_GetRuntimeSecurityCallbacks(JSRuntime
*rt
);
1889 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
1890 JS_SetContextSecurityCallbacks(JSContext
*cx
, JSSecurityCallbacks
*callbacks
);
1892 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
1893 JS_GetSecurityCallbacks(JSContext
*cx
);
1895 /************************************************************************/
1898 * Functions and scripts.
1900 extern JS_PUBLIC_API(JSFunction
*)
1901 JS_NewFunction(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
1902 JSObject
*parent
, const char *name
);
1904 extern JS_PUBLIC_API(JSObject
*)
1905 JS_GetFunctionObject(JSFunction
*fun
);
1908 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
1909 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
1911 extern JS_PUBLIC_API(const char *)
1912 JS_GetFunctionName(JSFunction
*fun
);
1915 * Return the function's identifier as a JSString, or null if fun is unnamed.
1916 * The returned string lives as long as fun, so you don't need to root a saved
1917 * reference to it if fun is well-connected or rooted, and provided you bound
1918 * the use of the saved reference by fun's lifetime.
1920 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
1921 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
1922 * from UTF-16-ish jschars.
1924 extern JS_PUBLIC_API(JSString
*)
1925 JS_GetFunctionId(JSFunction
*fun
);
1928 * Return JSFUN_* flags for fun.
1930 extern JS_PUBLIC_API(uintN
)
1931 JS_GetFunctionFlags(JSFunction
*fun
);
1934 * Return the arity (length) of fun.
1936 extern JS_PUBLIC_API(uint16
)
1937 JS_GetFunctionArity(JSFunction
*fun
);
1940 * Infallible predicate to test whether obj is a function object (faster than
1941 * comparing obj's class name to "Function", but equivalent unless someone has
1942 * overwritten the "Function" identifier with a different constructor and then
1943 * created instances using that constructor that might be passed in as obj).
1945 extern JS_PUBLIC_API(JSBool
)
1946 JS_ObjectIsFunction(JSContext
*cx
, JSObject
*obj
);
1948 extern JS_PUBLIC_API(JSBool
)
1949 JS_DefineFunctions(JSContext
*cx
, JSObject
*obj
, JSFunctionSpec
*fs
);
1951 extern JS_PUBLIC_API(JSFunction
*)
1952 JS_DefineFunction(JSContext
*cx
, JSObject
*obj
, const char *name
, JSNative call
,
1953 uintN nargs
, uintN attrs
);
1955 extern JS_PUBLIC_API(JSFunction
*)
1956 JS_DefineUCFunction(JSContext
*cx
, JSObject
*obj
,
1957 const jschar
*name
, size_t namelen
, JSNative call
,
1958 uintN nargs
, uintN attrs
);
1960 extern JS_PUBLIC_API(JSObject
*)
1961 JS_CloneFunctionObject(JSContext
*cx
, JSObject
*funobj
, JSObject
*parent
);
1964 * Given a buffer, return JS_FALSE if the buffer might become a valid
1965 * javascript statement with the addition of more lines. Otherwise return
1966 * JS_TRUE. The intent is to support interactive compilation - accumulate
1967 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
1970 extern JS_PUBLIC_API(JSBool
)
1971 JS_BufferIsCompilableUnit(JSContext
*cx
, JSObject
*obj
,
1972 const char *bytes
, size_t length
);
1975 * The JSScript objects returned by the following functions refer to string and
1976 * other kinds of literals, including doubles and RegExp objects. These
1977 * literals are vulnerable to garbage collection; to root script objects and
1978 * prevent literals from being collected, create a rootable object using
1979 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
1981 extern JS_PUBLIC_API(JSScript
*)
1982 JS_CompileScript(JSContext
*cx
, JSObject
*obj
,
1983 const char *bytes
, size_t length
,
1984 const char *filename
, uintN lineno
);
1986 extern JS_PUBLIC_API(JSScript
*)
1987 JS_CompileScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
1988 JSPrincipals
*principals
,
1989 const char *bytes
, size_t length
,
1990 const char *filename
, uintN lineno
);
1992 extern JS_PUBLIC_API(JSScript
*)
1993 JS_CompileUCScript(JSContext
*cx
, JSObject
*obj
,
1994 const jschar
*chars
, size_t length
,
1995 const char *filename
, uintN lineno
);
1997 extern JS_PUBLIC_API(JSScript
*)
1998 JS_CompileUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
1999 JSPrincipals
*principals
,
2000 const jschar
*chars
, size_t length
,
2001 const char *filename
, uintN lineno
);
2003 extern JS_PUBLIC_API(JSScript
*)
2004 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2006 extern JS_PUBLIC_API(JSScript
*)
2007 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2010 extern JS_PUBLIC_API(JSScript
*)
2011 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2012 const char *filename
, FILE *fh
,
2013 JSPrincipals
*principals
);
2016 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2017 * in order to keep a JSScript and its atoms safe from garbage collection after
2018 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2019 * E.g., and without error checks:
2021 * JSScript *script = JS_CompileFile(cx, global, filename);
2022 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2023 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
2026 * JS_ExecuteScript(cx, global, script, &result);
2028 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2029 * JS_RemoveRoot(cx, &scrobj);
2031 extern JS_PUBLIC_API(JSObject
*)
2032 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2035 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2036 * called on script yet, the return value will be null.
2038 extern JS_PUBLIC_API(JSObject
*)
2039 JS_GetScriptObject(JSScript
*script
);
2041 extern JS_PUBLIC_API(void)
2042 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2044 extern JS_PUBLIC_API(JSFunction
*)
2045 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2046 uintN nargs
, const char **argnames
,
2047 const char *bytes
, size_t length
,
2048 const char *filename
, uintN lineno
);
2050 extern JS_PUBLIC_API(JSFunction
*)
2051 JS_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2052 JSPrincipals
*principals
, const char *name
,
2053 uintN nargs
, const char **argnames
,
2054 const char *bytes
, size_t length
,
2055 const char *filename
, uintN lineno
);
2057 extern JS_PUBLIC_API(JSFunction
*)
2058 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2059 uintN nargs
, const char **argnames
,
2060 const jschar
*chars
, size_t length
,
2061 const char *filename
, uintN lineno
);
2063 extern JS_PUBLIC_API(JSFunction
*)
2064 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2065 JSPrincipals
*principals
, const char *name
,
2066 uintN nargs
, const char **argnames
,
2067 const jschar
*chars
, size_t length
,
2068 const char *filename
, uintN lineno
);
2070 extern JS_PUBLIC_API(JSString
*)
2071 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2075 * API extension: OR this into indent to avoid pretty-printing the decompiled
2076 * source resulting from JS_DecompileFunction{,Body}.
2078 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2080 extern JS_PUBLIC_API(JSString
*)
2081 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2083 extern JS_PUBLIC_API(JSString
*)
2084 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2087 * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
2088 * quadruplets all use the obj parameter as the initial scope chain header,
2089 * the 'this' keyword value, and the variables object (ECMA parlance for where
2090 * 'var' and 'function' bind names) of the execution context for script.
2092 * Using obj as the variables object is problematic if obj's parent (which is
2093 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2094 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2095 * created by assignment to an unbound id, 'x = 0', go in the last object on
2096 * the scope chain linked by parent.
2098 * ECMA calls that last scoping object the "global object", but note that many
2099 * embeddings have several such objects. ECMA requires that "global code" be
2100 * executed with the variables object equal to this global object. But these
2101 * JS API entry points provide freedom to execute code against a "sub-global",
2102 * i.e., a parented or scoped object, in which case the variables object will
2103 * differ from the last object on the scope chain, resulting in confusing and
2104 * non-ECMA explicit vs. implicit variable creation.
2106 * Caveat embedders: unless you already depend on this buggy variables object
2107 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2108 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2109 * someone may have set other options on cx already -- for each context in the
2110 * application, if you pass parented objects as the obj parameter, or may ever
2111 * pass such objects in the future.
2113 * Why a runtime option? The alternative is to add six or so new API entry
2114 * points with signatures matching the following six, and that doesn't seem
2115 * worth the code bloat cost. Such new entry points would probably have less
2116 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2117 * OTOH, can be more easily hacked into existing code that does not depend on
2118 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2119 * etc., entry points.
2121 extern JS_PUBLIC_API(JSBool
)
2122 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2125 * Execute either the function-defining prolog of a script, or the script's
2126 * main body, but not both.
2128 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2130 extern JS_PUBLIC_API(JSBool
)
2131 JS_ExecuteScriptPart(JSContext
*cx
, JSObject
*obj
, JSScript
*script
,
2132 JSExecPart part
, jsval
*rval
);
2134 extern JS_PUBLIC_API(JSBool
)
2135 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2136 const char *bytes
, uintN length
,
2137 const char *filename
, uintN lineno
,
2140 extern JS_PUBLIC_API(JSBool
)
2141 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2142 JSPrincipals
*principals
,
2143 const char *bytes
, uintN length
,
2144 const char *filename
, uintN lineno
,
2147 extern JS_PUBLIC_API(JSBool
)
2148 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2149 const jschar
*chars
, uintN length
,
2150 const char *filename
, uintN lineno
,
2153 extern JS_PUBLIC_API(JSBool
)
2154 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2155 JSPrincipals
*principals
,
2156 const jschar
*chars
, uintN length
,
2157 const char *filename
, uintN lineno
,
2160 extern JS_PUBLIC_API(JSBool
)
2161 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2162 jsval
*argv
, jsval
*rval
);
2164 extern JS_PUBLIC_API(JSBool
)
2165 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2166 jsval
*argv
, jsval
*rval
);
2168 extern JS_PUBLIC_API(JSBool
)
2169 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2170 jsval
*argv
, jsval
*rval
);
2173 * The maximum value of the operation limit to pass to JS_SetOperationCallback
2174 * and JS_SetOperationLimit.
2176 #define JS_MAX_OPERATION_LIMIT ((uint32) 0x7FFFFFFF)
2178 #define JS_OPERATION_WEIGHT_BASE 4096
2181 * Set the operation callback that the engine calls periodically after
2182 * the internal operation count reaches the specified limit.
2184 * When operationLimit is JS_OPERATION_WEIGHT_BASE, the callback will be
2185 * called at least after each backward jump in the interpreter. To minimize
2186 * the overhead of the callback invocation we suggest at least
2188 * 100 * JS_OPERATION_WEIGHT_BASE
2190 * as a value for operationLimit.
2192 extern JS_PUBLIC_API(void)
2193 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
,
2194 uint32 operationLimit
);
2196 extern JS_PUBLIC_API(void)
2197 JS_ClearOperationCallback(JSContext
*cx
);
2199 extern JS_PUBLIC_API(JSOperationCallback
)
2200 JS_GetOperationCallback(JSContext
*cx
);
2203 * Get the operation limit associated with the operation callback. This API
2204 * function may be called only when the result of JS_GetOperationCallback(cx)
2207 extern JS_PUBLIC_API(uint32
)
2208 JS_GetOperationLimit(JSContext
*cx
);
2211 * Change the operation limit associated with the operation callback. This API
2212 * function may be called only when the result of JS_GetOperationCallback(cx)
2215 extern JS_PUBLIC_API(void)
2216 JS_SetOperationLimit(JSContext
*cx
, uint32 operationLimit
);
2219 * Note well: JS_SetBranchCallback is deprecated. It is similar to
2221 * JS_SetOperationCallback(cx, callback, 4096, NULL);
2223 * except that the callback will not be called from a long-running native
2224 * function when JSOPTION_NATIVE_BRANCH_CALLBACK is not set and the top-most
2227 extern JS_PUBLIC_API(JSBranchCallback
)
2228 JS_SetBranchCallback(JSContext
*cx
, JSBranchCallback cb
);
2230 extern JS_PUBLIC_API(JSBool
)
2231 JS_IsRunning(JSContext
*cx
);
2233 extern JS_PUBLIC_API(JSBool
)
2234 JS_IsConstructing(JSContext
*cx
);
2237 * Returns true if a script is executing and its current bytecode is a set
2238 * (assignment) operation, even if there are native (no script) stack frames
2239 * between the script and the caller to JS_IsAssigning.
2241 extern JS_FRIEND_API(JSBool
)
2242 JS_IsAssigning(JSContext
*cx
);
2245 * Set the second return value, which should be a string or int jsval that
2246 * identifies a property in the returned object, to form an ECMA reference
2247 * type value (obj, id). Only native methods can return reference types,
2248 * and if the returned value is used on the left-hand side of an assignment
2249 * op, the identified property will be set. If the return value is in an
2250 * r-value, the interpreter just gets obj[id]'s value.
2252 extern JS_PUBLIC_API(void)
2253 JS_SetCallReturnValue2(JSContext
*cx
, jsval v
);
2256 * Saving and restoring frame chains.
2258 * These two functions are used to set aside cx's call stack while that stack
2259 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2260 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2261 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2262 * matching JS_RestoreFrameChain calls.
2264 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2265 * return does not signify an error, and JS_RestoreFrameChain handles a null
2266 * frame pointer argument safely.
2268 extern JS_PUBLIC_API(JSStackFrame
*)
2269 JS_SaveFrameChain(JSContext
*cx
);
2271 extern JS_PUBLIC_API(void)
2272 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2274 /************************************************************************/
2279 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2280 * on error (signified by null return), it leaves bytes owned by the caller.
2281 * So the caller must free bytes in the error case, if it has no use for them.
2282 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2283 * the character memory passed to them -- they copy it.
2285 extern JS_PUBLIC_API(JSString
*)
2286 JS_NewString(JSContext
*cx
, char *bytes
, size_t length
);
2288 extern JS_PUBLIC_API(JSString
*)
2289 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2291 extern JS_PUBLIC_API(JSString
*)
2292 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2294 extern JS_PUBLIC_API(JSString
*)
2295 JS_InternString(JSContext
*cx
, const char *s
);
2297 extern JS_PUBLIC_API(JSString
*)
2298 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
2300 extern JS_PUBLIC_API(JSString
*)
2301 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
2303 extern JS_PUBLIC_API(JSString
*)
2304 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
2306 extern JS_PUBLIC_API(JSString
*)
2307 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
2309 extern JS_PUBLIC_API(JSString
*)
2310 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
2312 extern JS_PUBLIC_API(char *)
2313 JS_GetStringBytes(JSString
*str
);
2315 extern JS_PUBLIC_API(jschar
*)
2316 JS_GetStringChars(JSString
*str
);
2318 extern JS_PUBLIC_API(size_t)
2319 JS_GetStringLength(JSString
*str
);
2321 extern JS_PUBLIC_API(intN
)
2322 JS_CompareStrings(JSString
*str1
, JSString
*str2
);
2325 * Mutable string support. A string's characters are never mutable in this JS
2326 * implementation, but a growable string has a buffer that can be reallocated,
2327 * and a dependent string is a substring of another (growable, dependent, or
2328 * immutable) string. The direct data members of the (opaque to API clients)
2329 * JSString struct may be changed in a single-threaded way for growable and
2330 * dependent strings.
2332 * Therefore mutable strings cannot be used by more than one thread at a time.
2333 * You may call JS_MakeStringImmutable to convert the string from a mutable
2334 * (growable or dependent) string to an immutable (and therefore thread-safe)
2335 * string. The engine takes care of converting growable and dependent strings
2336 * to immutable for you if you store strings in multi-threaded objects using
2337 * JS_SetProperty or kindred API entry points.
2339 * If you store a JSString pointer in a native data structure that is (safely)
2340 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2341 * retiring the store.
2343 extern JS_PUBLIC_API(JSString
*)
2344 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
2347 * Create a dependent string, i.e., a string that owns no character storage,
2348 * but that refers to a slice of another string's chars. Dependent strings
2349 * are mutable by definition, so the thread safety comments above apply.
2351 extern JS_PUBLIC_API(JSString
*)
2352 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
2356 * Concatenate two strings, resulting in a new growable string. If you create
2357 * the left string and pass it to JS_ConcatStrings on a single thread, try to
2358 * use JS_NewGrowableString to create the left string -- doing so helps Concat
2359 * avoid allocating a new buffer for the result and copying left's chars into
2360 * the new buffer. See above for thread safety comments.
2362 extern JS_PUBLIC_API(JSString
*)
2363 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
2366 * Convert a dependent string into an independent one. This function does not
2367 * change the string's mutability, so the thread safety comments above apply.
2369 extern JS_PUBLIC_API(const jschar
*)
2370 JS_UndependString(JSContext
*cx
, JSString
*str
);
2373 * Convert a mutable string (either growable or dependent) into an immutable,
2376 extern JS_PUBLIC_API(JSBool
)
2377 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
2380 * Return JS_TRUE if C (char []) strings passed via the API and internally
2383 JS_PUBLIC_API(JSBool
)
2384 JS_CStringsAreUTF8(void);
2387 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2388 * can never be changed. This API must be called before the first call to
2392 JS_SetCStringsAreUTF8(void);
2395 * Character encoding support.
2397 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2398 * of the destination buffer before the call; on return, *dstlenp contains the
2399 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2400 * stored. To determine the necessary destination buffer size, make a sizing
2401 * call that passes NULL for dst.
2403 * On errors, the functions report the error. In that case, *dstlenp contains
2404 * the number of characters or bytes transferred so far. If cx is NULL, no
2405 * error is reported on failure, and the functions simply return JS_FALSE.
2407 * NB: Neither function stores an additional zero byte or jschar after the
2408 * transcoded string.
2410 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2411 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2412 * errors if the character sequence is malformed. If UTF-8 support is
2413 * disabled, the functions deflate and inflate, respectively.
2415 JS_PUBLIC_API(JSBool
)
2416 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
2419 JS_PUBLIC_API(JSBool
)
2420 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
2424 * A variation on JS_EncodeCharacters where a null terminated string is
2425 * returned that you are expected to call JS_free on when done.
2427 JS_PUBLIC_API(char *)
2428 JS_EncodeString(JSContext
*cx
, JSString
*str
);
2430 /************************************************************************/
2434 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
2437 * JSON.stringify as specificed by ES3.1 (draft)
2439 JS_PUBLIC_API(JSBool
)
2440 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
,
2441 JSONWriteCallback callback
, void *data
);
2444 * Retrieve a toJSON function. If found, set vp to its result.
2446 JS_PUBLIC_API(JSBool
)
2447 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
2450 * JSON.parse as specificed by ES3.1 (draft)
2452 JS_PUBLIC_API(JSONParser
*)
2453 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
2455 JS_PUBLIC_API(JSBool
)
2456 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
2458 JS_PUBLIC_API(JSBool
)
2459 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
);
2461 /************************************************************************/
2464 * Locale specific string conversion and error message callbacks.
2466 struct JSLocaleCallbacks
{
2467 JSLocaleToUpperCase localeToUpperCase
;
2468 JSLocaleToLowerCase localeToLowerCase
;
2469 JSLocaleCompare localeCompare
;
2470 JSLocaleToUnicode localeToUnicode
;
2471 JSErrorCallback localeGetErrorMessage
;
2475 * Establish locale callbacks. The pointer must persist as long as the
2476 * JSContext. Passing NULL restores the default behaviour.
2478 extern JS_PUBLIC_API(void)
2479 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
2482 * Return the address of the current locale callbacks struct, which may
2485 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
2486 JS_GetLocaleCallbacks(JSContext
*cx
);
2488 /************************************************************************/
2495 * Report an exception represented by the sprintf-like conversion of format
2496 * and its arguments. This exception message string is passed to a pre-set
2497 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2498 * the JSErrorReporter typedef).
2500 extern JS_PUBLIC_API(void)
2501 JS_ReportError(JSContext
*cx
, const char *format
, ...);
2504 * Use an errorNumber to retrieve the format string, args are char *
2506 extern JS_PUBLIC_API(void)
2507 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
2508 void *userRef
, const uintN errorNumber
, ...);
2511 * Use an errorNumber to retrieve the format string, args are jschar *
2513 extern JS_PUBLIC_API(void)
2514 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
2515 void *userRef
, const uintN errorNumber
, ...);
2518 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2519 * Return true if there was no error trying to issue the warning, and if the
2520 * warning was not converted into an error due to the JSOPTION_WERROR option
2521 * being set, false otherwise.
2523 extern JS_PUBLIC_API(JSBool
)
2524 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
2526 extern JS_PUBLIC_API(JSBool
)
2527 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
2528 JSErrorCallback errorCallback
, void *userRef
,
2529 const uintN errorNumber
, ...);
2531 extern JS_PUBLIC_API(JSBool
)
2532 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
2533 JSErrorCallback errorCallback
, void *userRef
,
2534 const uintN errorNumber
, ...);
2537 * Complain when out of memory.
2539 extern JS_PUBLIC_API(void)
2540 JS_ReportOutOfMemory(JSContext
*cx
);
2543 * Complain when an allocation size overflows the maximum supported limit.
2545 extern JS_PUBLIC_API(void)
2546 JS_ReportAllocationOverflow(JSContext
*cx
);
2548 struct JSErrorReport
{
2549 const char *filename
; /* source file name, URL, etc., or null */
2550 uintN lineno
; /* source line number */
2551 const char *linebuf
; /* offending source line without final \n */
2552 const char *tokenptr
; /* pointer to error token in linebuf */
2553 const jschar
*uclinebuf
; /* unicode (original) line buffer */
2554 const jschar
*uctokenptr
; /* unicode (original) token pointer */
2555 uintN flags
; /* error/warning, etc. */
2556 uintN errorNumber
; /* the error number, e.g. see js.msg */
2557 const jschar
*ucmessage
; /* the (default) error message */
2558 const jschar
**messageArgs
; /* arguments for the error message */
2562 * JSErrorReport flag values. These may be freely composed.
2564 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2565 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2566 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2567 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2570 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2571 * has been thrown for this runtime error, and the host should ignore it.
2572 * Exception-aware hosts should also check for JS_IsExceptionPending if
2573 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2576 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2577 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2578 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2580 extern JS_PUBLIC_API(JSErrorReporter
)
2581 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
2583 /************************************************************************/
2586 * Regular Expressions.
2588 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
2589 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
2590 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
2591 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
2592 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
2594 extern JS_PUBLIC_API(JSObject
*)
2595 JS_NewRegExpObject(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
2597 extern JS_PUBLIC_API(JSObject
*)
2598 JS_NewUCRegExpObject(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
2600 extern JS_PUBLIC_API(void)
2601 JS_SetRegExpInput(JSContext
*cx
, JSString
*input
, JSBool multiline
);
2603 extern JS_PUBLIC_API(void)
2604 JS_ClearRegExpStatics(JSContext
*cx
);
2606 extern JS_PUBLIC_API(void)
2607 JS_ClearRegExpRoots(JSContext
*cx
);
2609 /* TODO: compile, exec, get/set other statics... */
2611 /************************************************************************/
2613 extern JS_PUBLIC_API(JSBool
)
2614 JS_IsExceptionPending(JSContext
*cx
);
2616 extern JS_PUBLIC_API(JSBool
)
2617 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
2619 extern JS_PUBLIC_API(void)
2620 JS_SetPendingException(JSContext
*cx
, jsval v
);
2622 extern JS_PUBLIC_API(void)
2623 JS_ClearPendingException(JSContext
*cx
);
2625 extern JS_PUBLIC_API(JSBool
)
2626 JS_ReportPendingException(JSContext
*cx
);
2629 * Save the current exception state. This takes a snapshot of cx's current
2630 * exception state without making any change to that state.
2632 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
2633 * (to restore that saved state, overriding any more recent state) or else to
2634 * JS_DropExceptionState (to free the state struct in case it is not correct
2635 * or desirable to restore it). Both Restore and Drop free the state struct,
2636 * so callers must stop using the pointer returned from Save after calling the
2637 * Release or Drop API.
2639 extern JS_PUBLIC_API(JSExceptionState
*)
2640 JS_SaveExceptionState(JSContext
*cx
);
2642 extern JS_PUBLIC_API(void)
2643 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2645 extern JS_PUBLIC_API(void)
2646 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2649 * If the given value is an exception object that originated from an error,
2650 * the exception will contain an error report struct, and this API will return
2651 * the address of that struct. Otherwise, it returns NULL. The lifetime of
2652 * the error report struct that might be returned is the same as the lifetime
2653 * of the exception object.
2655 extern JS_PUBLIC_API(JSErrorReport
*)
2656 JS_ErrorFromException(JSContext
*cx
, jsval v
);
2659 * Given a reported error's message and JSErrorReport struct pointer, throw
2660 * the corresponding exception on cx.
2662 extern JS_PUBLIC_API(JSBool
)
2663 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
2664 JSErrorReport
*reportp
);
2667 * Throws a StopIteration exception on cx.
2669 extern JS_PUBLIC_API(JSBool
)
2670 JS_ThrowStopIteration(JSContext
*cx
);
2673 * Associate the current thread with the given context. This is done
2674 * implicitly by JS_NewContext.
2676 * Returns the old thread id for this context, which should be treated as
2677 * an opaque value. This value is provided for comparison to 0, which
2678 * indicates that ClearContextThread has been called on this context
2679 * since the last SetContextThread, or non-0, which indicates the opposite.
2681 extern JS_PUBLIC_API(jsword
)
2682 JS_GetContextThread(JSContext
*cx
);
2684 extern JS_PUBLIC_API(jsword
)
2685 JS_SetContextThread(JSContext
*cx
);
2687 extern JS_PUBLIC_API(jsword
)
2688 JS_ClearContextThread(JSContext
*cx
);
2690 /************************************************************************/
2693 #define JS_GC_ZEAL 1
2697 extern JS_PUBLIC_API(void)
2698 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
2703 #endif /* jsapi_h___ */