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 typedef enum jsvaltag
{
58 JSVAL_OBJECT
= 0x0, /* untagged reference to object */
59 JSVAL_INT
= 0x1, /* tagged 31-bit integer value */
60 JSVAL_DOUBLE
= 0x2, /* tagged reference to double */
61 JSVAL_STRING
= 0x4, /* tagged reference to string */
62 JSVAL_BOOLEAN
= 0x6 /* tagged boolean value */
65 #define JSVAL_OBJECT ((jsvaltag)0x0)
66 #define JSVAL_INT ((jsvaltag)0x1)
67 #define JSVAL_DOUBLE ((jsvaltag)0x2)
68 #define JSVAL_STRING ((jsvaltag)0x4)
69 #define JSVAL_BOOLEAN ((jsvaltag)0x6)
71 /* Type tag bitfield length and derived macros. */
72 #define JSVAL_TAGBITS 3
73 #define JSVAL_TAGMASK JS_BITMASK(JSVAL_TAGBITS)
74 #define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
76 /* Not a function, because we have static asserts that use it */
77 #define JSVAL_TAG(v) ((jsvaltag)((v) & JSVAL_TAGMASK))
79 /* Not a function, because we have static asserts that use it */
80 #define JSVAL_SETTAG(v, t) ((v) | (t))
82 static JS_ALWAYS_INLINE jsval
85 return v
& ~(jsval
)JSVAL_TAGMASK
;
89 * Well-known JS values. The extern'd variables are initialized when the
90 * first JSContext is created by JS_NewContext (see below).
92 #define JSVAL_NULL ((jsval) 0)
93 #define JSVAL_ZERO INT_TO_JSVAL(0)
94 #define JSVAL_ONE INT_TO_JSVAL(1)
95 #define JSVAL_FALSE PSEUDO_BOOLEAN_TO_JSVAL(JS_FALSE)
96 #define JSVAL_TRUE PSEUDO_BOOLEAN_TO_JSVAL(JS_TRUE)
97 #define JSVAL_VOID PSEUDO_BOOLEAN_TO_JSVAL(2)
100 * A pseudo-boolean is a 29-bit (for 32-bit jsval) or 61-bit (for 64-bit jsval)
101 * value other than 0 or 1 encoded as a jsval whose tag is JSVAL_BOOLEAN.
103 * JSVAL_VOID happens to be defined as a jsval encoding a pseudo-boolean, but
104 * embedders MUST NOT rely on this. All other possible pseudo-boolean values
105 * are implementation-reserved and MUST NOT be constructed by any embedding of
108 #define JSVAL_TO_PSEUDO_BOOLEAN(v) ((JSBool) ((v) >> JSVAL_TAGBITS))
109 #define PSEUDO_BOOLEAN_TO_JSVAL(b) \
110 JSVAL_SETTAG((jsval) (b) << JSVAL_TAGBITS, JSVAL_BOOLEAN)
112 /* Predicates for type testing. */
113 static JS_ALWAYS_INLINE JSBool
114 JSVAL_IS_OBJECT(jsval v
)
116 return JSVAL_TAG(v
) == JSVAL_OBJECT
;
119 static JS_ALWAYS_INLINE JSBool
120 JSVAL_IS_INT(jsval v
)
122 return v
& JSVAL_INT
;
125 static JS_ALWAYS_INLINE JSBool
126 JSVAL_IS_DOUBLE(jsval v
)
128 return JSVAL_TAG(v
) == JSVAL_DOUBLE
;
131 static JS_ALWAYS_INLINE JSBool
132 JSVAL_IS_NUMBER(jsval v
)
134 return JSVAL_IS_INT(v
) || JSVAL_IS_DOUBLE(v
);
137 static JS_ALWAYS_INLINE JSBool
138 JSVAL_IS_STRING(jsval v
)
140 return JSVAL_TAG(v
) == JSVAL_STRING
;
143 static JS_ALWAYS_INLINE JSBool
144 JSVAL_IS_BOOLEAN(jsval v
)
146 return (v
& ~((jsval
)1 << JSVAL_TAGBITS
)) == JSVAL_BOOLEAN
;
149 static JS_ALWAYS_INLINE JSBool
150 JSVAL_IS_NULL(jsval v
)
152 return v
== JSVAL_NULL
;
155 static JS_ALWAYS_INLINE JSBool
156 JSVAL_IS_VOID(jsval v
)
158 return v
== JSVAL_VOID
;
161 static JS_ALWAYS_INLINE JSBool
162 JSVAL_IS_PRIMITIVE(jsval v
)
164 return !JSVAL_IS_OBJECT(v
) || JSVAL_IS_NULL(v
);
167 /* Objects, strings, and doubles are GC'ed. */
168 static JS_ALWAYS_INLINE JSBool
169 JSVAL_IS_GCTHING(jsval v
)
171 return !(v
& JSVAL_INT
) && JSVAL_TAG(v
) != JSVAL_BOOLEAN
;
174 static JS_ALWAYS_INLINE
void *
175 JSVAL_TO_GCTHING(jsval v
)
177 JS_ASSERT(JSVAL_IS_GCTHING(v
));
178 return (void *) JSVAL_CLRTAG(v
);
181 static JS_ALWAYS_INLINE JSObject
*
182 JSVAL_TO_OBJECT(jsval v
)
184 JS_ASSERT(JSVAL_IS_OBJECT(v
));
185 return (JSObject
*) JSVAL_TO_GCTHING(v
);
188 static JS_ALWAYS_INLINE jsdouble
*
189 JSVAL_TO_DOUBLE(jsval v
)
191 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
192 return (jsdouble
*) JSVAL_TO_GCTHING(v
);
195 static JS_ALWAYS_INLINE JSString
*
196 JSVAL_TO_STRING(jsval v
)
198 JS_ASSERT(JSVAL_IS_STRING(v
));
199 return (JSString
*) JSVAL_TO_GCTHING(v
);
202 static JS_ALWAYS_INLINE jsval
203 OBJECT_TO_JSVAL(JSObject
*obj
)
205 JS_ASSERT(((jsval
) obj
& JSVAL_TAGMASK
) == JSVAL_OBJECT
);
209 static JS_ALWAYS_INLINE jsval
210 DOUBLE_TO_JSVAL(jsdouble
*dp
)
212 JS_ASSERT(((jsword
) dp
& JSVAL_TAGMASK
) == 0);
213 return JSVAL_SETTAG((jsval
) dp
, JSVAL_DOUBLE
);
216 static JS_ALWAYS_INLINE jsval
217 STRING_TO_JSVAL(JSString
*str
)
219 return JSVAL_SETTAG((jsval
) str
, JSVAL_STRING
);
222 /* Lock and unlock the GC thing held by a jsval. */
223 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
224 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
226 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
227 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
230 /* Domain limits for the jsval int type. */
231 #define JSVAL_INT_BITS 31
232 #define JSVAL_INT_POW2(n) ((jsval)1 << (n))
233 #define JSVAL_INT_MIN (-JSVAL_INT_POW2(30))
234 #define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
236 /* Not a function, because we have static asserts that use it */
237 #define INT_FITS_IN_JSVAL(i) ((jsuint)(i) - (jsuint)JSVAL_INT_MIN <= \
238 (jsuint)(JSVAL_INT_MAX - JSVAL_INT_MIN))
240 static JS_ALWAYS_INLINE jsint
241 JSVAL_TO_INT(jsval v
)
243 JS_ASSERT(JSVAL_IS_INT(v
));
244 return (jsint
) v
>> 1;
247 /* Not a function, because we have static asserts that use it */
248 #define INT_TO_JSVAL_CONSTEXPR(i) (((jsval)(i) << 1) | JSVAL_INT)
250 static JS_ALWAYS_INLINE jsval
251 INT_TO_JSVAL(jsint i
)
253 JS_ASSERT(INT_FITS_IN_JSVAL(i
));
254 return INT_TO_JSVAL_CONSTEXPR(i
);
257 /* Convert between boolean and jsval, asserting that inputs are valid. */
258 static JS_ALWAYS_INLINE JSBool
259 JSVAL_TO_BOOLEAN(jsval v
)
261 JS_ASSERT(v
== JSVAL_TRUE
|| v
== JSVAL_FALSE
);
262 return JSVAL_TO_PSEUDO_BOOLEAN(v
);
265 static JS_ALWAYS_INLINE jsval
266 BOOLEAN_TO_JSVAL(JSBool b
)
268 JS_ASSERT(b
== JS_TRUE
|| b
== JS_FALSE
);
269 return PSEUDO_BOOLEAN_TO_JSVAL(b
);
272 /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
273 #define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
274 #define PRIVATE_TO_JSVAL(p) ((jsval)(p) | JSVAL_INT)
276 /* Property attributes, set in JSPropertySpec and passed to API functions. */
277 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
278 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
279 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
280 #define JSPROP_GETTER 0x10 /* property holds getter function */
281 #define JSPROP_SETTER 0x20 /* property holds setter function */
282 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
283 property; don't copy the property on
284 set of the same-named property in an
285 object that delegates to a prototype
286 containing this property */
287 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
289 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
290 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
291 #define JSFUN_GETTER JSPROP_GETTER
292 #define JSFUN_SETTER JSPROP_SETTER
293 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
294 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
296 #define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
297 #define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
299 #define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
300 #define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
301 #define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
302 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
304 #define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
306 #define JSFUN_THISP_FLAGS(f) (f)
307 #define JSFUN_THISP_TEST(f,t) ((f) & t)
309 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
310 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
311 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
312 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
314 #define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
316 #define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
317 bits 12-15 are used internally to
318 flag interpreted functions */
320 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
321 instead of defaulting to class gsops
322 for property holding function */
325 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
326 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
327 * methods of a class prototype that are exposed as static methods taking an
328 * extra leading argument: the generic |this| parameter.
330 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
331 * that struct must live at least as long as the native static method object
332 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
333 * JSFunctionSpec structs are allocated in static arrays.
335 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
338 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
339 * comment in jstypes.h regarding safe int64 usage.
341 extern JS_PUBLIC_API(int64
)
344 /* Don't want to export data, so provide accessors for non-inline jsvals. */
345 extern JS_PUBLIC_API(jsval
)
346 JS_GetNaNValue(JSContext
*cx
);
348 extern JS_PUBLIC_API(jsval
)
349 JS_GetNegativeInfinityValue(JSContext
*cx
);
351 extern JS_PUBLIC_API(jsval
)
352 JS_GetPositiveInfinityValue(JSContext
*cx
);
354 extern JS_PUBLIC_API(jsval
)
355 JS_GetEmptyStringValue(JSContext
*cx
);
358 * Format is a string of the following characters (spaces are insignificant),
359 * specifying the tabulated type conversions:
362 * c uint16/jschar ECMA uint16, Unicode char
364 * u uint32 ECMA uint32
365 * j int32 Rounded int32 (coordinate)
366 * d jsdouble IEEE double
367 * I jsdouble Integral IEEE double
369 * S JSString * Unicode string, accessed by a JSString pointer
370 * W jschar * Unicode character vector, 0-terminated (W for wide)
371 * o JSObject * Object reference
372 * f JSFunction * Function private
373 * v jsval Argument value (no conversion)
374 * * N/A Skip this argument (no vararg)
375 * / N/A End of required arguments
377 * The variable argument list after format must consist of &b, &c, &s, e.g.,
378 * where those variables have the types given above. For the pointer types
379 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
380 * to the JS runtime, not to the calling native code. The runtime promises
381 * to keep this memory valid so long as argv refers to allocated stack space
382 * (so long as the native function is active).
384 * Fewer arguments than format specifies may be passed only if there is a /
385 * in format after the last required argument specifier and argc is at least
386 * the number of required arguments. More arguments than format specifies
387 * may be passed without error; it is up to the caller to deal with trailing
388 * unconverted arguments.
390 extern JS_PUBLIC_API(JSBool
)
391 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
395 extern JS_PUBLIC_API(JSBool
)
396 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
397 const char *format
, va_list ap
);
401 * Inverse of JS_ConvertArguments: scan format and convert trailing arguments
402 * into jsvals, GC-rooted if necessary by the JS stack. Return null on error,
403 * and a pointer to the new argument vector on success. Also return a stack
404 * mark on success via *markp, in which case the caller must eventually clean
405 * up by calling JS_PopArguments.
407 * Note that the number of actual arguments supplied is specified exclusively
408 * by format, so there is no argc parameter.
410 extern JS_PUBLIC_API(jsval
*)
411 JS_PushArguments(JSContext
*cx
, void **markp
, const char *format
, ...);
414 extern JS_PUBLIC_API(jsval
*)
415 JS_PushArgumentsVA(JSContext
*cx
, void **markp
, const char *format
, va_list ap
);
418 extern JS_PUBLIC_API(void)
419 JS_PopArguments(JSContext
*cx
, void *mark
);
421 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
424 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
425 * The handler function has this signature (see jspubtd.h):
427 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
428 * JSBool fromJS, jsval **vpp, va_list *app);
430 * It should return true on success, and return false after reporting an error
431 * or detecting an already-reported error.
433 * For a given format string, for example "AA", the formatter is called from
434 * JS_ConvertArgumentsVA like so:
436 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
438 * sp points into the arguments array on the JS stack, while ap points into
439 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
440 * the formatter to convert zero or more jsvals at sp to zero or more C values
441 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
442 * (via *app) to point past the converted arguments and their result pointers
445 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
447 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
449 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
450 * format specifier and store them at sp, updating ap and sp appropriately.
452 * The "..." after "AA" is the rest of the format string that was passed into
453 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
454 * in each Convert or PushArguments call is passed to the formatter, so that
455 * one such function may implement several formats, in order to share code.
457 * Remove just forgets about any handler associated with format. Add does not
458 * copy format, it points at the string storage allocated by the caller, which
459 * is typically a string constant. If format is in dynamic storage, it is up
460 * to the caller to keep the string alive until Remove is called.
462 extern JS_PUBLIC_API(JSBool
)
463 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
464 JSArgumentFormatter formatter
);
466 extern JS_PUBLIC_API(void)
467 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
469 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
471 extern JS_PUBLIC_API(JSBool
)
472 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
474 extern JS_PUBLIC_API(JSBool
)
475 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
477 extern JS_PUBLIC_API(JSFunction
*)
478 JS_ValueToFunction(JSContext
*cx
, jsval v
);
480 extern JS_PUBLIC_API(JSFunction
*)
481 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
483 extern JS_PUBLIC_API(JSString
*)
484 JS_ValueToString(JSContext
*cx
, jsval v
);
486 extern JS_PUBLIC_API(JSString
*)
487 JS_ValueToSource(JSContext
*cx
, jsval v
);
489 extern JS_PUBLIC_API(JSBool
)
490 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
493 * Convert a value to a number, then to an int32, according to the ECMA rules
496 extern JS_PUBLIC_API(JSBool
)
497 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
500 * Convert a value to a number, then to a uint32, according to the ECMA rules
503 extern JS_PUBLIC_API(JSBool
)
504 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
507 * Convert a value to a number, then to an int32 if it fits by rounding to
508 * nearest; but failing with an error report if the double is out of range
511 extern JS_PUBLIC_API(JSBool
)
512 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
515 * ECMA ToUint16, for mapping a jsval to a Unicode point.
517 extern JS_PUBLIC_API(JSBool
)
518 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
520 extern JS_PUBLIC_API(JSBool
)
521 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
523 extern JS_PUBLIC_API(JSType
)
524 JS_TypeOfValue(JSContext
*cx
, jsval v
);
526 extern JS_PUBLIC_API(const char *)
527 JS_GetTypeName(JSContext
*cx
, JSType type
);
529 extern JS_PUBLIC_API(JSBool
)
530 JS_StrictlyEqual(JSContext
*cx
, jsval v1
, jsval v2
);
532 /************************************************************************/
535 * Initialization, locking, contexts, and memory allocation.
537 * It is important that the first runtime and first context be created in a
538 * single-threaded fashion, otherwise the behavior of the library is undefined.
539 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
541 #define JS_NewRuntime JS_Init
542 #define JS_DestroyRuntime JS_Finish
543 #define JS_LockRuntime JS_Lock
544 #define JS_UnlockRuntime JS_Unlock
546 extern JS_PUBLIC_API(JSRuntime
*)
547 JS_NewRuntime(uint32 maxbytes
);
549 extern JS_PUBLIC_API(void)
550 JS_DestroyRuntime(JSRuntime
*rt
);
552 extern JS_PUBLIC_API(void)
555 JS_PUBLIC_API(void *)
556 JS_GetRuntimePrivate(JSRuntime
*rt
);
559 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
561 extern JS_PUBLIC_API(void)
562 JS_BeginRequest(JSContext
*cx
);
564 extern JS_PUBLIC_API(void)
565 JS_EndRequest(JSContext
*cx
);
567 /* Yield to pending GC operations, regardless of request depth */
568 extern JS_PUBLIC_API(void)
569 JS_YieldRequest(JSContext
*cx
);
571 extern JS_PUBLIC_API(jsrefcount
)
572 JS_SuspendRequest(JSContext
*cx
);
574 extern JS_PUBLIC_API(void)
575 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
580 class JSAutoRequest
{
582 JSAutoRequest(JSContext
*cx
) : mContext(cx
), mSaveDepth(0) {
583 JS_BeginRequest(mContext
);
586 JS_EndRequest(mContext
);
590 mSaveDepth
= JS_SuspendRequest(mContext
);
593 JS_ResumeRequest(mContext
, mSaveDepth
);
598 jsrefcount mSaveDepth
;
602 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
603 static void operator delete(void *, size_t) { };
607 class JSAutoSuspendRequest
{
609 JSAutoSuspendRequest(JSContext
*cx
) : mContext(cx
), mSaveDepth(0) {
611 mSaveDepth
= JS_SuspendRequest(mContext
);
614 ~JSAutoSuspendRequest() {
620 JS_ResumeRequest(mContext
, mSaveDepth
);
627 jsrefcount mSaveDepth
;
631 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
632 static void operator delete(void *, size_t) { };
639 extern JS_PUBLIC_API(void)
640 JS_Lock(JSRuntime
*rt
);
642 extern JS_PUBLIC_API(void)
643 JS_Unlock(JSRuntime
*rt
);
645 extern JS_PUBLIC_API(JSContextCallback
)
646 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
648 extern JS_PUBLIC_API(JSContext
*)
649 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
651 extern JS_PUBLIC_API(void)
652 JS_DestroyContext(JSContext
*cx
);
654 extern JS_PUBLIC_API(void)
655 JS_DestroyContextNoGC(JSContext
*cx
);
657 extern JS_PUBLIC_API(void)
658 JS_DestroyContextMaybeGC(JSContext
*cx
);
660 extern JS_PUBLIC_API(void *)
661 JS_GetContextPrivate(JSContext
*cx
);
663 extern JS_PUBLIC_API(void)
664 JS_SetContextPrivate(JSContext
*cx
, void *data
);
666 extern JS_PUBLIC_API(JSRuntime
*)
667 JS_GetRuntime(JSContext
*cx
);
669 extern JS_PUBLIC_API(JSContext
*)
670 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
672 extern JS_PUBLIC_API(JSVersion
)
673 JS_GetVersion(JSContext
*cx
);
675 extern JS_PUBLIC_API(JSVersion
)
676 JS_SetVersion(JSContext
*cx
, JSVersion version
);
678 extern JS_PUBLIC_API(const char *)
679 JS_VersionToString(JSVersion version
);
681 extern JS_PUBLIC_API(JSVersion
)
682 JS_StringToVersion(const char *string
);
685 * JS options are orthogonal to version, and may be freely composed with one
686 * another as well as with version.
688 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
689 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
691 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
692 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
693 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
694 the last object on its 'obj'
695 param's scope chain as the
696 ECMA 'variables object' */
697 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
698 JS_BIT(3) /* context private data points
699 to an nsISupports subclass */
700 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
701 promises to execute compiled
702 script once only; enables
703 compile-time scope chain
704 resolution of consts. */
705 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
706 option supported for the
707 XUL preprocessor and kindred
709 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
710 parse <!-- --> as a token,
711 not backward compatible with
712 the comment-hiding hack used
713 in HTML script tags. */
714 #define JSOPTION_DONT_REPORT_UNCAUGHT \
715 JS_BIT(8) /* When returning from the
716 outermost API call, prevent
717 uncaught exceptions from
718 being converted to error
721 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
722 regular expression which
723 backtracks more than n^3
724 times, where n is length
725 of the input string */
726 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
727 statement context per
728 ECMA-262 Edition 3. */
730 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
732 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
733 that a null rval out-param
734 will be passed to each call
735 to JS_ExecuteScript. */
736 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
737 contexts' global objects
738 (see JS_GetGlobalObject),
739 leaving that up to the
742 extern JS_PUBLIC_API(uint32
)
743 JS_GetOptions(JSContext
*cx
);
745 extern JS_PUBLIC_API(uint32
)
746 JS_SetOptions(JSContext
*cx
, uint32 options
);
748 extern JS_PUBLIC_API(uint32
)
749 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
751 extern JS_PUBLIC_API(const char *)
752 JS_GetImplementationVersion(void);
754 extern JS_PUBLIC_API(JSObject
*)
755 JS_GetGlobalObject(JSContext
*cx
);
757 extern JS_PUBLIC_API(void)
758 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
761 * Initialize standard JS class constructors, prototypes, and any top-level
762 * functions and constants associated with the standard classes (e.g. isNaN
765 * NB: This sets cx's global object to obj if it was null.
767 extern JS_PUBLIC_API(JSBool
)
768 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
771 * Resolve id, which must contain either a string or an int, to a standard
772 * class name in obj if possible, defining the class's constructor and/or
773 * prototype and storing true in *resolved. If id does not name a standard
774 * class or a top-level property induced by initializing a standard class,
775 * store false in *resolved and just return true. Return false on error,
776 * as usual for JSBool result-typed API entry points.
778 * This API can be called directly from a global object class's resolve op,
779 * to define standard classes lazily. The class's enumerate op should call
780 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
781 * loops any classes not yet resolved lazily.
783 extern JS_PUBLIC_API(JSBool
)
784 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsval id
,
787 extern JS_PUBLIC_API(JSBool
)
788 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
791 * Enumerate any already-resolved standard class ids into ida, or into a new
792 * JSIdArray if ida is null. Return the augmented array on success, null on
793 * failure with ida (if it was non-null on entry) destroyed.
795 extern JS_PUBLIC_API(JSIdArray
*)
796 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
799 extern JS_PUBLIC_API(JSBool
)
800 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
803 extern JS_PUBLIC_API(JSObject
*)
804 JS_GetScopeChain(JSContext
*cx
);
806 extern JS_PUBLIC_API(JSObject
*)
807 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
810 * Macros to hide interpreter stack layout details from a JSFastNative using
811 * its jsval *vp parameter. The stack layout underlying invocation can't change
812 * without breaking source and binary compatibility (argv[-2] is well-known to
813 * be the callee jsval, and argv[-1] is as well known to be |this|).
815 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
816 * is the global object, so embeddings implementing fast natives *must* call
817 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
818 * which should propagate as a false return from native functions and hooks.
820 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
821 * handle a null obj parameter by returning false (throwing a TypeError if
822 * given non-null argv), so most native functions that type-check their |this|
823 * parameter need not add null checking.
825 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
826 * methods that may inspect their callee must defer setting their return value
827 * until after any such possible inspection. Otherwise the return value will be
828 * inspected instead of the callee function object.
830 * WARNING: These are not (yet) mandatory macros, but new code outside of the
831 * engine should use them. In the Mozilla 2.0 milestone their definitions may
832 * change incompatibly.
834 #define JS_CALLEE(cx,vp) ((vp)[0])
835 #define JS_ARGV_CALLEE(argv) ((argv)[-2])
836 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
837 #define JS_THIS_OBJECT(cx,vp) ((JSObject *) JS_THIS(cx,vp))
838 #define JS_ARGV(cx,vp) ((vp) + 2)
839 #define JS_RVAL(cx,vp) (*(vp))
840 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
842 extern JS_PUBLIC_API(jsval
)
843 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
845 extern JS_PUBLIC_API(void *)
846 JS_malloc(JSContext
*cx
, size_t nbytes
);
848 extern JS_PUBLIC_API(void *)
849 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
851 extern JS_PUBLIC_API(void)
852 JS_free(JSContext
*cx
, void *p
);
854 extern JS_PUBLIC_API(char *)
855 JS_strdup(JSContext
*cx
, const char *s
);
857 extern JS_PUBLIC_API(jsdouble
*)
858 JS_NewDouble(JSContext
*cx
, jsdouble d
);
860 extern JS_PUBLIC_API(JSBool
)
861 JS_NewDoubleValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
863 extern JS_PUBLIC_API(JSBool
)
864 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
867 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
868 * itself points into the GC heap (more recently, we support this extension:
869 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
871 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
872 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
873 * or the structure it is embedded within goes out of scope or is freed, you
874 * must call JS_RemoveRoot(cx, &obj).
876 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
877 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
878 * roots by their source callsites. This way, you can find the callsite while
879 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
880 * before freeing structPtr's memory.
882 extern JS_PUBLIC_API(JSBool
)
883 JS_AddRoot(JSContext
*cx
, void *rp
);
885 #ifdef NAME_ALL_GC_ROOTS
886 #define JS_DEFINE_TO_TOKEN(def) #def
887 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
888 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
891 extern JS_PUBLIC_API(JSBool
)
892 JS_AddNamedRoot(JSContext
*cx
, void *rp
, const char *name
);
894 extern JS_PUBLIC_API(JSBool
)
895 JS_AddNamedRootRT(JSRuntime
*rt
, void *rp
, const char *name
);
897 extern JS_PUBLIC_API(JSBool
)
898 JS_RemoveRoot(JSContext
*cx
, void *rp
);
900 extern JS_PUBLIC_API(JSBool
)
901 JS_RemoveRootRT(JSRuntime
*rt
, void *rp
);
904 * The last GC thing of each type (object, string, double, external string
905 * types) created on a given context is kept alive until another thing of the
906 * same type is created, using a newborn root in the context. These newborn
907 * roots help native code protect newly-created GC-things from GC invocations
908 * activated before those things can be rooted using local or global roots.
910 * However, the newborn roots can also entrain great gobs of garbage, so the
911 * JS_GC entry point clears them for the context on which GC is being forced.
912 * Embeddings may need to do likewise for all contexts.
914 * See the scoped local root API immediately below for a better way to manage
915 * newborns in cases where native hooks (functions, getters, setters, etc.)
916 * create many GC-things, potentially without connecting them to predefined
917 * local roots such as *rval or argv[i] in an active native function. Using
918 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
919 * newborn roots, until control flow unwinds and leaves the outermost nesting
922 extern JS_PUBLIC_API(void)
923 JS_ClearNewbornRoots(JSContext
*cx
);
926 * Scoped local root management allows native functions, getter/setters, etc.
927 * to avoid worrying about the newborn root pigeon-holes, overloading local
928 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
929 * and JS_RemoveRoot to manage global roots temporarily.
931 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
932 * the body of the native hook causes the engine to allocate a local root for
933 * each newborn created in between the two API calls, using a local root stack
934 * associated with cx. For example:
937 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
941 * if (!JS_EnterLocalRootScope(cx))
943 * ok = my_GetPropertyBody(cx, obj, id, vp);
944 * JS_LeaveLocalRootScope(cx);
948 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
949 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
950 * not make the matching JS_LeaveLocalRootScope call.
952 * JS_LeaveLocalRootScopeWithResult(cx, rval) is an alternative way to leave
953 * a local root scope that protects a result or return value, by effectively
954 * pushing it in the caller's local root scope.
956 * In case a native hook allocates many objects or other GC-things, but the
957 * native protects some of those GC-things by storing them as property values
958 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
959 * to free the local root automatically pushed for the now-protected GC-thing.
961 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
962 * root scope, but it's more time-efficient when called on references to more
963 * recently created GC-things. Calling it successively on other than the most
964 * recently allocated GC-thing will tend to average the time inefficiency, and
965 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
966 * many local roots if you can root as you go (build a tree of objects from
967 * the top down, forgetting each latest-allocated GC-thing immediately upon
968 * linking it to its parent).
970 extern JS_PUBLIC_API(JSBool
)
971 JS_EnterLocalRootScope(JSContext
*cx
);
973 extern JS_PUBLIC_API(void)
974 JS_LeaveLocalRootScope(JSContext
*cx
);
976 extern JS_PUBLIC_API(void)
977 JS_LeaveLocalRootScopeWithResult(JSContext
*cx
, jsval rval
);
979 extern JS_PUBLIC_API(void)
980 JS_ForgetLocalRoot(JSContext
*cx
, void *thing
);
985 class JSAutoLocalRootScope
{
987 JSAutoLocalRootScope(JSContext
*cx
) : mContext(cx
) {
988 JS_EnterLocalRootScope(mContext
);
990 ~JSAutoLocalRootScope() {
991 JS_LeaveLocalRootScope(mContext
);
994 void forget(void *thing
) {
995 JS_ForgetLocalRoot(mContext
, thing
);
1003 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
1004 static void operator delete(void *, size_t) { };
1012 extern JS_PUBLIC_API(void)
1013 JS_DumpNamedRoots(JSRuntime
*rt
,
1014 void (*dump
)(const char *name
, void *rp
, void *data
),
1019 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
1020 * The root is pointed at by rp; if the root is unnamed, name is null; data is
1021 * supplied from the third parameter to JS_MapGCRoots.
1023 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
1024 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
1025 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
1026 * constants are flags; you can OR them together.
1028 * This function acquires and releases rt's GC lock around the mapping of the
1029 * roots table, so the map function should run to completion in as few cycles
1030 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
1031 * or any JS API entry point that acquires locks, without double-tripping or
1032 * deadlocking on the GC lock.
1034 * JS_MapGCRoots returns the count of roots that were successfully mapped.
1036 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
1037 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
1038 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
1041 (* JSGCRootMapFun
)(void *rp
, const char *name
, void *data
);
1043 extern JS_PUBLIC_API(uint32
)
1044 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
1046 extern JS_PUBLIC_API(JSBool
)
1047 JS_LockGCThing(JSContext
*cx
, void *thing
);
1049 extern JS_PUBLIC_API(JSBool
)
1050 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
1052 extern JS_PUBLIC_API(JSBool
)
1053 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
1055 extern JS_PUBLIC_API(JSBool
)
1056 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
1059 * Register externally maintained GC roots.
1061 * traceOp: the trace operation. For each root the implementation should call
1062 * JS_CallTracer whenever the root contains a traceable thing.
1063 * data: the data argument to pass to each invocation of traceOp.
1065 extern JS_PUBLIC_API(void)
1066 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
1069 * For implementors of JSMarkOp. All new code should implement JSTraceOp
1072 extern JS_PUBLIC_API(void)
1073 JS_MarkGCThing(JSContext
*cx
, void *thing
, const char *name
, void *arg
);
1076 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
1077 * enumerate all references to traceable things reachable via a property or
1078 * other strong ref identified for debugging purposes by name or index or
1079 * a naming callback.
1081 * By definition references to traceable things include non-null pointers
1082 * to JSObject, JSString and jsdouble and corresponding jsvals.
1084 * See the JSTraceOp typedef in jspubtd.h.
1087 /* Trace kinds to pass to JS_Tracing. */
1088 #define JSTRACE_OBJECT 0
1089 #define JSTRACE_DOUBLE 1
1090 #define JSTRACE_STRING 2
1093 * Use the following macros to check if a particular jsval is a traceable
1094 * thing and to extract the thing and its kind to pass to JS_CallTracer.
1096 #define JSVAL_IS_TRACEABLE(v) (JSVAL_IS_GCTHING(v) && !JSVAL_IS_NULL(v))
1097 #define JSVAL_TO_TRACEABLE(v) (JSVAL_TO_GCTHING(v))
1098 #define JSVAL_TRACE_KIND(v) (JSVAL_TAG(v) >> 1)
1102 JSTraceCallback callback
;
1104 JSTraceNamePrinter debugPrinter
;
1105 const void *debugPrintArg
;
1106 size_t debugPrintIndex
;
1111 * The method to call on each reference to a traceable thing stored in a
1112 * particular JSObject or other runtime structure. With DEBUG defined the
1113 * caller before calling JS_CallTracer must initialize JSTracer fields
1114 * describing the reference using the macros below.
1116 extern JS_PUBLIC_API(void)
1117 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
1120 * Set debugging information about a reference to a traceable thing to prepare
1121 * for the following call to JS_CallTracer.
1123 * When printer is null, arg must be const char * or char * C string naming
1124 * the reference and index must be either (size_t)-1 indicating that the name
1125 * alone describes the reference or it must be an index into some array vector
1126 * that stores the reference.
1128 * When printer callback is not null, the arg and index arguments are
1129 * available to the callback as debugPrinterArg and debugPrintIndex fields
1132 * The storage for name or callback's arguments needs to live only until
1133 * the following call to JS_CallTracer returns.
1136 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1138 (trc)->debugPrinter = (printer); \
1139 (trc)->debugPrintArg = (arg); \
1140 (trc)->debugPrintIndex = (index); \
1143 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1149 * Convenience macro to describe the argument of JS_CallTracer using C string
1152 # define JS_SET_TRACING_INDEX(trc, name, index) \
1153 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1156 * Convenience macro to describe the argument of JS_CallTracer using C string.
1158 # define JS_SET_TRACING_NAME(trc, name) \
1159 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1162 * Convenience macro to invoke JS_CallTracer using C string as the name for
1163 * the reference to a traceable thing.
1165 # define JS_CALL_TRACER(trc, thing, kind, name) \
1167 JS_SET_TRACING_NAME(trc, name); \
1168 JS_CallTracer((trc), (thing), (kind)); \
1172 * Convenience macros to invoke JS_CallTracer when jsval represents a
1173 * reference to a traceable thing.
1175 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1177 if (JSVAL_IS_TRACEABLE(val)) { \
1178 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1179 JSVAL_TRACE_KIND(val), name); \
1183 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1185 JSObject *obj_ = (object); \
1187 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1190 #define JS_CALL_STRING_TRACER(trc, string, name) \
1192 JSString *str_ = (string); \
1194 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1197 #define JS_CALL_DOUBLE_TRACER(trc, number, name) \
1199 jsdouble *num_ = (number); \
1201 JS_CALL_TRACER((trc), num_, JSTRACE_DOUBLE, name); \
1205 * API for JSTraceCallback implementations.
1207 # define JS_TRACER_INIT(trc, cx_, callback_) \
1209 (trc)->context = (cx_); \
1210 (trc)->callback = (callback_); \
1211 JS_SET_TRACING_DETAILS(trc, NULL, NULL, (size_t)-1); \
1214 extern JS_PUBLIC_API(void)
1215 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1217 extern JS_PUBLIC_API(void)
1218 JS_TraceRuntime(JSTracer
*trc
);
1222 extern JS_PUBLIC_API(void)
1223 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1224 void *thing
, uint32 kind
, JSBool includeDetails
);
1227 * DEBUG-only method to dump the object graph of heap-allocated things.
1229 * fp: file for the dump output.
1230 * start: when non-null, dump only things reachable from start
1231 * thing. Otherwise dump all things reachable from the
1233 * startKind: trace kind of start if start is not null. Must be 0 when
1235 * thingToFind: dump only paths in the object graph leading to thingToFind
1237 * maxDepth: the upper bound on the number of edges to descend from the
1239 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1241 extern JS_PUBLIC_API(JSBool
)
1242 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1243 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1248 * Garbage collector API.
1250 extern JS_PUBLIC_API(void)
1251 JS_GC(JSContext
*cx
);
1253 extern JS_PUBLIC_API(void)
1254 JS_MaybeGC(JSContext
*cx
);
1256 extern JS_PUBLIC_API(JSGCCallback
)
1257 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1259 extern JS_PUBLIC_API(JSGCCallback
)
1260 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1262 extern JS_PUBLIC_API(JSBool
)
1263 JS_IsGCMarkingTracer(JSTracer
*trc
);
1265 extern JS_PUBLIC_API(JSBool
)
1266 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1268 typedef enum JSGCParamKey
{
1269 /* Maximum nominal heap before last ditch GC. */
1272 /* Number of JS_malloc bytes before last ditch GC. */
1273 JSGC_MAX_MALLOC_BYTES
= 1,
1275 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1276 JSGC_STACKPOOL_LIFESPAN
= 2,
1279 * The factor that defines when the GC is invoked. The factor is a
1280 * percent of the memory allocated by the GC after the last run of
1281 * the GC. When the current memory allocated by the GC is more than
1282 * this percent then the GC is invoked. The factor cannot be less
1283 * than 100 since the current memory allocated by the GC cannot be less
1284 * than the memory allocated after the last run of the GC.
1286 JSGC_TRIGGER_FACTOR
= 3,
1288 /* Amount of bytes allocated by the GC. */
1291 /* Number of times when GC was invoked. */
1294 /* Max size of the code cache in bytes. */
1295 JSGC_MAX_CODE_CACHE_BYTES
= 6
1298 extern JS_PUBLIC_API(void)
1299 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1301 extern JS_PUBLIC_API(uint32
)
1302 JS_GetGCParameter(JSRuntime
*rt
, JSGCParamKey key
);
1304 extern JS_PUBLIC_API(void)
1305 JS_SetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
, uint32 value
);
1307 extern JS_PUBLIC_API(uint32
)
1308 JS_GetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
);
1311 * Add a finalizer for external strings created by JS_NewExternalString (see
1312 * below) using a type-code returned from this function, and that understands
1313 * how to free or release the memory pointed at by JS_GetStringChars(str).
1315 * Return a nonnegative type index if there is room for finalizer in the
1316 * global GC finalizers table, else return -1. If the engine is compiled
1317 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1318 * be invoked on the primordial thread only, at startup -- or else the entire
1319 * program must single-thread itself while loading a module that calls this
1322 extern JS_PUBLIC_API(intN
)
1323 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1326 * Remove finalizer from the global GC finalizers table, returning its type
1327 * code if found, -1 if not found.
1329 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1330 * if you compile the engine JS_THREADSAFE: this function may be called for a
1331 * given finalizer pointer on only one thread; different threads may call to
1332 * remove distinct finalizers safely.
1334 * You must ensure that all strings with finalizer's type have been collected
1335 * before calling this function. Otherwise, string data will be leaked by the
1336 * GC, for want of a finalizer to call.
1338 extern JS_PUBLIC_API(intN
)
1339 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1342 * Create a new JSString whose chars member refers to external memory, i.e.,
1343 * memory requiring special, type-specific finalization. The type code must
1344 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1346 extern JS_PUBLIC_API(JSString
*)
1347 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1350 * Returns the external-string finalizer index for this string, or -1 if it is
1351 * an "internal" (native to JS engine) string.
1353 extern JS_PUBLIC_API(intN
)
1354 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1357 * Sets maximum (if stack grows upward) or minimum (downward) legal stack byte
1358 * address in limitAddr for the thread or process stack used by cx. To disable
1359 * stack size checking, pass 0 for limitAddr.
1361 extern JS_PUBLIC_API(void)
1362 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1365 * Set the quota on the number of bytes that stack-like data structures can
1366 * use when the runtime compiles and executes scripts. These structures
1367 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1368 * The default quota is 32MB which is quite generous.
1370 * The function must be called before any script compilation or execution API
1371 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1374 extern JS_PUBLIC_API(void)
1375 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1377 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1379 /************************************************************************/
1382 * Classes, objects, and properties.
1385 /* For detailed comments on the function pointer types, see jspubtd.h. */
1390 /* Mandatory non-null function pointer members. */
1391 JSPropertyOp addProperty
;
1392 JSPropertyOp delProperty
;
1393 JSPropertyOp getProperty
;
1394 JSPropertyOp setProperty
;
1395 JSEnumerateOp enumerate
;
1396 JSResolveOp resolve
;
1397 JSConvertOp convert
;
1398 JSFinalizeOp finalize
;
1400 /* Optionally non-null members start here. */
1401 JSGetObjectOps getObjectOps
;
1402 JSCheckAccessOp checkAccess
;
1405 JSXDRObjectOp xdrObject
;
1406 JSHasInstanceOp hasInstance
;
1408 JSReserveSlotsOp reserveSlots
;
1411 struct JSExtendedClass
{
1413 JSEqualityOp equality
;
1414 JSObjectOp outerObject
;
1415 JSObjectOp innerObject
;
1416 JSIteratorOp iteratorObject
;
1417 JSObjectOp wrappedObject
; /* NB: infallible, null
1418 returns are treated as
1419 the original object */
1420 void (*reserved0
)(void);
1421 void (*reserved1
)(void);
1422 void (*reserved2
)(void);
1425 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1426 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1427 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1428 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1429 #define JSCLASS_SHARE_ALL_PROPERTIES (1<<4) /* all properties are SHARED */
1430 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1431 object in prototype chain
1432 passed in via *objp in/out
1434 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1436 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1439 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1440 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1441 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1443 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1444 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1445 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1446 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1447 << JSCLASS_RESERVED_SLOTS_SHIFT)
1448 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1449 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1450 & JSCLASS_RESERVED_SLOTS_MASK)
1452 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1453 JSCLASS_RESERVED_SLOTS_WIDTH)
1455 /* True if JSClass is really a JSExtendedClass. */
1456 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1457 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1458 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1460 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1461 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1464 * ECMA-262 requires that most constructors used internally create objects
1465 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1466 * member initial value. The "original ... value" verbiage is there because
1467 * in ECMA-262, global properties naming class objects are read/write and
1468 * deleteable, for the most part.
1470 * Implementing this efficiently requires that global objects have classes
1471 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS won't break
1472 * anything except the ECMA-262 "original prototype value" behavior, which was
1473 * broken for years in SpiderMonkey. In other words, without these flags you
1474 * get backward compatibility.
1476 #define JSCLASS_GLOBAL_FLAGS \
1477 (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT))
1479 /* Fast access to the original value of each standard class's prototype. */
1480 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1481 #define JSCLASS_CACHED_PROTO_WIDTH 8
1482 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1483 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1484 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1486 >> JSCLASS_CACHED_PROTO_SHIFT) \
1487 & JSCLASS_CACHED_PROTO_MASK))
1489 /* Initializer for unused members of statically initialized JSClass structs. */
1490 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
1491 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
1495 jsid vector
[1]; /* actually, length jsid words */
1498 extern JS_PUBLIC_API(void)
1499 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
1501 extern JS_PUBLIC_API(JSBool
)
1502 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
1504 extern JS_PUBLIC_API(JSBool
)
1505 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
1508 * The magic XML namespace id is int-tagged, but not a valid integer jsval.
1509 * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
1510 * should handle this id specially before converting id via JSVAL_TO_INT.
1512 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
1515 * JSNewResolveOp flag bits.
1517 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1518 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1519 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1520 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1521 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1522 #define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
1524 extern JS_PUBLIC_API(JSBool
)
1525 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsval id
, jsval
*vp
);
1527 extern JS_PUBLIC_API(JSBool
)
1528 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
1530 extern JS_PUBLIC_API(JSBool
)
1531 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsval id
);
1533 extern JS_PUBLIC_API(JSBool
)
1534 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
1536 extern JS_PUBLIC_API(void)
1537 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
1539 struct JSConstDoubleSpec
{
1547 * To define an array element rather than a named property member, cast the
1548 * element's index to (const char *) and initialize name with it, and set the
1549 * JSPROP_INDEX bit in flags.
1551 struct JSPropertySpec
{
1555 JSPropertyOp getter
;
1556 JSPropertyOp setter
;
1559 struct JSFunctionSpec
{
1566 * extra & 0xFFFF: Number of extra argument slots for local GC roots.
1567 * If fast native, must be zero.
1568 * extra >> 16: Reserved for future use (must be 0).
1574 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1575 * that's passed to JS_DefineFunctions or JS_InitClass.
1577 #define JS_FS_END JS_FS(NULL,NULL,0,0,0)
1580 * Initializer macro for a JSFunctionSpec array element. This is the original
1581 * kind of native function specifier initializer. Use JS_FN ("fast native", see
1582 * JSFastNative in jspubtd.h) for all functions that do not need a stack frame
1585 #define JS_FS(name,call,nargs,flags,extra) \
1586 {name, call, nargs, flags, extra}
1589 * "Fast native" initializer macro for a JSFunctionSpec array element. Use this
1590 * in preference to JS_FS if the native in question does not need its own stack
1591 * frame when activated.
1593 #define JS_FN(name,fastcall,nargs,flags) \
1594 JS_FS(name, (JSNative)(fastcall), nargs, \
1595 (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0)
1597 extern JS_PUBLIC_API(JSObject
*)
1598 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
1599 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
1600 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
1601 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
1603 #ifdef JS_THREADSAFE
1604 extern JS_PUBLIC_API(JSClass
*)
1605 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
1607 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1609 extern JS_PUBLIC_API(JSClass
*)
1610 JS_GetClass(JSObject
*obj
);
1612 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1615 extern JS_PUBLIC_API(JSBool
)
1616 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
1618 extern JS_PUBLIC_API(JSBool
)
1619 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
1621 extern JS_PUBLIC_API(void *)
1622 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
1624 extern JS_PUBLIC_API(JSBool
)
1625 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
1627 extern JS_PUBLIC_API(void *)
1628 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
1631 extern JS_PUBLIC_API(JSObject
*)
1632 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
1634 extern JS_PUBLIC_API(JSBool
)
1635 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
1637 extern JS_PUBLIC_API(JSObject
*)
1638 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
1640 extern JS_PUBLIC_API(JSBool
)
1641 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
1643 extern JS_PUBLIC_API(JSObject
*)
1644 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
1647 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1648 * is moved by a copying GC). Return false on failure (likely out of memory),
1649 * and true with *idp containing the unique id on success.
1651 extern JS_PUBLIC_API(JSBool
)
1652 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
1654 extern JS_PUBLIC_API(JSObject
*)
1655 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
1658 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1659 * proto if proto's actual parameter value is null.
1661 extern JS_PUBLIC_API(JSObject
*)
1662 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1665 extern JS_PUBLIC_API(JSBool
)
1666 JS_SealObject(JSContext
*cx
, JSObject
*obj
, JSBool deep
);
1668 extern JS_PUBLIC_API(JSObject
*)
1669 JS_ConstructObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1672 extern JS_PUBLIC_API(JSObject
*)
1673 JS_ConstructObjectWithArguments(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1674 JSObject
*parent
, uintN argc
, jsval
*argv
);
1676 extern JS_PUBLIC_API(JSObject
*)
1677 JS_DefineObject(JSContext
*cx
, JSObject
*obj
, const char *name
, JSClass
*clasp
,
1678 JSObject
*proto
, uintN attrs
);
1680 extern JS_PUBLIC_API(JSBool
)
1681 JS_DefineConstDoubles(JSContext
*cx
, JSObject
*obj
, JSConstDoubleSpec
*cds
);
1683 extern JS_PUBLIC_API(JSBool
)
1684 JS_DefineProperties(JSContext
*cx
, JSObject
*obj
, JSPropertySpec
*ps
);
1686 extern JS_PUBLIC_API(JSBool
)
1687 JS_DefineProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval value
,
1688 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1690 extern JS_PUBLIC_API(JSBool
)
1691 JS_DefinePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval value
,
1692 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1695 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1697 * If the object does not have a property by that name, *foundp will be
1698 * JS_FALSE and the value of *attrsp is undefined.
1700 extern JS_PUBLIC_API(JSBool
)
1701 JS_GetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1702 uintN
*attrsp
, JSBool
*foundp
);
1705 * The same, but if the property is native, return its getter and setter via
1706 * *getterp and *setterp, respectively (and only if the out parameter pointer
1709 extern JS_PUBLIC_API(JSBool
)
1710 JS_GetPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1712 uintN
*attrsp
, JSBool
*foundp
,
1713 JSPropertyOp
*getterp
,
1714 JSPropertyOp
*setterp
);
1716 extern JS_PUBLIC_API(JSBool
)
1717 JS_GetPropertyAttrsGetterAndSetterById(JSContext
*cx
, JSObject
*obj
,
1719 uintN
*attrsp
, JSBool
*foundp
,
1720 JSPropertyOp
*getterp
,
1721 JSPropertyOp
*setterp
);
1724 * Set the attributes of a property on a given object.
1726 * If the object does not have a property by that name, *foundp will be
1727 * JS_FALSE and nothing will be altered.
1729 extern JS_PUBLIC_API(JSBool
)
1730 JS_SetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1731 uintN attrs
, JSBool
*foundp
);
1733 extern JS_PUBLIC_API(JSBool
)
1734 JS_DefinePropertyWithTinyId(JSContext
*cx
, JSObject
*obj
, const char *name
,
1735 int8 tinyid
, jsval value
,
1736 JSPropertyOp getter
, JSPropertyOp setter
,
1739 extern JS_PUBLIC_API(JSBool
)
1740 JS_AliasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1743 extern JS_PUBLIC_API(JSBool
)
1744 JS_AlreadyHasOwnProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1747 extern JS_PUBLIC_API(JSBool
)
1748 JS_AlreadyHasOwnPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1751 extern JS_PUBLIC_API(JSBool
)
1752 JS_HasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, JSBool
*foundp
);
1754 extern JS_PUBLIC_API(JSBool
)
1755 JS_HasPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool
*foundp
);
1757 extern JS_PUBLIC_API(JSBool
)
1758 JS_LookupProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1760 extern JS_PUBLIC_API(JSBool
)
1761 JS_LookupPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1763 extern JS_PUBLIC_API(JSBool
)
1764 JS_LookupPropertyWithFlags(JSContext
*cx
, JSObject
*obj
, const char *name
,
1765 uintN flags
, jsval
*vp
);
1767 extern JS_PUBLIC_API(JSBool
)
1768 JS_LookupPropertyWithFlagsById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1769 uintN flags
, JSObject
**objp
, jsval
*vp
);
1771 struct JSPropertyDescriptor
{
1774 JSPropertyOp getter
;
1775 JSPropertyOp setter
;
1780 * Like JS_GetPropertyAttrsGetterAndSetterById but will return a property on
1781 * an object on the prototype chain (returned in objp). If data->obj is null,
1782 * then this property was not found on the prototype chain.
1784 extern JS_PUBLIC_API(JSBool
)
1785 JS_GetPropertyDescriptorById(JSContext
*cx
, JSObject
*obj
, jsid id
, uintN flags
,
1786 JSPropertyDescriptor
*desc
);
1788 extern JS_PUBLIC_API(JSBool
)
1789 JS_GetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1791 extern JS_PUBLIC_API(JSBool
)
1792 JS_GetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1794 extern JS_PUBLIC_API(JSBool
)
1795 JS_GetMethodById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSObject
**objp
,
1798 extern JS_PUBLIC_API(JSBool
)
1799 JS_GetMethod(JSContext
*cx
, JSObject
*obj
, const char *name
, JSObject
**objp
,
1802 extern JS_PUBLIC_API(JSBool
)
1803 JS_SetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1805 extern JS_PUBLIC_API(JSBool
)
1806 JS_SetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1808 extern JS_PUBLIC_API(JSBool
)
1809 JS_DeleteProperty(JSContext
*cx
, JSObject
*obj
, const char *name
);
1811 extern JS_PUBLIC_API(JSBool
)
1812 JS_DeleteProperty2(JSContext
*cx
, JSObject
*obj
, const char *name
,
1815 extern JS_PUBLIC_API(JSBool
)
1816 JS_DeletePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
);
1818 extern JS_PUBLIC_API(JSBool
)
1819 JS_DeletePropertyById2(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*rval
);
1821 extern JS_PUBLIC_API(JSBool
)
1822 JS_DefineUCProperty(JSContext
*cx
, JSObject
*obj
,
1823 const jschar
*name
, size_t namelen
, jsval value
,
1824 JSPropertyOp getter
, JSPropertyOp setter
,
1828 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1830 * If the object does not have a property by that name, *foundp will be
1831 * JS_FALSE and the value of *attrsp is undefined.
1833 extern JS_PUBLIC_API(JSBool
)
1834 JS_GetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1835 const jschar
*name
, size_t namelen
,
1836 uintN
*attrsp
, JSBool
*foundp
);
1839 * The same, but if the property is native, return its getter and setter via
1840 * *getterp and *setterp, respectively (and only if the out parameter pointer
1843 extern JS_PUBLIC_API(JSBool
)
1844 JS_GetUCPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1845 const jschar
*name
, size_t namelen
,
1846 uintN
*attrsp
, JSBool
*foundp
,
1847 JSPropertyOp
*getterp
,
1848 JSPropertyOp
*setterp
);
1851 * Set the attributes of a property on a given object.
1853 * If the object does not have a property by that name, *foundp will be
1854 * JS_FALSE and nothing will be altered.
1856 extern JS_PUBLIC_API(JSBool
)
1857 JS_SetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1858 const jschar
*name
, size_t namelen
,
1859 uintN attrs
, JSBool
*foundp
);
1862 extern JS_PUBLIC_API(JSBool
)
1863 JS_DefineUCPropertyWithTinyId(JSContext
*cx
, JSObject
*obj
,
1864 const jschar
*name
, size_t namelen
,
1865 int8 tinyid
, jsval value
,
1866 JSPropertyOp getter
, JSPropertyOp setter
,
1869 extern JS_PUBLIC_API(JSBool
)
1870 JS_AlreadyHasOwnUCProperty(JSContext
*cx
, JSObject
*obj
, const jschar
*name
,
1871 size_t namelen
, JSBool
*foundp
);
1873 extern JS_PUBLIC_API(JSBool
)
1874 JS_HasUCProperty(JSContext
*cx
, JSObject
*obj
,
1875 const jschar
*name
, size_t namelen
,
1878 extern JS_PUBLIC_API(JSBool
)
1879 JS_LookupUCProperty(JSContext
*cx
, JSObject
*obj
,
1880 const jschar
*name
, size_t namelen
,
1883 extern JS_PUBLIC_API(JSBool
)
1884 JS_GetUCProperty(JSContext
*cx
, JSObject
*obj
,
1885 const jschar
*name
, size_t namelen
,
1888 extern JS_PUBLIC_API(JSBool
)
1889 JS_SetUCProperty(JSContext
*cx
, JSObject
*obj
,
1890 const jschar
*name
, size_t namelen
,
1893 extern JS_PUBLIC_API(JSBool
)
1894 JS_DeleteUCProperty2(JSContext
*cx
, JSObject
*obj
,
1895 const jschar
*name
, size_t namelen
,
1898 extern JS_PUBLIC_API(JSObject
*)
1899 JS_NewArrayObject(JSContext
*cx
, jsint length
, jsval
*vector
);
1901 extern JS_PUBLIC_API(JSBool
)
1902 JS_IsArrayObject(JSContext
*cx
, JSObject
*obj
);
1904 extern JS_PUBLIC_API(JSBool
)
1905 JS_GetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1907 extern JS_PUBLIC_API(JSBool
)
1908 JS_SetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint length
);
1910 extern JS_PUBLIC_API(JSBool
)
1911 JS_HasArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1913 extern JS_PUBLIC_API(JSBool
)
1914 JS_DefineElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval value
,
1915 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1917 extern JS_PUBLIC_API(JSBool
)
1918 JS_AliasElement(JSContext
*cx
, JSObject
*obj
, const char *name
, jsint alias
);
1920 extern JS_PUBLIC_API(JSBool
)
1921 JS_AlreadyHasOwnElement(JSContext
*cx
, JSObject
*obj
, jsint index
,
1924 extern JS_PUBLIC_API(JSBool
)
1925 JS_HasElement(JSContext
*cx
, JSObject
*obj
, jsint index
, JSBool
*foundp
);
1927 extern JS_PUBLIC_API(JSBool
)
1928 JS_LookupElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1930 extern JS_PUBLIC_API(JSBool
)
1931 JS_GetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1933 extern JS_PUBLIC_API(JSBool
)
1934 JS_SetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1936 extern JS_PUBLIC_API(JSBool
)
1937 JS_DeleteElement(JSContext
*cx
, JSObject
*obj
, jsint index
);
1939 extern JS_PUBLIC_API(JSBool
)
1940 JS_DeleteElement2(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*rval
);
1942 extern JS_PUBLIC_API(void)
1943 JS_ClearScope(JSContext
*cx
, JSObject
*obj
);
1945 extern JS_PUBLIC_API(JSIdArray
*)
1946 JS_Enumerate(JSContext
*cx
, JSObject
*obj
);
1949 * Create an object to iterate over enumerable properties of obj, in arbitrary
1950 * property definition order. NB: This differs from longstanding for..in loop
1951 * order, which uses order of property definition in obj.
1953 extern JS_PUBLIC_API(JSObject
*)
1954 JS_NewPropertyIterator(JSContext
*cx
, JSObject
*obj
);
1957 * Return true on success with *idp containing the id of the next enumerable
1958 * property to visit using iterobj, or JSVAL_VOID if there is no such property
1959 * left to visit. Return false on error.
1961 extern JS_PUBLIC_API(JSBool
)
1962 JS_NextProperty(JSContext
*cx
, JSObject
*iterobj
, jsid
*idp
);
1964 extern JS_PUBLIC_API(JSBool
)
1965 JS_CheckAccess(JSContext
*cx
, JSObject
*obj
, jsid id
, JSAccessMode mode
,
1966 jsval
*vp
, uintN
*attrsp
);
1968 extern JS_PUBLIC_API(JSBool
)
1969 JS_GetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval
*vp
);
1971 extern JS_PUBLIC_API(JSBool
)
1972 JS_SetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval v
);
1974 /************************************************************************/
1977 * Security protocol.
1979 struct JSPrincipals
{
1982 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
1983 void * (* getPrincipalArray
)(JSContext
*cx
, JSPrincipals
*);
1984 JSBool (* globalPrivilegesEnabled
)(JSContext
*cx
, JSPrincipals
*);
1986 /* Don't call "destroy"; use reference counting macros below. */
1987 jsrefcount refcount
;
1989 void (* destroy
)(JSContext
*cx
, JSPrincipals
*);
1990 JSBool (* subsume
)(JSPrincipals
*, JSPrincipals
*);
1993 #ifdef JS_THREADSAFE
1994 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
1995 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
1997 extern JS_PUBLIC_API(jsrefcount
)
1998 JS_HoldPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2000 extern JS_PUBLIC_API(jsrefcount
)
2001 JS_DropPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2004 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
2005 #define JSPRINCIPALS_DROP(cx, principals) \
2006 ((--(principals)->refcount == 0) \
2007 ? ((*(principals)->destroy)((cx), (principals)), 0) \
2008 : (principals)->refcount)
2012 struct JSSecurityCallbacks
{
2013 JSCheckAccessOp checkObjectAccess
;
2014 JSPrincipalsTranscoder principalsTranscoder
;
2015 JSObjectPrincipalsFinder findObjectPrincipals
;
2018 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2019 JS_SetRuntimeSecurityCallbacks(JSRuntime
*rt
, JSSecurityCallbacks
*callbacks
);
2021 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2022 JS_GetRuntimeSecurityCallbacks(JSRuntime
*rt
);
2024 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2025 JS_SetContextSecurityCallbacks(JSContext
*cx
, JSSecurityCallbacks
*callbacks
);
2027 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2028 JS_GetSecurityCallbacks(JSContext
*cx
);
2030 /************************************************************************/
2033 * Functions and scripts.
2035 extern JS_PUBLIC_API(JSFunction
*)
2036 JS_NewFunction(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
2037 JSObject
*parent
, const char *name
);
2039 extern JS_PUBLIC_API(JSObject
*)
2040 JS_GetFunctionObject(JSFunction
*fun
);
2043 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
2044 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
2046 extern JS_PUBLIC_API(const char *)
2047 JS_GetFunctionName(JSFunction
*fun
);
2050 * Return the function's identifier as a JSString, or null if fun is unnamed.
2051 * The returned string lives as long as fun, so you don't need to root a saved
2052 * reference to it if fun is well-connected or rooted, and provided you bound
2053 * the use of the saved reference by fun's lifetime.
2055 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
2056 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
2057 * from UTF-16-ish jschars.
2059 extern JS_PUBLIC_API(JSString
*)
2060 JS_GetFunctionId(JSFunction
*fun
);
2063 * Return JSFUN_* flags for fun.
2065 extern JS_PUBLIC_API(uintN
)
2066 JS_GetFunctionFlags(JSFunction
*fun
);
2069 * Return the arity (length) of fun.
2071 extern JS_PUBLIC_API(uint16
)
2072 JS_GetFunctionArity(JSFunction
*fun
);
2075 * Infallible predicate to test whether obj is a function object (faster than
2076 * comparing obj's class name to "Function", but equivalent unless someone has
2077 * overwritten the "Function" identifier with a different constructor and then
2078 * created instances using that constructor that might be passed in as obj).
2080 extern JS_PUBLIC_API(JSBool
)
2081 JS_ObjectIsFunction(JSContext
*cx
, JSObject
*obj
);
2083 extern JS_PUBLIC_API(JSBool
)
2084 JS_DefineFunctions(JSContext
*cx
, JSObject
*obj
, JSFunctionSpec
*fs
);
2086 extern JS_PUBLIC_API(JSFunction
*)
2087 JS_DefineFunction(JSContext
*cx
, JSObject
*obj
, const char *name
, JSNative call
,
2088 uintN nargs
, uintN attrs
);
2090 extern JS_PUBLIC_API(JSFunction
*)
2091 JS_DefineUCFunction(JSContext
*cx
, JSObject
*obj
,
2092 const jschar
*name
, size_t namelen
, JSNative call
,
2093 uintN nargs
, uintN attrs
);
2095 extern JS_PUBLIC_API(JSObject
*)
2096 JS_CloneFunctionObject(JSContext
*cx
, JSObject
*funobj
, JSObject
*parent
);
2099 * Given a buffer, return JS_FALSE if the buffer might become a valid
2100 * javascript statement with the addition of more lines. Otherwise return
2101 * JS_TRUE. The intent is to support interactive compilation - accumulate
2102 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
2105 extern JS_PUBLIC_API(JSBool
)
2106 JS_BufferIsCompilableUnit(JSContext
*cx
, JSObject
*obj
,
2107 const char *bytes
, size_t length
);
2110 * The JSScript objects returned by the following functions refer to string and
2111 * other kinds of literals, including doubles and RegExp objects. These
2112 * literals are vulnerable to garbage collection; to root script objects and
2113 * prevent literals from being collected, create a rootable object using
2114 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
2116 extern JS_PUBLIC_API(JSScript
*)
2117 JS_CompileScript(JSContext
*cx
, JSObject
*obj
,
2118 const char *bytes
, size_t length
,
2119 const char *filename
, uintN lineno
);
2121 extern JS_PUBLIC_API(JSScript
*)
2122 JS_CompileScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2123 JSPrincipals
*principals
,
2124 const char *bytes
, size_t length
,
2125 const char *filename
, uintN lineno
);
2127 extern JS_PUBLIC_API(JSScript
*)
2128 JS_CompileUCScript(JSContext
*cx
, JSObject
*obj
,
2129 const jschar
*chars
, size_t length
,
2130 const char *filename
, uintN lineno
);
2132 extern JS_PUBLIC_API(JSScript
*)
2133 JS_CompileUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2134 JSPrincipals
*principals
,
2135 const jschar
*chars
, size_t length
,
2136 const char *filename
, uintN lineno
);
2138 extern JS_PUBLIC_API(JSScript
*)
2139 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2141 extern JS_PUBLIC_API(JSScript
*)
2142 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2145 extern JS_PUBLIC_API(JSScript
*)
2146 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2147 const char *filename
, FILE *fh
,
2148 JSPrincipals
*principals
);
2151 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2152 * in order to keep a JSScript and its atoms safe from garbage collection after
2153 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2154 * E.g., and without error checks:
2156 * JSScript *script = JS_CompileFile(cx, global, filename);
2157 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2158 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
2161 * JS_ExecuteScript(cx, global, script, &result);
2163 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2164 * JS_RemoveRoot(cx, &scrobj);
2166 extern JS_PUBLIC_API(JSObject
*)
2167 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2170 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2171 * called on script yet, the return value will be null.
2173 extern JS_PUBLIC_API(JSObject
*)
2174 JS_GetScriptObject(JSScript
*script
);
2176 extern JS_PUBLIC_API(void)
2177 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2179 extern JS_PUBLIC_API(JSFunction
*)
2180 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2181 uintN nargs
, const char **argnames
,
2182 const char *bytes
, size_t length
,
2183 const char *filename
, uintN lineno
);
2185 extern JS_PUBLIC_API(JSFunction
*)
2186 JS_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2187 JSPrincipals
*principals
, const char *name
,
2188 uintN nargs
, const char **argnames
,
2189 const char *bytes
, size_t length
,
2190 const char *filename
, uintN lineno
);
2192 extern JS_PUBLIC_API(JSFunction
*)
2193 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2194 uintN nargs
, const char **argnames
,
2195 const jschar
*chars
, size_t length
,
2196 const char *filename
, uintN lineno
);
2198 extern JS_PUBLIC_API(JSFunction
*)
2199 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2200 JSPrincipals
*principals
, const char *name
,
2201 uintN nargs
, const char **argnames
,
2202 const jschar
*chars
, size_t length
,
2203 const char *filename
, uintN lineno
);
2205 extern JS_PUBLIC_API(JSString
*)
2206 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2210 * API extension: OR this into indent to avoid pretty-printing the decompiled
2211 * source resulting from JS_DecompileFunction{,Body}.
2213 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2215 extern JS_PUBLIC_API(JSString
*)
2216 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2218 extern JS_PUBLIC_API(JSString
*)
2219 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2222 * NB: JS_ExecuteScript, JS_ExecuteScriptPart, and the JS_Evaluate*Script*
2223 * quadruplets all use the obj parameter as the initial scope chain header,
2224 * the 'this' keyword value, and the variables object (ECMA parlance for where
2225 * 'var' and 'function' bind names) of the execution context for script.
2227 * Using obj as the variables object is problematic if obj's parent (which is
2228 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2229 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2230 * created by assignment to an unbound id, 'x = 0', go in the last object on
2231 * the scope chain linked by parent.
2233 * ECMA calls that last scoping object the "global object", but note that many
2234 * embeddings have several such objects. ECMA requires that "global code" be
2235 * executed with the variables object equal to this global object. But these
2236 * JS API entry points provide freedom to execute code against a "sub-global",
2237 * i.e., a parented or scoped object, in which case the variables object will
2238 * differ from the last object on the scope chain, resulting in confusing and
2239 * non-ECMA explicit vs. implicit variable creation.
2241 * Caveat embedders: unless you already depend on this buggy variables object
2242 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2243 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2244 * someone may have set other options on cx already -- for each context in the
2245 * application, if you pass parented objects as the obj parameter, or may ever
2246 * pass such objects in the future.
2248 * Why a runtime option? The alternative is to add six or so new API entry
2249 * points with signatures matching the following six, and that doesn't seem
2250 * worth the code bloat cost. Such new entry points would probably have less
2251 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2252 * OTOH, can be more easily hacked into existing code that does not depend on
2253 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2254 * etc., entry points.
2256 extern JS_PUBLIC_API(JSBool
)
2257 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2260 * Execute either the function-defining prolog of a script, or the script's
2261 * main body, but not both.
2263 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2265 extern JS_PUBLIC_API(JSBool
)
2266 JS_ExecuteScriptPart(JSContext
*cx
, JSObject
*obj
, JSScript
*script
,
2267 JSExecPart part
, jsval
*rval
);
2269 extern JS_PUBLIC_API(JSBool
)
2270 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2271 const char *bytes
, uintN length
,
2272 const char *filename
, uintN lineno
,
2275 extern JS_PUBLIC_API(JSBool
)
2276 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2277 JSPrincipals
*principals
,
2278 const char *bytes
, uintN length
,
2279 const char *filename
, uintN lineno
,
2282 extern JS_PUBLIC_API(JSBool
)
2283 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2284 const jschar
*chars
, uintN length
,
2285 const char *filename
, uintN lineno
,
2288 extern JS_PUBLIC_API(JSBool
)
2289 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2290 JSPrincipals
*principals
,
2291 const jschar
*chars
, uintN length
,
2292 const char *filename
, uintN lineno
,
2295 extern JS_PUBLIC_API(JSBool
)
2296 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2297 jsval
*argv
, jsval
*rval
);
2299 extern JS_PUBLIC_API(JSBool
)
2300 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2301 jsval
*argv
, jsval
*rval
);
2303 extern JS_PUBLIC_API(JSBool
)
2304 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2305 jsval
*argv
, jsval
*rval
);
2308 * These functions allow setting an operation callback that will be called
2309 * from the thread the context is associated with some time after any thread
2310 * triggered the callback using JS_TriggerOperationCallback(cx).
2312 * In a threadsafe build the engine internally triggers operation callbacks
2313 * under certain circumstances (i.e. GC and title transfer) to force the
2314 * context to yield its current request, which the engine always
2315 * automatically does immediately prior to calling the callback function.
2316 * The embedding should thus not rely on callbacks being triggered through
2317 * the external API only.
2319 * Important note: Additional callbacks can occur inside the callback handler
2320 * if it re-enters the JS engine. The embedding must ensure that the callback
2321 * is disconnected before attempting such re-entry.
2324 extern JS_PUBLIC_API(JSOperationCallback
)
2325 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
);
2327 extern JS_PUBLIC_API(JSOperationCallback
)
2328 JS_GetOperationCallback(JSContext
*cx
);
2330 extern JS_PUBLIC_API(void)
2331 JS_TriggerOperationCallback(JSContext
*cx
);
2333 extern JS_PUBLIC_API(void)
2334 JS_TriggerAllOperationCallbacks(JSRuntime
*rt
);
2336 extern JS_PUBLIC_API(JSBool
)
2337 JS_IsRunning(JSContext
*cx
);
2339 extern JS_PUBLIC_API(JSBool
)
2340 JS_IsConstructing(JSContext
*cx
);
2343 * Returns true if a script is executing and its current bytecode is a set
2344 * (assignment) operation, even if there are native (no script) stack frames
2345 * between the script and the caller to JS_IsAssigning.
2347 extern JS_FRIEND_API(JSBool
)
2348 JS_IsAssigning(JSContext
*cx
);
2351 * Set the second return value, which should be a string or int jsval that
2352 * identifies a property in the returned object, to form an ECMA reference
2353 * type value (obj, id). Only native methods can return reference types,
2354 * and if the returned value is used on the left-hand side of an assignment
2355 * op, the identified property will be set. If the return value is in an
2356 * r-value, the interpreter just gets obj[id]'s value.
2358 extern JS_PUBLIC_API(void)
2359 JS_SetCallReturnValue2(JSContext
*cx
, jsval v
);
2362 * Saving and restoring frame chains.
2364 * These two functions are used to set aside cx's call stack while that stack
2365 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2366 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2367 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2368 * matching JS_RestoreFrameChain calls.
2370 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2371 * return does not signify an error, and JS_RestoreFrameChain handles a null
2372 * frame pointer argument safely.
2374 extern JS_PUBLIC_API(JSStackFrame
*)
2375 JS_SaveFrameChain(JSContext
*cx
);
2377 extern JS_PUBLIC_API(void)
2378 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2380 /************************************************************************/
2385 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2386 * on error (signified by null return), it leaves bytes owned by the caller.
2387 * So the caller must free bytes in the error case, if it has no use for them.
2388 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2389 * the character memory passed to them -- they copy it.
2391 extern JS_PUBLIC_API(JSString
*)
2392 JS_NewString(JSContext
*cx
, char *bytes
, size_t length
);
2394 extern JS_PUBLIC_API(JSString
*)
2395 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2397 extern JS_PUBLIC_API(JSString
*)
2398 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2400 extern JS_PUBLIC_API(JSString
*)
2401 JS_InternString(JSContext
*cx
, const char *s
);
2403 extern JS_PUBLIC_API(JSString
*)
2404 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
2406 extern JS_PUBLIC_API(JSString
*)
2407 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
2409 extern JS_PUBLIC_API(JSString
*)
2410 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
2412 extern JS_PUBLIC_API(JSString
*)
2413 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
2415 extern JS_PUBLIC_API(JSString
*)
2416 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
2418 extern JS_PUBLIC_API(char *)
2419 JS_GetStringBytes(JSString
*str
);
2421 extern JS_PUBLIC_API(jschar
*)
2422 JS_GetStringChars(JSString
*str
);
2424 extern JS_PUBLIC_API(size_t)
2425 JS_GetStringLength(JSString
*str
);
2427 extern JS_PUBLIC_API(intN
)
2428 JS_CompareStrings(JSString
*str1
, JSString
*str2
);
2431 * Mutable string support. A string's characters are never mutable in this JS
2432 * implementation, but a growable string has a buffer that can be reallocated,
2433 * and a dependent string is a substring of another (growable, dependent, or
2434 * immutable) string. The direct data members of the (opaque to API clients)
2435 * JSString struct may be changed in a single-threaded way for growable and
2436 * dependent strings.
2438 * Therefore mutable strings cannot be used by more than one thread at a time.
2439 * You may call JS_MakeStringImmutable to convert the string from a mutable
2440 * (growable or dependent) string to an immutable (and therefore thread-safe)
2441 * string. The engine takes care of converting growable and dependent strings
2442 * to immutable for you if you store strings in multi-threaded objects using
2443 * JS_SetProperty or kindred API entry points.
2445 * If you store a JSString pointer in a native data structure that is (safely)
2446 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2447 * retiring the store.
2449 extern JS_PUBLIC_API(JSString
*)
2450 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
2453 * Create a dependent string, i.e., a string that owns no character storage,
2454 * but that refers to a slice of another string's chars. Dependent strings
2455 * are mutable by definition, so the thread safety comments above apply.
2457 extern JS_PUBLIC_API(JSString
*)
2458 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
2462 * Concatenate two strings, resulting in a new growable string. If you create
2463 * the left string and pass it to JS_ConcatStrings on a single thread, try to
2464 * use JS_NewGrowableString to create the left string -- doing so helps Concat
2465 * avoid allocating a new buffer for the result and copying left's chars into
2466 * the new buffer. See above for thread safety comments.
2468 extern JS_PUBLIC_API(JSString
*)
2469 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
2472 * Convert a dependent string into an independent one. This function does not
2473 * change the string's mutability, so the thread safety comments above apply.
2475 extern JS_PUBLIC_API(const jschar
*)
2476 JS_UndependString(JSContext
*cx
, JSString
*str
);
2479 * Convert a mutable string (either growable or dependent) into an immutable,
2482 extern JS_PUBLIC_API(JSBool
)
2483 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
2486 * Return JS_TRUE if C (char []) strings passed via the API and internally
2489 JS_PUBLIC_API(JSBool
)
2490 JS_CStringsAreUTF8(void);
2493 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2494 * can never be changed. This API must be called before the first call to
2498 JS_SetCStringsAreUTF8(void);
2501 * Character encoding support.
2503 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2504 * of the destination buffer before the call; on return, *dstlenp contains the
2505 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2506 * stored. To determine the necessary destination buffer size, make a sizing
2507 * call that passes NULL for dst.
2509 * On errors, the functions report the error. In that case, *dstlenp contains
2510 * the number of characters or bytes transferred so far. If cx is NULL, no
2511 * error is reported on failure, and the functions simply return JS_FALSE.
2513 * NB: Neither function stores an additional zero byte or jschar after the
2514 * transcoded string.
2516 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2517 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2518 * errors if the character sequence is malformed. If UTF-8 support is
2519 * disabled, the functions deflate and inflate, respectively.
2521 JS_PUBLIC_API(JSBool
)
2522 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
2525 JS_PUBLIC_API(JSBool
)
2526 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
2530 * A variation on JS_EncodeCharacters where a null terminated string is
2531 * returned that you are expected to call JS_free on when done.
2533 JS_PUBLIC_API(char *)
2534 JS_EncodeString(JSContext
*cx
, JSString
*str
);
2536 /************************************************************************/
2540 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
2543 * JSON.stringify as specified by ES3.1 (draft)
2545 JS_PUBLIC_API(JSBool
)
2546 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
, jsval space
,
2547 JSONWriteCallback callback
, void *data
);
2550 * Retrieve a toJSON function. If found, set vp to its result.
2552 JS_PUBLIC_API(JSBool
)
2553 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
2556 * JSON.parse as specified by ES3.1 (draft)
2558 JS_PUBLIC_API(JSONParser
*)
2559 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
2561 JS_PUBLIC_API(JSBool
)
2562 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
2564 JS_PUBLIC_API(JSBool
)
2565 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
, jsval reviver
);
2567 /************************************************************************/
2570 * Locale specific string conversion and error message callbacks.
2572 struct JSLocaleCallbacks
{
2573 JSLocaleToUpperCase localeToUpperCase
;
2574 JSLocaleToLowerCase localeToLowerCase
;
2575 JSLocaleCompare localeCompare
;
2576 JSLocaleToUnicode localeToUnicode
;
2577 JSErrorCallback localeGetErrorMessage
;
2581 * Establish locale callbacks. The pointer must persist as long as the
2582 * JSContext. Passing NULL restores the default behaviour.
2584 extern JS_PUBLIC_API(void)
2585 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
2588 * Return the address of the current locale callbacks struct, which may
2591 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
2592 JS_GetLocaleCallbacks(JSContext
*cx
);
2594 /************************************************************************/
2601 * Report an exception represented by the sprintf-like conversion of format
2602 * and its arguments. This exception message string is passed to a pre-set
2603 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2604 * the JSErrorReporter typedef).
2606 extern JS_PUBLIC_API(void)
2607 JS_ReportError(JSContext
*cx
, const char *format
, ...);
2610 * Use an errorNumber to retrieve the format string, args are char *
2612 extern JS_PUBLIC_API(void)
2613 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
2614 void *userRef
, const uintN errorNumber
, ...);
2617 * Use an errorNumber to retrieve the format string, args are jschar *
2619 extern JS_PUBLIC_API(void)
2620 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
2621 void *userRef
, const uintN errorNumber
, ...);
2624 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2625 * Return true if there was no error trying to issue the warning, and if the
2626 * warning was not converted into an error due to the JSOPTION_WERROR option
2627 * being set, false otherwise.
2629 extern JS_PUBLIC_API(JSBool
)
2630 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
2632 extern JS_PUBLIC_API(JSBool
)
2633 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
2634 JSErrorCallback errorCallback
, void *userRef
,
2635 const uintN errorNumber
, ...);
2637 extern JS_PUBLIC_API(JSBool
)
2638 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
2639 JSErrorCallback errorCallback
, void *userRef
,
2640 const uintN errorNumber
, ...);
2643 * Complain when out of memory.
2645 extern JS_PUBLIC_API(void)
2646 JS_ReportOutOfMemory(JSContext
*cx
);
2649 * Complain when an allocation size overflows the maximum supported limit.
2651 extern JS_PUBLIC_API(void)
2652 JS_ReportAllocationOverflow(JSContext
*cx
);
2654 struct JSErrorReport
{
2655 const char *filename
; /* source file name, URL, etc., or null */
2656 uintN lineno
; /* source line number */
2657 const char *linebuf
; /* offending source line without final \n */
2658 const char *tokenptr
; /* pointer to error token in linebuf */
2659 const jschar
*uclinebuf
; /* unicode (original) line buffer */
2660 const jschar
*uctokenptr
; /* unicode (original) token pointer */
2661 uintN flags
; /* error/warning, etc. */
2662 uintN errorNumber
; /* the error number, e.g. see js.msg */
2663 const jschar
*ucmessage
; /* the (default) error message */
2664 const jschar
**messageArgs
; /* arguments for the error message */
2668 * JSErrorReport flag values. These may be freely composed.
2670 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2671 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2672 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2673 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2676 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2677 * has been thrown for this runtime error, and the host should ignore it.
2678 * Exception-aware hosts should also check for JS_IsExceptionPending if
2679 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2682 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2683 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2684 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2686 extern JS_PUBLIC_API(JSErrorReporter
)
2687 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
2689 /************************************************************************/
2692 * Regular Expressions.
2694 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
2695 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
2696 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
2697 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
2698 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
2699 #define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
2701 extern JS_PUBLIC_API(JSObject
*)
2702 JS_NewRegExpObject(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
2704 extern JS_PUBLIC_API(JSObject
*)
2705 JS_NewUCRegExpObject(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
2707 extern JS_PUBLIC_API(void)
2708 JS_SetRegExpInput(JSContext
*cx
, JSString
*input
, JSBool multiline
);
2710 extern JS_PUBLIC_API(void)
2711 JS_ClearRegExpStatics(JSContext
*cx
);
2713 extern JS_PUBLIC_API(void)
2714 JS_ClearRegExpRoots(JSContext
*cx
);
2716 /* TODO: compile, exec, get/set other statics... */
2718 /************************************************************************/
2720 extern JS_PUBLIC_API(JSBool
)
2721 JS_IsExceptionPending(JSContext
*cx
);
2723 extern JS_PUBLIC_API(JSBool
)
2724 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
2726 extern JS_PUBLIC_API(void)
2727 JS_SetPendingException(JSContext
*cx
, jsval v
);
2729 extern JS_PUBLIC_API(void)
2730 JS_ClearPendingException(JSContext
*cx
);
2732 extern JS_PUBLIC_API(JSBool
)
2733 JS_ReportPendingException(JSContext
*cx
);
2736 * Save the current exception state. This takes a snapshot of cx's current
2737 * exception state without making any change to that state.
2739 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
2740 * (to restore that saved state, overriding any more recent state) or else to
2741 * JS_DropExceptionState (to free the state struct in case it is not correct
2742 * or desirable to restore it). Both Restore and Drop free the state struct,
2743 * so callers must stop using the pointer returned from Save after calling the
2744 * Release or Drop API.
2746 extern JS_PUBLIC_API(JSExceptionState
*)
2747 JS_SaveExceptionState(JSContext
*cx
);
2749 extern JS_PUBLIC_API(void)
2750 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2752 extern JS_PUBLIC_API(void)
2753 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2756 * If the given value is an exception object that originated from an error,
2757 * the exception will contain an error report struct, and this API will return
2758 * the address of that struct. Otherwise, it returns NULL. The lifetime of
2759 * the error report struct that might be returned is the same as the lifetime
2760 * of the exception object.
2762 extern JS_PUBLIC_API(JSErrorReport
*)
2763 JS_ErrorFromException(JSContext
*cx
, jsval v
);
2766 * Given a reported error's message and JSErrorReport struct pointer, throw
2767 * the corresponding exception on cx.
2769 extern JS_PUBLIC_API(JSBool
)
2770 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
2771 JSErrorReport
*reportp
);
2774 * Throws a StopIteration exception on cx.
2776 extern JS_PUBLIC_API(JSBool
)
2777 JS_ThrowStopIteration(JSContext
*cx
);
2780 * Associate the current thread with the given context. This is done
2781 * implicitly by JS_NewContext.
2783 * Returns the old thread id for this context, which should be treated as
2784 * an opaque value. This value is provided for comparison to 0, which
2785 * indicates that ClearContextThread has been called on this context
2786 * since the last SetContextThread, or non-0, which indicates the opposite.
2788 extern JS_PUBLIC_API(jsword
)
2789 JS_GetContextThread(JSContext
*cx
);
2791 extern JS_PUBLIC_API(jsword
)
2792 JS_SetContextThread(JSContext
*cx
);
2794 extern JS_PUBLIC_API(jsword
)
2795 JS_ClearContextThread(JSContext
*cx
);
2797 /************************************************************************/
2800 #define JS_GC_ZEAL 1
2804 extern JS_PUBLIC_API(void)
2805 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
2810 #endif /* jsapi_h___ */