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"
54 /* Well-known JS values, initialized on startup. */
56 extern JS_PUBLIC_DATA(jsval
) JSVAL_NULL
;
57 extern JS_PUBLIC_DATA(jsval
) JSVAL_ZERO
;
58 extern JS_PUBLIC_DATA(jsval
) JSVAL_ONE
;
59 extern JS_PUBLIC_DATA(jsval
) JSVAL_FALSE
;
60 extern JS_PUBLIC_DATA(jsval
) JSVAL_TRUE
;
61 extern JS_PUBLIC_DATA(jsval
) JSVAL_VOID
;
63 # define JSVAL_NULL BUILD_JSVAL(JSVAL_TAG_NULL, 0)
64 # define JSVAL_ZERO BUILD_JSVAL(JSVAL_TAG_INT32, 0)
65 # define JSVAL_ONE BUILD_JSVAL(JSVAL_TAG_INT32, 1)
66 # define JSVAL_FALSE BUILD_JSVAL(JSVAL_TAG_BOOLEAN, JS_FALSE)
67 # define JSVAL_TRUE BUILD_JSVAL(JSVAL_TAG_BOOLEAN, JS_TRUE)
68 # define JSVAL_VOID BUILD_JSVAL(JSVAL_TAG_UNDEFINED, 0)
71 static JS_ALWAYS_INLINE JSBool
72 JSVAL_IS_NULL(jsval v
)
75 l
.asBits
= JSVAL_BITS(v
);
76 return JSVAL_IS_NULL_IMPL(l
);
79 static JS_ALWAYS_INLINE JSBool
80 JSVAL_IS_VOID(jsval v
)
83 l
.asBits
= JSVAL_BITS(v
);
84 return JSVAL_IS_UNDEFINED_IMPL(l
);
87 static JS_ALWAYS_INLINE JSBool
91 l
.asBits
= JSVAL_BITS(v
);
92 return JSVAL_IS_INT32_IMPL(l
);
95 static JS_ALWAYS_INLINE jsint
99 JS_ASSERT(JSVAL_IS_INT(v
));
100 l
.asBits
= JSVAL_BITS(v
);
101 return JSVAL_TO_INT32_IMPL(l
);
104 #define JSVAL_INT_BITS 32
105 #define JSVAL_INT_MIN ((jsint)0x80000000)
106 #define JSVAL_INT_MAX ((jsint)0x7fffffff)
108 static JS_ALWAYS_INLINE jsval
109 INT_TO_JSVAL(int32 i
)
111 return IMPL_TO_JSVAL(INT32_TO_JSVAL_IMPL(i
));
114 static JS_ALWAYS_INLINE JSBool
115 JSVAL_IS_DOUBLE(jsval v
)
118 l
.asBits
= JSVAL_BITS(v
);
119 return JSVAL_IS_DOUBLE_IMPL(l
);
122 static JS_ALWAYS_INLINE jsdouble
123 JSVAL_TO_DOUBLE(jsval v
)
126 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
127 l
.asBits
= JSVAL_BITS(v
);
131 static JS_ALWAYS_INLINE jsval
132 DOUBLE_TO_JSVAL(jsdouble d
)
134 return IMPL_TO_JSVAL(DOUBLE_TO_JSVAL_IMPL(d
));
137 static JS_ALWAYS_INLINE jsval
138 UINT_TO_JSVAL(uint32 i
)
140 if (i
<= JSVAL_INT_MAX
)
141 return INT_TO_JSVAL((int32
)i
);
142 return DOUBLE_TO_JSVAL((jsdouble
)i
);
145 static JS_ALWAYS_INLINE JSBool
146 JSVAL_IS_NUMBER(jsval v
)
149 l
.asBits
= JSVAL_BITS(v
);
150 return JSVAL_IS_NUMBER_IMPL(l
);
153 static JS_ALWAYS_INLINE JSBool
154 JSVAL_IS_STRING(jsval v
)
157 l
.asBits
= JSVAL_BITS(v
);
158 return JSVAL_IS_STRING_IMPL(l
);
161 static JS_ALWAYS_INLINE JSString
*
162 JSVAL_TO_STRING(jsval v
)
165 JS_ASSERT(JSVAL_IS_STRING(v
));
166 l
.asBits
= JSVAL_BITS(v
);
167 return JSVAL_TO_STRING_IMPL(l
);
170 static JS_ALWAYS_INLINE jsval
171 STRING_TO_JSVAL(JSString
*str
)
173 return IMPL_TO_JSVAL(STRING_TO_JSVAL_IMPL(str
));
176 static JS_ALWAYS_INLINE JSBool
177 JSVAL_IS_OBJECT(jsval v
)
180 l
.asBits
= JSVAL_BITS(v
);
181 return JSVAL_IS_OBJECT_OR_NULL_IMPL(l
);
184 static JS_ALWAYS_INLINE JSObject
*
185 JSVAL_TO_OBJECT(jsval v
)
188 JS_ASSERT(JSVAL_IS_OBJECT(v
));
189 l
.asBits
= JSVAL_BITS(v
);
190 return JSVAL_TO_OBJECT_IMPL(l
);
193 static JS_ALWAYS_INLINE jsval
194 OBJECT_TO_JSVAL(JSObject
*obj
)
199 type
= JS_OBJ_IS_FUN_IMPL(obj
) ? JSVAL_TYPE_FUNOBJ
: JSVAL_TYPE_NONFUNOBJ
;
200 return IMPL_TO_JSVAL(OBJECT_TO_JSVAL_IMPL(type
, obj
));
203 static JS_ALWAYS_INLINE JSBool
204 JSVAL_IS_BOOLEAN(jsval v
)
207 l
.asBits
= JSVAL_BITS(v
);
208 return JSVAL_IS_BOOLEAN_IMPL(l
);
211 static JS_ALWAYS_INLINE JSBool
212 JSVAL_TO_BOOLEAN(jsval v
)
215 JS_ASSERT(JSVAL_IS_BOOLEAN(v
));
216 l
.asBits
= JSVAL_BITS(v
);
217 return JSVAL_TO_BOOLEAN_IMPL(l
);
220 static JS_ALWAYS_INLINE jsval
221 BOOLEAN_TO_JSVAL(JSBool b
)
223 return IMPL_TO_JSVAL(BOOLEAN_TO_JSVAL_IMPL(b
));
226 static JS_ALWAYS_INLINE JSBool
227 JSVAL_IS_PRIMITIVE(jsval v
)
230 l
.asBits
= JSVAL_BITS(v
);
231 return JSVAL_IS_PRIMITIVE_IMPL(l
);
234 static JS_ALWAYS_INLINE JSBool
235 JSVAL_IS_GCTHING(jsval v
)
238 l
.asBits
= JSVAL_BITS(v
);
239 return JSVAL_IS_GCTHING_IMPL(l
);
242 static JS_ALWAYS_INLINE
void *
243 JSVAL_TO_GCTHING(jsval v
)
246 JS_ASSERT(JSVAL_IS_GCTHING(v
));
247 l
.asBits
= JSVAL_BITS(v
);
248 return JSVAL_TO_GCTHING_IMPL(l
);
251 /* To be GC-safe, privates are tagged as doubles. */
253 static JS_ALWAYS_INLINE jsval
254 PRIVATE_TO_JSVAL(void *ptr
)
256 return IMPL_TO_JSVAL(PRIVATE_PTR_TO_JSVAL_IMPL(ptr
));
259 static JS_ALWAYS_INLINE
void *
260 JSVAL_TO_PRIVATE(jsval v
)
263 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
264 l
.asBits
= JSVAL_BITS(v
);
265 return JSVAL_TO_PRIVATE_PTR_IMPL(l
);
268 static JS_ALWAYS_INLINE JSBool
269 JSVAL_IS_UNDERLYING_TYPE_OF_PRIVATE(jsval v
)
272 l
.asBits
= JSVAL_BITS(v
);
273 return JSVAL_IS_UNDERLYING_TYPE_OF_PRIVATE_IMPL(l
);
276 /************************************************************************/
279 * A jsid is an identifier for a property or method of an object which is
280 * either a 31-bit signed integer, interned string or object. If XML is
281 * enabled, there is an additional singleton jsid value; see
282 * JS_DEFAULT_XML_NAMESPACE_ID below. Finally, there is an additional jsid
283 * value, JSID_VOID, which does not occur in JS scripts but may be used to
284 * indicate the absence of a valid jsid.
286 * A jsid is not implicitly convertible to or from a jsval; JS_ValueToId or
287 * JS_IdToValue must be used instead.
290 #define JSID_STRING_TYPE 0x0
291 #define JSID_INT_TYPE 0x1
292 #define JSID_OBJECT_TYPE 0x2
293 #define JSID_VOID_TYPE 0x4
294 #define JSID_DEFAULT_XML_NAMESPACE_TYPE 0x6
295 #define JSID_TYPE_MASK 0x7
298 * Do not use canonical 'id' for jsid parameters since this is a magic word in
299 * Objective-C++ which, apparently, wants to be able to #include jsapi.h.
302 static JS_ALWAYS_INLINE JSBool
303 JSID_IS_STRING(jsid iden
)
305 return (JSID_BITS(iden
) & JSID_TYPE_MASK
) == 0;
308 static JS_ALWAYS_INLINE JSString
*
309 JSID_TO_STRING(jsid iden
)
311 JS_ASSERT(JSID_IS_STRING(iden
));
312 return (JSString
*)(JSID_BITS(iden
));
315 JS_PUBLIC_API(JSBool
)
316 JS_StringHasBeenInterned(JSString
*str
);
318 static JS_ALWAYS_INLINE jsid
319 INTERNED_STRING_TO_JSID(JSString
*str
)
322 JS_ASSERT(JS_StringHasBeenInterned(str
));
323 JS_ASSERT(((size_t)str
& JSID_TYPE_MASK
) == 0);
324 JSID_BITS(iden
) = (size_t)str
;
328 static JS_ALWAYS_INLINE JSBool
329 JSID_IS_INT(jsid iden
)
331 return !!(JSID_BITS(iden
) & JSID_INT_TYPE
);
334 static JS_ALWAYS_INLINE int32
335 JSID_TO_INT(jsid iden
)
337 JS_ASSERT(JSID_IS_INT(iden
));
338 return ((int32
)JSID_BITS(iden
)) >> 1;
341 #define JSID_INT_MIN (-(1 << 30))
342 #define JSID_INT_MAX ((1 << 30) - 1)
344 static JS_ALWAYS_INLINE JSBool
345 INT_FITS_IN_JSID(int32 i
)
347 return ((jsuint
)(i
) - (jsuint
)JSID_INT_MIN
<=
348 (jsuint
)(JSID_INT_MAX
- JSID_INT_MIN
));
351 static JS_ALWAYS_INLINE jsid
355 JS_ASSERT(INT_FITS_IN_JSID(i
));
356 JSID_BITS(iden
) = ((i
<< 1) | JSID_INT_TYPE
);
360 static JS_ALWAYS_INLINE JSBool
361 JSID_IS_OBJECT(jsid iden
)
363 return (JSID_BITS(iden
) & JSID_TYPE_MASK
) == JSID_OBJECT_TYPE
;
366 static JS_ALWAYS_INLINE JSObject
*
367 JSID_TO_OBJECT(jsid iden
)
369 JS_ASSERT(JSID_IS_OBJECT(iden
));
370 return (JSObject
*)(JSID_BITS(iden
) & ~(size_t)JSID_TYPE_MASK
);
373 static JS_ALWAYS_INLINE jsid
374 OBJECT_TO_JSID(JSObject
*obj
)
377 JS_ASSERT(obj
!= NULL
);
378 JS_ASSERT(((size_t)obj
& JSID_TYPE_MASK
) == 0);
379 JSID_BITS(iden
) = ((size_t)obj
| JSID_OBJECT_TYPE
);
383 static JS_ALWAYS_INLINE JSBool
384 JSID_IS_GCTHING(jsid iden
)
386 return JSID_IS_STRING(iden
) || JSID_IS_OBJECT(iden
);
389 static JS_ALWAYS_INLINE
void *
390 JSID_TO_GCTHING(jsid iden
)
392 return (void *)(JSID_BITS(iden
) & ~(size_t)JSID_TYPE_MASK
);
396 * The magic XML namespace id is not a valid jsid. Global object classes in
397 * embeddings that enable JS_HAS_XML_SUPPORT (E4X) should handle this id.
400 static JS_ALWAYS_INLINE JSBool
401 JSID_IS_DEFAULT_XML_NAMESPACE(jsid iden
)
403 JS_ASSERT_IF(((size_t)JSID_BITS(iden
) & JSID_TYPE_MASK
) == JSID_DEFAULT_XML_NAMESPACE_TYPE
,
404 JSID_BITS(iden
) == JSID_DEFAULT_XML_NAMESPACE_TYPE
);
405 return ((size_t)JSID_BITS(iden
) == JSID_DEFAULT_XML_NAMESPACE_TYPE
);
408 static JS_ALWAYS_INLINE jsid
409 JSID_DEFAULT_XML_NAMESPACE()
412 JSID_BITS(iden
) = JSID_DEFAULT_XML_NAMESPACE_TYPE
;
417 * A void jsid is not a valid id and only arises as an exceptional API return
418 * value, such as in JS_NextProperty.
421 static JS_ALWAYS_INLINE JSBool
422 JSID_IS_VOID(jsid iden
)
424 JS_ASSERT_IF(((size_t)JSID_BITS(iden
) & JSID_TYPE_MASK
) == JSID_VOID_TYPE
,
425 JSID_BITS(iden
) == JSID_VOID_TYPE
);
426 return ((size_t)JSID_BITS(iden
) == JSID_VOID_TYPE
);
430 extern JS_PUBLIC_DATA(jsid
) JSID_VOID
;
432 # define JSID_VOID ((jsid)JSID_VOID_TYPE)
435 /************************************************************************/
437 /* Lock and unlock the GC thing held by a jsval. */
438 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
439 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
441 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
442 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
445 /* Property attributes, set in JSPropertySpec and passed to API functions. */
446 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
447 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
448 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
449 #define JSPROP_GETTER 0x10 /* property holds getter function */
450 #define JSPROP_SETTER 0x20 /* property holds setter function */
451 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
452 property; don't copy the property on
453 set of the same-named property in an
454 object that delegates to a prototype
455 containing this property */
456 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
457 #define JSPROP_SHORTID 0x100 /* set in JSPropertyDescriptor.attrs
458 if getters/setters use a shortid */
460 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
461 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
462 #define JSFUN_GETTER JSPROP_GETTER
463 #define JSFUN_SETTER JSPROP_SETTER
464 #define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
465 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
467 #define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
468 #define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
470 #define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
471 #define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
472 #define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
473 #define JSFUN_HEAVYWEIGHT_TEST(f) (!!((f) & JSFUN_HEAVYWEIGHT))
475 #define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
477 #define JSFUN_THISP_FLAGS(f) (f)
478 #define JSFUN_THISP_TEST(f,t) ((f) & t)
480 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
481 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
482 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
483 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
485 #define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
487 #define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
488 bits 12-15 are used internally to
489 flag interpreted functions */
491 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
492 instead of defaulting to class gsops
493 for property holding function */
496 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
497 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
498 * methods of a class prototype that are exposed as static methods taking an
499 * extra leading argument: the generic |this| parameter.
501 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
502 * that struct must live at least as long as the native static method object
503 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
504 * JSFunctionSpec structs are allocated in static arrays.
506 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
509 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
510 * comment in jstypes.h regarding safe int64 usage.
512 extern JS_PUBLIC_API(int64
)
515 /* Don't want to export data, so provide accessors for non-inline jsvals. */
516 extern JS_PUBLIC_API(jsval
)
517 JS_GetNaNValue(JSContext
*cx
);
519 extern JS_PUBLIC_API(jsval
)
520 JS_GetNegativeInfinityValue(JSContext
*cx
);
522 extern JS_PUBLIC_API(jsval
)
523 JS_GetPositiveInfinityValue(JSContext
*cx
);
525 extern JS_PUBLIC_API(jsval
)
526 JS_GetEmptyStringValue(JSContext
*cx
);
529 * Format is a string of the following characters (spaces are insignificant),
530 * specifying the tabulated type conversions:
533 * c uint16/jschar ECMA uint16, Unicode char
535 * u uint32 ECMA uint32
536 * j int32 Rounded int32 (coordinate)
537 * d jsdouble IEEE double
538 * I jsdouble Integral IEEE double
540 * S JSString * Unicode string, accessed by a JSString pointer
541 * W jschar * Unicode character vector, 0-terminated (W for wide)
542 * o JSObject * Object reference
543 * f JSFunction * Function private
544 * v jsval Argument value (no conversion)
545 * * N/A Skip this argument (no vararg)
546 * / N/A End of required arguments
548 * The variable argument list after format must consist of &b, &c, &s, e.g.,
549 * where those variables have the types given above. For the pointer types
550 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
551 * to the JS runtime, not to the calling native code. The runtime promises
552 * to keep this memory valid so long as argv refers to allocated stack space
553 * (so long as the native function is active).
555 * Fewer arguments than format specifies may be passed only if there is a /
556 * in format after the last required argument specifier and argc is at least
557 * the number of required arguments. More arguments than format specifies
558 * may be passed without error; it is up to the caller to deal with trailing
559 * unconverted arguments.
561 extern JS_PUBLIC_API(JSBool
)
562 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
566 extern JS_PUBLIC_API(JSBool
)
567 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
568 const char *format
, va_list ap
);
571 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
574 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
575 * The handler function has this signature (see jspubtd.h):
577 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
578 * JSBool fromJS, jsval **vpp, va_list *app);
580 * It should return true on success, and return false after reporting an error
581 * or detecting an already-reported error.
583 * For a given format string, for example "AA", the formatter is called from
584 * JS_ConvertArgumentsVA like so:
586 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
588 * sp points into the arguments array on the JS stack, while ap points into
589 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
590 * the formatter to convert zero or more jsvals at sp to zero or more C values
591 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
592 * (via *app) to point past the converted arguments and their result pointers
595 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
597 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
599 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
600 * format specifier and store them at sp, updating ap and sp appropriately.
602 * The "..." after "AA" is the rest of the format string that was passed into
603 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
604 * in each Convert or PushArguments call is passed to the formatter, so that
605 * one such function may implement several formats, in order to share code.
607 * Remove just forgets about any handler associated with format. Add does not
608 * copy format, it points at the string storage allocated by the caller, which
609 * is typically a string constant. If format is in dynamic storage, it is up
610 * to the caller to keep the string alive until Remove is called.
612 extern JS_PUBLIC_API(JSBool
)
613 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
614 JSArgumentFormatter formatter
);
616 extern JS_PUBLIC_API(void)
617 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
619 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
621 extern JS_PUBLIC_API(JSBool
)
622 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
624 extern JS_PUBLIC_API(JSBool
)
625 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
627 extern JS_PUBLIC_API(JSFunction
*)
628 JS_ValueToFunction(JSContext
*cx
, jsval v
);
630 extern JS_PUBLIC_API(JSFunction
*)
631 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
633 extern JS_PUBLIC_API(JSString
*)
634 JS_ValueToString(JSContext
*cx
, jsval v
);
636 extern JS_PUBLIC_API(JSString
*)
637 JS_ValueToSource(JSContext
*cx
, jsval v
);
639 extern JS_PUBLIC_API(JSBool
)
640 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
642 extern JS_PUBLIC_API(JSBool
)
643 JS_DoubleIsInt32(jsdouble d
, jsint
*ip
);
646 * Convert a value to a number, then to an int32, according to the ECMA rules
649 extern JS_PUBLIC_API(JSBool
)
650 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
653 * Convert a value to a number, then to a uint32, according to the ECMA rules
656 extern JS_PUBLIC_API(JSBool
)
657 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
660 * Convert a value to a number, then to an int32 if it fits by rounding to
661 * nearest; but failing with an error report if the double is out of range
664 extern JS_PUBLIC_API(JSBool
)
665 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
668 * ECMA ToUint16, for mapping a jsval to a Unicode point.
670 extern JS_PUBLIC_API(JSBool
)
671 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
673 extern JS_PUBLIC_API(JSBool
)
674 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
676 extern JS_PUBLIC_API(JSType
)
677 JS_TypeOfValue(JSContext
*cx
, jsval v
);
679 extern JS_PUBLIC_API(const char *)
680 JS_GetTypeName(JSContext
*cx
, JSType type
);
682 extern JS_PUBLIC_API(JSBool
)
683 JS_StrictlyEqual(JSContext
*cx
, jsval v1
, jsval v2
);
685 extern JS_PUBLIC_API(JSBool
)
686 JS_SameValue(JSContext
*cx
, jsval v1
, jsval v2
);
688 /************************************************************************/
691 * Initialization, locking, contexts, and memory allocation.
693 * It is important that the first runtime and first context be created in a
694 * single-threaded fashion, otherwise the behavior of the library is undefined.
695 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
697 #define JS_NewRuntime JS_Init
698 #define JS_DestroyRuntime JS_Finish
699 #define JS_LockRuntime JS_Lock
700 #define JS_UnlockRuntime JS_Unlock
702 extern JS_PUBLIC_API(JSRuntime
*)
703 JS_NewRuntime(uint32 maxbytes
);
705 extern JS_PUBLIC_API(void)
706 JS_CommenceRuntimeShutDown(JSRuntime
*rt
);
708 extern JS_PUBLIC_API(void)
709 JS_DestroyRuntime(JSRuntime
*rt
);
711 extern JS_PUBLIC_API(void)
714 JS_PUBLIC_API(void *)
715 JS_GetRuntimePrivate(JSRuntime
*rt
);
718 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
720 extern JS_PUBLIC_API(void)
721 JS_BeginRequest(JSContext
*cx
);
723 extern JS_PUBLIC_API(void)
724 JS_EndRequest(JSContext
*cx
);
726 /* Yield to pending GC operations, regardless of request depth */
727 extern JS_PUBLIC_API(void)
728 JS_YieldRequest(JSContext
*cx
);
730 extern JS_PUBLIC_API(jsrefcount
)
731 JS_SuspendRequest(JSContext
*cx
);
733 extern JS_PUBLIC_API(void)
734 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
736 extern JS_PUBLIC_API(void)
737 JS_TransferRequest(JSContext
*cx
, JSContext
*another
);
742 class JSAutoRequest
{
744 JSAutoRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
745 : mContext(cx
), mSaveDepth(0) {
746 JS_GUARD_OBJECT_NOTIFIER_INIT
;
747 JS_BeginRequest(mContext
);
750 JS_EndRequest(mContext
);
754 mSaveDepth
= JS_SuspendRequest(mContext
);
757 JS_ResumeRequest(mContext
, mSaveDepth
);
762 jsrefcount mSaveDepth
;
763 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
767 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
768 static void operator delete(void *, size_t) { };
772 class JSAutoSuspendRequest
{
774 JSAutoSuspendRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
775 : mContext(cx
), mSaveDepth(0) {
776 JS_GUARD_OBJECT_NOTIFIER_INIT
;
778 mSaveDepth
= JS_SuspendRequest(mContext
);
781 ~JSAutoSuspendRequest() {
787 JS_ResumeRequest(mContext
, mSaveDepth
);
794 jsrefcount mSaveDepth
;
795 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
799 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
800 static void operator delete(void *, size_t) { };
804 class JSAutoTransferRequest
807 JSAutoTransferRequest(JSContext
* cx1
, JSContext
* cx2
)
808 : cx1(cx1
), cx2(cx2
) {
810 JS_TransferRequest(cx1
, cx2
);
812 ~JSAutoTransferRequest() {
814 JS_TransferRequest(cx2
, cx1
);
817 JSContext
* const cx1
;
818 JSContext
* const cx2
;
821 JSAutoTransferRequest(JSAutoTransferRequest
&);
822 void operator =(JSAutoTransferRequest
&);
828 extern JS_PUBLIC_API(void)
829 JS_Lock(JSRuntime
*rt
);
831 extern JS_PUBLIC_API(void)
832 JS_Unlock(JSRuntime
*rt
);
834 extern JS_PUBLIC_API(JSContextCallback
)
835 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
837 extern JS_PUBLIC_API(JSContext
*)
838 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
840 extern JS_PUBLIC_API(void)
841 JS_DestroyContext(JSContext
*cx
);
843 extern JS_PUBLIC_API(void)
844 JS_DestroyContextNoGC(JSContext
*cx
);
846 extern JS_PUBLIC_API(void)
847 JS_DestroyContextMaybeGC(JSContext
*cx
);
849 extern JS_PUBLIC_API(void *)
850 JS_GetContextPrivate(JSContext
*cx
);
852 extern JS_PUBLIC_API(void)
853 JS_SetContextPrivate(JSContext
*cx
, void *data
);
855 extern JS_PUBLIC_API(JSRuntime
*)
856 JS_GetRuntime(JSContext
*cx
);
858 extern JS_PUBLIC_API(JSContext
*)
859 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
861 extern JS_PUBLIC_API(JSVersion
)
862 JS_GetVersion(JSContext
*cx
);
864 extern JS_PUBLIC_API(JSVersion
)
865 JS_SetVersion(JSContext
*cx
, JSVersion version
);
867 extern JS_PUBLIC_API(const char *)
868 JS_VersionToString(JSVersion version
);
870 extern JS_PUBLIC_API(JSVersion
)
871 JS_StringToVersion(const char *string
);
874 * JS options are orthogonal to version, and may be freely composed with one
875 * another as well as with version.
877 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
878 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
880 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
881 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
882 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
883 the last object on its 'obj'
884 param's scope chain as the
885 ECMA 'variables object' */
886 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
887 JS_BIT(3) /* context private data points
888 to an nsISupports subclass */
889 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
890 promises to execute compiled
891 script once only; enables
892 compile-time scope chain
893 resolution of consts. */
894 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
895 option supported for the
896 XUL preprocessor and kindred
898 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
899 parse <!-- --> as a token,
900 not backward compatible with
901 the comment-hiding hack used
902 in HTML script tags. */
903 #define JSOPTION_DONT_REPORT_UNCAUGHT \
904 JS_BIT(8) /* When returning from the
905 outermost API call, prevent
906 uncaught exceptions from
907 being converted to error
910 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
911 regular expression which
912 backtracks more than n^3
913 times, where n is length
914 of the input string */
915 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
916 statement context per
917 ECMA-262 Edition 3. */
919 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
921 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
922 that a null rval out-param
923 will be passed to each call
924 to JS_ExecuteScript. */
925 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
926 contexts' global objects
927 (see JS_GetGlobalObject),
928 leaving that up to the
931 #define JSOPTION_METHODJIT JS_BIT(14) /* Whole-method JIT. */
933 extern JS_PUBLIC_API(uint32
)
934 JS_GetOptions(JSContext
*cx
);
936 extern JS_PUBLIC_API(uint32
)
937 JS_SetOptions(JSContext
*cx
, uint32 options
);
939 extern JS_PUBLIC_API(uint32
)
940 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
942 extern JS_PUBLIC_API(const char *)
943 JS_GetImplementationVersion(void);
945 extern JS_PUBLIC_API(JSObject
*)
946 JS_GetGlobalObject(JSContext
*cx
);
948 extern JS_PUBLIC_API(void)
949 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
952 * Initialize standard JS class constructors, prototypes, and any top-level
953 * functions and constants associated with the standard classes (e.g. isNaN
956 * NB: This sets cx's global object to obj if it was null.
958 extern JS_PUBLIC_API(JSBool
)
959 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
962 * Resolve id, which must contain either a string or an int, to a standard
963 * class name in obj if possible, defining the class's constructor and/or
964 * prototype and storing true in *resolved. If id does not name a standard
965 * class or a top-level property induced by initializing a standard class,
966 * store false in *resolved and just return true. Return false on error,
967 * as usual for JSBool result-typed API entry points.
969 * This API can be called directly from a global object class's resolve op,
970 * to define standard classes lazily. The class's enumerate op should call
971 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
972 * loops any classes not yet resolved lazily.
974 extern JS_PUBLIC_API(JSBool
)
975 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsid id
,
978 extern JS_PUBLIC_API(JSBool
)
979 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
982 * Enumerate any already-resolved standard class ids into ida, or into a new
983 * JSIdArray if ida is null. Return the augmented array on success, null on
984 * failure with ida (if it was non-null on entry) destroyed.
986 extern JS_PUBLIC_API(JSIdArray
*)
987 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
990 extern JS_PUBLIC_API(JSBool
)
991 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
994 extern JS_PUBLIC_API(JSObject
*)
995 JS_GetScopeChain(JSContext
*cx
);
997 extern JS_PUBLIC_API(JSObject
*)
998 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
1000 extern JS_PUBLIC_API(JSObject
*)
1001 JS_GetGlobalForScopeChain(JSContext
*cx
);
1003 #ifdef JS_HAS_CTYPES
1005 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
1006 * object will be sealed.
1008 extern JS_PUBLIC_API(JSBool
)
1009 JS_InitCTypesClass(JSContext
*cx
, JSObject
*global
);
1013 * Macros to hide interpreter stack layout details from a JSFastNative using
1014 * its jsval *vp parameter. The stack layout underlying invocation can't change
1015 * without breaking source and binary compatibility (argv[-2] is well-known to
1016 * be the callee jsval, and argv[-1] is as well known to be |this|).
1018 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
1019 * is the global object, so embeddings implementing fast natives *must* call
1020 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
1021 * which should propagate as a false return from native functions and hooks.
1023 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
1024 * handle a null obj parameter by returning false (throwing a TypeError if
1025 * given non-null argv), so most native functions that type-check their |this|
1026 * parameter need not add null checking.
1028 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
1029 * methods that may inspect their callee must defer setting their return value
1030 * until after any such possible inspection. Otherwise the return value will be
1031 * inspected instead of the callee function object.
1033 * WARNING: These are not (yet) mandatory macros, but new code outside of the
1034 * engine should use them. In the Mozilla 2.0 milestone their definitions may
1035 * change incompatibly.
1037 #define JS_CALLEE(cx,vp) ((vp)[0])
1038 #define JS_ARGV_CALLEE(argv) ((argv)[-2])
1039 #define JS_THIS_OBJECT(cx,vp) (JSVAL_TO_OBJECT(JS_THIS(cx,vp)))
1040 #define JS_ARGV(cx,vp) ((vp) + 2)
1041 #define JS_RVAL(cx,vp) (*(vp))
1042 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
1044 extern JS_PUBLIC_API(jsval
)
1045 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
1048 JS_THIS(JSContext
*cx
, jsval
*vp
)
1050 return JSVAL_IS_PRIMITIVE(vp
[1]) ? JS_ComputeThis(cx
, vp
) : vp
[1];
1053 extern JS_PUBLIC_API(void *)
1054 JS_malloc(JSContext
*cx
, size_t nbytes
);
1056 extern JS_PUBLIC_API(void *)
1057 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
1059 extern JS_PUBLIC_API(void)
1060 JS_free(JSContext
*cx
, void *p
);
1062 extern JS_PUBLIC_API(void)
1063 JS_updateMallocCounter(JSContext
*cx
, size_t nbytes
);
1065 extern JS_PUBLIC_API(char *)
1066 JS_strdup(JSContext
*cx
, const char *s
);
1068 extern JS_PUBLIC_API(JSBool
)
1069 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
1072 * A GC root is a pointer to a jsval, JSObject * or JSString * that itself
1073 * points into the GC heap. JS_AddValueRoot takes a pointer to a jsval and
1074 * JS_AddGCThingRoot takes a pointer to a JSObject * or JString *.
1076 * Note that, since JS_Add*Root stores the address of a variable (of type
1077 * jsval, JSString *, or JSObject *), that variable must live until
1078 * JS_Remove*Root is called to remove that variable. For example, after:
1080 * void some_function() {
1082 * JS_AddNamedRootedValue(cx, &v, "name");
1084 * the caller must perform
1086 * JS_RemoveRootedValue(cx, &v);
1088 * before some_function() returns.
1090 * Also, use JS_AddNamed*Root(cx, &structPtr->memberObj, "structPtr->memberObj")
1091 * in preference to JS_Add*Root(cx, &structPtr->memberObj), in order to identify
1092 * roots by their source callsites. This way, you can find the callsite while
1093 * debugging if you should fail to do JS_Remove*Root(cx, &structPtr->memberObj)
1094 * before freeing structPtr's memory.
1096 extern JS_PUBLIC_API(JSBool
)
1097 JS_AddValueRoot(JSContext
*cx
, jsval
*vp
);
1099 extern JS_PUBLIC_API(JSBool
)
1100 JS_AddStringRoot(JSContext
*cx
, JSString
**rp
);
1102 extern JS_PUBLIC_API(JSBool
)
1103 JS_AddObjectRoot(JSContext
*cx
, JSObject
**rp
);
1105 extern JS_PUBLIC_API(JSBool
)
1106 JS_AddGCThingRoot(JSContext
*cx
, void **rp
);
1108 #ifdef NAME_ALL_GC_ROOTS
1109 #define JS_DEFINE_TO_TOKEN(def) #def
1110 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
1111 #define JS_AddValueRoot(cx,vp) JS_AddNamedValueRoot((cx), (vp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1112 #define JS_AddStringRoot(cx,rp) JS_AddNamedStringRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1113 #define JS_AddObjectRoot(cx,rp) JS_AddNamedObjectRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1116 extern JS_PUBLIC_API(JSBool
)
1117 JS_AddNamedValueRoot(JSContext
*cx
, jsval
*vp
, const char *name
);
1119 extern JS_PUBLIC_API(JSBool
)
1120 JS_AddNamedStringRoot(JSContext
*cx
, JSString
**rp
, const char *name
);
1122 extern JS_PUBLIC_API(JSBool
)
1123 JS_AddNamedObjectRoot(JSContext
*cx
, JSObject
**rp
, const char *name
);
1125 extern JS_PUBLIC_API(JSBool
)
1126 JS_AddNamedGCThingRoot(JSContext
*cx
, void **rp
, const char *name
);
1128 extern JS_PUBLIC_API(JSBool
)
1129 JS_RemoveValueRoot(JSContext
*cx
, jsval
*vp
);
1131 extern JS_PUBLIC_API(JSBool
)
1132 JS_RemoveStringRoot(JSContext
*cx
, JSString
**rp
);
1134 extern JS_PUBLIC_API(JSBool
)
1135 JS_RemoveObjectRoot(JSContext
*cx
, JSObject
**rp
);
1137 extern JS_PUBLIC_API(JSBool
)
1138 JS_RemoveGCThingRoot(JSContext
*cx
, void **rp
);
1140 /* TODO: remove these APIs */
1142 extern JS_FRIEND_API(JSBool
)
1143 js_AddRootRT(JSRuntime
*rt
, jsval
*vp
, const char *name
);
1145 extern JS_FRIEND_API(JSBool
)
1146 js_AddGCThingRootRT(JSRuntime
*rt
, void **rp
, const char *name
);
1148 extern JS_FRIEND_API(JSBool
)
1149 js_RemoveRoot(JSRuntime
*rt
, void *rp
);
1152 * The last GC thing of each type (object, string, double, external string
1153 * types) created on a given context is kept alive until another thing of the
1154 * same type is created, using a newborn root in the context. These newborn
1155 * roots help native code protect newly-created GC-things from GC invocations
1156 * activated before those things can be rooted using local or global roots.
1158 * However, the newborn roots can also entrain great gobs of garbage, so the
1159 * JS_GC entry point clears them for the context on which GC is being forced.
1160 * Embeddings may need to do likewise for all contexts.
1162 * See the scoped local root API immediately below for a better way to manage
1163 * newborns in cases where native hooks (functions, getters, setters, etc.)
1164 * create many GC-things, potentially without connecting them to predefined
1165 * local roots such as *rval or argv[i] in an active native function. Using
1166 * JS_EnterLocalRootScope disables updating of the context's per-gc-thing-type
1167 * newborn roots, until control flow unwinds and leaves the outermost nesting
1170 extern JS_PUBLIC_API(void)
1171 JS_ClearNewbornRoots(JSContext
*cx
);
1174 * Scoped local root management allows native functions, getter/setters, etc.
1175 * to avoid worrying about the newborn root pigeon-holes, overloading local
1176 * roots allocated in argv and *rval, or ending up having to call JS_Add*Root
1177 * and JS_Remove*Root to manage global roots temporarily.
1179 * Instead, calling JS_EnterLocalRootScope and JS_LeaveLocalRootScope around
1180 * the body of the native hook causes the engine to allocate a local root for
1181 * each newborn created in between the two API calls, using a local root stack
1182 * associated with cx. For example:
1185 * my_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
1189 * if (!JS_EnterLocalRootScope(cx))
1191 * ok = my_GetPropertyBody(cx, obj, id, vp);
1192 * JS_LeaveLocalRootScope(cx);
1196 * NB: JS_LeaveLocalRootScope must be called once for every prior successful
1197 * call to JS_EnterLocalRootScope. If JS_EnterLocalRootScope fails, you must
1198 * not make the matching JS_LeaveLocalRootScope call.
1200 * JS_LeaveLocalRootScopeWithResult(cx, rval) is an alternative way to leave
1201 * a local root scope that protects a result or return value, by effectively
1202 * pushing it in the caller's local root scope.
1204 * In case a native hook allocates many objects or other GC-things, but the
1205 * native protects some of those GC-things by storing them as property values
1206 * in an object that is itself protected, the hook can call JS_ForgetLocalRoot
1207 * to free the local root automatically pushed for the now-protected GC-thing.
1209 * JS_ForgetLocalRoot works on any GC-thing allocated in the current local
1210 * root scope, but it's more time-efficient when called on references to more
1211 * recently created GC-things. Calling it successively on other than the most
1212 * recently allocated GC-thing will tend to average the time inefficiency, and
1213 * may risk O(n^2) growth rate, but in any event, you shouldn't allocate too
1214 * many local roots if you can root as you go (build a tree of objects from
1215 * the top down, forgetting each latest-allocated GC-thing immediately upon
1216 * linking it to its parent).
1218 extern JS_PUBLIC_API(JSBool
)
1219 JS_EnterLocalRootScope(JSContext
*cx
);
1221 extern JS_PUBLIC_API(void)
1222 JS_LeaveLocalRootScope(JSContext
*cx
);
1224 extern JS_PUBLIC_API(void)
1225 JS_LeaveLocalRootScopeWithResult(JSContext
*cx
, jsval rval
);
1227 extern JS_PUBLIC_API(void)
1228 JS_ForgetLocalRoot(JSContext
*cx
, void *thing
);
1233 class JSAutoLocalRootScope
{
1235 JSAutoLocalRootScope(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
1237 JS_GUARD_OBJECT_NOTIFIER_INIT
;
1238 JS_EnterLocalRootScope(mContext
);
1240 ~JSAutoLocalRootScope() {
1241 JS_LeaveLocalRootScope(mContext
);
1244 void forget(void *thing
) {
1245 JS_ForgetLocalRoot(mContext
, thing
);
1249 JSContext
*mContext
;
1250 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1254 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
1255 static void operator delete(void *, size_t) { };
1262 typedef enum JSGCRootType
{
1263 JS_GC_ROOT_VALUE_PTR
,
1264 JS_GC_ROOT_GCTHING_PTR
1268 extern JS_PUBLIC_API(void)
1269 JS_DumpNamedRoots(JSRuntime
*rt
,
1270 void (*dump
)(const char *name
, void *rp
, JSGCRootType type
, void *data
),
1275 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
1276 * The root is pointed at by rp; if the root is unnamed, name is null; data is
1277 * supplied from the third parameter to JS_MapGCRoots.
1279 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
1280 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
1281 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
1282 * constants are flags; you can OR them together.
1284 * This function acquires and releases rt's GC lock around the mapping of the
1285 * roots table, so the map function should run to completion in as few cycles
1286 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
1287 * or any JS API entry point that acquires locks, without double-tripping or
1288 * deadlocking on the GC lock.
1290 * The JSGCRootType parameter indicates whether rp is a pointer to a Value
1291 * (which is obtained by '(Value *)rp') or a pointer to a GC-thing pointer
1292 * (which is obtained by '(void **)rp').
1294 * JS_MapGCRoots returns the count of roots that were successfully mapped.
1296 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
1297 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
1298 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
1301 (* JSGCRootMapFun
)(void *rp
, JSGCRootType type
, const char *name
, void *data
);
1303 extern JS_PUBLIC_API(uint32
)
1304 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
1306 extern JS_PUBLIC_API(JSBool
)
1307 JS_LockGCThing(JSContext
*cx
, void *thing
);
1309 extern JS_PUBLIC_API(JSBool
)
1310 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
1312 extern JS_PUBLIC_API(JSBool
)
1313 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
1315 extern JS_PUBLIC_API(JSBool
)
1316 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
1319 * Register externally maintained GC roots.
1321 * traceOp: the trace operation. For each root the implementation should call
1322 * JS_CallTracer whenever the root contains a traceable thing.
1323 * data: the data argument to pass to each invocation of traceOp.
1325 extern JS_PUBLIC_API(void)
1326 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
1329 * For implementors of JSMarkOp. All new code should implement JSTraceOp
1332 extern JS_PUBLIC_API(void)
1333 JS_MarkGCThing(JSContext
*cx
, jsval v
, const char *name
, void *arg
);
1336 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
1337 * enumerate all references to traceable things reachable via a property or
1338 * other strong ref identified for debugging purposes by name or index or
1339 * a naming callback.
1341 * By definition references to traceable things include non-null pointers
1342 * to JSObject, JSString and jsdouble and corresponding jsvals.
1344 * See the JSTraceOp typedef in jspubtd.h.
1347 /* Trace kinds to pass to JS_Tracing. */
1348 #define JSTRACE_OBJECT 0
1349 #define JSTRACE_STRING 1
1352 * Use the following macros to check if a particular jsval is a traceable
1353 * thing and to extract the thing and its kind to pass to JS_CallTracer.
1355 static JS_ALWAYS_INLINE JSBool
1356 JSVAL_IS_TRACEABLE(jsval v
)
1358 return JSVAL_IS_GCTHING(v
);
1361 static JS_ALWAYS_INLINE
void *
1362 JSVAL_TO_TRACEABLE(jsval v
)
1364 return JSVAL_TO_GCTHING(v
);
1367 static JS_ALWAYS_INLINE uint32
1368 JSVAL_TRACE_KIND(jsval v
)
1371 JS_ASSERT(JSVAL_IS_GCTHING(v
));
1372 l
.asBits
= JSVAL_BITS(v
);
1373 return JSVAL_TRACE_KIND_IMPL(l
);
1378 JSTraceCallback callback
;
1379 JSTraceNamePrinter debugPrinter
;
1380 const void *debugPrintArg
;
1381 size_t debugPrintIndex
;
1385 * The method to call on each reference to a traceable thing stored in a
1386 * particular JSObject or other runtime structure. With DEBUG defined the
1387 * caller before calling JS_CallTracer must initialize JSTracer fields
1388 * describing the reference using the macros below.
1390 extern JS_PUBLIC_API(void)
1391 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
1394 * Set debugging information about a reference to a traceable thing to prepare
1395 * for the following call to JS_CallTracer.
1397 * When printer is null, arg must be const char * or char * C string naming
1398 * the reference and index must be either (size_t)-1 indicating that the name
1399 * alone describes the reference or it must be an index into some array vector
1400 * that stores the reference.
1402 * When printer callback is not null, the arg and index arguments are
1403 * available to the callback as debugPrinterArg and debugPrintIndex fields
1406 * The storage for name or callback's arguments needs to live only until
1407 * the following call to JS_CallTracer returns.
1410 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1412 (trc)->debugPrinter = (printer); \
1413 (trc)->debugPrintArg = (arg); \
1414 (trc)->debugPrintIndex = (index); \
1417 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1423 * Convenience macro to describe the argument of JS_CallTracer using C string
1426 # define JS_SET_TRACING_INDEX(trc, name, index) \
1427 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1430 * Convenience macro to describe the argument of JS_CallTracer using C string.
1432 # define JS_SET_TRACING_NAME(trc, name) \
1433 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1436 * Convenience macro to invoke JS_CallTracer using C string as the name for
1437 * the reference to a traceable thing.
1439 # define JS_CALL_TRACER(trc, thing, kind, name) \
1441 JS_SET_TRACING_NAME(trc, name); \
1442 JS_CallTracer((trc), (thing), (kind)); \
1446 * Convenience macros to invoke JS_CallTracer when jsval represents a
1447 * reference to a traceable thing.
1449 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1451 if (JSVAL_IS_TRACEABLE(val)) { \
1452 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1453 JSVAL_TRACE_KIND(val), name); \
1457 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1459 JSObject *obj_ = (object); \
1461 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1464 #define JS_CALL_STRING_TRACER(trc, string, name) \
1466 JSString *str_ = (string); \
1468 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1472 * API for JSTraceCallback implementations.
1474 # define JS_TRACER_INIT(trc, cx_, callback_) \
1476 (trc)->context = (cx_); \
1477 (trc)->callback = (callback_); \
1478 (trc)->debugPrinter = NULL; \
1479 (trc)->debugPrintArg = NULL; \
1480 (trc)->debugPrintIndex = (size_t)-1; \
1483 extern JS_PUBLIC_API(void)
1484 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1486 extern JS_PUBLIC_API(void)
1487 JS_TraceRuntime(JSTracer
*trc
);
1491 extern JS_PUBLIC_API(void)
1492 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1493 void *thing
, uint32 kind
, JSBool includeDetails
);
1496 * DEBUG-only method to dump the object graph of heap-allocated things.
1498 * fp: file for the dump output.
1499 * start: when non-null, dump only things reachable from start
1500 * thing. Otherwise dump all things reachable from the
1502 * startKind: trace kind of start if start is not null. Must be 0 when
1504 * thingToFind: dump only paths in the object graph leading to thingToFind
1506 * maxDepth: the upper bound on the number of edges to descend from the
1508 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1510 extern JS_PUBLIC_API(JSBool
)
1511 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1512 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1517 * Garbage collector API.
1519 extern JS_PUBLIC_API(void)
1520 JS_GC(JSContext
*cx
);
1522 extern JS_PUBLIC_API(void)
1523 JS_MaybeGC(JSContext
*cx
);
1525 extern JS_PUBLIC_API(JSGCCallback
)
1526 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1528 extern JS_PUBLIC_API(JSGCCallback
)
1529 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1531 extern JS_PUBLIC_API(JSBool
)
1532 JS_IsGCMarkingTracer(JSTracer
*trc
);
1534 extern JS_PUBLIC_API(JSBool
)
1535 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1537 typedef enum JSGCParamKey
{
1538 /* Maximum nominal heap before last ditch GC. */
1541 /* Number of JS_malloc bytes before last ditch GC. */
1542 JSGC_MAX_MALLOC_BYTES
= 1,
1544 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1545 JSGC_STACKPOOL_LIFESPAN
= 2,
1548 * The factor that defines when the GC is invoked. The factor is a
1549 * percent of the memory allocated by the GC after the last run of
1550 * the GC. When the current memory allocated by the GC is more than
1551 * this percent then the GC is invoked. The factor cannot be less
1552 * than 100 since the current memory allocated by the GC cannot be less
1553 * than the memory allocated after the last run of the GC.
1555 JSGC_TRIGGER_FACTOR
= 3,
1557 /* Amount of bytes allocated by the GC. */
1560 /* Number of times when GC was invoked. */
1563 /* Max size of the code cache in bytes. */
1564 JSGC_MAX_CODE_CACHE_BYTES
= 6
1567 extern JS_PUBLIC_API(void)
1568 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1570 extern JS_PUBLIC_API(uint32
)
1571 JS_GetGCParameter(JSRuntime
*rt
, JSGCParamKey key
);
1573 extern JS_PUBLIC_API(void)
1574 JS_SetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
, uint32 value
);
1576 extern JS_PUBLIC_API(uint32
)
1577 JS_GetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
);
1580 * Flush the code cache for the current thread. The operation might be
1581 * delayed if the cache cannot be flushed currently because native
1582 * code is currently executing.
1585 extern JS_PUBLIC_API(void)
1586 JS_FlushCaches(JSContext
*cx
);
1589 * Add a finalizer for external strings created by JS_NewExternalString (see
1590 * below) using a type-code returned from this function, and that understands
1591 * how to free or release the memory pointed at by JS_GetStringChars(str).
1593 * Return a nonnegative type index if there is room for finalizer in the
1594 * global GC finalizers table, else return -1. If the engine is compiled
1595 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1596 * be invoked on the primordial thread only, at startup -- or else the entire
1597 * program must single-thread itself while loading a module that calls this
1600 extern JS_PUBLIC_API(intN
)
1601 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1604 * Remove finalizer from the global GC finalizers table, returning its type
1605 * code if found, -1 if not found.
1607 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1608 * if you compile the engine JS_THREADSAFE: this function may be called for a
1609 * given finalizer pointer on only one thread; different threads may call to
1610 * remove distinct finalizers safely.
1612 * You must ensure that all strings with finalizer's type have been collected
1613 * before calling this function. Otherwise, string data will be leaked by the
1614 * GC, for want of a finalizer to call.
1616 extern JS_PUBLIC_API(intN
)
1617 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1620 * Create a new JSString whose chars member refers to external memory, i.e.,
1621 * memory requiring spe, type-specific finalization. The type code must
1622 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1624 extern JS_PUBLIC_API(JSString
*)
1625 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1628 * Returns the external-string finalizer index for this string, or -1 if it is
1629 * an "internal" (native to JS engine) string.
1631 extern JS_PUBLIC_API(intN
)
1632 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1635 * Deprecated. Use JS_SetNativeStackQuoata instead.
1637 extern JS_PUBLIC_API(void)
1638 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1641 * Set the size of the native stack that should not be exceed. To disable
1642 * stack size checking pass 0.
1644 extern JS_PUBLIC_API(void)
1645 JS_SetNativeStackQuota(JSContext
*cx
, size_t stackSize
);
1649 * Set the quota on the number of bytes that stack-like data structures can
1650 * use when the runtime compiles and executes scripts. These structures
1651 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1652 * The default quota is 32MB which is quite generous.
1654 * The function must be called before any script compilation or execution API
1655 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1658 extern JS_PUBLIC_API(void)
1659 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1661 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1663 /************************************************************************/
1666 * Classes, objects, and properties.
1669 /* For detailed comments on the function pointer types, see jspubtd.h. */
1674 /* Mandatory non-null function pointer members. */
1675 JSPropertyOp addProperty
;
1676 JSPropertyOp delProperty
;
1677 JSPropertyOp getProperty
;
1678 JSPropertyOp setProperty
;
1679 JSEnumerateOp enumerate
;
1680 JSResolveOp resolve
;
1681 JSConvertOp convert
;
1682 JSFinalizeOp finalize
;
1684 /* Optionally non-null members start here. */
1685 JSGetObjectOps getObjectOps
;
1686 JSCheckAccessOp checkAccess
;
1689 JSXDRObjectOp xdrObject
;
1690 JSHasInstanceOp hasInstance
;
1692 JSReserveSlotsOp reserveSlots
;
1695 struct JSExtendedClass
{
1697 JSEqualityOp equality
;
1698 JSObjectOp outerObject
;
1699 JSObjectOp innerObject
;
1700 JSIteratorOp iteratorObject
;
1701 JSObjectOp wrappedObject
; /* NB: infallible, null
1702 returns are treated as
1703 the original object */
1704 void (*reserved0
)(void);
1705 void (*reserved1
)(void);
1706 void (*reserved2
)(void);
1709 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1710 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1711 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1712 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1713 /* (1<<4) was JSCLASS_SHARE_ALL_PROPERTIES, now obsolete. See bug 527805. */
1714 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1715 object in prototype chain
1716 passed in via *objp in/out
1718 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1720 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1723 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1724 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1725 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1727 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1728 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1729 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1730 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1731 << JSCLASS_RESERVED_SLOTS_SHIFT)
1732 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1733 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1734 & JSCLASS_RESERVED_SLOTS_MASK)
1736 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1737 JSCLASS_RESERVED_SLOTS_WIDTH)
1739 /* True if JSClass is really a JSExtendedClass. */
1740 #define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1741 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1742 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1744 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1745 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1748 * ECMA-262 requires that most constructors used internally create objects
1749 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1750 * member initial value. The "original ... value" verbiage is there because
1751 * in ECMA-262, global properties naming class objects are read/write and
1752 * deleteable, for the most part.
1754 * Implementing this efficiently requires that global objects have classes
1755 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was
1756 * prevously allowed, but is now an ES5 violation and thus unsupported.
1758 #define JSCLASS_GLOBAL_FLAGS \
1759 (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT * 3 + 1))
1761 #define JSRESERVED_GLOBAL_COMPARTMENT (JSProto_LIMIT * 3)
1763 /* Fast access to the original value of each standard class's prototype. */
1764 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1765 #define JSCLASS_CACHED_PROTO_WIDTH 8
1766 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1767 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1768 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1770 >> JSCLASS_CACHED_PROTO_SHIFT) \
1771 & JSCLASS_CACHED_PROTO_MASK))
1773 /* Initializer for unused members of statically initialized JSClass structs. */
1774 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
1775 #define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
1780 jsid vector
[1]; /* actually, length jsid words */
1783 extern JS_PUBLIC_API(void)
1784 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
1786 extern JS_PUBLIC_API(JSBool
)
1787 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
1789 extern JS_PUBLIC_API(JSBool
)
1790 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
1793 * JSNewResolveOp flag bits.
1795 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1796 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1797 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1798 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1799 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1800 #define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
1802 extern JS_PUBLIC_API(JSBool
)
1803 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1805 extern JS_PUBLIC_API(JSBool
)
1806 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
1808 extern JS_PUBLIC_API(JSBool
)
1809 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsid id
);
1811 extern JS_PUBLIC_API(JSBool
)
1812 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
1814 extern JS_PUBLIC_API(void)
1815 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
1817 struct JSConstDoubleSpec
{
1825 * To define an array element rather than a named property member, cast the
1826 * element's index to (const char *) and initialize name with it, and set the
1827 * JSPROP_INDEX bit in flags.
1829 struct JSPropertySpec
{
1833 JSPropertyOp getter
;
1834 JSPropertyOp setter
;
1837 struct JSFunctionSpec
{
1844 * extra & 0xFFFF: Number of extra argument slots for local GC roots.
1845 * If fast native, must be zero.
1846 * extra >> 16: Reserved for future use (must be 0).
1852 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1853 * that's passed to JS_DefineFunctions or JS_InitClass.
1855 #define JS_FS_END JS_FS(NULL,NULL,0,0,0)
1858 * Initializer macro for a JSFunctionSpec array element. This is the original
1859 * kind of native function specifier initializer. Use JS_FN ("fast native", see
1860 * JSFastNative in jspubtd.h) for all functions that do not need a stack frame
1863 #define JS_FS(name,call,nargs,flags,extra) \
1864 {name, call, nargs, flags, extra}
1867 * "Fast native" initializer macro for a JSFunctionSpec array element. Use this
1868 * in preference to JS_FS if the native in question does not need its own stack
1869 * frame when activated.
1871 #define JS_FN(name,fastcall,nargs,flags) \
1872 JS_FS(name, (JSNative)(fastcall), nargs, \
1873 (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0)
1875 extern JS_PUBLIC_API(JSObject
*)
1876 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
1877 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
1878 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
1879 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
1881 #ifdef JS_THREADSAFE
1882 extern JS_PUBLIC_API(JSClass
*)
1883 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
1885 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1887 extern JS_PUBLIC_API(JSClass
*)
1888 JS_GetClass(JSObject
*obj
);
1890 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1893 extern JS_PUBLIC_API(JSBool
)
1894 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
1896 extern JS_PUBLIC_API(JSBool
)
1897 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
1899 extern JS_PUBLIC_API(void *)
1900 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
1902 extern JS_PUBLIC_API(JSBool
)
1903 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
1905 extern JS_PUBLIC_API(void *)
1906 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
1909 extern JS_PUBLIC_API(JSObject
*)
1910 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
1912 extern JS_PUBLIC_API(JSBool
)
1913 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
1915 extern JS_PUBLIC_API(JSObject
*)
1916 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
1918 extern JS_PUBLIC_API(JSBool
)
1919 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
1921 extern JS_PUBLIC_API(JSObject
*)
1922 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
1925 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1926 * is moved by a copying GC). Return false on failure (likely out of memory),
1927 * and true with *idp containing the unique id on success.
1929 extern JS_PUBLIC_API(JSBool
)
1930 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
1932 extern JS_PUBLIC_API(JSObject
*)
1933 JS_NewGlobalObject(JSContext
*cx
, JSClass
*clasp
);
1935 extern JS_PUBLIC_API(JSObject
*)
1936 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
1939 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1940 * proto if proto's actual parameter value is null.
1942 extern JS_PUBLIC_API(JSObject
*)
1943 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1946 extern JS_PUBLIC_API(JSBool
)
1947 JS_SealObject(JSContext
*cx
, JSObject
*obj
, JSBool deep
);
1949 extern JS_PUBLIC_API(JSObject
*)
1950 JS_ConstructObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1953 extern JS_PUBLIC_API(JSObject
*)
1954 JS_ConstructObjectWithArguments(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1955 JSObject
*parent
, uintN argc
, jsval
*argv
);
1957 extern JS_PUBLIC_API(JSObject
*)
1958 JS_New(JSContext
*cx
, JSObject
*ctor
, uintN argc
, jsval
*argv
);
1960 extern JS_PUBLIC_API(JSObject
*)
1961 JS_DefineObject(JSContext
*cx
, JSObject
*obj
, const char *name
, JSClass
*clasp
,
1962 JSObject
*proto
, uintN attrs
);
1964 extern JS_PUBLIC_API(JSBool
)
1965 JS_DefineConstDoubles(JSContext
*cx
, JSObject
*obj
, JSConstDoubleSpec
*cds
);
1967 extern JS_PUBLIC_API(JSBool
)
1968 JS_DefineProperties(JSContext
*cx
, JSObject
*obj
, JSPropertySpec
*ps
);
1970 extern JS_PUBLIC_API(JSBool
)
1971 JS_DefineProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval value
,
1972 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1974 extern JS_PUBLIC_API(JSBool
)
1975 JS_DefinePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval value
,
1976 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
1978 extern JS_PUBLIC_API(JSBool
)
1979 JS_DefineOwnProperty(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval descriptor
, JSBool
*bp
);
1982 * Determine the attributes (JSPROP_* flags) of a property on a given object.
1984 * If the object does not have a property by that name, *foundp will be
1985 * JS_FALSE and the value of *attrsp is undefined.
1987 extern JS_PUBLIC_API(JSBool
)
1988 JS_GetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
1989 uintN
*attrsp
, JSBool
*foundp
);
1992 * The same, but if the property is native, return its getter and setter via
1993 * *getterp and *setterp, respectively (and only if the out parameter pointer
1996 extern JS_PUBLIC_API(JSBool
)
1997 JS_GetPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
1999 uintN
*attrsp
, JSBool
*foundp
,
2000 JSPropertyOp
*getterp
,
2001 JSPropertyOp
*setterp
);
2003 extern JS_PUBLIC_API(JSBool
)
2004 JS_GetPropertyAttrsGetterAndSetterById(JSContext
*cx
, JSObject
*obj
,
2006 uintN
*attrsp
, JSBool
*foundp
,
2007 JSPropertyOp
*getterp
,
2008 JSPropertyOp
*setterp
);
2011 * Set the attributes of a property on a given object.
2013 * If the object does not have a property by that name, *foundp will be
2014 * JS_FALSE and nothing will be altered.
2016 extern JS_PUBLIC_API(JSBool
)
2017 JS_SetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
2018 uintN attrs
, JSBool
*foundp
);
2020 extern JS_PUBLIC_API(JSBool
)
2021 JS_DefinePropertyWithTinyId(JSContext
*cx
, JSObject
*obj
, const char *name
,
2022 int8 tinyid
, jsval value
,
2023 JSPropertyOp getter
, JSPropertyOp setter
,
2026 extern JS_PUBLIC_API(JSBool
)
2027 JS_AliasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
2030 extern JS_PUBLIC_API(JSBool
)
2031 JS_AlreadyHasOwnProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
2034 extern JS_PUBLIC_API(JSBool
)
2035 JS_AlreadyHasOwnPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
,
2038 extern JS_PUBLIC_API(JSBool
)
2039 JS_HasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, JSBool
*foundp
);
2041 extern JS_PUBLIC_API(JSBool
)
2042 JS_HasPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool
*foundp
);
2044 extern JS_PUBLIC_API(JSBool
)
2045 JS_LookupProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2047 extern JS_PUBLIC_API(JSBool
)
2048 JS_LookupPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2050 extern JS_PUBLIC_API(JSBool
)
2051 JS_LookupPropertyWithFlags(JSContext
*cx
, JSObject
*obj
, const char *name
,
2052 uintN flags
, jsval
*vp
);
2054 extern JS_PUBLIC_API(JSBool
)
2055 JS_LookupPropertyWithFlagsById(JSContext
*cx
, JSObject
*obj
, jsid id
,
2056 uintN flags
, JSObject
**objp
, jsval
*vp
);
2058 struct JSPropertyDescriptor
{
2061 JSPropertyOp getter
;
2062 JSPropertyOp setter
;
2068 * Like JS_GetPropertyAttrsGetterAndSetterById but will return a property on
2069 * an object on the prototype chain (returned in objp). If data->obj is null,
2070 * then this property was not found on the prototype chain.
2072 extern JS_PUBLIC_API(JSBool
)
2073 JS_GetPropertyDescriptorById(JSContext
*cx
, JSObject
*obj
, jsid id
, uintN flags
,
2074 JSPropertyDescriptor
*desc
);
2076 extern JS_PUBLIC_API(JSBool
)
2077 JS_GetOwnPropertyDescriptor(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2079 extern JS_PUBLIC_API(JSBool
)
2080 JS_GetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2082 extern JS_PUBLIC_API(JSBool
)
2083 JS_GetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2085 extern JS_PUBLIC_API(JSBool
)
2086 JS_GetMethodById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSObject
**objp
,
2089 extern JS_PUBLIC_API(JSBool
)
2090 JS_GetMethod(JSContext
*cx
, JSObject
*obj
, const char *name
, JSObject
**objp
,
2093 extern JS_PUBLIC_API(JSBool
)
2094 JS_SetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2096 extern JS_PUBLIC_API(JSBool
)
2097 JS_SetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2099 extern JS_PUBLIC_API(JSBool
)
2100 JS_DeleteProperty(JSContext
*cx
, JSObject
*obj
, const char *name
);
2102 extern JS_PUBLIC_API(JSBool
)
2103 JS_DeleteProperty2(JSContext
*cx
, JSObject
*obj
, const char *name
,
2106 extern JS_PUBLIC_API(JSBool
)
2107 JS_DeletePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
);
2109 extern JS_PUBLIC_API(JSBool
)
2110 JS_DeletePropertyById2(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*rval
);
2112 extern JS_PUBLIC_API(JSBool
)
2113 JS_DefineUCProperty(JSContext
*cx
, JSObject
*obj
,
2114 const jschar
*name
, size_t namelen
, jsval value
,
2115 JSPropertyOp getter
, JSPropertyOp setter
,
2119 * Determine the attributes (JSPROP_* flags) of a property on a given object.
2121 * If the object does not have a property by that name, *foundp will be
2122 * JS_FALSE and the value of *attrsp is undefined.
2124 extern JS_PUBLIC_API(JSBool
)
2125 JS_GetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
2126 const jschar
*name
, size_t namelen
,
2127 uintN
*attrsp
, JSBool
*foundp
);
2130 * The same, but if the property is native, return its getter and setter via
2131 * *getterp and *setterp, respectively (and only if the out parameter pointer
2134 extern JS_PUBLIC_API(JSBool
)
2135 JS_GetUCPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
2136 const jschar
*name
, size_t namelen
,
2137 uintN
*attrsp
, JSBool
*foundp
,
2138 JSPropertyOp
*getterp
,
2139 JSPropertyOp
*setterp
);
2142 * Set the attributes of a property on a given object.
2144 * If the object does not have a property by that name, *foundp will be
2145 * JS_FALSE and nothing will be altered.
2147 extern JS_PUBLIC_API(JSBool
)
2148 JS_SetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
2149 const jschar
*name
, size_t namelen
,
2150 uintN attrs
, JSBool
*foundp
);
2153 extern JS_PUBLIC_API(JSBool
)
2154 JS_DefineUCPropertyWithTinyId(JSContext
*cx
, JSObject
*obj
,
2155 const jschar
*name
, size_t namelen
,
2156 int8 tinyid
, jsval value
,
2157 JSPropertyOp getter
, JSPropertyOp setter
,
2160 extern JS_PUBLIC_API(JSBool
)
2161 JS_AlreadyHasOwnUCProperty(JSContext
*cx
, JSObject
*obj
, const jschar
*name
,
2162 size_t namelen
, JSBool
*foundp
);
2164 extern JS_PUBLIC_API(JSBool
)
2165 JS_HasUCProperty(JSContext
*cx
, JSObject
*obj
,
2166 const jschar
*name
, size_t namelen
,
2169 extern JS_PUBLIC_API(JSBool
)
2170 JS_LookupUCProperty(JSContext
*cx
, JSObject
*obj
,
2171 const jschar
*name
, size_t namelen
,
2174 extern JS_PUBLIC_API(JSBool
)
2175 JS_GetUCProperty(JSContext
*cx
, JSObject
*obj
,
2176 const jschar
*name
, size_t namelen
,
2179 extern JS_PUBLIC_API(JSBool
)
2180 JS_SetUCProperty(JSContext
*cx
, JSObject
*obj
,
2181 const jschar
*name
, size_t namelen
,
2184 extern JS_PUBLIC_API(JSBool
)
2185 JS_DeleteUCProperty2(JSContext
*cx
, JSObject
*obj
,
2186 const jschar
*name
, size_t namelen
,
2189 extern JS_PUBLIC_API(JSObject
*)
2190 JS_NewArrayObject(JSContext
*cx
, jsint length
, jsval
*vector
);
2192 extern JS_PUBLIC_API(JSBool
)
2193 JS_IsArrayObject(JSContext
*cx
, JSObject
*obj
);
2195 extern JS_PUBLIC_API(JSBool
)
2196 JS_GetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
2198 extern JS_PUBLIC_API(JSBool
)
2199 JS_SetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint length
);
2201 extern JS_PUBLIC_API(JSBool
)
2202 JS_HasArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
2204 extern JS_PUBLIC_API(JSBool
)
2205 JS_DefineElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval value
,
2206 JSPropertyOp getter
, JSPropertyOp setter
, uintN attrs
);
2208 extern JS_PUBLIC_API(JSBool
)
2209 JS_AliasElement(JSContext
*cx
, JSObject
*obj
, const char *name
, jsint alias
);
2211 extern JS_PUBLIC_API(JSBool
)
2212 JS_AlreadyHasOwnElement(JSContext
*cx
, JSObject
*obj
, jsint index
,
2215 extern JS_PUBLIC_API(JSBool
)
2216 JS_HasElement(JSContext
*cx
, JSObject
*obj
, jsint index
, JSBool
*foundp
);
2218 extern JS_PUBLIC_API(JSBool
)
2219 JS_LookupElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2221 extern JS_PUBLIC_API(JSBool
)
2222 JS_GetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2224 extern JS_PUBLIC_API(JSBool
)
2225 JS_SetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2227 extern JS_PUBLIC_API(JSBool
)
2228 JS_DeleteElement(JSContext
*cx
, JSObject
*obj
, jsint index
);
2230 extern JS_PUBLIC_API(JSBool
)
2231 JS_DeleteElement2(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*rval
);
2233 extern JS_PUBLIC_API(void)
2234 JS_ClearScope(JSContext
*cx
, JSObject
*obj
);
2236 extern JS_PUBLIC_API(JSIdArray
*)
2237 JS_Enumerate(JSContext
*cx
, JSObject
*obj
);
2240 * Create an object to iterate over enumerable properties of obj, in arbitrary
2241 * property definition order. NB: This differs from longstanding for..in loop
2242 * order, which uses order of property definition in obj.
2244 extern JS_PUBLIC_API(JSObject
*)
2245 JS_NewPropertyIterator(JSContext
*cx
, JSObject
*obj
);
2248 * Return true on success with *idp containing the id of the next enumerable
2249 * property to visit using iterobj, or JSID_IS_VOID if there is no such property
2250 * left to visit. Return false on error.
2252 extern JS_PUBLIC_API(JSBool
)
2253 JS_NextProperty(JSContext
*cx
, JSObject
*iterobj
, jsid
*idp
);
2255 extern JS_PUBLIC_API(JSBool
)
2256 JS_CheckAccess(JSContext
*cx
, JSObject
*obj
, jsid id
, JSAccessMode mode
,
2257 jsval
*vp
, uintN
*attrsp
);
2259 extern JS_PUBLIC_API(JSBool
)
2260 JS_GetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval
*vp
);
2262 extern JS_PUBLIC_API(JSBool
)
2263 JS_SetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval v
);
2265 /************************************************************************/
2268 * Security protocol.
2270 struct JSPrincipals
{
2273 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
2274 void * (* getPrincipalArray
)(JSContext
*cx
, JSPrincipals
*);
2275 JSBool (* globalPrivilegesEnabled
)(JSContext
*cx
, JSPrincipals
*);
2277 /* Don't call "destroy"; use reference counting macros below. */
2278 jsrefcount refcount
;
2280 void (* destroy
)(JSContext
*cx
, JSPrincipals
*);
2281 JSBool (* subsume
)(JSPrincipals
*, JSPrincipals
*);
2284 #ifdef JS_THREADSAFE
2285 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
2286 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
2288 extern JS_PUBLIC_API(jsrefcount
)
2289 JS_HoldPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2291 extern JS_PUBLIC_API(jsrefcount
)
2292 JS_DropPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2295 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
2296 #define JSPRINCIPALS_DROP(cx, principals) \
2297 ((--(principals)->refcount == 0) \
2298 ? ((*(principals)->destroy)((cx), (principals)), 0) \
2299 : (principals)->refcount)
2303 struct JSSecurityCallbacks
{
2304 JSCheckAccessOp checkObjectAccess
;
2305 JSPrincipalsTranscoder principalsTranscoder
;
2306 JSObjectPrincipalsFinder findObjectPrincipals
;
2307 JSCSPEvalChecker contentSecurityPolicyAllows
;
2310 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2311 JS_SetRuntimeSecurityCallbacks(JSRuntime
*rt
, JSSecurityCallbacks
*callbacks
);
2313 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2314 JS_GetRuntimeSecurityCallbacks(JSRuntime
*rt
);
2316 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2317 JS_SetContextSecurityCallbacks(JSContext
*cx
, JSSecurityCallbacks
*callbacks
);
2319 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2320 JS_GetSecurityCallbacks(JSContext
*cx
);
2322 /************************************************************************/
2325 * Functions and scripts.
2327 extern JS_PUBLIC_API(JSFunction
*)
2328 JS_NewFunction(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
2329 JSObject
*parent
, const char *name
);
2331 extern JS_PUBLIC_API(JSObject
*)
2332 JS_GetFunctionObject(JSFunction
*fun
);
2335 * Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
2336 * anonymous vs. "anonymous" disambiguation and Unicode fidelity.
2338 extern JS_PUBLIC_API(const char *)
2339 JS_GetFunctionName(JSFunction
*fun
);
2342 * Return the function's identifier as a JSString, or null if fun is unnamed.
2343 * The returned string lives as long as fun, so you don't need to root a saved
2344 * reference to it if fun is well-connected or rooted, and provided you bound
2345 * the use of the saved reference by fun's lifetime.
2347 * Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
2348 * truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
2349 * from UTF-16-ish jschars.
2351 extern JS_PUBLIC_API(JSString
*)
2352 JS_GetFunctionId(JSFunction
*fun
);
2355 * Return JSFUN_* flags for fun.
2357 extern JS_PUBLIC_API(uintN
)
2358 JS_GetFunctionFlags(JSFunction
*fun
);
2361 * Return the arity (length) of fun.
2363 extern JS_PUBLIC_API(uint16
)
2364 JS_GetFunctionArity(JSFunction
*fun
);
2367 * Infallible predicate to test whether obj is a function object (faster than
2368 * comparing obj's class name to "Function", but equivalent unless someone has
2369 * overwritten the "Function" identifier with a different constructor and then
2370 * created instances using that constructor that might be passed in as obj).
2372 extern JS_PUBLIC_API(JSBool
)
2373 JS_ObjectIsFunction(JSContext
*cx
, JSObject
*obj
);
2375 extern JS_PUBLIC_API(JSBool
)
2376 JS_DefineFunctions(JSContext
*cx
, JSObject
*obj
, JSFunctionSpec
*fs
);
2378 extern JS_PUBLIC_API(JSFunction
*)
2379 JS_DefineFunction(JSContext
*cx
, JSObject
*obj
, const char *name
, JSNative call
,
2380 uintN nargs
, uintN attrs
);
2382 extern JS_PUBLIC_API(JSFunction
*)
2383 JS_DefineUCFunction(JSContext
*cx
, JSObject
*obj
,
2384 const jschar
*name
, size_t namelen
, JSNative call
,
2385 uintN nargs
, uintN attrs
);
2387 extern JS_PUBLIC_API(JSObject
*)
2388 JS_CloneFunctionObject(JSContext
*cx
, JSObject
*funobj
, JSObject
*parent
);
2391 * Given a buffer, return JS_FALSE if the buffer might become a valid
2392 * javascript statement with the addition of more lines. Otherwise return
2393 * JS_TRUE. The intent is to support interactive compilation - accumulate
2394 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
2397 extern JS_PUBLIC_API(JSBool
)
2398 JS_BufferIsCompilableUnit(JSContext
*cx
, JSObject
*obj
,
2399 const char *bytes
, size_t length
);
2402 * The JSScript objects returned by the following functions refer to string and
2403 * other kinds of literals, including doubles and RegExp objects. These
2404 * literals are vulnerable to garbage collection; to root script objects and
2405 * prevent literals from being collected, create a rootable object using
2406 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
2408 extern JS_PUBLIC_API(JSScript
*)
2409 JS_CompileScript(JSContext
*cx
, JSObject
*obj
,
2410 const char *bytes
, size_t length
,
2411 const char *filename
, uintN lineno
);
2413 extern JS_PUBLIC_API(JSScript
*)
2414 JS_CompileScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2415 JSPrincipals
*principals
,
2416 const char *bytes
, size_t length
,
2417 const char *filename
, uintN lineno
);
2419 extern JS_PUBLIC_API(JSScript
*)
2420 JS_CompileUCScript(JSContext
*cx
, JSObject
*obj
,
2421 const jschar
*chars
, size_t length
,
2422 const char *filename
, uintN lineno
);
2424 extern JS_PUBLIC_API(JSScript
*)
2425 JS_CompileUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2426 JSPrincipals
*principals
,
2427 const jschar
*chars
, size_t length
,
2428 const char *filename
, uintN lineno
);
2430 extern JS_PUBLIC_API(JSScript
*)
2431 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2433 extern JS_PUBLIC_API(JSScript
*)
2434 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2437 extern JS_PUBLIC_API(JSScript
*)
2438 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2439 const char *filename
, FILE *fh
,
2440 JSPrincipals
*principals
);
2443 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2444 * in order to keep a JSScript and its atoms safe from garbage collection after
2445 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2446 * E.g., and without error checks:
2448 * JSScript *script = JS_CompileFile(cx, global, filename);
2449 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2450 * JS_AddNamedObjectRoot(cx, &scrobj, "scrobj");
2453 * JS_ExecuteScript(cx, global, script, &result);
2455 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2456 * JS_RemoveObjectRoot(cx, &scrobj);
2458 extern JS_PUBLIC_API(JSObject
*)
2459 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2462 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2463 * called on script yet, the return value will be null.
2465 extern JS_PUBLIC_API(JSObject
*)
2466 JS_GetScriptObject(JSScript
*script
);
2468 extern JS_PUBLIC_API(void)
2469 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2471 extern JS_PUBLIC_API(JSFunction
*)
2472 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2473 uintN nargs
, const char **argnames
,
2474 const char *bytes
, size_t length
,
2475 const char *filename
, uintN lineno
);
2477 extern JS_PUBLIC_API(JSFunction
*)
2478 JS_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2479 JSPrincipals
*principals
, const char *name
,
2480 uintN nargs
, const char **argnames
,
2481 const char *bytes
, size_t length
,
2482 const char *filename
, uintN lineno
);
2484 extern JS_PUBLIC_API(JSFunction
*)
2485 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2486 uintN nargs
, const char **argnames
,
2487 const jschar
*chars
, size_t length
,
2488 const char *filename
, uintN lineno
);
2490 extern JS_PUBLIC_API(JSFunction
*)
2491 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2492 JSPrincipals
*principals
, const char *name
,
2493 uintN nargs
, const char **argnames
,
2494 const jschar
*chars
, size_t length
,
2495 const char *filename
, uintN lineno
);
2497 extern JS_PUBLIC_API(JSString
*)
2498 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2502 * API extension: OR this into indent to avoid pretty-printing the decompiled
2503 * source resulting from JS_DecompileFunction{,Body}.
2505 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2507 extern JS_PUBLIC_API(JSString
*)
2508 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2510 extern JS_PUBLIC_API(JSString
*)
2511 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2514 * NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
2515 * parameter as the initial scope chain header, the 'this' keyword value, and
2516 * the variables object (ECMA parlance for where 'var' and 'function' bind
2517 * names) of the execution context for script.
2519 * Using obj as the variables object is problematic if obj's parent (which is
2520 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2521 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2522 * created by assignment to an unbound id, 'x = 0', go in the last object on
2523 * the scope chain linked by parent.
2525 * ECMA calls that last scoping object the "global object", but note that many
2526 * embeddings have several such objects. ECMA requires that "global code" be
2527 * executed with the variables object equal to this global object. But these
2528 * JS API entry points provide freedom to execute code against a "sub-global",
2529 * i.e., a parented or scoped object, in which case the variables object will
2530 * differ from the last object on the scope chain, resulting in confusing and
2531 * non-ECMA explicit vs. implicit variable creation.
2533 * Caveat embedders: unless you already depend on this buggy variables object
2534 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2535 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2536 * someone may have set other options on cx already -- for each context in the
2537 * application, if you pass parented objects as the obj parameter, or may ever
2538 * pass such objects in the future.
2540 * Why a runtime option? The alternative is to add six or so new API entry
2541 * points with signatures matching the following six, and that doesn't seem
2542 * worth the code bloat cost. Such new entry points would probably have less
2543 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2544 * OTOH, can be more easily hacked into existing code that does not depend on
2545 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2546 * etc., entry points.
2548 extern JS_PUBLIC_API(JSBool
)
2549 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2552 * Execute either the function-defining prolog of a script, or the script's
2553 * main body, but not both.
2555 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2557 extern JS_PUBLIC_API(JSBool
)
2558 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2559 const char *bytes
, uintN length
,
2560 const char *filename
, uintN lineno
,
2563 extern JS_PUBLIC_API(JSBool
)
2564 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2565 JSPrincipals
*principals
,
2566 const char *bytes
, uintN length
,
2567 const char *filename
, uintN lineno
,
2570 extern JS_PUBLIC_API(JSBool
)
2571 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2572 const jschar
*chars
, uintN length
,
2573 const char *filename
, uintN lineno
,
2576 extern JS_PUBLIC_API(JSBool
)
2577 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2578 JSPrincipals
*principals
,
2579 const jschar
*chars
, uintN length
,
2580 const char *filename
, uintN lineno
,
2583 extern JS_PUBLIC_API(JSBool
)
2584 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2585 jsval
*argv
, jsval
*rval
);
2587 extern JS_PUBLIC_API(JSBool
)
2588 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2589 jsval
*argv
, jsval
*rval
);
2591 extern JS_PUBLIC_API(JSBool
)
2592 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2593 jsval
*argv
, jsval
*rval
);
2596 * These functions allow setting an operation callback that will be called
2597 * from the thread the context is associated with some time after any thread
2598 * triggered the callback using JS_TriggerOperationCallback(cx).
2600 * In a threadsafe build the engine internally triggers operation callbacks
2601 * under certain circumstances (i.e. GC and title transfer) to force the
2602 * context to yield its current request, which the engine always
2603 * automatically does immediately prior to calling the callback function.
2604 * The embedding should thus not rely on callbacks being triggered through
2605 * the external API only.
2607 * Important note: Additional callbacks can occur inside the callback handler
2608 * if it re-enters the JS engine. The embedding must ensure that the callback
2609 * is disconnected before attempting such re-entry.
2612 extern JS_PUBLIC_API(JSOperationCallback
)
2613 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
);
2615 extern JS_PUBLIC_API(JSOperationCallback
)
2616 JS_GetOperationCallback(JSContext
*cx
);
2618 extern JS_PUBLIC_API(void)
2619 JS_TriggerOperationCallback(JSContext
*cx
);
2621 extern JS_PUBLIC_API(void)
2622 JS_TriggerAllOperationCallbacks(JSRuntime
*rt
);
2624 extern JS_PUBLIC_API(JSBool
)
2625 JS_IsRunning(JSContext
*cx
);
2627 extern JS_PUBLIC_API(JSBool
)
2628 JS_IsConstructing(JSContext
*cx
);
2631 * Saving and restoring frame chains.
2633 * These two functions are used to set aside cx's call stack while that stack
2634 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2635 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2636 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2637 * matching JS_RestoreFrameChain calls.
2639 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2640 * return does not signify an error, and JS_RestoreFrameChain handles a null
2641 * frame pointer argument safely.
2643 extern JS_PUBLIC_API(JSStackFrame
*)
2644 JS_SaveFrameChain(JSContext
*cx
);
2646 extern JS_PUBLIC_API(void)
2647 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2649 /************************************************************************/
2654 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2655 * on error (signified by null return), it leaves bytes owned by the caller.
2656 * So the caller must free bytes in the error case, if it has no use for them.
2657 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2658 * the character memory passed to them -- they copy it.
2660 extern JS_PUBLIC_API(JSString
*)
2661 JS_NewString(JSContext
*cx
, char *bytes
, size_t length
);
2663 extern JS_PUBLIC_API(JSString
*)
2664 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2666 extern JS_PUBLIC_API(JSString
*)
2667 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2669 extern JS_PUBLIC_API(JSString
*)
2670 JS_InternString(JSContext
*cx
, const char *s
);
2672 extern JS_PUBLIC_API(JSString
*)
2673 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
2675 extern JS_PUBLIC_API(JSString
*)
2676 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
2678 extern JS_PUBLIC_API(JSString
*)
2679 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
2681 extern JS_PUBLIC_API(JSString
*)
2682 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
2684 extern JS_PUBLIC_API(JSString
*)
2685 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
2687 extern JS_PUBLIC_API(char *)
2688 JS_GetStringBytes(JSString
*str
);
2690 extern JS_PUBLIC_API(jschar
*)
2691 JS_GetStringChars(JSString
*str
);
2693 extern JS_PUBLIC_API(size_t)
2694 JS_GetStringLength(JSString
*str
);
2696 extern JS_PUBLIC_API(const char *)
2697 JS_GetStringBytesZ(JSContext
*cx
, JSString
*str
);
2699 extern JS_PUBLIC_API(const jschar
*)
2700 JS_GetStringCharsZ(JSContext
*cx
, JSString
*str
);
2702 extern JS_PUBLIC_API(intN
)
2703 JS_CompareStrings(JSString
*str1
, JSString
*str2
);
2706 * Mutable string support. A string's characters are never mutable in this JS
2707 * implementation, but a growable string has a buffer that can be reallocated,
2708 * and a dependent string is a substring of another (growable, dependent, or
2709 * immutable) string. The direct data members of the (opaque to API clients)
2710 * JSString struct may be changed in a single-threaded way for growable and
2711 * dependent strings.
2713 * Therefore mutable strings cannot be used by more than one thread at a time.
2714 * You may call JS_MakeStringImmutable to convert the string from a mutable
2715 * (growable or dependent) string to an immutable (and therefore thread-safe)
2716 * string. The engine takes care of converting growable and dependent strings
2717 * to immutable for you if you store strings in multi-threaded objects using
2718 * JS_SetProperty or kindred API entry points.
2720 * If you store a JSString pointer in a native data structure that is (safely)
2721 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2722 * retiring the store.
2724 extern JS_PUBLIC_API(JSString
*)
2725 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
2728 * Create a dependent string, i.e., a string that owns no character storage,
2729 * but that refers to a slice of another string's chars. Dependent strings
2730 * are mutable by definition, so the thread safety comments above apply.
2732 extern JS_PUBLIC_API(JSString
*)
2733 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
2737 * Concatenate two strings, resulting in a new growable string. If you create
2738 * the left string and pass it to JS_ConcatStrings on a single thread, try to
2739 * use JS_NewGrowableString to create the left string -- doing so helps Concat
2740 * avoid allocating a new buffer for the result and copying left's chars into
2741 * the new buffer. See above for thread safety comments.
2743 extern JS_PUBLIC_API(JSString
*)
2744 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
2747 * Convert a dependent string into an independent one. This function does not
2748 * change the string's mutability, so the thread safety comments above apply.
2750 extern JS_PUBLIC_API(const jschar
*)
2751 JS_UndependString(JSContext
*cx
, JSString
*str
);
2754 * Convert a mutable string (either growable or dependent) into an immutable,
2757 extern JS_PUBLIC_API(JSBool
)
2758 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
2761 * Return JS_TRUE if C (char []) strings passed via the API and internally
2764 JS_PUBLIC_API(JSBool
)
2765 JS_CStringsAreUTF8(void);
2768 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2769 * can never be changed. This API must be called before the first call to
2773 JS_SetCStringsAreUTF8(void);
2776 * Character encoding support.
2778 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2779 * of the destination buffer before the call; on return, *dstlenp contains the
2780 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2781 * stored. To determine the necessary destination buffer size, make a sizing
2782 * call that passes NULL for dst.
2784 * On errors, the functions report the error. In that case, *dstlenp contains
2785 * the number of characters or bytes transferred so far. If cx is NULL, no
2786 * error is reported on failure, and the functions simply return JS_FALSE.
2788 * NB: Neither function stores an additional zero byte or jschar after the
2789 * transcoded string.
2791 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2792 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2793 * errors if the character sequence is malformed. If UTF-8 support is
2794 * disabled, the functions deflate and inflate, respectively.
2796 JS_PUBLIC_API(JSBool
)
2797 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
2800 JS_PUBLIC_API(JSBool
)
2801 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
2805 * A variation on JS_EncodeCharacters where a null terminated string is
2806 * returned that you are expected to call JS_free on when done.
2808 JS_PUBLIC_API(char *)
2809 JS_EncodeString(JSContext
*cx
, JSString
*str
);
2811 /************************************************************************/
2815 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
2818 * JSON.stringify as specified by ES3.1 (draft)
2820 JS_PUBLIC_API(JSBool
)
2821 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
, jsval space
,
2822 JSONWriteCallback callback
, void *data
);
2825 * Retrieve a toJSON function. If found, set vp to its result.
2827 JS_PUBLIC_API(JSBool
)
2828 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
2831 * JSON.parse as specified by ES3.1 (draft)
2833 JS_PUBLIC_API(JSONParser
*)
2834 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
2836 JS_PUBLIC_API(JSBool
)
2837 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
2839 JS_PUBLIC_API(JSBool
)
2840 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
, jsval reviver
);
2842 /************************************************************************/
2845 * Locale specific string conversion and error message callbacks.
2847 struct JSLocaleCallbacks
{
2848 JSLocaleToUpperCase localeToUpperCase
;
2849 JSLocaleToLowerCase localeToLowerCase
;
2850 JSLocaleCompare localeCompare
;
2851 JSLocaleToUnicode localeToUnicode
;
2852 JSErrorCallback localeGetErrorMessage
;
2856 * Establish locale callbacks. The pointer must persist as long as the
2857 * JSContext. Passing NULL restores the default behaviour.
2859 extern JS_PUBLIC_API(void)
2860 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
2863 * Return the address of the current locale callbacks struct, which may
2866 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
2867 JS_GetLocaleCallbacks(JSContext
*cx
);
2869 /************************************************************************/
2876 * Report an exception represented by the sprintf-like conversion of format
2877 * and its arguments. This exception message string is passed to a pre-set
2878 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2879 * the JSErrorReporter typedef).
2881 extern JS_PUBLIC_API(void)
2882 JS_ReportError(JSContext
*cx
, const char *format
, ...);
2885 * Use an errorNumber to retrieve the format string, args are char *
2887 extern JS_PUBLIC_API(void)
2888 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
2889 void *userRef
, const uintN errorNumber
, ...);
2892 * Use an errorNumber to retrieve the format string, args are jschar *
2894 extern JS_PUBLIC_API(void)
2895 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
2896 void *userRef
, const uintN errorNumber
, ...);
2899 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2900 * Return true if there was no error trying to issue the warning, and if the
2901 * warning was not converted into an error due to the JSOPTION_WERROR option
2902 * being set, false otherwise.
2904 extern JS_PUBLIC_API(JSBool
)
2905 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
2907 extern JS_PUBLIC_API(JSBool
)
2908 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
2909 JSErrorCallback errorCallback
, void *userRef
,
2910 const uintN errorNumber
, ...);
2912 extern JS_PUBLIC_API(JSBool
)
2913 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
2914 JSErrorCallback errorCallback
, void *userRef
,
2915 const uintN errorNumber
, ...);
2918 * Complain when out of memory.
2920 extern JS_PUBLIC_API(void)
2921 JS_ReportOutOfMemory(JSContext
*cx
);
2924 * Complain when an allocation size overflows the maximum supported limit.
2926 extern JS_PUBLIC_API(void)
2927 JS_ReportAllocationOverflow(JSContext
*cx
);
2929 struct JSErrorReport
{
2930 const char *filename
; /* source file name, URL, etc., or null */
2931 uintN lineno
; /* source line number */
2932 const char *linebuf
; /* offending source line without final \n */
2933 const char *tokenptr
; /* pointer to error token in linebuf */
2934 const jschar
*uclinebuf
; /* unicode (original) line buffer */
2935 const jschar
*uctokenptr
; /* unicode (original) token pointer */
2936 uintN flags
; /* error/warning, etc. */
2937 uintN errorNumber
; /* the error number, e.g. see js.msg */
2938 const jschar
*ucmessage
; /* the (default) error message */
2939 const jschar
**messageArgs
; /* arguments for the error message */
2943 * JSErrorReport flag values. These may be freely composed.
2945 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2946 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2947 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2948 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2951 * This condition is an error in strict mode code, a warning if
2952 * JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
2953 * all. We check the strictness of the context's top frame's script;
2954 * where that isn't appropriate, the caller should do the right checks
2955 * itself instead of using this flag.
2957 #define JSREPORT_STRICT_MODE_ERROR 0x8
2960 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2961 * has been thrown for this runtime error, and the host should ignore it.
2962 * Exception-aware hosts should also check for JS_IsExceptionPending if
2963 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2966 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2967 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2968 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2969 #define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) & \
2970 JSREPORT_STRICT_MODE_ERROR) != 0)
2972 extern JS_PUBLIC_API(JSErrorReporter
)
2973 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
2975 /************************************************************************/
2978 * Regular Expressions.
2980 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
2981 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
2982 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
2983 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
2984 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
2985 #define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
2987 extern JS_PUBLIC_API(JSObject
*)
2988 JS_NewRegExpObject(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
2990 extern JS_PUBLIC_API(JSObject
*)
2991 JS_NewUCRegExpObject(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
2993 extern JS_PUBLIC_API(void)
2994 JS_SetRegExpInput(JSContext
*cx
, JSString
*input
, JSBool multiline
);
2996 extern JS_PUBLIC_API(void)
2997 JS_ClearRegExpStatics(JSContext
*cx
);
2999 extern JS_PUBLIC_API(void)
3000 JS_ClearRegExpRoots(JSContext
*cx
);
3002 /* TODO: compile, exec, get/set other statics... */
3004 /************************************************************************/
3006 extern JS_PUBLIC_API(JSBool
)
3007 JS_IsExceptionPending(JSContext
*cx
);
3009 extern JS_PUBLIC_API(JSBool
)
3010 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
3012 extern JS_PUBLIC_API(void)
3013 JS_SetPendingException(JSContext
*cx
, jsval v
);
3015 extern JS_PUBLIC_API(void)
3016 JS_ClearPendingException(JSContext
*cx
);
3018 extern JS_PUBLIC_API(JSBool
)
3019 JS_ReportPendingException(JSContext
*cx
);
3022 * Save the current exception state. This takes a snapshot of cx's current
3023 * exception state without making any change to that state.
3025 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
3026 * (to restore that saved state, overriding any more recent state) or else to
3027 * JS_DropExceptionState (to free the state struct in case it is not correct
3028 * or desirable to restore it). Both Restore and Drop free the state struct,
3029 * so callers must stop using the pointer returned from Save after calling the
3030 * Release or Drop API.
3032 extern JS_PUBLIC_API(JSExceptionState
*)
3033 JS_SaveExceptionState(JSContext
*cx
);
3035 extern JS_PUBLIC_API(void)
3036 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3038 extern JS_PUBLIC_API(void)
3039 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3042 * If the given value is an exception object that originated from an error,
3043 * the exception will contain an error report struct, and this API will return
3044 * the address of that struct. Otherwise, it returns NULL. The lifetime of
3045 * the error report struct that might be returned is the same as the lifetime
3046 * of the exception object.
3048 extern JS_PUBLIC_API(JSErrorReport
*)
3049 JS_ErrorFromException(JSContext
*cx
, jsval v
);
3052 * Given a reported error's message and JSErrorReport struct pointer, throw
3053 * the corresponding exception on cx.
3055 extern JS_PUBLIC_API(JSBool
)
3056 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
3057 JSErrorReport
*reportp
);
3060 * Throws a StopIteration exception on cx.
3062 extern JS_PUBLIC_API(JSBool
)
3063 JS_ThrowStopIteration(JSContext
*cx
);
3066 * Associate the current thread with the given context. This is done
3067 * implicitly by JS_NewContext.
3069 * Returns the old thread id for this context, which should be treated as
3070 * an opaque value. This value is provided for comparison to 0, which
3071 * indicates that ClearContextThread has been called on this context
3072 * since the last SetContextThread, or non-0, which indicates the opposite.
3074 extern JS_PUBLIC_API(jsword
)
3075 JS_GetContextThread(JSContext
*cx
);
3077 extern JS_PUBLIC_API(jsword
)
3078 JS_SetContextThread(JSContext
*cx
);
3080 extern JS_PUBLIC_API(jsword
)
3081 JS_ClearContextThread(JSContext
*cx
);
3083 /************************************************************************/
3086 #define JS_GC_ZEAL 1
3090 extern JS_PUBLIC_API(void)
3091 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
3096 /************************************************************************/
3101 * Spanky new C++ API
3107 explicit NullTag() {}
3110 struct UndefinedTag
{
3111 explicit UndefinedTag() {}
3115 explicit Int32Tag(int32 i32
) : i32(i32
) {}
3120 explicit DoubleTag(double dbl
) : dbl(dbl
) {}
3125 explicit NumberTag(double dbl
) : dbl(dbl
) {}
3130 explicit StringTag(JSString
*str
) : str(str
) {}
3135 explicit FunObjTag(JSObject
&obj
) : obj(obj
) {}
3139 struct FunObjOrNull
{
3140 explicit FunObjOrNull(JSObject
*obj
) : obj(obj
) {}
3144 struct NonFunObjTag
{
3145 explicit NonFunObjTag(JSObject
&obj
) : obj(obj
) {}
3149 struct NonFunObjOrNullTag
{
3150 explicit NonFunObjOrNullTag(JSObject
*obj
) : obj(obj
) {}
3155 explicit ObjectTag(JSObject
&obj
) : obj(obj
) {}
3159 struct ObjectOrNullTag
{
3160 explicit ObjectOrNullTag(JSObject
*obj
) : obj(obj
) {}
3164 struct ObjectOrUndefinedTag
{
3165 /* Interpret null JSObject* as undefined value */
3166 explicit ObjectOrUndefinedTag(JSObject
*obj
) : obj(obj
) {}
3171 explicit BooleanTag(bool boo
) : boo(boo
) {}
3176 explicit PrivateTag(void *ptr
) : ptr(ptr
) {}
3181 * While there is a single representation for values, there are two declared
3182 * types for dealing with these values: jsval and js::Value. jsval allows a
3183 * high-degree of source compatibility with the old word-sized boxed value
3184 * representation. js::Value is a new C++-only type and more accurately
3185 * reflects the current, unboxed value representation. As these two types are
3186 * layout-compatible, pointers to jsval and js::Value are interchangeable and
3187 * may be cast safely and canonically using js::Jsvalify and js::asValue.
3191 void staticAssertions() {
3192 JS_STATIC_ASSERT(sizeof(JSValueType
) == 1);
3193 JS_STATIC_ASSERT(sizeof(JSValueTag
) == 4);
3194 JS_STATIC_ASSERT(sizeof(JSBool
) == 4);
3195 JS_STATIC_ASSERT(sizeof(JSWhyMagic
) <= 4);
3196 JS_STATIC_ASSERT(sizeof(jsval
) == 8);
3204 /* Value's default constructor leaves Value undefined */
3207 /* Construct a Value of a single type */
3209 Value(NullTag
) { setNull(); }
3210 Value(UndefinedTag
) { setUndefined(); }
3211 Value(Int32Tag arg
) { setInt32(arg
.i32
); }
3212 Value(DoubleTag arg
) { setDouble(arg
.dbl
); }
3213 Value(StringTag arg
) { setString(arg
.str
); }
3214 Value(FunObjTag arg
) { setFunObj(arg
.obj
); }
3215 Value(NonFunObjTag arg
) { setNonFunObj(arg
.obj
); }
3216 Value(BooleanTag arg
) { setBoolean(arg
.boo
); }
3217 Value(JSWhyMagic arg
) { setMagic(arg
); }
3219 /* Construct a Value of a type dynamically chosen from a set of types */
3221 Value(FunObjOrNull arg
) { setFunObjOrNull(arg
.obj
); }
3222 Value(NonFunObjOrNullTag arg
) { setNonFunObjOrNull(arg
.obj
); }
3223 inline Value(NumberTag arg
);
3224 inline Value(ObjectTag arg
) { setObject(arg
.obj
); }
3225 inline Value(ObjectOrNullTag arg
) { setObjectOrNull(arg
.obj
); }
3226 inline Value(ObjectOrUndefinedTag arg
) { setObjectOrUndefined(arg
.obj
); }
3228 /* Change to a Value of a single type */
3231 data
.asBits
= JSVAL_BITS(JSVAL_NULL
);
3234 void setUndefined() {
3235 data
.asBits
= JSVAL_BITS(JSVAL_VOID
);
3238 void setInt32(int32 i
) {
3239 data
= INT32_TO_JSVAL_IMPL(i
);
3242 int32
&asInt32Ref() {
3243 JS_ASSERT(isInt32());
3244 return data
.s
.payload
.i32
;
3247 void setDouble(double d
) {
3248 data
= DOUBLE_TO_JSVAL_IMPL(d
);
3251 double &asDoubleRef() {
3252 JS_ASSERT(isDouble());
3253 return data
.asDouble
;
3256 void setString(JSString
*str
) {
3257 data
= STRING_TO_JSVAL_IMPL(str
);
3260 void setFunObj(JSObject
&arg
) {
3261 JS_ASSERT(JS_OBJ_IS_FUN_IMPL(&arg
));
3262 data
= OBJECT_TO_JSVAL_IMPL(JSVAL_TYPE_FUNOBJ
, &arg
);
3265 void setNonFunObj(JSObject
&arg
) {
3266 JS_ASSERT(!JS_OBJ_IS_FUN_IMPL(&arg
));
3267 data
= OBJECT_TO_JSVAL_IMPL(JSVAL_TYPE_NONFUNOBJ
, &arg
);
3270 void setBoolean(bool b
) {
3271 data
= BOOLEAN_TO_JSVAL_IMPL(b
);
3274 void setMagic(JSWhyMagic why
) {
3275 data
= MAGIC_TO_JSVAL_IMPL(why
);
3278 /* Change to a Value of a type dynamically chosen from a set of types */
3280 void setNumber(uint32 ui
) {
3281 if (ui
> JSVAL_INT_MAX
)
3282 data
= DOUBLE_TO_JSVAL_IMPL(ui
);
3284 data
= INT32_TO_JSVAL_IMPL((int32
)ui
);
3287 inline void setNumber(double d
);
3289 void setFunObjOrNull(JSObject
*arg
) {
3290 JS_ASSERT_IF(arg
, JS_OBJ_IS_FUN_IMPL(arg
));
3291 JSValueType type
= arg
? JSVAL_TYPE_FUNOBJ
: JSVAL_TYPE_NULL
;
3292 data
= OBJECT_TO_JSVAL_IMPL(type
, arg
);
3295 void setNonFunObjOrNull(JSObject
*arg
) {
3296 JS_ASSERT_IF(arg
, !JS_OBJ_IS_FUN_IMPL(arg
));
3297 JSValueType type
= arg
? JSVAL_TYPE_NONFUNOBJ
: JSVAL_TYPE_NULL
;
3298 data
= OBJECT_TO_JSVAL_IMPL(type
, arg
);
3301 inline void setObject(JSObject
&arg
) {
3302 JSValueType type
= JS_OBJ_IS_FUN_IMPL(&arg
) ? JSVAL_TYPE_FUNOBJ
3303 : JSVAL_TYPE_NONFUNOBJ
;
3304 data
= OBJECT_TO_JSVAL_IMPL(type
, &arg
);
3307 inline void setObjectOrNull(JSObject
*arg
) {
3308 JSValueType type
= arg
? JS_OBJ_IS_FUN_IMPL(arg
) ? JSVAL_TYPE_FUNOBJ
3309 : JSVAL_TYPE_NONFUNOBJ
3311 data
= OBJECT_TO_JSVAL_IMPL(type
, arg
);
3314 inline void setObjectOrUndefined(JSObject
*arg
) {
3321 /* Query a Value's type */
3323 bool isUndefined() const {
3324 return JSVAL_IS_UNDEFINED_IMPL(data
);
3327 bool isNull() const {
3328 return JSVAL_IS_NULL_IMPL(data
);
3331 bool isNullOrUndefined() const {
3332 return isNull() || isUndefined();
3335 bool isInt32() const {
3336 return JSVAL_IS_INT32_IMPL(data
);
3339 bool isInt32(int32 i32
) const {
3340 return JSVAL_IS_SPECIFIC_INT32_IMPL(data
, i32
);
3343 bool isDouble() const {
3344 return JSVAL_IS_DOUBLE_IMPL(data
);
3347 bool isNumber() const {
3348 return JSVAL_IS_NUMBER_IMPL(data
);
3351 bool isString() const {
3352 return JSVAL_IS_STRING_IMPL(data
);
3355 bool isNonFunObj() const {
3356 return JSVAL_IS_NONFUNOBJ_IMPL(data
);
3359 bool isFunObj() const {
3360 return JSVAL_IS_FUNOBJ_IMPL(data
);
3363 bool isObject() const {
3364 return JSVAL_IS_OBJECT_IMPL(data
);
3367 bool isPrimitive() const {
3368 return JSVAL_IS_PRIMITIVE_IMPL(data
);
3371 bool isObjectOrNull() const {
3372 return JSVAL_IS_OBJECT_OR_NULL_IMPL(data
);
3375 bool isGCThing() const {
3376 return JSVAL_IS_GCTHING_IMPL(data
);
3379 bool isBoolean() const {
3380 return JSVAL_IS_BOOLEAN_IMPL(data
);
3383 bool isTrue() const {
3384 return JSVAL_IS_SPECIFIC_BOOLEAN(data
, true);
3387 bool isFalse() const {
3388 return JSVAL_IS_SPECIFIC_BOOLEAN(data
, false);
3391 bool isMagic() const {
3392 return JSVAL_IS_MAGIC_IMPL(data
);
3395 bool isMagic(JSWhyMagic why
) const {
3396 JS_ASSERT_IF(isMagic(), data
.s
.payload
.why
== why
);
3397 return JSVAL_IS_MAGIC_IMPL(data
);
3400 int32
traceKind() const {
3401 JS_ASSERT(isGCThing());
3402 return JSVAL_TRACE_KIND_IMPL(data
);
3406 JSWhyMagic
whyMagic() const {
3407 JS_ASSERT(isMagic());
3408 return data
.s
.payload
.why
;
3414 bool operator==(const Value
&rhs
) const {
3415 return data
.asBits
== rhs
.data
.asBits
;
3418 bool operator!=(const Value
&rhs
) const {
3419 return data
.asBits
!= rhs
.data
.asBits
;
3422 friend bool SamePrimitiveTypeOrBothObjects(const Value
&lhs
, const Value
&rhs
) {
3423 return JSVAL_SAME_PRIMITIVE_TYPE_OR_BOTH_OBJECTS_IMPL(lhs
.data
, rhs
.data
);
3426 /* Extract a Value's payload */
3428 int32
asInt32() const {
3429 JS_ASSERT(isInt32());
3430 return JSVAL_TO_INT32_IMPL(data
);
3433 double asDouble() const {
3434 JS_ASSERT(isDouble());
3435 return data
.asDouble
;
3438 double asNumber() const {
3439 JS_ASSERT(isNumber());
3440 return isDouble() ? asDouble() : double(asInt32());
3443 JSString
*asString() const {
3444 JS_ASSERT(isString());
3445 return JSVAL_TO_STRING_IMPL(data
);
3448 JSObject
&asNonFunObj() const {
3449 JS_ASSERT(isNonFunObj());
3450 return *JSVAL_TO_OBJECT_IMPL(data
);
3453 JSObject
&asFunObj() const {
3454 JS_ASSERT(isFunObj());
3455 return *JSVAL_TO_OBJECT_IMPL(data
);
3458 JSObject
&asObject() const {
3459 JS_ASSERT(isObject());
3460 JS_ASSERT(JSVAL_TO_OBJECT_IMPL(data
));
3461 return *JSVAL_TO_OBJECT_IMPL(data
);
3464 JSObject
*asObjectOrNull() const {
3465 JS_ASSERT(isObjectOrNull());
3466 return JSVAL_TO_OBJECT_IMPL(data
);
3469 void *asGCThing() const {
3470 JS_ASSERT(isGCThing());
3471 return JSVAL_TO_GCTHING_IMPL(data
);
3474 bool asBoolean() const {
3475 JS_ASSERT(isBoolean());
3476 return JSVAL_TO_BOOLEAN_IMPL(data
);
3479 uint32
asRawUint32() const {
3480 JS_ASSERT(!isDouble());
3481 return data
.s
.payload
.u32
;
3484 uint64
asRawBits() const {
3488 JSValueType
extractNonDoubleType() const {
3489 return JSVAL_EXTRACT_NON_DOUBLE_TYPE_IMPL(data
);
3492 JSValueTag
extractNonDoubleTag() const {
3493 return JSVAL_EXTRACT_NON_DOUBLE_TAG_IMPL(data
);
3496 void unboxNonDoubleTo(uint64
*out
) const {
3497 UNBOX_NON_DOUBLE_JSVAL(data
, out
);
3500 void boxNonDoubleFrom(JSValueType type
, uint64
*out
) {
3501 data
= BOX_NON_DOUBLE_JSVAL(type
, out
);
3504 /* Swap two Values */
3506 void swap(Value
&rhs
) {
3507 jsval_layout tmp
= data
;
3515 * Private setters/getters allow the caller to read/write arbitrary types
3516 * that fit in the 64-bit payload. It is the caller's responsibility, after
3517 * storing to a value with setPrivateX to only read with getPrivateX.
3518 * Privates values are given a valid type of Int32Tag and are thus GC-safe.
3521 Value(PrivateTag arg
) {
3522 setPrivate(arg
.ptr
);
3525 bool isUnderlyingTypeOfPrivate() const {
3526 return JSVAL_IS_UNDERLYING_TYPE_OF_PRIVATE_IMPL(data
);
3529 void setPrivate(void *ptr
) {
3530 data
= PRIVATE_PTR_TO_JSVAL_IMPL(ptr
);
3533 void *asPrivate() const {
3534 JS_ASSERT(JSVAL_IS_UNDERLYING_TYPE_OF_PRIVATE_IMPL(data
));
3535 return JSVAL_TO_PRIVATE_PTR_IMPL(data
);
3538 void setPrivateUint32(uint32 ui
) {
3539 data
= PRIVATE_UINT32_TO_JSVAL_IMPL(ui
);
3542 uint32
asPrivateUint32() const {
3543 JS_ASSERT(JSVAL_IS_UNDERLYING_TYPE_OF_PRIVATE_IMPL(data
));
3544 return JSVAL_TO_PRIVATE_UINT32_IMPL(data
);
3547 uint32
&asPrivateUint32Ref() {
3548 JS_ASSERT(isDouble());
3549 return data
.s
.payload
.u32
;
3555 * As asserted above, js::Value and jsval are layout equivalent. To prevent
3556 * widespread casting, the following safe casts are provided.
3558 static inline jsval
* Jsvalify(Value
*v
) { return (jsval
*)v
; }
3559 static inline const jsval
* Jsvalify(const Value
*v
) { return (const jsval
*)v
; }
3560 static inline jsval
& Jsvalify(Value
&v
) { return (jsval
&)v
; }
3561 static inline const jsval
& Jsvalify(const Value
&v
) { return (const jsval
&)v
; }
3562 static inline Value
* Valueify(jsval
*v
) { return (Value
*)v
; }
3563 static inline const Value
* Valueify(const jsval
*v
) { return (const Value
*)v
; }
3564 static inline Value
** Valueify(jsval
**v
) { return (Value
**)v
; }
3565 static inline Value
& Valueify(jsval
&v
) { return (Value
&)v
; }
3566 static inline const Value
& Valueify(const jsval
&v
) { return (const Value
&)v
; }
3568 /* Convenience inlines. */
3569 static inline Value
undefinedValue() { return UndefinedTag(); }
3570 static inline Value
nullValue() { return NullTag(); }
3573 * js::Class is layout compatible with JSCalss and thus may be safely converted back
3574 * and forth at no cost using Jsvalify and Valueify.
3580 /* Mandatory non-null function pointer members. */
3581 PropertyOp addProperty
;
3582 PropertyOp delProperty
;
3583 PropertyOp getProperty
;
3584 PropertyOp setProperty
;
3585 JSEnumerateOp enumerate
;
3586 JSResolveOp resolve
;
3588 JSFinalizeOp finalize
;
3590 /* Optionally non-null members start here. */
3591 GetObjectOps getObjectOps
;
3592 CheckAccessOp checkAccess
;
3595 JSXDRObjectOp xdrObject
;
3596 HasInstanceOp hasInstance
;
3598 JSReserveSlotsOp reserveSlots
;
3601 JS_STATIC_ASSERT(sizeof(JSClass
) == sizeof(Class
));
3604 * js::ExtendedClass is layout compatible with JSExtendedClass and thus may be
3605 * safely converted back and forth at no cost using Jsvalify and Valueify.
3607 struct ExtendedClass
{
3609 EqualityOp equality
;
3610 JSObjectOp outerObject
;
3611 JSObjectOp innerObject
;
3612 JSIteratorOp iteratorObject
;
3613 JSObjectOp wrappedObject
; /* NB: infallible, null
3614 returns are treated as
3615 the original object */
3616 void (*reserved0
)(void);
3617 void (*reserved1
)(void);
3618 void (*reserved2
)(void);
3621 JS_STATIC_ASSERT(sizeof(JSExtendedClass
) == sizeof(ExtendedClass
));
3624 * js::PropertyDescriptor is layout compatible with JSPropertyDescriptor and
3625 * thus may be safely converted back and forth at no cost using Jsvalify and
3628 struct PropertyDescriptor
{
3637 JS_STATIC_ASSERT(sizeof(JSPropertyDescriptor
) == sizeof(PropertyDescriptor
));
3639 static JS_ALWAYS_INLINE JSClass
* Jsvalify(Class
*c
) { return (JSClass
*)c
; }
3640 static JS_ALWAYS_INLINE Class
* Valueify(JSClass
*c
) { return (Class
*)c
; }
3641 static JS_ALWAYS_INLINE JSExtendedClass
* Jsvalify(ExtendedClass
*c
) { return (JSExtendedClass
*)c
; }
3642 static JS_ALWAYS_INLINE ExtendedClass
* Valueify(JSExtendedClass
*c
) { return (ExtendedClass
*)c
; }
3643 static JS_ALWAYS_INLINE JSPropertyDescriptor
* Jsvalify(PropertyDescriptor
*p
) { return (JSPropertyDescriptor
*) p
; }
3644 static JS_ALWAYS_INLINE PropertyDescriptor
* Valueify(JSPropertyDescriptor
*p
) { return (PropertyDescriptor
*) p
; }
3647 * In some cases (quickstubs) we want to take a value in whatever manner is
3648 * appropriate for the architecture and normalize to a const js::Value &. On
3649 * x64, passing a js::Value may cause the to unnecessarily be passed through
3650 * memory instead of registers, so jsval, which is a builtin uint64 is used.
3652 #if JS_BITS_PER_WORD == 32
3653 typedef const js::Value
*ValueArgType
;
3655 static JS_ALWAYS_INLINE
const js::Value
&
3656 ValueArgToConstRef(const js::Value
*arg
)
3661 #elif JS_BITS_PER_WORD == 64
3662 typedef js::Value ValueArgType
;
3664 static JS_ALWAYS_INLINE
const Value
&
3665 ValueArgToConstRef(const Value
&v
)
3671 } /* namespace js */
3672 #endif /* __cplusplus */
3674 #endif /* jsapi_h___ */