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_SPECIAL
= 0x6 /* tagged boolean or private value */
65 /* Type tag bitfield length and derived macros. */
66 #define JSVAL_TAGBITS 3
67 #define JSVAL_TAGMASK ((jsval) JS_BITMASK(JSVAL_TAGBITS))
68 #define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
70 /* Not a function, because we have static asserts that use it */
71 #define JSVAL_TAG(v) ((jsvaltag)((v) & JSVAL_TAGMASK))
73 /* Not a function, because we have static asserts that use it */
74 #define JSVAL_SETTAG(v, t) ((v) | (t))
76 static JS_ALWAYS_INLINE jsval
79 return v
& ~(jsval
)JSVAL_TAGMASK
;
83 * Well-known JS values. The extern'd variables are initialized when the
84 * first JSContext is created by JS_NewContext (see below).
86 #define JSVAL_NULL ((jsval) 0)
87 #define JSVAL_ZERO INT_TO_JSVAL(0)
88 #define JSVAL_ONE INT_TO_JSVAL(1)
89 #define JSVAL_FALSE SPECIAL_TO_JSVAL(JS_FALSE)
90 #define JSVAL_TRUE SPECIAL_TO_JSVAL(JS_TRUE)
91 #define JSVAL_VOID SPECIAL_TO_JSVAL(2)
94 * A "special" value is a 29-bit (for 32-bit jsval) or 61-bit (for 64-bit jsval)
95 * value whose tag is JSVAL_SPECIAL. These values include the booleans 0 and 1.
97 * JSVAL_VOID is a non-boolean special value, but embedders MUST NOT rely on
98 * this. All other possible special values are implementation-reserved
99 * and MUST NOT be constructed by any embedding of SpiderMonkey.
101 #define JSVAL_TO_SPECIAL(v) ((JSBool) ((v) >> JSVAL_TAGBITS))
102 #define SPECIAL_TO_JSVAL(b) \
103 JSVAL_SETTAG((jsval) (b) << JSVAL_TAGBITS, JSVAL_SPECIAL)
105 /* Predicates for type testing. */
106 static JS_ALWAYS_INLINE JSBool
107 JSVAL_IS_OBJECT(jsval v
)
109 return JSVAL_TAG(v
) == JSVAL_OBJECT
;
112 static JS_ALWAYS_INLINE JSBool
113 JSVAL_IS_INT(jsval v
)
115 return (JSBool
)(v
& JSVAL_INT
);
118 static JS_ALWAYS_INLINE JSBool
119 JSVAL_IS_DOUBLE(jsval v
)
121 return JSVAL_TAG(v
) == JSVAL_DOUBLE
;
124 static JS_ALWAYS_INLINE JSBool
125 JSVAL_IS_NUMBER(jsval v
)
127 return JSVAL_IS_INT(v
) || JSVAL_IS_DOUBLE(v
);
130 static JS_ALWAYS_INLINE JSBool
131 JSVAL_IS_STRING(jsval v
)
133 return JSVAL_TAG(v
) == JSVAL_STRING
;
136 static JS_ALWAYS_INLINE JSBool
137 JSVAL_IS_SPECIAL(jsval v
)
139 return JSVAL_TAG(v
) == JSVAL_SPECIAL
;
142 static JS_ALWAYS_INLINE JSBool
143 JSVAL_IS_BOOLEAN(jsval v
)
145 return (v
& ~((jsval
)1 << JSVAL_TAGBITS
)) == JSVAL_SPECIAL
;
148 static JS_ALWAYS_INLINE JSBool
149 JSVAL_IS_NULL(jsval v
)
151 return v
== JSVAL_NULL
;
154 static JS_ALWAYS_INLINE JSBool
155 JSVAL_IS_VOID(jsval v
)
157 return v
== JSVAL_VOID
;
160 static JS_ALWAYS_INLINE JSBool
161 JSVAL_IS_PRIMITIVE(jsval v
)
163 return !JSVAL_IS_OBJECT(v
) || JSVAL_IS_NULL(v
);
166 /* Objects, strings, and doubles are GC'ed. */
167 static JS_ALWAYS_INLINE JSBool
168 JSVAL_IS_GCTHING(jsval v
)
170 return !(v
& JSVAL_INT
) && JSVAL_TAG(v
) != JSVAL_SPECIAL
;
173 static JS_ALWAYS_INLINE
void *
174 JSVAL_TO_GCTHING(jsval v
)
176 JS_ASSERT(JSVAL_IS_GCTHING(v
));
177 return (void *) JSVAL_CLRTAG(v
);
180 static JS_ALWAYS_INLINE JSObject
*
181 JSVAL_TO_OBJECT(jsval v
)
183 JS_ASSERT(JSVAL_IS_OBJECT(v
));
184 return (JSObject
*) JSVAL_TO_GCTHING(v
);
187 static JS_ALWAYS_INLINE jsdouble
*
188 JSVAL_TO_DOUBLE(jsval v
)
190 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
191 return (jsdouble
*) JSVAL_TO_GCTHING(v
);
194 static JS_ALWAYS_INLINE JSString
*
195 JSVAL_TO_STRING(jsval v
)
197 JS_ASSERT(JSVAL_IS_STRING(v
));
198 return (JSString
*) JSVAL_TO_GCTHING(v
);
201 static JS_ALWAYS_INLINE jsval
202 OBJECT_TO_JSVAL(JSObject
*obj
)
204 JS_ASSERT(((jsval
) obj
& JSVAL_TAGMASK
) == JSVAL_OBJECT
);
208 static JS_ALWAYS_INLINE jsval
209 DOUBLE_TO_JSVAL(jsdouble
*dp
)
211 JS_ASSERT(((jsword
) dp
& JSVAL_TAGMASK
) == 0);
212 return JSVAL_SETTAG((jsval
) dp
, JSVAL_DOUBLE
);
215 static JS_ALWAYS_INLINE jsval
216 STRING_TO_JSVAL(JSString
*str
)
218 return JSVAL_SETTAG((jsval
) str
, JSVAL_STRING
);
221 /* Lock and unlock the GC thing held by a jsval. */
222 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
223 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
225 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
226 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
229 /* Domain limits for the jsval int type. */
230 #define JSVAL_INT_BITS 31
231 #define JSVAL_INT_POW2(n) ((jsval)1 << (n))
232 #define JSVAL_INT_MIN (-JSVAL_INT_POW2(30))
233 #define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
235 /* Not a function, because we have static asserts that use it */
236 #define INT_FITS_IN_JSVAL(i) ((jsuint)(i) - (jsuint)JSVAL_INT_MIN <= \
237 (jsuint)(JSVAL_INT_MAX - JSVAL_INT_MIN))
239 static JS_ALWAYS_INLINE jsint
240 JSVAL_TO_INT(jsval v
)
242 JS_ASSERT(JSVAL_IS_INT(v
));
243 return (jsint
) v
>> 1;
246 /* Not a function, because we have static asserts that use it */
247 #define INT_TO_JSVAL_CONSTEXPR(i) (((jsval)(i) << 1) | JSVAL_INT)
249 static JS_ALWAYS_INLINE jsval
250 INT_TO_JSVAL(jsint i
)
252 JS_ASSERT(INT_FITS_IN_JSVAL(i
));
253 return INT_TO_JSVAL_CONSTEXPR(i
);
256 /* Convert between boolean and jsval, asserting that inputs are valid. */
257 static JS_ALWAYS_INLINE JSBool
258 JSVAL_TO_BOOLEAN(jsval v
)
260 JS_ASSERT(v
== JSVAL_TRUE
|| v
== JSVAL_FALSE
);
261 return JSVAL_TO_SPECIAL(v
);
264 static JS_ALWAYS_INLINE jsval
265 BOOLEAN_TO_JSVAL(JSBool b
)
267 JS_ASSERT(b
== JS_TRUE
|| b
== JS_FALSE
);
268 return SPECIAL_TO_JSVAL(b
);
271 /* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
272 #define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
273 #define PRIVATE_TO_JSVAL(p) ((jsval)(ptrdiff_t)(p) | JSVAL_INT)
275 /* Property attributes, set in JSPropertySpec and passed to API functions. */
276 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
277 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
278 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
279 #define JSPROP_GETTER 0x10 /* property holds getter function */
280 #define JSPROP_SETTER 0x20 /* property holds setter function */
281 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
282 property; don't copy the property on
283 set of the same-named property in an
284 object that delegates to a prototype
285 containing this property */
286 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
287 #define JSPROP_SHORTID 0x100 /* set in JSPropertyDescriptor.attrs
288 if getters/setters use a shortid */
290 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
291 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
292 #define JSFUN_GETTER JSPROP_GETTER
293 #define JSFUN_SETTER JSPROP_SETTER
294 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
295 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
297 #define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
298 #define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
300 #define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
301 #define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
302 #define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
303 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
305 #define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
307 #define JSFUN_THISP_FLAGS(f) (f)
308 #define JSFUN_THISP_TEST(f,t) ((f) & t)
310 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
311 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
312 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
313 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
315 #define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
317 #define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
318 bits 12-15 are used internally to
319 flag interpreted functions */
321 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
322 instead of defaulting to class gsops
323 for property holding function */
326 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
327 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
328 * methods of a class prototype that are exposed as static methods taking an
329 * extra leading argument: the generic |this| parameter.
331 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
332 * that struct must live at least as long as the native static method object
333 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
334 * JSFunctionSpec structs are allocated in static arrays.
336 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
339 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
340 * comment in jstypes.h regarding safe int64 usage.
342 extern JS_PUBLIC_API(int64
)
345 /* Don't want to export data, so provide accessors for non-inline jsvals. */
346 extern JS_PUBLIC_API(jsval
)
347 JS_GetNaNValue(JSContext
*cx
);
349 extern JS_PUBLIC_API(jsval
)
350 JS_GetNegativeInfinityValue(JSContext
*cx
);
352 extern JS_PUBLIC_API(jsval
)
353 JS_GetPositiveInfinityValue(JSContext
*cx
);
355 extern JS_PUBLIC_API(jsval
)
356 JS_GetEmptyStringValue(JSContext
*cx
);
359 * Format is a string of the following characters (spaces are insignificant),
360 * specifying the tabulated type conversions:
363 * c uint16/jschar ECMA uint16, Unicode char
365 * u uint32 ECMA uint32
366 * j int32 Rounded int32 (coordinate)
367 * d jsdouble IEEE double
368 * I jsdouble Integral IEEE double
370 * S JSString * Unicode string, accessed by a JSString pointer
371 * W jschar * Unicode character vector, 0-terminated (W for wide)
372 * o JSObject * Object reference
373 * f JSFunction * Function private
374 * v jsval Argument value (no conversion)
375 * * N/A Skip this argument (no vararg)
376 * / N/A End of required arguments
378 * The variable argument list after format must consist of &b, &c, &s, e.g.,
379 * where those variables have the types given above. For the pointer types
380 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
381 * to the JS runtime, not to the calling native code. The runtime promises
382 * to keep this memory valid so long as argv refers to allocated stack space
383 * (so long as the native function is active).
385 * Fewer arguments than format specifies may be passed only if there is a /
386 * in format after the last required argument specifier and argc is at least
387 * the number of required arguments. More arguments than format specifies
388 * may be passed without error; it is up to the caller to deal with trailing
389 * unconverted arguments.
391 extern JS_PUBLIC_API(JSBool
)
392 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
396 extern JS_PUBLIC_API(JSBool
)
397 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
398 const char *format
, va_list ap
);
401 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
404 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
405 * The handler function has this signature (see jspubtd.h):
407 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
408 * JSBool fromJS, jsval **vpp, va_list *app);
410 * It should return true on success, and return false after reporting an error
411 * or detecting an already-reported error.
413 * For a given format string, for example "AA", the formatter is called from
414 * JS_ConvertArgumentsVA like so:
416 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
418 * sp points into the arguments array on the JS stack, while ap points into
419 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
420 * the formatter to convert zero or more jsvals at sp to zero or more C values
421 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
422 * (via *app) to point past the converted arguments and their result pointers
425 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
427 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
429 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
430 * format specifier and store them at sp, updating ap and sp appropriately.
432 * The "..." after "AA" is the rest of the format string that was passed into
433 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
434 * in each Convert or PushArguments call is passed to the formatter, so that
435 * one such function may implement several formats, in order to share code.
437 * Remove just forgets about any handler associated with format. Add does not
438 * copy format, it points at the string storage allocated by the caller, which
439 * is typically a string constant. If format is in dynamic storage, it is up
440 * to the caller to keep the string alive until Remove is called.
442 extern JS_PUBLIC_API(JSBool
)
443 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
444 JSArgumentFormatter formatter
);
446 extern JS_PUBLIC_API(void)
447 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
449 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
451 extern JS_PUBLIC_API(JSBool
)
452 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
454 extern JS_PUBLIC_API(JSBool
)
455 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
457 extern JS_PUBLIC_API(JSFunction
*)
458 JS_ValueToFunction(JSContext
*cx
, jsval v
);
460 extern JS_PUBLIC_API(JSFunction
*)
461 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
463 extern JS_PUBLIC_API(JSString
*)
464 JS_ValueToString(JSContext
*cx
, jsval v
);
466 extern JS_PUBLIC_API(JSString
*)
467 JS_ValueToSource(JSContext
*cx
, jsval v
);
469 extern JS_PUBLIC_API(JSBool
)
470 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
472 extern JS_PUBLIC_API(JSBool
)
473 JS_DoubleIsInt32(jsdouble d
, jsint
*ip
);
476 * Convert a value to a number, then to an int32, according to the ECMA rules
479 extern JS_PUBLIC_API(JSBool
)
480 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
483 * Convert a value to a number, then to a uint32, according to the ECMA rules
486 extern JS_PUBLIC_API(JSBool
)
487 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
490 * Convert a value to a number, then to an int32 if it fits by rounding to
491 * nearest; but failing with an error report if the double is out of range
494 extern JS_PUBLIC_API(JSBool
)
495 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
498 * ECMA ToUint16, for mapping a jsval to a Unicode point.
500 extern JS_PUBLIC_API(JSBool
)
501 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
503 extern JS_PUBLIC_API(JSBool
)
504 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
506 extern JS_PUBLIC_API(JSType
)
507 JS_TypeOfValue(JSContext
*cx
, jsval v
);
509 extern JS_PUBLIC_API(const char *)
510 JS_GetTypeName(JSContext
*cx
, JSType type
);
512 extern JS_PUBLIC_API(JSBool
)
513 JS_StrictlyEqual(JSContext
*cx
, jsval v1
, jsval v2
);
515 extern JS_PUBLIC_API(JSBool
)
516 JS_SameValue(JSContext
*cx
, jsval v1
, jsval v2
);
518 /************************************************************************/
521 * Initialization, locking, contexts, and memory allocation.
523 * It is important that the first runtime and first context be created in a
524 * single-threaded fashion, otherwise the behavior of the library is undefined.
525 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
527 #define JS_NewRuntime JS_Init
528 #define JS_DestroyRuntime JS_Finish
529 #define JS_LockRuntime JS_Lock
530 #define JS_UnlockRuntime JS_Unlock
532 extern JS_PUBLIC_API(JSRuntime
*)
533 JS_NewRuntime(uint32 maxbytes
);
535 extern JS_PUBLIC_API(void)
536 JS_CommenceRuntimeShutDown(JSRuntime
*rt
);
538 extern JS_PUBLIC_API(void)
539 JS_DestroyRuntime(JSRuntime
*rt
);
541 extern JS_PUBLIC_API(void)
544 JS_PUBLIC_API(void *)
545 JS_GetRuntimePrivate(JSRuntime
*rt
);
548 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
550 extern JS_PUBLIC_API(void)
551 JS_BeginRequest(JSContext
*cx
);
553 extern JS_PUBLIC_API(void)
554 JS_EndRequest(JSContext
*cx
);
556 /* Yield to pending GC operations, regardless of request depth */
557 extern JS_PUBLIC_API(void)
558 JS_YieldRequest(JSContext
*cx
);
560 extern JS_PUBLIC_API(jsrefcount
)
561 JS_SuspendRequest(JSContext
*cx
);
563 extern JS_PUBLIC_API(void)
564 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
566 extern JS_PUBLIC_API(void)
567 JS_TransferRequest(JSContext
*cx
, JSContext
*another
);
572 class JSAutoRequest
{
574 JSAutoRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
575 : mContext(cx
), mSaveDepth(0) {
576 JS_GUARD_OBJECT_NOTIFIER_INIT
;
577 JS_BeginRequest(mContext
);
580 JS_EndRequest(mContext
);
584 mSaveDepth
= JS_SuspendRequest(mContext
);
587 JS_ResumeRequest(mContext
, mSaveDepth
);
592 jsrefcount mSaveDepth
;
593 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
597 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
598 static void operator delete(void *, size_t) { };
602 class JSAutoSuspendRequest
{
604 JSAutoSuspendRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
605 : mContext(cx
), mSaveDepth(0) {
606 JS_GUARD_OBJECT_NOTIFIER_INIT
;
608 mSaveDepth
= JS_SuspendRequest(mContext
);
611 ~JSAutoSuspendRequest() {
617 JS_ResumeRequest(mContext
, mSaveDepth
);
624 jsrefcount mSaveDepth
;
625 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
629 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
630 static void operator delete(void *, size_t) { };
634 class JSAutoTransferRequest
637 JSAutoTransferRequest(JSContext
* cx1
, JSContext
* cx2
)
638 : cx1(cx1
), cx2(cx2
) {
640 JS_TransferRequest(cx1
, cx2
);
642 ~JSAutoTransferRequest() {
644 JS_TransferRequest(cx2
, cx1
);
647 JSContext
* const cx1
;
648 JSContext
* const cx2
;
651 JSAutoTransferRequest(JSAutoTransferRequest
&);
652 void operator =(JSAutoTransferRequest
&);
658 extern JS_PUBLIC_API(void)
659 JS_Lock(JSRuntime
*rt
);
661 extern JS_PUBLIC_API(void)
662 JS_Unlock(JSRuntime
*rt
);
664 extern JS_PUBLIC_API(JSContextCallback
)
665 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
667 extern JS_PUBLIC_API(JSContext
*)
668 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
670 extern JS_PUBLIC_API(void)
671 JS_DestroyContext(JSContext
*cx
);
673 extern JS_PUBLIC_API(void)
674 JS_DestroyContextNoGC(JSContext
*cx
);
676 extern JS_PUBLIC_API(void)
677 JS_DestroyContextMaybeGC(JSContext
*cx
);
679 extern JS_PUBLIC_API(void *)
680 JS_GetContextPrivate(JSContext
*cx
);
682 extern JS_PUBLIC_API(void)
683 JS_SetContextPrivate(JSContext
*cx
, void *data
);
685 extern JS_PUBLIC_API(JSRuntime
*)
686 JS_GetRuntime(JSContext
*cx
);
688 extern JS_PUBLIC_API(JSContext
*)
689 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
691 extern JS_PUBLIC_API(JSVersion
)
692 JS_GetVersion(JSContext
*cx
);
694 extern JS_PUBLIC_API(JSVersion
)
695 JS_SetVersion(JSContext
*cx
, JSVersion version
);
697 extern JS_PUBLIC_API(const char *)
698 JS_VersionToString(JSVersion version
);
700 extern JS_PUBLIC_API(JSVersion
)
701 JS_StringToVersion(const char *string
);
704 * JS options are orthogonal to version, and may be freely composed with one
705 * another as well as with version.
707 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
708 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
710 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
711 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
712 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
713 the last object on its 'obj'
714 param's scope chain as the
715 ECMA 'variables object' */
716 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
717 JS_BIT(3) /* context private data points
718 to an nsISupports subclass */
719 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
720 promises to execute compiled
721 script once only; enables
722 compile-time scope chain
723 resolution of consts. */
724 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
725 option supported for the
726 XUL preprocessor and kindred
728 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
729 parse <!-- --> as a token,
730 not backward compatible with
731 the comment-hiding hack used
732 in HTML script tags. */
733 #define JSOPTION_DONT_REPORT_UNCAUGHT \
734 JS_BIT(8) /* When returning from the
735 outermost API call, prevent
736 uncaught exceptions from
737 being converted to error
740 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
741 regular expression which
742 backtracks more than n^3
743 times, where n is length
744 of the input string */
745 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
746 statement context per
747 ECMA-262 Edition 3. */
749 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
751 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
752 that a null rval out-param
753 will be passed to each call
754 to JS_ExecuteScript. */
755 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
756 contexts' global objects
757 (see JS_GetGlobalObject),
758 leaving that up to the
761 extern JS_PUBLIC_API(uint32
)
762 JS_GetOptions(JSContext
*cx
);
764 extern JS_PUBLIC_API(uint32
)
765 JS_SetOptions(JSContext
*cx
, uint32 options
);
767 extern JS_PUBLIC_API(uint32
)
768 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
770 extern JS_PUBLIC_API(const char *)
771 JS_GetImplementationVersion(void);
773 extern JS_PUBLIC_API(JSObject
*)
774 JS_GetGlobalObject(JSContext
*cx
);
776 extern JS_PUBLIC_API(void)
777 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
780 * Initialize standard JS class constructors, prototypes, and any top-level
781 * functions and constants associated with the standard classes (e.g. isNaN
784 * NB: This sets cx's global object to obj if it was null.
786 extern JS_PUBLIC_API(JSBool
)
787 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
790 * Resolve id, which must contain either a string or an int, to a standard
791 * class name in obj if possible, defining the class's constructor and/or
792 * prototype and storing true in *resolved. If id does not name a standard
793 * class or a top-level property induced by initializing a standard class,
794 * store false in *resolved and just return true. Return false on error,
795 * as usual for JSBool result-typed API entry points.
797 * This API can be called directly from a global object class's resolve op,
798 * to define standard classes lazily. The class's enumerate op should call
799 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
800 * loops any classes not yet resolved lazily.
802 extern JS_PUBLIC_API(JSBool
)
803 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsval id
,
806 extern JS_PUBLIC_API(JSBool
)
807 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
810 * Enumerate any already-resolved standard class ids into ida, or into a new
811 * JSIdArray if ida is null. Return the augmented array on success, null on
812 * failure with ida (if it was non-null on entry) destroyed.
814 extern JS_PUBLIC_API(JSIdArray
*)
815 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
818 extern JS_PUBLIC_API(JSBool
)
819 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
822 extern JS_PUBLIC_API(JSObject
*)
823 JS_GetScopeChain(JSContext
*cx
);
825 extern JS_PUBLIC_API(JSObject
*)
826 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
828 extern JS_PUBLIC_API(JSObject
*)
829 JS_GetGlobalForScopeChain(JSContext
*cx
);
833 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
834 * object will be sealed.
836 extern JS_PUBLIC_API(JSBool
)
837 JS_InitCTypesClass(JSContext
*cx
, JSObject
*global
);
841 * Macros to hide interpreter stack layout details from a JSFastNative using
842 * its jsval *vp parameter. The stack layout underlying invocation can't change
843 * without breaking source and binary compatibility (argv[-2] is well-known to
844 * be the callee jsval, and argv[-1] is as well known to be |this|).
846 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
847 * is the global object, so embeddings implementing fast natives *must* call
848 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
849 * which should propagate as a false return from native functions and hooks.
851 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
852 * handle a null obj parameter by returning false (throwing a TypeError if
853 * given non-null argv), so most native functions that type-check their |this|
854 * parameter need not add null checking.
856 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
857 * methods that may inspect their callee must defer setting their return value
858 * until after any such possible inspection. Otherwise the return value will be
859 * inspected instead of the callee function object.
861 * WARNING: These are not (yet) mandatory macros, but new code outside of the
862 * engine should use them. In the Mozilla 2.0 milestone their definitions may
863 * change incompatibly.
865 #define JS_CALLEE(cx,vp) ((vp)[0])
866 #define JS_ARGV_CALLEE(argv) ((argv)[-2])
867 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
868 #define JS_THIS_OBJECT(cx,vp) ((JSObject *) JS_THIS(cx,vp))
869 #define JS_ARGV(cx,vp) ((vp) + 2)
870 #define JS_RVAL(cx,vp) (*(vp))
871 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
873 extern JS_PUBLIC_API(jsval
)
874 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
876 extern JS_PUBLIC_API(void *)
877 JS_malloc(JSContext
*cx
, size_t nbytes
);
879 extern JS_PUBLIC_API(void *)
880 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
882 extern JS_PUBLIC_API(void)
883 JS_free(JSContext
*cx
, void *p
);
885 extern JS_PUBLIC_API(void)
886 JS_updateMallocCounter(JSContext
*cx
, size_t nbytes
);
888 extern JS_PUBLIC_API(char *)
889 JS_strdup(JSContext
*cx
, const char *s
);
891 extern JS_PUBLIC_API(jsdouble
*)
892 JS_NewDouble(JSContext
*cx
, jsdouble d
);
894 extern JS_PUBLIC_API(JSBool
)
895 JS_NewDoubleValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
897 extern JS_PUBLIC_API(JSBool
)
898 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
901 * A JS GC root is a pointer to a JSObject *, JSString *, or jsdouble * that
902 * itself points into the GC heap (more recently, we support this extension:
903 * a root may be a pointer to a jsval v for which JSVAL_IS_GCTHING(v) is true).
905 * Therefore, you never pass JSObject *obj to JS_AddRoot(cx, obj). You always
906 * call JS_AddRoot(cx, &obj), passing obj by reference. And later, before obj
907 * or the structure it is embedded within goes out of scope or is freed, you
908 * must call JS_RemoveRoot(cx, &obj).
910 * Also, use JS_AddNamedRoot(cx, &structPtr->memberObj, "structPtr->memberObj")
911 * in preference to JS_AddRoot(cx, &structPtr->memberObj), in order to identify
912 * roots by their source callsites. This way, you can find the callsite while
913 * debugging if you should fail to do JS_RemoveRoot(cx, &structPtr->memberObj)
914 * before freeing structPtr's memory.
916 extern JS_PUBLIC_API(JSBool
)
917 JS_AddRoot(JSContext
*cx
, void *rp
);
919 #ifdef NAME_ALL_GC_ROOTS
920 #define JS_DEFINE_TO_TOKEN(def) #def
921 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
922 #define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
925 extern JS_PUBLIC_API(JSBool
)
926 JS_AddNamedRoot(JSContext
*cx
, void *rp
, const char *name
);
928 extern JS_PUBLIC_API(JSBool
)
929 JS_AddNamedRootRT(JSRuntime
*rt
, void *rp
, const char *name
);
931 extern JS_PUBLIC_API(JSBool
)
932 JS_RemoveRoot(JSContext
*cx
, void *rp
);
934 extern JS_PUBLIC_API(JSBool
)
935 JS_RemoveRootRT(JSRuntime
*rt
, void *rp
);
938 * The last GC thing of each type (object, string, double, external string
939 * types) created on a given context is kept alive until another thing of the
940 * same type is created, using a newborn root in the context. These newborn
941 * roots help native code protect newly-created GC-things from GC invocations
942 * activated before those things can be rooted using local or global roots.
944 * However, the newborn roots can also entrain great gobs of garbage, so the
945 * JS_GC entry point clears them for the context on which GC is being forced.
946 * Embeddings may need to do likewise for all contexts.
948 * See the scoped local root API immediately below for a better way to manage
949 * newborns in cases where native hooks (functions, getters, setters, etc.)
950 * create many GC-things, potentially without connecting them to predefined
951 * local roots such as *rval or argv[i] in an active native function. Using
952 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
953 * newborn roots, until control flow unwinds and leaves the outermost nesting
956 extern JS_PUBLIC_API(void)
957 JS_ClearNewbornRoots(JSContext
*cx
);
960 * Scoped local root management allows native functions, getter/setters, etc.
961 * to avoid worrying about the newborn root pigeon-holes, overloading local
962 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
963 * and JS_RemoveRoot to manage global roots temporarily.
965 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
966 * the body of the native hook causes the engine to allocate a local root for
967 * each newborn created in between the two API calls, using a local root stack
968 * associated with cx. For example:
971 * my_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
975 * if (!JS_EnterLocalRootScope(cx))
977 * ok = my_GetPropertyBody(cx, obj, id, vp);
978 * JS_LeaveLocalRootScope(cx);
982 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
983 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
984 * not make the matching JS_LeaveLocalRootScope call.
986 * JS_LeaveLocalRootScopeWithResult(cx, rval) is an alternative way to leave
987 * a local root scope that protects a result or return value, by effectively
988 * pushing it in the caller's local root scope.
990 * In case a native hook allocates many objects or other GC-things, but the
991 * native protects some of those GC-things by storing them as property values
992 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
993 * to free the local root automatically pushed for the now-protected GC-thing.
995 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
996 * root scope, but it's more time-efficient when called on references to more
997 * recently created GC-things. Calling it successively on other than the most
998 * recently allocated GC-thing will tend to average the time inefficiency, and
999 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
1000 * many local roots if you can root as you go (build a tree of objects from
1001 * the top down, forgetting each latest-allocated GC-thing immediately upon
1002 * linking it to its parent).
1004 extern JS_PUBLIC_API(JSBool
)
1005 JS_EnterLocalRootScope(JSContext
*cx
);
1007 extern JS_PUBLIC_API(void)
1008 JS_LeaveLocalRootScope(JSContext
*cx
);
1010 extern JS_PUBLIC_API(void)
1011 JS_LeaveLocalRootScopeWithResult(JSContext
*cx
, jsval rval
);
1013 extern JS_PUBLIC_API(void)
1014 JS_ForgetLocalRoot(JSContext
*cx
, void *thing
);
1019 class JSAutoLocalRootScope
{
1021 JSAutoLocalRootScope(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
1023 JS_GUARD_OBJECT_NOTIFIER_INIT
;
1024 JS_EnterLocalRootScope(mContext
);
1026 ~JSAutoLocalRootScope() {
1027 JS_LeaveLocalRootScope(mContext
);
1030 void forget(void *thing
) {
1031 JS_ForgetLocalRoot(mContext
, thing
);
1035 JSContext
*mContext
;
1036 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1040 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
1041 static void operator delete(void *, size_t) { };
1049 extern JS_PUBLIC_API(void)
1050 JS_DumpNamedRoots(JSRuntime
*rt
,
1051 void (*dump
)(const char *name
, void *rp
, void *data
),
1056 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
1057 * The root is pointed at by rp; if the root is unnamed, name is null; data is
1058 * supplied from the third parameter to JS_MapGCRoots.
1060 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
1061 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
1062 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
1063 * constants are flags; you can OR them together.
1065 * This function acquires and releases rt's GC lock around the mapping of the
1066 * roots table, so the map function should run to completion in as few cycles
1067 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
1068 * or any JS API entry point that acquires locks, without double-tripping or
1069 * deadlocking on the GC lock.
1071 * JS_MapGCRoots returns the count of roots that were successfully mapped.
1073 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
1074 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
1075 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
1078 (* JSGCRootMapFun
)(void *rp
, const char *name
, void *data
);
1080 extern JS_PUBLIC_API(uint32
)
1081 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
1083 extern JS_PUBLIC_API(JSBool
)
1084 JS_LockGCThing(JSContext
*cx
, void *thing
);
1086 extern JS_PUBLIC_API(JSBool
)
1087 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
1089 extern JS_PUBLIC_API(JSBool
)
1090 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
1092 extern JS_PUBLIC_API(JSBool
)
1093 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
1096 * Register externally maintained GC roots.
1098 * traceOp: the trace operation. For each root the implementation should call
1099 * JS_CallTracer whenever the root contains a traceable thing.
1100 * data: the data argument to pass to each invocation of traceOp.
1102 extern JS_PUBLIC_API(void)
1103 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
1106 * For implementors of JSMarkOp. All new code should implement JSTraceOp
1109 extern JS_PUBLIC_API(void)
1110 JS_MarkGCThing(JSContext
*cx
, void *thing
, const char *name
, void *arg
);
1113 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
1114 * enumerate all references to traceable things reachable via a property or
1115 * other strong ref identified for debugging purposes by name or index or
1116 * a naming callback.
1118 * By definition references to traceable things include non-null pointers
1119 * to JSObject, JSString and jsdouble and corresponding jsvals.
1121 * See the JSTraceOp typedef in jspubtd.h.
1124 /* Trace kinds to pass to JS_Tracing. */
1125 #define JSTRACE_OBJECT 0
1126 #define JSTRACE_DOUBLE 1
1127 #define JSTRACE_STRING 2
1130 * Use the following macros to check if a particular jsval is a traceable
1131 * thing and to extract the thing and its kind to pass to JS_CallTracer.
1133 #define JSVAL_IS_TRACEABLE(v) (JSVAL_IS_GCTHING(v) && !JSVAL_IS_NULL(v))
1134 #define JSVAL_TO_TRACEABLE(v) (JSVAL_TO_GCTHING(v))
1135 #define JSVAL_TRACE_KIND(v) (JSVAL_TAG(v) >> 1)
1139 JSTraceCallback callback
;
1140 JSTraceNamePrinter debugPrinter
;
1141 const void *debugPrintArg
;
1142 size_t debugPrintIndex
;
1146 * The method to call on each reference to a traceable thing stored in a
1147 * particular JSObject or other runtime structure. With DEBUG defined the
1148 * caller before calling JS_CallTracer must initialize JSTracer fields
1149 * describing the reference using the macros below.
1151 extern JS_PUBLIC_API(void)
1152 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
1155 * Set debugging information about a reference to a traceable thing to prepare
1156 * for the following call to JS_CallTracer.
1158 * When printer is null, arg must be const char * or char * C string naming
1159 * the reference and index must be either (size_t)-1 indicating that the name
1160 * alone describes the reference or it must be an index into some array vector
1161 * that stores the reference.
1163 * When printer callback is not null, the arg and index arguments are
1164 * available to the callback as debugPrinterArg and debugPrintIndex fields
1167 * The storage for name or callback's arguments needs to live only until
1168 * the following call to JS_CallTracer returns.
1171 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1173 (trc)->debugPrinter = (printer); \
1174 (trc)->debugPrintArg = (arg); \
1175 (trc)->debugPrintIndex = (index); \
1178 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1184 * Convenience macro to describe the argument of JS_CallTracer using C string
1187 # define JS_SET_TRACING_INDEX(trc, name, index) \
1188 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1191 * Convenience macro to describe the argument of JS_CallTracer using C string.
1193 # define JS_SET_TRACING_NAME(trc, name) \
1194 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1197 * Convenience macro to invoke JS_CallTracer using C string as the name for
1198 * the reference to a traceable thing.
1200 # define JS_CALL_TRACER(trc, thing, kind, name) \
1202 JS_SET_TRACING_NAME(trc, name); \
1203 JS_CallTracer((trc), (thing), (kind)); \
1207 * Convenience macros to invoke JS_CallTracer when jsval represents a
1208 * reference to a traceable thing.
1210 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1212 if (JSVAL_IS_TRACEABLE(val)) { \
1213 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1214 JSVAL_TRACE_KIND(val), name); \
1218 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1220 JSObject *obj_ = (object); \
1222 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1225 #define JS_CALL_STRING_TRACER(trc, string, name) \
1227 JSString *str_ = (string); \
1229 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1232 #define JS_CALL_DOUBLE_TRACER(trc, number, name) \
1234 jsdouble *num_ = (number); \
1236 JS_CALL_TRACER((trc), num_, JSTRACE_DOUBLE, name); \
1240 * API for JSTraceCallback implementations.
1242 # define JS_TRACER_INIT(trc, cx_, callback_) \
1244 (trc)->context = (cx_); \
1245 (trc)->callback = (callback_); \
1246 (trc)->debugPrinter = NULL; \
1247 (trc)->debugPrintArg = NULL; \
1248 (trc)->debugPrintIndex = (size_t)-1; \
1251 extern JS_PUBLIC_API(void)
1252 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1254 extern JS_PUBLIC_API(void)
1255 JS_TraceRuntime(JSTracer
*trc
);
1259 extern JS_PUBLIC_API(void)
1260 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1261 void *thing
, uint32 kind
, JSBool includeDetails
);
1264 * DEBUG-only method to dump the object graph of heap-allocated things.
1266 * fp: file for the dump output.
1267 * start: when non-null, dump only things reachable from start
1268 * thing. Otherwise dump all things reachable from the
1270 * startKind: trace kind of start if start is not null. Must be 0 when
1272 * thingToFind: dump only paths in the object graph leading to thingToFind
1274 * maxDepth: the upper bound on the number of edges to descend from the
1276 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1278 extern JS_PUBLIC_API(JSBool
)
1279 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1280 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1285 * Garbage collector API.
1287 extern JS_PUBLIC_API(void)
1288 JS_GC(JSContext
*cx
);
1290 extern JS_PUBLIC_API(void)
1291 JS_MaybeGC(JSContext
*cx
);
1293 extern JS_PUBLIC_API(JSGCCallback
)
1294 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1296 extern JS_PUBLIC_API(JSGCCallback
)
1297 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1299 extern JS_PUBLIC_API(JSBool
)
1300 JS_IsGCMarkingTracer(JSTracer
*trc
);
1302 extern JS_PUBLIC_API(JSBool
)
1303 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1305 typedef enum JSGCParamKey
{
1306 /* Maximum nominal heap before last ditch GC. */
1309 /* Number of JS_malloc bytes before last ditch GC. */
1310 JSGC_MAX_MALLOC_BYTES
= 1,
1312 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1313 JSGC_STACKPOOL_LIFESPAN
= 2,
1316 * The factor that defines when the GC is invoked. The factor is a
1317 * percent of the memory allocated by the GC after the last run of
1318 * the GC. When the current memory allocated by the GC is more than
1319 * this percent then the GC is invoked. The factor cannot be less
1320 * than 100 since the current memory allocated by the GC cannot be less
1321 * than the memory allocated after the last run of the GC.
1323 JSGC_TRIGGER_FACTOR
= 3,
1325 /* Amount of bytes allocated by the GC. */
1328 /* Number of times when GC was invoked. */
1331 /* Max size of the code cache in bytes. */
1332 JSGC_MAX_CODE_CACHE_BYTES
= 6
1335 extern JS_PUBLIC_API(void)
1336 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1338 extern JS_PUBLIC_API(uint32
)
1339 JS_GetGCParameter(JSRuntime
*rt
, JSGCParamKey key
);
1341 extern JS_PUBLIC_API(void)
1342 JS_SetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
, uint32 value
);
1344 extern JS_PUBLIC_API(uint32
)
1345 JS_GetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
);
1348 * Flush the code cache for the current thread. The operation might be
1349 * delayed if the cache cannot be flushed currently because native
1350 * code is currently executing.
1353 extern JS_PUBLIC_API(void)
1354 JS_FlushCaches(JSContext
*cx
);
1357 * Add a finalizer for external strings created by JS_NewExternalString (see
1358 * below) using a type-code returned from this function, and that understands
1359 * how to free or release the memory pointed at by JS_GetStringChars(str).
1361 * Return a nonnegative type index if there is room for finalizer in the
1362 * global GC finalizers table, else return -1. If the engine is compiled
1363 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1364 * be invoked on the primordial thread only, at startup -- or else the entire
1365 * program must single-thread itself while loading a module that calls this
1368 extern JS_PUBLIC_API(intN
)
1369 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1372 * Remove finalizer from the global GC finalizers table, returning its type
1373 * code if found, -1 if not found.
1375 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1376 * if you compile the engine JS_THREADSAFE: this function may be called for a
1377 * given finalizer pointer on only one thread; different threads may call to
1378 * remove distinct finalizers safely.
1380 * You must ensure that all strings with finalizer's type have been collected
1381 * before calling this function. Otherwise, string data will be leaked by the
1382 * GC, for want of a finalizer to call.
1384 extern JS_PUBLIC_API(intN
)
1385 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1388 * Create a new JSString whose chars member refers to external memory, i.e.,
1389 * memory requiring special, type-specific finalization. The type code must
1390 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1392 extern JS_PUBLIC_API(JSString
*)
1393 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1396 * Returns the external-string finalizer index for this string, or -1 if it is
1397 * an "internal" (native to JS engine) string.
1399 extern JS_PUBLIC_API(intN
)
1400 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1403 * Deprecated. Use JS_SetNativeStackQuoata instead.
1405 extern JS_PUBLIC_API(void)
1406 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1409 * Set the size of the native stack that should not be exceed. To disable
1410 * stack size checking pass 0.
1412 extern JS_PUBLIC_API(void)
1413 JS_SetNativeStackQuota(JSContext
*cx
, size_t stackSize
);
1417 * Set the quota on the number of bytes that stack-like data structures can
1418 * use when the runtime compiles and executes scripts. These structures
1419 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1420 * The default quota is 32MB which is quite generous.
1422 * The function must be called before any script compilation or execution API
1423 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1426 extern JS_PUBLIC_API(void)
1427 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1429 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1431 /************************************************************************/
1434 * Classes, objects, and properties.
1437 /* For detailed comments on the function pointer types, see jspubtd.h. */
1442 /* Mandatory non-null function pointer members. */
1443 JSPropertyOp addProperty
;
1444 JSPropertyOp delProperty
;
1445 JSPropertyOp getProperty
;
1446 JSPropertyOp setProperty
;
1447 JSEnumerateOp enumerate
;
1448 JSResolveOp resolve
;
1449 JSConvertOp convert
;
1450 JSFinalizeOp finalize
;
1452 /* Optionally non-null members start here. */
1453 JSGetObjectOps getObjectOps
;
1454 JSCheckAccessOp checkAccess
;
1457 JSXDRObjectOp xdrObject
;
1458 JSHasInstanceOp hasInstance
;
1460 JSReserveSlotsOp reserveSlots
;
1463 struct JSExtendedClass
{
1465 JSEqualityOp equality
;
1466 JSObjectOp outerObject
;
1467 JSObjectOp innerObject
;
1468 JSIteratorOp iteratorObject
;
1469 JSObjectOp wrappedObject
; /* NB: infallible, null
1470 returns are treated as
1471 the original object */
1472 void (*reserved0
)(void);
1473 void (*reserved1
)(void);
1474 void (*reserved2
)(void);
1477 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1478 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1479 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1480 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1481 /* (1<<4) was JSCLASS_SHARE_ALL_PROPERTIES, now obsolete. See bug 527805. */
1482 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1483 object in prototype chain
1484 passed in via *objp in/out
1486 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1488 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1491 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1492 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1493 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1495 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1496 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1497 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1498 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1499 << JSCLASS_RESERVED_SLOTS_SHIFT)
1500 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1501 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1502 & JSCLASS_RESERVED_SLOTS_MASK)
1504 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1505 JSCLASS_RESERVED_SLOTS_WIDTH)
1507 /* True if JSClass is really a JSExtendedClass. */
1508 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1509 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1510 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1512 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1513 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1516 * ECMA-262 requires that most constructors used internally create objects
1517 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1518 * member initial value. The "original ... value" verbiage is there because
1519 * in ECMA-262, global properties naming class objects are read/write and
1520 * deleteable, for the most part.
1522 * Implementing this efficiently requires that global objects have classes
1523 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was
1524 * prevously allowed, but is now an ES5 violation and thus unsupported.
1526 #define JSCLASS_GLOBAL_FLAGS \
1527 (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT * 2))
1529 /* Fast access to the original value of each standard class's prototype. */
1530 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1531 #define JSCLASS_CACHED_PROTO_WIDTH 8
1532 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1533 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1534 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1536 >> JSCLASS_CACHED_PROTO_SHIFT) \
1537 & JSCLASS_CACHED_PROTO_MASK))
1539 /* Initializer for unused members of statically initialized JSClass structs. */
1540 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
1541 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
1546 jsid vector
[1]; /* actually, length jsid words */
1549 extern JS_PUBLIC_API(void)
1550 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
1552 extern JS_PUBLIC_API(JSBool
)
1553 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
1555 extern JS_PUBLIC_API(JSBool
)
1556 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
1559 * The magic XML namespace id is int-tagged, but not a valid integer jsval.
1560 * Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
1561 * should handle this id specially before converting id via JSVAL_TO_INT.
1563 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
1566 * JSNewResolveOp flag bits.
1568 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1569 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1570 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1571 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1572 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1573 #define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
1575 extern JS_PUBLIC_API(JSBool
)
1576 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsval id
, jsval
*vp
);
1578 extern JS_PUBLIC_API(JSBool
)
1579 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
1581 extern JS_PUBLIC_API(JSBool
)
1582 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsval id
);
1584 extern JS_PUBLIC_API(JSBool
)
1585 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
1587 extern JS_PUBLIC_API(void)
1588 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
1590 struct JSConstDoubleSpec
{
1598 * To define an array element rather than a named property member, cast the
1599 * element's index to (const char *) and initialize name with it, and set the
1600 * JSPROP_INDEX bit in flags.
1602 struct JSPropertySpec
{
1606 JSPropertyOp getter
;
1607 JSPropertyOp setter
;
1610 struct JSFunctionSpec
{
1617 * extra & 0xFFFF: Number of extra argument slots for local GC roots.
1618 * If fast native, must be zero.
1619 * extra >> 16: Reserved for future use (must be 0).
1625 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1626 * that's passed to JS_DefineFunctions or JS_InitClass.
1628 #define JS_FS_END JS_FS(NULL,NULL,0,0,0)
1631 * Initializer macro for a JSFunctionSpec array element. This is the original
1632 * kind of native function specifier initializer. Use JS_FN ("fast native", see
1633 * JSFastNative in jspubtd.h) for all functions that do not need a stack frame
1636 #define JS_FS(name,call,nargs,flags,extra) \
1637 {name, call, nargs, flags, extra}
1640 * "Fast native" initializer macro for a JSFunctionSpec array element. Use this
1641 * in preference to JS_FS if the native in question does not need its own stack
1642 * frame when activated.
1644 #define JS_FN(name,fastcall,nargs,flags) \
1645 JS_FS(name, (JSNative)(fastcall), nargs, \
1646 (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0)
1648 extern JS_PUBLIC_API(JSObject
*)
1649 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
1650 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
1651 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
1652 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
1654 #ifdef JS_THREADSAFE
1655 extern JS_PUBLIC_API(JSClass
*)
1656 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
1658 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1660 extern JS_PUBLIC_API(JSClass
*)
1661 JS_GetClass(JSObject
*obj
);
1663 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1666 extern JS_PUBLIC_API(JSBool
)
1667 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
1669 extern JS_PUBLIC_API(JSBool
)
1670 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
1672 extern JS_PUBLIC_API(void *)
1673 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
1675 extern JS_PUBLIC_API(JSBool
)
1676 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
1678 extern JS_PUBLIC_API(void *)
1679 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
1682 extern JS_PUBLIC_API(JSObject
*)
1683 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
1685 extern JS_PUBLIC_API(JSBool
)
1686 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
1688 extern JS_PUBLIC_API(JSObject
*)
1689 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
1691 extern JS_PUBLIC_API(JSBool
)
1692 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
1694 extern JS_PUBLIC_API(JSObject
*)
1695 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
1698 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1699 * is moved by a copying GC). Return false on failure (likely out of memory),
1700 * and true with *idp containing the unique id on success.
1702 extern JS_PUBLIC_API(JSBool
)
1703 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
1705 extern JS_PUBLIC_API(JSObject
*)
1706 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
1709 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1710 * proto if proto's actual parameter value is null.
1712 extern JS_PUBLIC_API(JSObject
*)
1713 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1716 extern JS_PUBLIC_API(JSBool
)
1717 JS_SealObject(JSContext
*cx
, JSObject
*obj
, JSBool deep
);
1719 extern JS_PUBLIC_API(JSObject
*)
1720 JS_ConstructObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1723 extern JS_PUBLIC_API(JSObject
*)
1724 JS_ConstructObjectWithArguments(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1725 JSObject
*parent
, uintN argc
, jsval
*argv
);
1727 extern JS_PUBLIC_API(JSObject
*)
1728 JS_New(JSContext
*cx
, JSObject
*ctor
, uintN argc
, jsval
*argv
);
1730 extern JS_PUBLIC_API(JSObject
*)
1731 JS_DefineObject(JSContext
*cx
, JSObject
*obj
, const char *name
, JSClass
*clasp
,
1732 JSObject
*proto
, uintN attrs
);
1734 extern JS_PUBLIC_API(JSBool
)
1735 JS_DefineConstDoubles(JSContext
*cx
, JSObject
*obj
, JSConstDoubleSpec
*cds
);
1737 extern JS_PUBLIC_API(JSBool
)
1738 JS_DefineProperties(JSContext
*cx
, JSObject
*obj
, JSPropertySpec
*ps
);
1740 extern JS_PUBLIC_API(JSBool
)
1741 JS_DefineProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval value
,
1742 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1744 extern JS_PUBLIC_API(JSBool
)
1745 JS_DefinePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval value
,
1746 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1748 extern JS_PUBLIC_API(JSBool
)
1749 JS_DefineOwnProperty(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval descriptor
, JSBool
*bp
);
1752 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1754 * If the object does not have a property by that name, *foundp will be
1755 * JS_FALSE and the value of *attrsp is undefined.
1757 extern JS_PUBLIC_API(JSBool
)
1758 JS_GetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1759 uintN
*attrsp
, JSBool
*foundp
);
1762 * The same, but if the property is native, return its getter and setter via
1763 * *getterp and *setterp, respectively (and only if the out parameter pointer
1766 extern JS_PUBLIC_API(JSBool
)
1767 JS_GetPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1769 uintN
*attrsp
, JSBool
*foundp
,
1770 JSPropertyOp
*getterp
,
1771 JSPropertyOp
*setterp
);
1773 extern JS_PUBLIC_API(JSBool
)
1774 JS_GetPropertyAttrsGetterAndSetterById(JSContext
*cx
, JSObject
*obj
,
1776 uintN
*attrsp
, JSBool
*foundp
,
1777 JSPropertyOp
*getterp
,
1778 JSPropertyOp
*setterp
);
1781 * Set the attributes of a property on a given object.
1783 * If the object does not have a property by that name, *foundp will be
1784 * JS_FALSE and nothing will be altered.
1786 extern JS_PUBLIC_API(JSBool
)
1787 JS_SetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1788 uintN attrs
, JSBool
*foundp
);
1790 extern JS_PUBLIC_API(JSBool
)
1791 JS_DefinePropertyWithTinyId(JSContext
*cx
, JSObject
*obj
, const char *name
,
1792 int8 tinyid
, jsval value
,
1793 JSPropertyOp getter
, JSPropertyOp setter
,
1796 extern JS_PUBLIC_API(JSBool
)
1797 JS_AliasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1800 extern JS_PUBLIC_API(JSBool
)
1801 JS_AlreadyHasOwnProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
1804 extern JS_PUBLIC_API(JSBool
)
1805 JS_AlreadyHasOwnPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1808 extern JS_PUBLIC_API(JSBool
)
1809 JS_HasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, JSBool
*foundp
);
1811 extern JS_PUBLIC_API(JSBool
)
1812 JS_HasPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool
*foundp
);
1814 extern JS_PUBLIC_API(JSBool
)
1815 JS_LookupProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1817 extern JS_PUBLIC_API(JSBool
)
1818 JS_LookupPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1820 extern JS_PUBLIC_API(JSBool
)
1821 JS_LookupPropertyWithFlags(JSContext
*cx
, JSObject
*obj
, const char *name
,
1822 uintN flags
, jsval
*vp
);
1824 extern JS_PUBLIC_API(JSBool
)
1825 JS_LookupPropertyWithFlagsById(JSContext
*cx
, JSObject
*obj
, jsid id
,
1826 uintN flags
, JSObject
**objp
, jsval
*vp
);
1828 struct JSPropertyDescriptor
{
1831 JSPropertyOp getter
;
1832 JSPropertyOp setter
;
1838 * Like JS_GetPropertyAttrsGetterAndSetterById but will return a property on
1839 * an object on the prototype chain (returned in objp). If data->obj is null,
1840 * then this property was not found on the prototype chain.
1842 extern JS_PUBLIC_API(JSBool
)
1843 JS_GetPropertyDescriptorById(JSContext
*cx
, JSObject
*obj
, jsid id
, uintN flags
,
1844 JSPropertyDescriptor
*desc
);
1846 extern JS_PUBLIC_API(JSBool
)
1847 JS_GetOwnPropertyDescriptor(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1849 extern JS_PUBLIC_API(JSBool
)
1850 JS_GetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1852 extern JS_PUBLIC_API(JSBool
)
1853 JS_GetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1855 extern JS_PUBLIC_API(JSBool
)
1856 JS_GetMethodById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSObject
**objp
,
1859 extern JS_PUBLIC_API(JSBool
)
1860 JS_GetMethod(JSContext
*cx
, JSObject
*obj
, const char *name
, JSObject
**objp
,
1863 extern JS_PUBLIC_API(JSBool
)
1864 JS_SetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
1866 extern JS_PUBLIC_API(JSBool
)
1867 JS_SetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1869 extern JS_PUBLIC_API(JSBool
)
1870 JS_DeleteProperty(JSContext
*cx
, JSObject
*obj
, const char *name
);
1872 extern JS_PUBLIC_API(JSBool
)
1873 JS_DeleteProperty2(JSContext
*cx
, JSObject
*obj
, const char *name
,
1876 extern JS_PUBLIC_API(JSBool
)
1877 JS_DeletePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
);
1879 extern JS_PUBLIC_API(JSBool
)
1880 JS_DeletePropertyById2(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*rval
);
1882 extern JS_PUBLIC_API(JSBool
)
1883 JS_DefineUCProperty(JSContext
*cx
, JSObject
*obj
,
1884 const jschar
*name
, size_t namelen
, jsval value
,
1885 JSPropertyOp getter
, JSPropertyOp setter
,
1889 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1891 * If the object does not have a property by that name, *foundp will be
1892 * JS_FALSE and the value of *attrsp is undefined.
1894 extern JS_PUBLIC_API(JSBool
)
1895 JS_GetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1896 const jschar
*name
, size_t namelen
,
1897 uintN
*attrsp
, JSBool
*foundp
);
1900 * The same, but if the property is native, return its getter and setter via
1901 * *getterp and *setterp, respectively (and only if the out parameter pointer
1904 extern JS_PUBLIC_API(JSBool
)
1905 JS_GetUCPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1906 const jschar
*name
, size_t namelen
,
1907 uintN
*attrsp
, JSBool
*foundp
,
1908 JSPropertyOp
*getterp
,
1909 JSPropertyOp
*setterp
);
1912 * Set the attributes of a property on a given object.
1914 * If the object does not have a property by that name, *foundp will be
1915 * JS_FALSE and nothing will be altered.
1917 extern JS_PUBLIC_API(JSBool
)
1918 JS_SetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
1919 const jschar
*name
, size_t namelen
,
1920 uintN attrs
, JSBool
*foundp
);
1923 extern JS_PUBLIC_API(JSBool
)
1924 JS_DefineUCPropertyWithTinyId(JSContext
*cx
, JSObject
*obj
,
1925 const jschar
*name
, size_t namelen
,
1926 int8 tinyid
, jsval value
,
1927 JSPropertyOp getter
, JSPropertyOp setter
,
1930 extern JS_PUBLIC_API(JSBool
)
1931 JS_AlreadyHasOwnUCProperty(JSContext
*cx
, JSObject
*obj
, const jschar
*name
,
1932 size_t namelen
, JSBool
*foundp
);
1934 extern JS_PUBLIC_API(JSBool
)
1935 JS_HasUCProperty(JSContext
*cx
, JSObject
*obj
,
1936 const jschar
*name
, size_t namelen
,
1939 extern JS_PUBLIC_API(JSBool
)
1940 JS_LookupUCProperty(JSContext
*cx
, JSObject
*obj
,
1941 const jschar
*name
, size_t namelen
,
1944 extern JS_PUBLIC_API(JSBool
)
1945 JS_GetUCProperty(JSContext
*cx
, JSObject
*obj
,
1946 const jschar
*name
, size_t namelen
,
1949 extern JS_PUBLIC_API(JSBool
)
1950 JS_SetUCProperty(JSContext
*cx
, JSObject
*obj
,
1951 const jschar
*name
, size_t namelen
,
1954 extern JS_PUBLIC_API(JSBool
)
1955 JS_DeleteUCProperty2(JSContext
*cx
, JSObject
*obj
,
1956 const jschar
*name
, size_t namelen
,
1959 extern JS_PUBLIC_API(JSObject
*)
1960 JS_NewArrayObject(JSContext
*cx
, jsint length
, jsval
*vector
);
1962 extern JS_PUBLIC_API(JSBool
)
1963 JS_IsArrayObject(JSContext
*cx
, JSObject
*obj
);
1965 extern JS_PUBLIC_API(JSBool
)
1966 JS_GetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1968 extern JS_PUBLIC_API(JSBool
)
1969 JS_SetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint length
);
1971 extern JS_PUBLIC_API(JSBool
)
1972 JS_HasArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
1974 extern JS_PUBLIC_API(JSBool
)
1975 JS_DefineElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval value
,
1976 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1978 extern JS_PUBLIC_API(JSBool
)
1979 JS_AliasElement(JSContext
*cx
, JSObject
*obj
, const char *name
, jsint alias
);
1981 extern JS_PUBLIC_API(JSBool
)
1982 JS_AlreadyHasOwnElement(JSContext
*cx
, JSObject
*obj
, jsint index
,
1985 extern JS_PUBLIC_API(JSBool
)
1986 JS_HasElement(JSContext
*cx
, JSObject
*obj
, jsint index
, JSBool
*foundp
);
1988 extern JS_PUBLIC_API(JSBool
)
1989 JS_LookupElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1991 extern JS_PUBLIC_API(JSBool
)
1992 JS_GetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1994 extern JS_PUBLIC_API(JSBool
)
1995 JS_SetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
1997 extern JS_PUBLIC_API(JSBool
)
1998 JS_DeleteElement(JSContext
*cx
, JSObject
*obj
, jsint index
);
2000 extern JS_PUBLIC_API(JSBool
)
2001 JS_DeleteElement2(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*rval
);
2003 extern JS_PUBLIC_API(void)
2004 JS_ClearScope(JSContext
*cx
, JSObject
*obj
);
2006 extern JS_PUBLIC_API(JSIdArray
*)
2007 JS_Enumerate(JSContext
*cx
, JSObject
*obj
);
2010 * Create an object to iterate over enumerable properties of obj, in arbitrary
2011 * property definition order. NB: This differs from longstanding for..in loop
2012 * order, which uses order of property definition in obj.
2014 extern JS_PUBLIC_API(JSObject
*)
2015 JS_NewPropertyIterator(JSContext
*cx
, JSObject
*obj
);
2018 * Return true on success with *idp containing the id of the next enumerable
2019 * property to visit using iterobj, or JSVAL_VOID if there is no such property
2020 * left to visit. Return false on error.
2022 extern JS_PUBLIC_API(JSBool
)
2023 JS_NextProperty(JSContext
*cx
, JSObject
*iterobj
, jsid
*idp
);
2025 extern JS_PUBLIC_API(JSBool
)
2026 JS_CheckAccess(JSContext
*cx
, JSObject
*obj
, jsid id
, JSAccessMode mode
,
2027 jsval
*vp
, uintN
*attrsp
);
2029 extern JS_PUBLIC_API(JSBool
)
2030 JS_GetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval
*vp
);
2032 extern JS_PUBLIC_API(JSBool
)
2033 JS_SetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval v
);
2035 /************************************************************************/
2038 * Security protocol.
2040 struct JSPrincipals
{
2043 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
2044 void * (* getPrincipalArray
)(JSContext
*cx
, JSPrincipals
*);
2045 JSBool (* globalPrivilegesEnabled
)(JSContext
*cx
, JSPrincipals
*);
2047 /* Don't call "destroy"; use reference counting macros below. */
2048 jsrefcount refcount
;
2050 void (* destroy
)(JSContext
*cx
, JSPrincipals
*);
2051 JSBool (* subsume
)(JSPrincipals
*, JSPrincipals
*);
2054 #ifdef JS_THREADSAFE
2055 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
2056 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
2058 extern JS_PUBLIC_API(jsrefcount
)
2059 JS_HoldPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2061 extern JS_PUBLIC_API(jsrefcount
)
2062 JS_DropPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2065 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
2066 #define JSPRINCIPALS_DROP(cx, principals) \
2067 ((--(principals)->refcount == 0) \
2068 ? ((*(principals)->destroy)((cx), (principals)), 0) \
2069 : (principals)->refcount)
2073 struct JSSecurityCallbacks
{
2074 JSCheckAccessOp checkObjectAccess
;
2075 JSPrincipalsTranscoder principalsTranscoder
;
2076 JSObjectPrincipalsFinder findObjectPrincipals
;
2077 JSCSPEvalChecker contentSecurityPolicyAllows
;
2080 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2081 JS_SetRuntimeSecurityCallbacks(JSRuntime
*rt
, JSSecurityCallbacks
*callbacks
);
2083 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2084 JS_GetRuntimeSecurityCallbacks(JSRuntime
*rt
);
2086 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2087 JS_SetContextSecurityCallbacks(JSContext
*cx
, JSSecurityCallbacks
*callbacks
);
2089 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2090 JS_GetSecurityCallbacks(JSContext
*cx
);
2092 /************************************************************************/
2095 * Functions and scripts.
2097 extern JS_PUBLIC_API(JSFunction
*)
2098 JS_NewFunction(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
2099 JSObject
*parent
, const char *name
);
2101 extern JS_PUBLIC_API(JSObject
*)
2102 JS_GetFunctionObject(JSFunction
*fun
);
2105 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
2106 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
2108 extern JS_PUBLIC_API(const char *)
2109 JS_GetFunctionName(JSFunction
*fun
);
2112 * Return the function's identifier as a JSString, or null if fun is unnamed.
2113 * The returned string lives as long as fun, so you don't need to root a saved
2114 * reference to it if fun is well-connected or rooted, and provided you bound
2115 * the use of the saved reference by fun's lifetime.
2117 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
2118 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
2119 * from UTF-16-ish jschars.
2121 extern JS_PUBLIC_API(JSString
*)
2122 JS_GetFunctionId(JSFunction
*fun
);
2125 * Return JSFUN_* flags for fun.
2127 extern JS_PUBLIC_API(uintN
)
2128 JS_GetFunctionFlags(JSFunction
*fun
);
2131 * Return the arity (length) of fun.
2133 extern JS_PUBLIC_API(uint16
)
2134 JS_GetFunctionArity(JSFunction
*fun
);
2137 * Infallible predicate to test whether obj is a function object (faster than
2138 * comparing obj's class name to "Function", but equivalent unless someone has
2139 * overwritten the "Function" identifier with a different constructor and then
2140 * created instances using that constructor that might be passed in as obj).
2142 extern JS_PUBLIC_API(JSBool
)
2143 JS_ObjectIsFunction(JSContext
*cx
, JSObject
*obj
);
2145 extern JS_PUBLIC_API(JSBool
)
2146 JS_DefineFunctions(JSContext
*cx
, JSObject
*obj
, JSFunctionSpec
*fs
);
2148 extern JS_PUBLIC_API(JSFunction
*)
2149 JS_DefineFunction(JSContext
*cx
, JSObject
*obj
, const char *name
, JSNative call
,
2150 uintN nargs
, uintN attrs
);
2152 extern JS_PUBLIC_API(JSFunction
*)
2153 JS_DefineUCFunction(JSContext
*cx
, JSObject
*obj
,
2154 const jschar
*name
, size_t namelen
, JSNative call
,
2155 uintN nargs
, uintN attrs
);
2157 extern JS_PUBLIC_API(JSObject
*)
2158 JS_CloneFunctionObject(JSContext
*cx
, JSObject
*funobj
, JSObject
*parent
);
2161 * Given a buffer, return JS_FALSE if the buffer might become a valid
2162 * javascript statement with the addition of more lines. Otherwise return
2163 * JS_TRUE. The intent is to support interactive compilation - accumulate
2164 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
2167 extern JS_PUBLIC_API(JSBool
)
2168 JS_BufferIsCompilableUnit(JSContext
*cx
, JSObject
*obj
,
2169 const char *bytes
, size_t length
);
2172 * The JSScript objects returned by the following functions refer to string and
2173 * other kinds of literals, including doubles and RegExp objects. These
2174 * literals are vulnerable to garbage collection; to root script objects and
2175 * prevent literals from being collected, create a rootable object using
2176 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
2178 extern JS_PUBLIC_API(JSScript
*)
2179 JS_CompileScript(JSContext
*cx
, JSObject
*obj
,
2180 const char *bytes
, size_t length
,
2181 const char *filename
, uintN lineno
);
2183 extern JS_PUBLIC_API(JSScript
*)
2184 JS_CompileScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2185 JSPrincipals
*principals
,
2186 const char *bytes
, size_t length
,
2187 const char *filename
, uintN lineno
);
2189 extern JS_PUBLIC_API(JSScript
*)
2190 JS_CompileUCScript(JSContext
*cx
, JSObject
*obj
,
2191 const jschar
*chars
, size_t length
,
2192 const char *filename
, uintN lineno
);
2194 extern JS_PUBLIC_API(JSScript
*)
2195 JS_CompileUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2196 JSPrincipals
*principals
,
2197 const jschar
*chars
, size_t length
,
2198 const char *filename
, uintN lineno
);
2200 extern JS_PUBLIC_API(JSScript
*)
2201 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2203 extern JS_PUBLIC_API(JSScript
*)
2204 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2207 extern JS_PUBLIC_API(JSScript
*)
2208 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2209 const char *filename
, FILE *fh
,
2210 JSPrincipals
*principals
);
2213 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2214 * in order to keep a JSScript and its atoms safe from garbage collection after
2215 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2216 * E.g., and without error checks:
2218 * JSScript *script = JS_CompileFile(cx, global, filename);
2219 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2220 * JS_AddNamedRoot(cx, &scrobj, "scrobj");
2223 * JS_ExecuteScript(cx, global, script, &result);
2225 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2226 * JS_RemoveRoot(cx, &scrobj);
2228 extern JS_PUBLIC_API(JSObject
*)
2229 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2232 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2233 * called on script yet, the return value will be null.
2235 extern JS_PUBLIC_API(JSObject
*)
2236 JS_GetScriptObject(JSScript
*script
);
2238 extern JS_PUBLIC_API(void)
2239 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2241 extern JS_PUBLIC_API(JSFunction
*)
2242 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2243 uintN nargs
, const char **argnames
,
2244 const char *bytes
, size_t length
,
2245 const char *filename
, uintN lineno
);
2247 extern JS_PUBLIC_API(JSFunction
*)
2248 JS_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2249 JSPrincipals
*principals
, const char *name
,
2250 uintN nargs
, const char **argnames
,
2251 const char *bytes
, size_t length
,
2252 const char *filename
, uintN lineno
);
2254 extern JS_PUBLIC_API(JSFunction
*)
2255 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2256 uintN nargs
, const char **argnames
,
2257 const jschar
*chars
, size_t length
,
2258 const char *filename
, uintN lineno
);
2260 extern JS_PUBLIC_API(JSFunction
*)
2261 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2262 JSPrincipals
*principals
, const char *name
,
2263 uintN nargs
, const char **argnames
,
2264 const jschar
*chars
, size_t length
,
2265 const char *filename
, uintN lineno
);
2267 extern JS_PUBLIC_API(JSString
*)
2268 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2272 * API extension: OR this into indent to avoid pretty-printing the decompiled
2273 * source resulting from JS_DecompileFunction{,Body}.
2275 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2277 extern JS_PUBLIC_API(JSString
*)
2278 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2280 extern JS_PUBLIC_API(JSString
*)
2281 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2284 * NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
2285 * parameter as the initial scope chain header, the 'this' keyword value, and
2286 * the variables object (ECMA parlance for where 'var' and 'function' bind
2287 * names) of the execution context for script.
2289 * Using obj as the variables object is problematic if obj's parent (which is
2290 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2291 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2292 * created by assignment to an unbound id, 'x = 0', go in the last object on
2293 * the scope chain linked by parent.
2295 * ECMA calls that last scoping object the "global object", but note that many
2296 * embeddings have several such objects. ECMA requires that "global code" be
2297 * executed with the variables object equal to this global object. But these
2298 * JS API entry points provide freedom to execute code against a "sub-global",
2299 * i.e., a parented or scoped object, in which case the variables object will
2300 * differ from the last object on the scope chain, resulting in confusing and
2301 * non-ECMA explicit vs. implicit variable creation.
2303 * Caveat embedders: unless you already depend on this buggy variables object
2304 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2305 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2306 * someone may have set other options on cx already -- for each context in the
2307 * application, if you pass parented objects as the obj parameter, or may ever
2308 * pass such objects in the future.
2310 * Why a runtime option? The alternative is to add six or so new API entry
2311 * points with signatures matching the following six, and that doesn't seem
2312 * worth the code bloat cost. Such new entry points would probably have less
2313 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2314 * OTOH, can be more easily hacked into existing code that does not depend on
2315 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2316 * etc., entry points.
2318 extern JS_PUBLIC_API(JSBool
)
2319 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2322 * Execute either the function-defining prolog of a script, or the script's
2323 * main body, but not both.
2325 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2327 extern JS_PUBLIC_API(JSBool
)
2328 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2329 const char *bytes
, uintN length
,
2330 const char *filename
, uintN lineno
,
2333 extern JS_PUBLIC_API(JSBool
)
2334 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2335 JSPrincipals
*principals
,
2336 const char *bytes
, uintN length
,
2337 const char *filename
, uintN lineno
,
2340 extern JS_PUBLIC_API(JSBool
)
2341 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2342 const jschar
*chars
, uintN length
,
2343 const char *filename
, uintN lineno
,
2346 extern JS_PUBLIC_API(JSBool
)
2347 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2348 JSPrincipals
*principals
,
2349 const jschar
*chars
, uintN length
,
2350 const char *filename
, uintN lineno
,
2353 extern JS_PUBLIC_API(JSBool
)
2354 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2355 jsval
*argv
, jsval
*rval
);
2357 extern JS_PUBLIC_API(JSBool
)
2358 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2359 jsval
*argv
, jsval
*rval
);
2361 extern JS_PUBLIC_API(JSBool
)
2362 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2363 jsval
*argv
, jsval
*rval
);
2366 * These functions allow setting an operation callback that will be called
2367 * from the thread the context is associated with some time after any thread
2368 * triggered the callback using JS_TriggerOperationCallback(cx).
2370 * In a threadsafe build the engine internally triggers operation callbacks
2371 * under certain circumstances (i.e. GC and title transfer) to force the
2372 * context to yield its current request, which the engine always
2373 * automatically does immediately prior to calling the callback function.
2374 * The embedding should thus not rely on callbacks being triggered through
2375 * the external API only.
2377 * Important note: Additional callbacks can occur inside the callback handler
2378 * if it re-enters the JS engine. The embedding must ensure that the callback
2379 * is disconnected before attempting such re-entry.
2382 extern JS_PUBLIC_API(JSOperationCallback
)
2383 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
);
2385 extern JS_PUBLIC_API(JSOperationCallback
)
2386 JS_GetOperationCallback(JSContext
*cx
);
2388 extern JS_PUBLIC_API(void)
2389 JS_TriggerOperationCallback(JSContext
*cx
);
2391 extern JS_PUBLIC_API(void)
2392 JS_TriggerAllOperationCallbacks(JSRuntime
*rt
);
2394 extern JS_PUBLIC_API(JSBool
)
2395 JS_IsRunning(JSContext
*cx
);
2397 extern JS_PUBLIC_API(JSBool
)
2398 JS_IsConstructing(JSContext
*cx
);
2401 * Saving and restoring frame chains.
2403 * These two functions are used to set aside cx's call stack while that stack
2404 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2405 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2406 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2407 * matching JS_RestoreFrameChain calls.
2409 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2410 * return does not signify an error, and JS_RestoreFrameChain handles a null
2411 * frame pointer argument safely.
2413 extern JS_PUBLIC_API(JSStackFrame
*)
2414 JS_SaveFrameChain(JSContext
*cx
);
2416 extern JS_PUBLIC_API(void)
2417 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2419 /************************************************************************/
2424 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2425 * on error (signified by null return), it leaves bytes owned by the caller.
2426 * So the caller must free bytes in the error case, if it has no use for them.
2427 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2428 * the character memory passed to them -- they copy it.
2430 extern JS_PUBLIC_API(JSString
*)
2431 JS_NewString(JSContext
*cx
, char *bytes
, size_t length
);
2433 extern JS_PUBLIC_API(JSString
*)
2434 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2436 extern JS_PUBLIC_API(JSString
*)
2437 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2439 extern JS_PUBLIC_API(JSString
*)
2440 JS_InternString(JSContext
*cx
, const char *s
);
2442 extern JS_PUBLIC_API(JSString
*)
2443 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
2445 extern JS_PUBLIC_API(JSString
*)
2446 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
2448 extern JS_PUBLIC_API(JSString
*)
2449 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
2451 extern JS_PUBLIC_API(JSString
*)
2452 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
2454 extern JS_PUBLIC_API(JSString
*)
2455 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
2457 extern JS_PUBLIC_API(char *)
2458 JS_GetStringBytes(JSString
*str
);
2460 extern JS_PUBLIC_API(jschar
*)
2461 JS_GetStringChars(JSString
*str
);
2463 extern JS_PUBLIC_API(size_t)
2464 JS_GetStringLength(JSString
*str
);
2466 extern JS_PUBLIC_API(const char *)
2467 JS_GetStringBytesZ(JSContext
*cx
, JSString
*str
);
2469 extern JS_PUBLIC_API(const jschar
*)
2470 JS_GetStringCharsZ(JSContext
*cx
, JSString
*str
);
2472 extern JS_PUBLIC_API(intN
)
2473 JS_CompareStrings(JSString
*str1
, JSString
*str2
);
2476 * Mutable string support. A string's characters are never mutable in this JS
2477 * implementation, but a growable string has a buffer that can be reallocated,
2478 * and a dependent string is a substring of another (growable, dependent, or
2479 * immutable) string. The direct data members of the (opaque to API clients)
2480 * JSString struct may be changed in a single-threaded way for growable and
2481 * dependent strings.
2483 * Therefore mutable strings cannot be used by more than one thread at a time.
2484 * You may call JS_MakeStringImmutable to convert the string from a mutable
2485 * (growable or dependent) string to an immutable (and therefore thread-safe)
2486 * string. The engine takes care of converting growable and dependent strings
2487 * to immutable for you if you store strings in multi-threaded objects using
2488 * JS_SetProperty or kindred API entry points.
2490 * If you store a JSString pointer in a native data structure that is (safely)
2491 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2492 * retiring the store.
2494 extern JS_PUBLIC_API(JSString
*)
2495 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
2498 * Create a dependent string, i.e., a string that owns no character storage,
2499 * but that refers to a slice of another string's chars. Dependent strings
2500 * are mutable by definition, so the thread safety comments above apply.
2502 extern JS_PUBLIC_API(JSString
*)
2503 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
2507 * Concatenate two strings, resulting in a new growable string. If you create
2508 * the left string and pass it to JS_ConcatStrings on a single thread, try to
2509 * use JS_NewGrowableString to create the left string -- doing so helps Concat
2510 * avoid allocating a new buffer for the result and copying left's chars into
2511 * the new buffer. See above for thread safety comments.
2513 extern JS_PUBLIC_API(JSString
*)
2514 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
2517 * Convert a dependent string into an independent one. This function does not
2518 * change the string's mutability, so the thread safety comments above apply.
2520 extern JS_PUBLIC_API(const jschar
*)
2521 JS_UndependString(JSContext
*cx
, JSString
*str
);
2524 * Convert a mutable string (either growable or dependent) into an immutable,
2527 extern JS_PUBLIC_API(JSBool
)
2528 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
2531 * Return JS_TRUE if C (char []) strings passed via the API and internally
2534 JS_PUBLIC_API(JSBool
)
2535 JS_CStringsAreUTF8(void);
2538 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2539 * can never be changed. This API must be called before the first call to
2543 JS_SetCStringsAreUTF8(void);
2546 * Character encoding support.
2548 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2549 * of the destination buffer before the call; on return, *dstlenp contains the
2550 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2551 * stored. To determine the necessary destination buffer size, make a sizing
2552 * call that passes NULL for dst.
2554 * On errors, the functions report the error. In that case, *dstlenp contains
2555 * the number of characters or bytes transferred so far. If cx is NULL, no
2556 * error is reported on failure, and the functions simply return JS_FALSE.
2558 * NB: Neither function stores an additional zero byte or jschar after the
2559 * transcoded string.
2561 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2562 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2563 * errors if the character sequence is malformed. If UTF-8 support is
2564 * disabled, the functions deflate and inflate, respectively.
2566 JS_PUBLIC_API(JSBool
)
2567 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
2570 JS_PUBLIC_API(JSBool
)
2571 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
2575 * A variation on JS_EncodeCharacters where a null terminated string is
2576 * returned that you are expected to call JS_free on when done.
2578 JS_PUBLIC_API(char *)
2579 JS_EncodeString(JSContext
*cx
, JSString
*str
);
2581 /************************************************************************/
2585 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
2588 * JSON.stringify as specified by ES3.1 (draft)
2590 JS_PUBLIC_API(JSBool
)
2591 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
, jsval space
,
2592 JSONWriteCallback callback
, void *data
);
2595 * Retrieve a toJSON function. If found, set vp to its result.
2597 JS_PUBLIC_API(JSBool
)
2598 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
2601 * JSON.parse as specified by ES3.1 (draft)
2603 JS_PUBLIC_API(JSONParser
*)
2604 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
2606 JS_PUBLIC_API(JSBool
)
2607 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
2609 JS_PUBLIC_API(JSBool
)
2610 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
, jsval reviver
);
2612 /************************************************************************/
2615 * Locale specific string conversion and error message callbacks.
2617 struct JSLocaleCallbacks
{
2618 JSLocaleToUpperCase localeToUpperCase
;
2619 JSLocaleToLowerCase localeToLowerCase
;
2620 JSLocaleCompare localeCompare
;
2621 JSLocaleToUnicode localeToUnicode
;
2622 JSErrorCallback localeGetErrorMessage
;
2626 * Establish locale callbacks. The pointer must persist as long as the
2627 * JSContext. Passing NULL restores the default behaviour.
2629 extern JS_PUBLIC_API(void)
2630 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
2633 * Return the address of the current locale callbacks struct, which may
2636 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
2637 JS_GetLocaleCallbacks(JSContext
*cx
);
2639 /************************************************************************/
2646 * Report an exception represented by the sprintf-like conversion of format
2647 * and its arguments. This exception message string is passed to a pre-set
2648 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2649 * the JSErrorReporter typedef).
2651 extern JS_PUBLIC_API(void)
2652 JS_ReportError(JSContext
*cx
, const char *format
, ...);
2655 * Use an errorNumber to retrieve the format string, args are char *
2657 extern JS_PUBLIC_API(void)
2658 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
2659 void *userRef
, const uintN errorNumber
, ...);
2662 * Use an errorNumber to retrieve the format string, args are jschar *
2664 extern JS_PUBLIC_API(void)
2665 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
2666 void *userRef
, const uintN errorNumber
, ...);
2669 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2670 * Return true if there was no error trying to issue the warning, and if the
2671 * warning was not converted into an error due to the JSOPTION_WERROR option
2672 * being set, false otherwise.
2674 extern JS_PUBLIC_API(JSBool
)
2675 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
2677 extern JS_PUBLIC_API(JSBool
)
2678 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
2679 JSErrorCallback errorCallback
, void *userRef
,
2680 const uintN errorNumber
, ...);
2682 extern JS_PUBLIC_API(JSBool
)
2683 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
2684 JSErrorCallback errorCallback
, void *userRef
,
2685 const uintN errorNumber
, ...);
2688 * Complain when out of memory.
2690 extern JS_PUBLIC_API(void)
2691 JS_ReportOutOfMemory(JSContext
*cx
);
2694 * Complain when an allocation size overflows the maximum supported limit.
2696 extern JS_PUBLIC_API(void)
2697 JS_ReportAllocationOverflow(JSContext
*cx
);
2699 struct JSErrorReport
{
2700 const char *filename
; /* source file name, URL, etc., or null */
2701 uintN lineno
; /* source line number */
2702 const char *linebuf
; /* offending source line without final \n */
2703 const char *tokenptr
; /* pointer to error token in linebuf */
2704 const jschar
*uclinebuf
; /* unicode (original) line buffer */
2705 const jschar
*uctokenptr
; /* unicode (original) token pointer */
2706 uintN flags
; /* error/warning, etc. */
2707 uintN errorNumber
; /* the error number, e.g. see js.msg */
2708 const jschar
*ucmessage
; /* the (default) error message */
2709 const jschar
**messageArgs
; /* arguments for the error message */
2713 * JSErrorReport flag values. These may be freely composed.
2715 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2716 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2717 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2718 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2721 * This condition is an error in strict mode code, a warning if
2722 * JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
2723 * all. We check the strictness of the context's top frame's script;
2724 * where that isn't appropriate, the caller should do the right checks
2725 * itself instead of using this flag.
2727 #define JSREPORT_STRICT_MODE_ERROR 0x8
2730 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2731 * has been thrown for this runtime error, and the host should ignore it.
2732 * Exception-aware hosts should also check for JS_IsExceptionPending if
2733 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2736 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2737 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2738 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2739 #define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) & \
2740 JSREPORT_STRICT_MODE_ERROR) != 0)
2742 extern JS_PUBLIC_API(JSErrorReporter
)
2743 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
2745 /************************************************************************/
2748 * Regular Expressions.
2750 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
2751 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
2752 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
2753 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
2754 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
2755 #define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
2757 extern JS_PUBLIC_API(JSObject
*)
2758 JS_NewRegExpObject(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
2760 extern JS_PUBLIC_API(JSObject
*)
2761 JS_NewUCRegExpObject(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
2763 extern JS_PUBLIC_API(void)
2764 JS_SetRegExpInput(JSContext
*cx
, JSString
*input
, JSBool multiline
);
2766 extern JS_PUBLIC_API(void)
2767 JS_ClearRegExpStatics(JSContext
*cx
);
2769 extern JS_PUBLIC_API(void)
2770 JS_ClearRegExpRoots(JSContext
*cx
);
2772 /* TODO: compile, exec, get/set other statics... */
2774 /************************************************************************/
2776 extern JS_PUBLIC_API(JSBool
)
2777 JS_IsExceptionPending(JSContext
*cx
);
2779 extern JS_PUBLIC_API(JSBool
)
2780 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
2782 extern JS_PUBLIC_API(void)
2783 JS_SetPendingException(JSContext
*cx
, jsval v
);
2785 extern JS_PUBLIC_API(void)
2786 JS_ClearPendingException(JSContext
*cx
);
2788 extern JS_PUBLIC_API(JSBool
)
2789 JS_ReportPendingException(JSContext
*cx
);
2792 * Save the current exception state. This takes a snapshot of cx's current
2793 * exception state without making any change to that state.
2795 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
2796 * (to restore that saved state, overriding any more recent state) or else to
2797 * JS_DropExceptionState (to free the state struct in case it is not correct
2798 * or desirable to restore it). Both Restore and Drop free the state struct,
2799 * so callers must stop using the pointer returned from Save after calling the
2800 * Release or Drop API.
2802 extern JS_PUBLIC_API(JSExceptionState
*)
2803 JS_SaveExceptionState(JSContext
*cx
);
2805 extern JS_PUBLIC_API(void)
2806 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2808 extern JS_PUBLIC_API(void)
2809 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
2812 * If the given value is an exception object that originated from an error,
2813 * the exception will contain an error report struct, and this API will return
2814 * the address of that struct. Otherwise, it returns NULL. The lifetime of
2815 * the error report struct that might be returned is the same as the lifetime
2816 * of the exception object.
2818 extern JS_PUBLIC_API(JSErrorReport
*)
2819 JS_ErrorFromException(JSContext
*cx
, jsval v
);
2822 * Given a reported error's message and JSErrorReport struct pointer, throw
2823 * the corresponding exception on cx.
2825 extern JS_PUBLIC_API(JSBool
)
2826 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
2827 JSErrorReport
*reportp
);
2830 * Throws a StopIteration exception on cx.
2832 extern JS_PUBLIC_API(JSBool
)
2833 JS_ThrowStopIteration(JSContext
*cx
);
2836 * Associate the current thread with the given context. This is done
2837 * implicitly by JS_NewContext.
2839 * Returns the old thread id for this context, which should be treated as
2840 * an opaque value. This value is provided for comparison to 0, which
2841 * indicates that ClearContextThread has been called on this context
2842 * since the last SetContextThread, or non-0, which indicates the opposite.
2844 extern JS_PUBLIC_API(jsword
)
2845 JS_GetContextThread(JSContext
*cx
);
2847 extern JS_PUBLIC_API(jsword
)
2848 JS_SetContextThread(JSContext
*cx
);
2850 extern JS_PUBLIC_API(jsword
)
2851 JS_ClearContextThread(JSContext
*cx
);
2853 /************************************************************************/
2856 #define JS_GC_ZEAL 1
2860 extern JS_PUBLIC_API(void)
2861 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
2866 #endif /* jsapi_h___ */