1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
48 #include "js-config.h"
55 * In release builds, jsval and jsid are defined to be integral types. This
56 * prevents many bugs from being caught at compile time. E.g.:
59 * if (v == JS_TRUE) // error
62 * jsid id = v; // error
64 * To catch more errors, jsval and jsid are given struct types in debug builds.
65 * Struct assignment and (in C++) operator== allow correct code to be mostly
66 * oblivious to the change. This feature can be explicitly disabled in debug
67 * builds by defining JS_NO_JSVAL_JSID_STRUCT_TYPES.
69 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
71 /* Well-known JS values. N.B. These constants are initialized at startup. */
72 extern JS_PUBLIC_DATA(jsval
) JSVAL_NULL
;
73 extern JS_PUBLIC_DATA(jsval
) JSVAL_ZERO
;
74 extern JS_PUBLIC_DATA(jsval
) JSVAL_ONE
;
75 extern JS_PUBLIC_DATA(jsval
) JSVAL_FALSE
;
76 extern JS_PUBLIC_DATA(jsval
) JSVAL_TRUE
;
77 extern JS_PUBLIC_DATA(jsval
) JSVAL_VOID
;
81 /* Well-known JS values. */
82 #define JSVAL_NULL BUILD_JSVAL(JSVAL_TAG_NULL, 0)
83 #define JSVAL_ZERO BUILD_JSVAL(JSVAL_TAG_INT32, 0)
84 #define JSVAL_ONE BUILD_JSVAL(JSVAL_TAG_INT32, 1)
85 #define JSVAL_FALSE BUILD_JSVAL(JSVAL_TAG_BOOLEAN, JS_FALSE)
86 #define JSVAL_TRUE BUILD_JSVAL(JSVAL_TAG_BOOLEAN, JS_TRUE)
87 #define JSVAL_VOID BUILD_JSVAL(JSVAL_TAG_UNDEFINED, 0)
91 /************************************************************************/
93 static JS_ALWAYS_INLINE JSBool
94 JSVAL_IS_NULL(jsval v
)
97 l
.asBits
= JSVAL_BITS(v
);
98 return JSVAL_IS_NULL_IMPL(l
);
101 static JS_ALWAYS_INLINE JSBool
102 JSVAL_IS_VOID(jsval v
)
105 l
.asBits
= JSVAL_BITS(v
);
106 return JSVAL_IS_UNDEFINED_IMPL(l
);
109 static JS_ALWAYS_INLINE JSBool
110 JSVAL_IS_INT(jsval v
)
113 l
.asBits
= JSVAL_BITS(v
);
114 return JSVAL_IS_INT32_IMPL(l
);
117 static JS_ALWAYS_INLINE jsint
118 JSVAL_TO_INT(jsval v
)
121 JS_ASSERT(JSVAL_IS_INT(v
));
122 l
.asBits
= JSVAL_BITS(v
);
123 return JSVAL_TO_INT32_IMPL(l
);
126 #define JSVAL_INT_BITS 32
127 #define JSVAL_INT_MIN ((jsint)0x80000000)
128 #define JSVAL_INT_MAX ((jsint)0x7fffffff)
130 static JS_ALWAYS_INLINE jsval
131 INT_TO_JSVAL(int32 i
)
133 return IMPL_TO_JSVAL(INT32_TO_JSVAL_IMPL(i
));
136 static JS_ALWAYS_INLINE JSBool
137 JSVAL_IS_DOUBLE(jsval v
)
140 l
.asBits
= JSVAL_BITS(v
);
141 return JSVAL_IS_DOUBLE_IMPL(l
);
144 static JS_ALWAYS_INLINE jsdouble
145 JSVAL_TO_DOUBLE(jsval v
)
148 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
149 l
.asBits
= JSVAL_BITS(v
);
153 static JS_ALWAYS_INLINE jsval
154 DOUBLE_TO_JSVAL(jsdouble d
)
156 d
= JS_CANONICALIZE_NAN(d
);
157 return IMPL_TO_JSVAL(DOUBLE_TO_JSVAL_IMPL(d
));
160 static JS_ALWAYS_INLINE jsval
161 UINT_TO_JSVAL(uint32 i
)
163 if (i
<= JSVAL_INT_MAX
)
164 return INT_TO_JSVAL((int32
)i
);
165 return DOUBLE_TO_JSVAL((jsdouble
)i
);
168 static JS_ALWAYS_INLINE JSBool
169 JSVAL_IS_NUMBER(jsval v
)
172 l
.asBits
= JSVAL_BITS(v
);
173 return JSVAL_IS_NUMBER_IMPL(l
);
176 static JS_ALWAYS_INLINE JSBool
177 JSVAL_IS_STRING(jsval v
)
180 l
.asBits
= JSVAL_BITS(v
);
181 return JSVAL_IS_STRING_IMPL(l
);
184 static JS_ALWAYS_INLINE JSString
*
185 JSVAL_TO_STRING(jsval v
)
188 JS_ASSERT(JSVAL_IS_STRING(v
));
189 l
.asBits
= JSVAL_BITS(v
);
190 return JSVAL_TO_STRING_IMPL(l
);
193 static JS_ALWAYS_INLINE jsval
194 STRING_TO_JSVAL(JSString
*str
)
196 return IMPL_TO_JSVAL(STRING_TO_JSVAL_IMPL(str
));
199 static JS_ALWAYS_INLINE JSBool
200 JSVAL_IS_OBJECT(jsval v
)
203 l
.asBits
= JSVAL_BITS(v
);
204 return JSVAL_IS_OBJECT_OR_NULL_IMPL(l
);
207 static JS_ALWAYS_INLINE JSObject
*
208 JSVAL_TO_OBJECT(jsval v
)
211 JS_ASSERT(JSVAL_IS_OBJECT(v
));
212 l
.asBits
= JSVAL_BITS(v
);
213 return JSVAL_TO_OBJECT_IMPL(l
);
216 static JS_ALWAYS_INLINE jsval
217 OBJECT_TO_JSVAL(JSObject
*obj
)
220 return IMPL_TO_JSVAL(OBJECT_TO_JSVAL_IMPL(obj
));
224 static JS_ALWAYS_INLINE JSBool
225 JSVAL_IS_BOOLEAN(jsval v
)
228 l
.asBits
= JSVAL_BITS(v
);
229 return JSVAL_IS_BOOLEAN_IMPL(l
);
232 static JS_ALWAYS_INLINE JSBool
233 JSVAL_TO_BOOLEAN(jsval v
)
236 JS_ASSERT(JSVAL_IS_BOOLEAN(v
));
237 l
.asBits
= JSVAL_BITS(v
);
238 return JSVAL_TO_BOOLEAN_IMPL(l
);
241 static JS_ALWAYS_INLINE jsval
242 BOOLEAN_TO_JSVAL(JSBool b
)
244 return IMPL_TO_JSVAL(BOOLEAN_TO_JSVAL_IMPL(b
));
247 static JS_ALWAYS_INLINE JSBool
248 JSVAL_IS_PRIMITIVE(jsval v
)
251 l
.asBits
= JSVAL_BITS(v
);
252 return JSVAL_IS_PRIMITIVE_IMPL(l
);
255 static JS_ALWAYS_INLINE JSBool
256 JSVAL_IS_GCTHING(jsval v
)
259 l
.asBits
= JSVAL_BITS(v
);
260 return JSVAL_IS_GCTHING_IMPL(l
);
263 static JS_ALWAYS_INLINE
void *
264 JSVAL_TO_GCTHING(jsval v
)
267 JS_ASSERT(JSVAL_IS_GCTHING(v
));
268 l
.asBits
= JSVAL_BITS(v
);
269 return JSVAL_TO_GCTHING_IMPL(l
);
272 /* To be GC-safe, privates are tagged as doubles. */
274 static JS_ALWAYS_INLINE jsval
275 PRIVATE_TO_JSVAL(void *ptr
)
277 return IMPL_TO_JSVAL(PRIVATE_PTR_TO_JSVAL_IMPL(ptr
));
280 static JS_ALWAYS_INLINE
void *
281 JSVAL_TO_PRIVATE(jsval v
)
284 JS_ASSERT(JSVAL_IS_DOUBLE(v
));
285 l
.asBits
= JSVAL_BITS(v
);
286 return JSVAL_TO_PRIVATE_PTR_IMPL(l
);
289 /************************************************************************/
292 * A jsid is an identifier for a property or method of an object which is
293 * either a 31-bit signed integer, interned string or object. If XML is
294 * enabled, there is an additional singleton jsid value; see
295 * JS_DEFAULT_XML_NAMESPACE_ID below. Finally, there is an additional jsid
296 * value, JSID_VOID, which does not occur in JS scripts but may be used to
297 * indicate the absence of a valid jsid.
299 * A jsid is not implicitly convertible to or from a jsval; JS_ValueToId or
300 * JS_IdToValue must be used instead.
303 #define JSID_TYPE_STRING 0x0
304 #define JSID_TYPE_INT 0x1
305 #define JSID_TYPE_VOID 0x2
306 #define JSID_TYPE_OBJECT 0x4
307 #define JSID_TYPE_DEFAULT_XML_NAMESPACE 0x6
308 #define JSID_TYPE_MASK 0x7
311 * Avoid using canonical 'id' for jsid parameters since this is a magic word in
312 * Objective-C++ which, apparently, wants to be able to #include jsapi.h.
316 static JS_ALWAYS_INLINE JSBool
317 JSID_IS_STRING(jsid id
)
319 return (JSID_BITS(id
) & JSID_TYPE_MASK
) == 0;
322 static JS_ALWAYS_INLINE JSString
*
323 JSID_TO_STRING(jsid id
)
325 JS_ASSERT(JSID_IS_STRING(id
));
326 return (JSString
*)(JSID_BITS(id
));
329 JS_PUBLIC_API(JSBool
)
330 JS_StringHasBeenInterned(JSString
*str
);
332 /* A jsid may only hold an interned JSString. */
333 static JS_ALWAYS_INLINE jsid
334 INTERNED_STRING_TO_JSID(JSString
*str
)
337 JS_ASSERT(JS_StringHasBeenInterned(str
));
338 JS_ASSERT(((size_t)str
& JSID_TYPE_MASK
) == 0);
339 JSID_BITS(id
) = (size_t)str
;
343 static JS_ALWAYS_INLINE JSBool
346 return !!(JSID_BITS(id
) & JSID_TYPE_INT
);
349 static JS_ALWAYS_INLINE int32
352 JS_ASSERT(JSID_IS_INT(id
));
353 return ((int32
)JSID_BITS(id
)) >> 1;
356 #define JSID_INT_MIN (-(1 << 30))
357 #define JSID_INT_MAX ((1 << 30) - 1)
359 static JS_ALWAYS_INLINE JSBool
360 INT_FITS_IN_JSID(int32 i
)
362 return ((jsuint
)(i
) - (jsuint
)JSID_INT_MIN
<=
363 (jsuint
)(JSID_INT_MAX
- JSID_INT_MIN
));
366 static JS_ALWAYS_INLINE jsid
370 JS_ASSERT(INT_FITS_IN_JSID(i
));
371 JSID_BITS(id
) = ((i
<< 1) | JSID_TYPE_INT
);
375 static JS_ALWAYS_INLINE JSBool
376 JSID_IS_OBJECT(jsid id
)
378 return (JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_OBJECT
&&
379 (size_t)JSID_BITS(id
) != JSID_TYPE_OBJECT
;
382 static JS_ALWAYS_INLINE JSObject
*
383 JSID_TO_OBJECT(jsid id
)
385 JS_ASSERT(JSID_IS_OBJECT(id
));
386 return (JSObject
*)(JSID_BITS(id
) & ~(size_t)JSID_TYPE_MASK
);
389 static JS_ALWAYS_INLINE jsid
390 OBJECT_TO_JSID(JSObject
*obj
)
393 JS_ASSERT(obj
!= NULL
);
394 JS_ASSERT(((size_t)obj
& JSID_TYPE_MASK
) == 0);
395 JSID_BITS(id
) = ((size_t)obj
| JSID_TYPE_OBJECT
);
399 static JS_ALWAYS_INLINE JSBool
400 JSID_IS_GCTHING(jsid id
)
402 return JSID_IS_STRING(id
) || JSID_IS_OBJECT(id
);
405 static JS_ALWAYS_INLINE
void *
406 JSID_TO_GCTHING(jsid id
)
408 return (void *)(JSID_BITS(id
) & ~(size_t)JSID_TYPE_MASK
);
412 * The magic XML namespace id is not a valid jsid. Global object classes in
413 * embeddings that enable JS_HAS_XML_SUPPORT (E4X) should handle this id.
416 static JS_ALWAYS_INLINE JSBool
417 JSID_IS_DEFAULT_XML_NAMESPACE(jsid id
)
419 JS_ASSERT_IF(((size_t)JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
,
420 JSID_BITS(id
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
);
421 return ((size_t)JSID_BITS(id
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
);
424 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
425 extern JS_PUBLIC_DATA(jsid
) JS_DEFAULT_XML_NAMESPACE_ID
;
427 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid)JSID_TYPE_DEFAULT_XML_NAMESPACE)
431 * A void jsid is not a valid id and only arises as an exceptional API return
432 * value, such as in JS_NextProperty. Embeddings must not pass JSID_VOID into
433 * JSAPI entry points expecting a jsid and do not need to handle JSID_VOID in
434 * hooks receiving a jsid except when explicitly noted in the API contract.
437 static JS_ALWAYS_INLINE JSBool
438 JSID_IS_VOID(jsid id
)
440 JS_ASSERT_IF(((size_t)JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_VOID
,
441 JSID_BITS(id
) == JSID_TYPE_VOID
);
442 return ((size_t)JSID_BITS(id
) == JSID_TYPE_VOID
);
445 static JS_ALWAYS_INLINE JSBool
446 JSID_IS_EMPTY(jsid id
)
448 return ((size_t)JSID_BITS(id
) == JSID_TYPE_OBJECT
);
453 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
454 extern JS_PUBLIC_DATA(jsid
) JSID_VOID
;
455 extern JS_PUBLIC_DATA(jsid
) JSID_EMPTY
;
457 # define JSID_VOID ((jsid)JSID_TYPE_VOID)
458 # define JSID_EMPTY ((jsid)JSID_TYPE_OBJECT)
461 /************************************************************************/
463 /* Lock and unlock the GC thing held by a jsval. */
464 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
465 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
467 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
468 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
471 /* Property attributes, set in JSPropertySpec and passed to API functions. */
472 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
473 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
474 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
475 #define JSPROP_GETTER 0x10 /* property holds getter function */
476 #define JSPROP_SETTER 0x20 /* property holds setter function */
477 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
478 property; don't copy the property on
479 set of the same-named property in an
480 object that delegates to a prototype
481 containing this property */
482 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
483 #define JSPROP_SHORTID 0x100 /* set in JSPropertyDescriptor.attrs
484 if getters/setters use a shortid */
486 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
487 #define JSFUN_CONSTRUCTOR 0x02 /* native that can be called as a ctor
488 without creating a this object */
489 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
490 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
492 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
494 #define JSFUN_THISP_FLAGS(f) (f)
495 #define JSFUN_THISP_TEST(f,t) ((f) & t)
497 #define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
498 #define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
499 #define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
500 #define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
502 #define JSFUN_FLAGS_MASK 0x07fa /* overlay JSFUN_* attributes --
503 bits 12-15 are used internally to
504 flag interpreted functions */
506 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
507 instead of defaulting to class gsops
508 for property holding function */
511 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
512 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
513 * methods of a class prototype that are exposed as static methods taking an
514 * extra leading argument: the generic |this| parameter.
516 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
517 * that struct must live at least as long as the native static method object
518 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
519 * JSFunctionSpec structs are allocated in static arrays.
521 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
524 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
525 * comment in jstypes.h regarding safe int64 usage.
527 extern JS_PUBLIC_API(int64
)
530 /* Don't want to export data, so provide accessors for non-inline jsvals. */
531 extern JS_PUBLIC_API(jsval
)
532 JS_GetNaNValue(JSContext
*cx
);
534 extern JS_PUBLIC_API(jsval
)
535 JS_GetNegativeInfinityValue(JSContext
*cx
);
537 extern JS_PUBLIC_API(jsval
)
538 JS_GetPositiveInfinityValue(JSContext
*cx
);
540 extern JS_PUBLIC_API(jsval
)
541 JS_GetEmptyStringValue(JSContext
*cx
);
544 * Format is a string of the following characters (spaces are insignificant),
545 * specifying the tabulated type conversions:
548 * c uint16/jschar ECMA uint16, Unicode char
550 * u uint32 ECMA uint32
551 * j int32 Rounded int32 (coordinate)
552 * d jsdouble IEEE double
553 * I jsdouble Integral IEEE double
555 * S JSString * Unicode string, accessed by a JSString pointer
556 * W jschar * Unicode character vector, 0-terminated (W for wide)
557 * o JSObject * Object reference
558 * f JSFunction * Function private
559 * v jsval Argument value (no conversion)
560 * * N/A Skip this argument (no vararg)
561 * / N/A End of required arguments
563 * The variable argument list after format must consist of &b, &c, &s, e.g.,
564 * where those variables have the types given above. For the pointer types
565 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
566 * to the JS runtime, not to the calling native code. The runtime promises
567 * to keep this memory valid so long as argv refers to allocated stack space
568 * (so long as the native function is active).
570 * Fewer arguments than format specifies may be passed only if there is a /
571 * in format after the last required argument specifier and argc is at least
572 * the number of required arguments. More arguments than format specifies
573 * may be passed without error; it is up to the caller to deal with trailing
574 * unconverted arguments.
576 extern JS_PUBLIC_API(JSBool
)
577 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
581 extern JS_PUBLIC_API(JSBool
)
582 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
583 const char *format
, va_list ap
);
586 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
589 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
590 * The handler function has this signature (see jspubtd.h):
592 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
593 * JSBool fromJS, jsval **vpp, va_list *app);
595 * It should return true on success, and return false after reporting an error
596 * or detecting an already-reported error.
598 * For a given format string, for example "AA", the formatter is called from
599 * JS_ConvertArgumentsVA like so:
601 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
603 * sp points into the arguments array on the JS stack, while ap points into
604 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
605 * the formatter to convert zero or more jsvals at sp to zero or more C values
606 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
607 * (via *app) to point past the converted arguments and their result pointers
610 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
612 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
614 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
615 * format specifier and store them at sp, updating ap and sp appropriately.
617 * The "..." after "AA" is the rest of the format string that was passed into
618 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
619 * in each Convert or PushArguments call is passed to the formatter, so that
620 * one such function may implement several formats, in order to share code.
622 * Remove just forgets about any handler associated with format. Add does not
623 * copy format, it points at the string storage allocated by the caller, which
624 * is typically a string constant. If format is in dynamic storage, it is up
625 * to the caller to keep the string alive until Remove is called.
627 extern JS_PUBLIC_API(JSBool
)
628 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
629 JSArgumentFormatter formatter
);
631 extern JS_PUBLIC_API(void)
632 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
634 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
636 extern JS_PUBLIC_API(JSBool
)
637 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
639 extern JS_PUBLIC_API(JSBool
)
640 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
642 extern JS_PUBLIC_API(JSFunction
*)
643 JS_ValueToFunction(JSContext
*cx
, jsval v
);
645 extern JS_PUBLIC_API(JSFunction
*)
646 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
648 extern JS_PUBLIC_API(JSString
*)
649 JS_ValueToString(JSContext
*cx
, jsval v
);
651 extern JS_PUBLIC_API(JSString
*)
652 JS_ValueToSource(JSContext
*cx
, jsval v
);
654 extern JS_PUBLIC_API(JSBool
)
655 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
657 extern JS_PUBLIC_API(JSBool
)
658 JS_DoubleIsInt32(jsdouble d
, jsint
*ip
);
661 * Convert a value to a number, then to an int32, according to the ECMA rules
664 extern JS_PUBLIC_API(JSBool
)
665 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
668 * Convert a value to a number, then to a uint32, according to the ECMA rules
671 extern JS_PUBLIC_API(JSBool
)
672 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
675 * Convert a value to a number, then to an int32 if it fits by rounding to
676 * nearest; but failing with an error report if the double is out of range
679 extern JS_PUBLIC_API(JSBool
)
680 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
683 * ECMA ToUint16, for mapping a jsval to a Unicode point.
685 extern JS_PUBLIC_API(JSBool
)
686 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
688 extern JS_PUBLIC_API(JSBool
)
689 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
691 extern JS_PUBLIC_API(JSType
)
692 JS_TypeOfValue(JSContext
*cx
, jsval v
);
694 extern JS_PUBLIC_API(const char *)
695 JS_GetTypeName(JSContext
*cx
, JSType type
);
697 extern JS_PUBLIC_API(JSBool
)
698 JS_StrictlyEqual(JSContext
*cx
, jsval v1
, jsval v2
);
700 extern JS_PUBLIC_API(JSBool
)
701 JS_SameValue(JSContext
*cx
, jsval v1
, jsval v2
);
703 /************************************************************************/
706 * Initialization, locking, contexts, and memory allocation.
708 * It is important that the first runtime and first context be created in a
709 * single-threaded fashion, otherwise the behavior of the library is undefined.
710 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
712 #define JS_NewRuntime JS_Init
713 #define JS_DestroyRuntime JS_Finish
714 #define JS_LockRuntime JS_Lock
715 #define JS_UnlockRuntime JS_Unlock
717 extern JS_PUBLIC_API(JSRuntime
*)
718 JS_NewRuntime(uint32 maxbytes
);
720 extern JS_PUBLIC_API(void)
721 JS_CommenceRuntimeShutDown(JSRuntime
*rt
);
723 extern JS_PUBLIC_API(void)
724 JS_DestroyRuntime(JSRuntime
*rt
);
726 extern JS_PUBLIC_API(void)
729 JS_PUBLIC_API(void *)
730 JS_GetRuntimePrivate(JSRuntime
*rt
);
733 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
735 extern JS_PUBLIC_API(void)
736 JS_BeginRequest(JSContext
*cx
);
738 extern JS_PUBLIC_API(void)
739 JS_EndRequest(JSContext
*cx
);
741 /* Yield to pending GC operations, regardless of request depth */
742 extern JS_PUBLIC_API(void)
743 JS_YieldRequest(JSContext
*cx
);
745 extern JS_PUBLIC_API(jsrefcount
)
746 JS_SuspendRequest(JSContext
*cx
);
748 extern JS_PUBLIC_API(void)
749 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
754 class JSAutoRequest
{
756 JSAutoRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
757 : mContext(cx
), mSaveDepth(0) {
758 JS_GUARD_OBJECT_NOTIFIER_INIT
;
759 JS_BeginRequest(mContext
);
762 JS_EndRequest(mContext
);
766 mSaveDepth
= JS_SuspendRequest(mContext
);
769 JS_ResumeRequest(mContext
, mSaveDepth
);
774 jsrefcount mSaveDepth
;
775 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
779 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
780 static void operator delete(void *, size_t) { };
784 class JSAutoSuspendRequest
{
786 JSAutoSuspendRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
787 : mContext(cx
), mSaveDepth(0) {
788 JS_GUARD_OBJECT_NOTIFIER_INIT
;
790 mSaveDepth
= JS_SuspendRequest(mContext
);
793 ~JSAutoSuspendRequest() {
799 JS_ResumeRequest(mContext
, mSaveDepth
);
806 jsrefcount mSaveDepth
;
807 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
811 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
812 static void operator delete(void *, size_t) { };
819 extern JS_PUBLIC_API(void)
820 JS_Lock(JSRuntime
*rt
);
822 extern JS_PUBLIC_API(void)
823 JS_Unlock(JSRuntime
*rt
);
825 extern JS_PUBLIC_API(JSContextCallback
)
826 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
828 extern JS_PUBLIC_API(JSContext
*)
829 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
831 extern JS_PUBLIC_API(void)
832 JS_DestroyContext(JSContext
*cx
);
834 extern JS_PUBLIC_API(void)
835 JS_DestroyContextNoGC(JSContext
*cx
);
837 extern JS_PUBLIC_API(void)
838 JS_DestroyContextMaybeGC(JSContext
*cx
);
840 extern JS_PUBLIC_API(void *)
841 JS_GetContextPrivate(JSContext
*cx
);
843 extern JS_PUBLIC_API(void)
844 JS_SetContextPrivate(JSContext
*cx
, void *data
);
846 extern JS_PUBLIC_API(JSRuntime
*)
847 JS_GetRuntime(JSContext
*cx
);
849 extern JS_PUBLIC_API(JSContext
*)
850 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
852 extern JS_PUBLIC_API(JSVersion
)
853 JS_GetVersion(JSContext
*cx
);
855 extern JS_PUBLIC_API(JSVersion
)
856 JS_SetVersion(JSContext
*cx
, JSVersion version
);
858 extern JS_PUBLIC_API(const char *)
859 JS_VersionToString(JSVersion version
);
861 extern JS_PUBLIC_API(JSVersion
)
862 JS_StringToVersion(const char *string
);
865 * JS options are orthogonal to version, and may be freely composed with one
866 * another as well as with version.
868 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
869 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
871 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
872 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
873 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
874 the last object on its 'obj'
875 param's scope chain as the
876 ECMA 'variables object' */
877 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
878 JS_BIT(3) /* context private data points
879 to an nsISupports subclass */
880 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
881 promises to execute compiled
882 script once only; enables
883 compile-time scope chain
884 resolution of consts. */
885 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
886 option supported for the
887 XUL preprocessor and kindred
889 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
890 parse <!-- --> as a token,
891 not backward compatible with
892 the comment-hiding hack used
893 in HTML script tags. */
894 #define JSOPTION_DONT_REPORT_UNCAUGHT \
895 JS_BIT(8) /* When returning from the
896 outermost API call, prevent
897 uncaught exceptions from
898 being converted to error
901 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
902 regular expression which
903 backtracks more than n^3
904 times, where n is length
905 of the input string */
906 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
907 statement context per
908 ECMA-262 Edition 3. */
910 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
912 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
913 that a null rval out-param
914 will be passed to each call
915 to JS_ExecuteScript. */
916 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
917 contexts' global objects
918 (see JS_GetGlobalObject),
919 leaving that up to the
922 #define JSOPTION_METHODJIT JS_BIT(14) /* Whole-method JIT. */
924 extern JS_PUBLIC_API(uint32
)
925 JS_GetOptions(JSContext
*cx
);
927 extern JS_PUBLIC_API(uint32
)
928 JS_SetOptions(JSContext
*cx
, uint32 options
);
930 extern JS_PUBLIC_API(uint32
)
931 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
933 extern JS_PUBLIC_API(const char *)
934 JS_GetImplementationVersion(void);
936 extern JS_PUBLIC_API(JSCompartmentCallback
)
937 JS_SetCompartmentCallback(JSRuntime
*rt
, JSCompartmentCallback callback
);
939 extern JS_PUBLIC_API(JSWrapObjectCallback
)
940 JS_SetWrapObjectCallback(JSContext
*cx
, JSWrapObjectCallback callback
);
942 extern JS_PUBLIC_API(JSCrossCompartmentCall
*)
943 JS_EnterCrossCompartmentCall(JSContext
*cx
, JSObject
*target
);
945 extern JS_PUBLIC_API(void)
946 JS_LeaveCrossCompartmentCall(JSCrossCompartmentCall
*call
);
948 extern JS_PUBLIC_API(void *)
949 JS_SetCompartmentPrivate(JSContext
*cx
, JSCompartment
*compartment
, void *data
);
951 extern JS_PUBLIC_API(void *)
952 JS_GetCompartmentPrivate(JSContext
*cx
, JSCompartment
*compartment
);
954 extern JS_PUBLIC_API(JSBool
)
955 JS_WrapObject(JSContext
*cx
, JSObject
**objp
);
957 extern JS_PUBLIC_API(JSBool
)
958 JS_WrapValue(JSContext
*cx
, jsval
*vp
);
960 extern JS_FRIEND_API(JSCompartment
*)
961 js_SwitchToCompartment(JSContext
*cx
, JSCompartment
*compartment
);
963 extern JS_FRIEND_API(JSCompartment
*)
964 js_SwitchToObjectCompartment(JSContext
*cx
, JSObject
*obj
);
969 class JS_PUBLIC_API(JSAutoCrossCompartmentCall
)
971 JSCrossCompartmentCall
*call
;
973 JSAutoCrossCompartmentCall() : call(NULL
) {}
975 bool enter(JSContext
*cx
, JSObject
*target
);
977 bool entered() const { return call
!= NULL
; }
979 ~JSAutoCrossCompartmentCall() {
981 JS_LeaveCrossCompartmentCall(call
);
984 void swap(JSAutoCrossCompartmentCall
&other
) {
985 JSCrossCompartmentCall
*tmp
= call
;
991 class JSAutoEnterCompartment
994 JSCompartment
*compartment
;
996 JSAutoEnterCompartment(JSContext
*cx
, JSCompartment
*newCompartment
) : cx(cx
) {
997 compartment
= js_SwitchToCompartment(cx
, newCompartment
);
999 JSAutoEnterCompartment(JSContext
*cx
, JSObject
*target
) : cx(cx
) {
1000 compartment
= js_SwitchToObjectCompartment(cx
, target
);
1002 ~JSAutoEnterCompartment() {
1003 js_SwitchToCompartment(cx
, compartment
);
1010 extern JS_PUBLIC_API(JSObject
*)
1011 JS_GetGlobalObject(JSContext
*cx
);
1013 extern JS_PUBLIC_API(void)
1014 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
1017 * Initialize standard JS class constructors, prototypes, and any top-level
1018 * functions and constants associated with the standard classes (e.g. isNaN
1021 * NB: This sets cx's global object to obj if it was null.
1023 extern JS_PUBLIC_API(JSBool
)
1024 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
1027 * Resolve id, which must contain either a string or an int, to a standard
1028 * class name in obj if possible, defining the class's constructor and/or
1029 * prototype and storing true in *resolved. If id does not name a standard
1030 * class or a top-level property induced by initializing a standard class,
1031 * store false in *resolved and just return true. Return false on error,
1032 * as usual for JSBool result-typed API entry points.
1034 * This API can be called directly from a global object class's resolve op,
1035 * to define standard classes lazily. The class's enumerate op should call
1036 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
1037 * loops any classes not yet resolved lazily.
1039 extern JS_PUBLIC_API(JSBool
)
1040 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsid id
,
1043 extern JS_PUBLIC_API(JSBool
)
1044 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
1047 * Enumerate any already-resolved standard class ids into ida, or into a new
1048 * JSIdArray if ida is null. Return the augmented array on success, null on
1049 * failure with ida (if it was non-null on entry) destroyed.
1051 extern JS_PUBLIC_API(JSIdArray
*)
1052 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
1055 extern JS_PUBLIC_API(JSBool
)
1056 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
1059 extern JS_PUBLIC_API(JSObject
*)
1060 JS_GetScopeChain(JSContext
*cx
);
1062 extern JS_PUBLIC_API(JSObject
*)
1063 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
1065 extern JS_PUBLIC_API(JSObject
*)
1066 JS_GetGlobalForScopeChain(JSContext
*cx
);
1068 #ifdef JS_HAS_CTYPES
1070 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
1071 * object will be sealed.
1073 extern JS_PUBLIC_API(JSBool
)
1074 JS_InitCTypesClass(JSContext
*cx
, JSObject
*global
);
1077 * Convert a unicode string 'source' of length 'slen' to the platform native
1078 * charset, returning a null-terminated string allocated with JS_malloc. On
1079 * failure, this function should report an error.
1082 (* JSCTypesUnicodeToNativeFun
)(JSContext
*cx
, const jschar
*source
, size_t slen
);
1085 * Set of function pointers that ctypes can use for various internal functions.
1086 * See JS_SetCTypesCallbacks below. Providing NULL for a function is safe,
1087 * and will result in the applicable ctypes functionality not being available.
1089 struct JSCTypesCallbacks
{
1090 JSCTypesUnicodeToNativeFun unicodeToNative
;
1093 typedef struct JSCTypesCallbacks JSCTypesCallbacks
;
1096 * Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
1097 * pointer to static data that exists for the lifetime of 'ctypesObj', but it
1098 * may safely be altered after calling this function and without having
1099 * to call this function again.
1101 extern JS_PUBLIC_API(JSBool
)
1102 JS_SetCTypesCallbacks(JSContext
*cx
, JSObject
*ctypesObj
, JSCTypesCallbacks
*callbacks
);
1106 * Macros to hide interpreter stack layout details from a JSFastNative using
1107 * its jsval *vp parameter. The stack layout underlying invocation can't change
1108 * without breaking source and binary compatibility (argv[-2] is well-known to
1109 * be the callee jsval, and argv[-1] is as well known to be |this|).
1111 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
1112 * is the global object, so embeddings implementing fast natives *must* call
1113 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
1114 * which should propagate as a false return from native functions and hooks.
1116 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
1117 * handle a null obj parameter by returning false (throwing a TypeError if
1118 * given non-null argv), so most native functions that type-check their |this|
1119 * parameter need not add null checking.
1121 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
1122 * methods that may inspect their callee must defer setting their return value
1123 * until after any such possible inspection. Otherwise the return value will be
1124 * inspected instead of the callee function object.
1126 * WARNING: These are not (yet) mandatory macros, but new code outside of the
1127 * engine should use them. In the Mozilla 2.0 milestone their definitions may
1128 * change incompatibly.
1130 * N.B. constructors must not use JS_THIS, as no 'this' object has been created.
1133 #define JS_CALLEE(cx,vp) ((vp)[0])
1134 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
1135 #define JS_THIS_OBJECT(cx,vp) (JSVAL_TO_OBJECT(JS_THIS(cx,vp)))
1136 #define JS_ARGV(cx,vp) ((vp) + 2)
1137 #define JS_RVAL(cx,vp) (*(vp))
1138 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
1140 extern JS_PUBLIC_API(jsval
)
1141 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
1146 JS_THIS(JSContext
*cx
, jsval
*vp
)
1148 return JSVAL_IS_PRIMITIVE(vp
[1]) ? JS_ComputeThis(cx
, vp
) : vp
[1];
1152 extern JS_PUBLIC_API(void *)
1153 JS_malloc(JSContext
*cx
, size_t nbytes
);
1155 extern JS_PUBLIC_API(void *)
1156 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
1158 extern JS_PUBLIC_API(void)
1159 JS_free(JSContext
*cx
, void *p
);
1161 extern JS_PUBLIC_API(void)
1162 JS_updateMallocCounter(JSContext
*cx
, size_t nbytes
);
1164 extern JS_PUBLIC_API(char *)
1165 JS_strdup(JSContext
*cx
, const char *s
);
1167 extern JS_PUBLIC_API(JSBool
)
1168 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
1171 * A GC root is a pointer to a jsval, JSObject * or JSString * that itself
1172 * points into the GC heap. JS_AddValueRoot takes a pointer to a jsval and
1173 * JS_AddGCThingRoot takes a pointer to a JSObject * or JString *.
1175 * Note that, since JS_Add*Root stores the address of a variable (of type
1176 * jsval, JSString *, or JSObject *), that variable must live until
1177 * JS_Remove*Root is called to remove that variable. For example, after:
1179 * void some_function() {
1181 * JS_AddNamedRootedValue(cx, &v, "name");
1183 * the caller must perform
1185 * JS_RemoveRootedValue(cx, &v);
1187 * before some_function() returns.
1189 * Also, use JS_AddNamed*Root(cx, &structPtr->memberObj, "structPtr->memberObj")
1190 * in preference to JS_Add*Root(cx, &structPtr->memberObj), in order to identify
1191 * roots by their source callsites. This way, you can find the callsite while
1192 * debugging if you should fail to do JS_Remove*Root(cx, &structPtr->memberObj)
1193 * before freeing structPtr's memory.
1195 extern JS_PUBLIC_API(JSBool
)
1196 JS_AddValueRoot(JSContext
*cx
, jsval
*vp
);
1198 extern JS_PUBLIC_API(JSBool
)
1199 JS_AddStringRoot(JSContext
*cx
, JSString
**rp
);
1201 extern JS_PUBLIC_API(JSBool
)
1202 JS_AddObjectRoot(JSContext
*cx
, JSObject
**rp
);
1204 extern JS_PUBLIC_API(JSBool
)
1205 JS_AddGCThingRoot(JSContext
*cx
, void **rp
);
1207 #ifdef NAME_ALL_GC_ROOTS
1208 #define JS_DEFINE_TO_TOKEN(def) #def
1209 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
1210 #define JS_AddValueRoot(cx,vp) JS_AddNamedValueRoot((cx), (vp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1211 #define JS_AddStringRoot(cx,rp) JS_AddNamedStringRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1212 #define JS_AddObjectRoot(cx,rp) JS_AddNamedObjectRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1213 #define JS_AddGCThingRoot(cx,rp) JS_AddNamedGCThingRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1216 extern JS_PUBLIC_API(JSBool
)
1217 JS_AddNamedValueRoot(JSContext
*cx
, jsval
*vp
, const char *name
);
1219 extern JS_PUBLIC_API(JSBool
)
1220 JS_AddNamedStringRoot(JSContext
*cx
, JSString
**rp
, const char *name
);
1222 extern JS_PUBLIC_API(JSBool
)
1223 JS_AddNamedObjectRoot(JSContext
*cx
, JSObject
**rp
, const char *name
);
1225 extern JS_PUBLIC_API(JSBool
)
1226 JS_AddNamedGCThingRoot(JSContext
*cx
, void **rp
, const char *name
);
1228 extern JS_PUBLIC_API(JSBool
)
1229 JS_RemoveValueRoot(JSContext
*cx
, jsval
*vp
);
1231 extern JS_PUBLIC_API(JSBool
)
1232 JS_RemoveStringRoot(JSContext
*cx
, JSString
**rp
);
1234 extern JS_PUBLIC_API(JSBool
)
1235 JS_RemoveObjectRoot(JSContext
*cx
, JSObject
**rp
);
1237 extern JS_PUBLIC_API(JSBool
)
1238 JS_RemoveGCThingRoot(JSContext
*cx
, void **rp
);
1240 /* TODO: remove these APIs */
1242 extern JS_FRIEND_API(JSBool
)
1243 js_AddRootRT(JSRuntime
*rt
, jsval
*vp
, const char *name
);
1245 extern JS_FRIEND_API(JSBool
)
1246 js_AddGCThingRootRT(JSRuntime
*rt
, void **rp
, const char *name
);
1248 extern JS_FRIEND_API(JSBool
)
1249 js_RemoveRoot(JSRuntime
*rt
, void *rp
);
1252 * This symbol may be used by embedders to detect the change from the old
1253 * JS_AddRoot(JSContext *, void *) APIs to the new ones above.
1255 #define JS_TYPED_ROOTING_API
1257 /* Obsolete rooting APIs. */
1258 #define JS_ClearNewbornRoots(cx) ((void) 0)
1259 #define JS_EnterLocalRootScope(cx) (JS_TRUE)
1260 #define JS_LeaveLocalRootScope(cx) ((void) 0)
1261 #define JS_LeaveLocalRootScopeWithResult(cx, rval) ((void) 0)
1262 #define JS_ForgetLocalRoot(cx, thing) ((void) 0)
1264 typedef enum JSGCRootType
{
1265 JS_GC_ROOT_VALUE_PTR
,
1266 JS_GC_ROOT_GCTHING_PTR
1270 extern JS_PUBLIC_API(void)
1271 JS_DumpNamedRoots(JSRuntime
*rt
,
1272 void (*dump
)(const char *name
, void *rp
, JSGCRootType type
, void *data
),
1277 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
1278 * The root is pointed at by rp; if the root is unnamed, name is null; data is
1279 * supplied from the third parameter to JS_MapGCRoots.
1281 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
1282 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
1283 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
1284 * constants are flags; you can OR them together.
1286 * This function acquires and releases rt's GC lock around the mapping of the
1287 * roots table, so the map function should run to completion in as few cycles
1288 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
1289 * or any JS API entry point that acquires locks, without double-tripping or
1290 * deadlocking on the GC lock.
1292 * The JSGCRootType parameter indicates whether rp is a pointer to a Value
1293 * (which is obtained by '(Value *)rp') or a pointer to a GC-thing pointer
1294 * (which is obtained by '(void **)rp').
1296 * JS_MapGCRoots returns the count of roots that were successfully mapped.
1298 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
1299 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
1300 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
1303 (* JSGCRootMapFun
)(void *rp
, JSGCRootType type
, const char *name
, void *data
);
1305 extern JS_PUBLIC_API(uint32
)
1306 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
1308 extern JS_PUBLIC_API(JSBool
)
1309 JS_LockGCThing(JSContext
*cx
, void *thing
);
1311 extern JS_PUBLIC_API(JSBool
)
1312 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
1314 extern JS_PUBLIC_API(JSBool
)
1315 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
1317 extern JS_PUBLIC_API(JSBool
)
1318 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
1321 * Register externally maintained GC roots.
1323 * traceOp: the trace operation. For each root the implementation should call
1324 * JS_CallTracer whenever the root contains a traceable thing.
1325 * data: the data argument to pass to each invocation of traceOp.
1327 extern JS_PUBLIC_API(void)
1328 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
1331 * For implementors of JSMarkOp. All new code should implement JSTraceOp
1334 extern JS_PUBLIC_API(void)
1335 JS_MarkGCThing(JSContext
*cx
, jsval v
, const char *name
, void *arg
);
1338 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
1339 * enumerate all references to traceable things reachable via a property or
1340 * other strong ref identified for debugging purposes by name or index or
1341 * a naming callback.
1343 * By definition references to traceable things include non-null pointers
1344 * to JSObject, JSString and jsdouble and corresponding jsvals.
1346 * See the JSTraceOp typedef in jspubtd.h.
1349 /* Trace kinds to pass to JS_Tracing. */
1350 #define JSTRACE_OBJECT 0
1351 #define JSTRACE_STRING 1
1354 * Use the following macros to check if a particular jsval is a traceable
1355 * thing and to extract the thing and its kind to pass to JS_CallTracer.
1357 static JS_ALWAYS_INLINE JSBool
1358 JSVAL_IS_TRACEABLE(jsval v
)
1361 l
.asBits
= JSVAL_BITS(v
);
1362 return JSVAL_IS_TRACEABLE_IMPL(l
);
1365 static JS_ALWAYS_INLINE
void *
1366 JSVAL_TO_TRACEABLE(jsval v
)
1368 return JSVAL_TO_GCTHING(v
);
1371 static JS_ALWAYS_INLINE uint32
1372 JSVAL_TRACE_KIND(jsval v
)
1375 JS_ASSERT(JSVAL_IS_GCTHING(v
));
1376 l
.asBits
= JSVAL_BITS(v
);
1377 return JSVAL_TRACE_KIND_IMPL(l
);
1382 JSTraceCallback callback
;
1383 JSTraceNamePrinter debugPrinter
;
1384 const void *debugPrintArg
;
1385 size_t debugPrintIndex
;
1389 * The method to call on each reference to a traceable thing stored in a
1390 * particular JSObject or other runtime structure. With DEBUG defined the
1391 * caller before calling JS_CallTracer must initialize JSTracer fields
1392 * describing the reference using the macros below.
1394 extern JS_PUBLIC_API(void)
1395 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
1398 * Set debugging information about a reference to a traceable thing to prepare
1399 * for the following call to JS_CallTracer.
1401 * When printer is null, arg must be const char * or char * C string naming
1402 * the reference and index must be either (size_t)-1 indicating that the name
1403 * alone describes the reference or it must be an index into some array vector
1404 * that stores the reference.
1406 * When printer callback is not null, the arg and index arguments are
1407 * available to the callback as debugPrinterArg and debugPrintIndex fields
1410 * The storage for name or callback's arguments needs to live only until
1411 * the following call to JS_CallTracer returns.
1414 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1416 (trc)->debugPrinter = (printer); \
1417 (trc)->debugPrintArg = (arg); \
1418 (trc)->debugPrintIndex = (index); \
1421 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1427 * Convenience macro to describe the argument of JS_CallTracer using C string
1430 # define JS_SET_TRACING_INDEX(trc, name, index) \
1431 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1434 * Convenience macro to describe the argument of JS_CallTracer using C string.
1436 # define JS_SET_TRACING_NAME(trc, name) \
1437 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1440 * Convenience macro to invoke JS_CallTracer using C string as the name for
1441 * the reference to a traceable thing.
1443 # define JS_CALL_TRACER(trc, thing, kind, name) \
1445 JS_SET_TRACING_NAME(trc, name); \
1446 JS_CallTracer((trc), (thing), (kind)); \
1450 * Convenience macros to invoke JS_CallTracer when jsval represents a
1451 * reference to a traceable thing.
1453 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1455 if (JSVAL_IS_TRACEABLE(val)) { \
1456 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1457 JSVAL_TRACE_KIND(val), name); \
1461 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1463 JSObject *obj_ = (object); \
1465 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1468 #define JS_CALL_STRING_TRACER(trc, string, name) \
1470 JSString *str_ = (string); \
1472 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1476 * API for JSTraceCallback implementations.
1478 # define JS_TRACER_INIT(trc, cx_, callback_) \
1480 (trc)->context = (cx_); \
1481 (trc)->callback = (callback_); \
1482 (trc)->debugPrinter = NULL; \
1483 (trc)->debugPrintArg = NULL; \
1484 (trc)->debugPrintIndex = (size_t)-1; \
1487 extern JS_PUBLIC_API(void)
1488 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1490 extern JS_PUBLIC_API(void)
1491 JS_TraceRuntime(JSTracer
*trc
);
1495 extern JS_PUBLIC_API(void)
1496 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1497 void *thing
, uint32 kind
, JSBool includeDetails
);
1500 * DEBUG-only method to dump the object graph of heap-allocated things.
1502 * fp: file for the dump output.
1503 * start: when non-null, dump only things reachable from start
1504 * thing. Otherwise dump all things reachable from the
1506 * startKind: trace kind of start if start is not null. Must be 0 when
1508 * thingToFind: dump only paths in the object graph leading to thingToFind
1510 * maxDepth: the upper bound on the number of edges to descend from the
1512 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1514 extern JS_PUBLIC_API(JSBool
)
1515 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1516 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1521 * Garbage collector API.
1523 extern JS_PUBLIC_API(void)
1524 JS_GC(JSContext
*cx
);
1526 extern JS_PUBLIC_API(void)
1527 JS_MaybeGC(JSContext
*cx
);
1529 extern JS_PUBLIC_API(JSGCCallback
)
1530 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1532 extern JS_PUBLIC_API(JSGCCallback
)
1533 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1535 extern JS_PUBLIC_API(JSBool
)
1536 JS_IsGCMarkingTracer(JSTracer
*trc
);
1538 extern JS_PUBLIC_API(JSBool
)
1539 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1541 typedef enum JSGCParamKey
{
1542 /* Maximum nominal heap before last ditch GC. */
1545 /* Number of JS_malloc bytes before last ditch GC. */
1546 JSGC_MAX_MALLOC_BYTES
= 1,
1548 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1549 JSGC_STACKPOOL_LIFESPAN
= 2,
1552 * The factor that defines when the GC is invoked. The factor is a
1553 * percent of the memory allocated by the GC after the last run of
1554 * the GC. When the current memory allocated by the GC is more than
1555 * this percent then the GC is invoked. The factor cannot be less
1556 * than 100 since the current memory allocated by the GC cannot be less
1557 * than the memory allocated after the last run of the GC.
1559 JSGC_TRIGGER_FACTOR
= 3,
1561 /* Amount of bytes allocated by the GC. */
1564 /* Number of times when GC was invoked. */
1567 /* Max size of the code cache in bytes. */
1568 JSGC_MAX_CODE_CACHE_BYTES
= 6
1571 extern JS_PUBLIC_API(void)
1572 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1574 extern JS_PUBLIC_API(uint32
)
1575 JS_GetGCParameter(JSRuntime
*rt
, JSGCParamKey key
);
1577 extern JS_PUBLIC_API(void)
1578 JS_SetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
, uint32 value
);
1580 extern JS_PUBLIC_API(uint32
)
1581 JS_GetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
);
1584 * Flush the code cache for the current thread. The operation might be
1585 * delayed if the cache cannot be flushed currently because native
1586 * code is currently executing.
1589 extern JS_PUBLIC_API(void)
1590 JS_FlushCaches(JSContext
*cx
);
1593 * Add a finalizer for external strings created by JS_NewExternalString (see
1594 * below) using a type-code returned from this function, and that understands
1595 * how to free or release the memory pointed at by JS_GetStringChars(str).
1597 * Return a nonnegative type index if there is room for finalizer in the
1598 * global GC finalizers table, else return -1. If the engine is compiled
1599 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1600 * be invoked on the primordial thread only, at startup -- or else the entire
1601 * program must single-thread itself while loading a module that calls this
1604 extern JS_PUBLIC_API(intN
)
1605 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1608 * Remove finalizer from the global GC finalizers table, returning its type
1609 * code if found, -1 if not found.
1611 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1612 * if you compile the engine JS_THREADSAFE: this function may be called for a
1613 * given finalizer pointer on only one thread; different threads may call to
1614 * remove distinct finalizers safely.
1616 * You must ensure that all strings with finalizer's type have been collected
1617 * before calling this function. Otherwise, string data will be leaked by the
1618 * GC, for want of a finalizer to call.
1620 extern JS_PUBLIC_API(intN
)
1621 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1624 * Create a new JSString whose chars member refers to external memory, i.e.,
1625 * memory requiring spe, type-specific finalization. The type code must
1626 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1628 extern JS_PUBLIC_API(JSString
*)
1629 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1632 * Returns the external-string finalizer index for this string, or -1 if it is
1633 * an "internal" (native to JS engine) string.
1635 extern JS_PUBLIC_API(intN
)
1636 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1639 * Deprecated. Use JS_SetNativeStackQuoata instead.
1641 extern JS_PUBLIC_API(void)
1642 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1645 * Set the size of the native stack that should not be exceed. To disable
1646 * stack size checking pass 0.
1648 extern JS_PUBLIC_API(void)
1649 JS_SetNativeStackQuota(JSContext
*cx
, size_t stackSize
);
1653 * Set the quota on the number of bytes that stack-like data structures can
1654 * use when the runtime compiles and executes scripts. These structures
1655 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1656 * The default quota is 32MB which is quite generous.
1658 * The function must be called before any script compilation or execution API
1659 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1662 extern JS_PUBLIC_API(void)
1663 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1665 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1667 /************************************************************************/
1670 * Classes, objects, and properties.
1672 typedef void (*JSClassInternal
)();
1674 /* For detailed comments on the function pointer types, see jspubtd.h. */
1679 /* Mandatory non-null function pointer members. */
1680 JSPropertyOp addProperty
;
1681 JSPropertyOp delProperty
;
1682 JSPropertyOp getProperty
;
1683 JSPropertyOp setProperty
;
1684 JSEnumerateOp enumerate
;
1685 JSResolveOp resolve
;
1686 JSConvertOp convert
;
1687 JSFinalizeOp finalize
;
1689 /* Optionally non-null members start here. */
1690 JSClassInternal reserved0
;
1691 JSCheckAccessOp checkAccess
;
1694 JSXDRObjectOp xdrObject
;
1695 JSHasInstanceOp hasInstance
;
1698 JSClassInternal reserved1
;
1702 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1703 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1704 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1705 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1706 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1707 object in prototype chain
1708 passed in via *objp in/out
1710 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1712 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1715 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1716 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1717 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1719 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1720 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1721 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1722 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1723 << JSCLASS_RESERVED_SLOTS_SHIFT)
1724 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1725 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1726 & JSCLASS_RESERVED_SLOTS_MASK)
1728 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1729 JSCLASS_RESERVED_SLOTS_WIDTH)
1731 #define JSCLASS_INTERNAL_FLAG1 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1732 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1733 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1735 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1736 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1737 #define JSCLASS_INTERNAL_FLAG2 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+4))
1739 /* Additional global reserved slots, beyond those for standard prototypes. */
1740 #define JSRESERVED_GLOBAL_SLOTS_COUNT 4
1741 #define JSRESERVED_GLOBAL_COMPARTMENT (JSProto_LIMIT * 3)
1742 #define JSRESERVED_GLOBAL_THIS (JSRESERVED_GLOBAL_COMPARTMENT + 1)
1743 #define JSRESERVED_GLOBAL_THROWTYPEERROR (JSRESERVED_GLOBAL_THIS + 1)
1744 #define JSRESERVED_GLOBAL_REGEXP_STATICS (JSRESERVED_GLOBAL_THROWTYPEERROR + 1)
1747 * ECMA-262 requires that most constructors used internally create objects
1748 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1749 * member initial value. The "original ... value" verbiage is there because
1750 * in ECMA-262, global properties naming class objects are read/write and
1751 * deleteable, for the most part.
1753 * Implementing this efficiently requires that global objects have classes
1754 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was
1755 * prevously allowed, but is now an ES5 violation and thus unsupported.
1757 #define JSCLASS_GLOBAL_FLAGS \
1758 (JSCLASS_IS_GLOBAL | \
1759 JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT * 3 + JSRESERVED_GLOBAL_SLOTS_COUNT))
1761 /* Fast access to the original value of each standard class's prototype. */
1762 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
1763 #define JSCLASS_CACHED_PROTO_WIDTH 8
1764 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
1765 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
1766 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
1768 >> JSCLASS_CACHED_PROTO_SHIFT) \
1769 & JSCLASS_CACHED_PROTO_MASK))
1771 /* Initializer for unused members of statically initialized JSClass structs. */
1772 #define JSCLASS_NO_INTERNAL_MEMBERS 0,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
1773 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,JSCLASS_NO_INTERNAL_MEMBERS
1777 jsid vector
[1]; /* actually, length jsid words */
1780 extern JS_PUBLIC_API(void)
1781 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
1783 extern JS_PUBLIC_API(JSBool
)
1784 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
1786 extern JS_PUBLIC_API(JSBool
)
1787 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
1790 * JSNewResolveOp flag bits.
1792 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
1793 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
1794 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
1795 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
1796 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
1797 #define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
1799 extern JS_PUBLIC_API(JSBool
)
1800 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
1802 extern JS_PUBLIC_API(JSBool
)
1803 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
1805 extern JS_PUBLIC_API(JSBool
)
1806 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsid id
);
1808 extern JS_PUBLIC_API(JSBool
)
1809 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
1811 extern JS_PUBLIC_API(void)
1812 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
1814 struct JSConstDoubleSpec
{
1822 * To define an array element rather than a named property member, cast the
1823 * element's index to (const char *) and initialize name with it, and set the
1824 * JSPROP_INDEX bit in flags.
1826 struct JSPropertySpec
{
1830 JSPropertyOp getter
;
1831 JSPropertyOp setter
;
1834 struct JSFunctionSpec
{
1842 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
1843 * that's passed to JS_DefineFunctions or JS_InitClass.
1845 #define JS_FS_END JS_FS(NULL,NULL,0,0)
1848 * Initializer macros for a JSFunctionSpec array element. JS_FN (whose name
1849 * pays homage to the old JSNative/JSFastNative split) simply adds the flag
1852 #define JS_FS(name,call,nargs,flags) \
1853 {name, call, nargs, flags}
1854 #define JS_FN(name,call,nargs,flags) \
1855 {name, call, nargs, (flags) | JSFUN_STUB_GSOPS}
1857 extern JS_PUBLIC_API(JSObject
*)
1858 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
1859 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
1860 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
1861 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
1863 #ifdef JS_THREADSAFE
1864 extern JS_PUBLIC_API(JSClass
*)
1865 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
1867 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
1869 extern JS_PUBLIC_API(JSClass
*)
1870 JS_GetClass(JSObject
*obj
);
1872 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
1875 extern JS_PUBLIC_API(JSBool
)
1876 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
1878 extern JS_PUBLIC_API(JSBool
)
1879 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
1881 extern JS_PUBLIC_API(void *)
1882 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
1884 extern JS_PUBLIC_API(JSBool
)
1885 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
1887 extern JS_PUBLIC_API(void *)
1888 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
1891 extern JS_PUBLIC_API(JSObject
*)
1892 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
1894 extern JS_PUBLIC_API(JSBool
)
1895 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
1897 extern JS_PUBLIC_API(JSObject
*)
1898 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
1900 extern JS_PUBLIC_API(JSBool
)
1901 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
1903 extern JS_PUBLIC_API(JSObject
*)
1904 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
1907 * Get a unique identifier for obj, good for the lifetime of obj (even if it
1908 * is moved by a copying GC). Return false on failure (likely out of memory),
1909 * and true with *idp containing the unique id on success.
1911 extern JS_PUBLIC_API(JSBool
)
1912 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
1914 extern JS_PUBLIC_API(JSObject
*)
1915 JS_NewGlobalObject(JSContext
*cx
, JSClass
*clasp
);
1917 extern JS_PUBLIC_API(JSObject
*)
1918 JS_NewCompartmentAndGlobalObject(JSContext
*cx
, JSClass
*clasp
, JSPrincipals
*principals
);
1920 extern JS_PUBLIC_API(JSObject
*)
1921 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
1923 /* Queries the [[Extensible]] property of the object. */
1924 extern JS_PUBLIC_API(JSBool
)
1925 JS_IsExtensible(JSObject
*obj
);
1928 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1929 * proto if proto's actual parameter value is null.
1931 extern JS_PUBLIC_API(JSObject
*)
1932 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
1936 * Freeze obj, and all objects it refers to, recursively. This will not recurse
1937 * through non-extensible objects, on the assumption that those are already
1940 extern JS_PUBLIC_API(JSBool
)
1941 JS_DeepFreezeObject(JSContext
*cx
, JSObject
*obj
);
1944 * Freezes an object; see ES5's Object.freeze(obj) method.
1946 extern JS_PUBLIC_API(JSBool
)
1947 JS_FreezeObject(JSContext
*cx
, JSObject
*obj
);
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_CompileUCScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2432 JSPrincipals
*principals
,
2433 const jschar
*chars
, size_t length
,
2434 const char *filename
, uintN lineno
,
2437 extern JS_PUBLIC_API(JSScript
*)
2438 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2440 extern JS_PUBLIC_API(JSScript
*)
2441 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2444 extern JS_PUBLIC_API(JSScript
*)
2445 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2446 const char *filename
, FILE *fh
,
2447 JSPrincipals
*principals
);
2450 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2451 * in order to keep a JSScript and its atoms safe from garbage collection after
2452 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2453 * E.g., and without error checks:
2455 * JSScript *script = JS_CompileFile(cx, global, filename);
2456 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2457 * JS_AddNamedObjectRoot(cx, &scrobj, "scrobj");
2460 * JS_ExecuteScript(cx, global, script, &result);
2462 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2463 * JS_RemoveObjectRoot(cx, &scrobj);
2465 extern JS_PUBLIC_API(JSObject
*)
2466 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2469 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2470 * called on script yet, the return value will be null.
2472 extern JS_PUBLIC_API(JSObject
*)
2473 JS_GetScriptObject(JSScript
*script
);
2475 extern JS_PUBLIC_API(void)
2476 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2478 extern JS_PUBLIC_API(JSFunction
*)
2479 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, 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_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2486 JSPrincipals
*principals
, const char *name
,
2487 uintN nargs
, const char **argnames
,
2488 const char *bytes
, size_t length
,
2489 const char *filename
, uintN lineno
);
2491 extern JS_PUBLIC_API(JSFunction
*)
2492 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, 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(JSFunction
*)
2498 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2499 JSPrincipals
*principals
, const char *name
,
2500 uintN nargs
, const char **argnames
,
2501 const jschar
*chars
, size_t length
,
2502 const char *filename
, uintN lineno
);
2504 extern JS_PUBLIC_API(JSFunction
*)
2505 JS_CompileUCFunctionForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2506 JSPrincipals
*principals
, const char *name
,
2507 uintN nargs
, const char **argnames
,
2508 const jschar
*chars
, size_t length
,
2509 const char *filename
, uintN lineno
,
2512 extern JS_PUBLIC_API(JSString
*)
2513 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2517 * API extension: OR this into indent to avoid pretty-printing the decompiled
2518 * source resulting from JS_DecompileFunction{,Body}.
2520 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2522 extern JS_PUBLIC_API(JSString
*)
2523 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2525 extern JS_PUBLIC_API(JSString
*)
2526 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2529 * NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
2530 * parameter as the initial scope chain header, the 'this' keyword value, and
2531 * the variables object (ECMA parlance for where 'var' and 'function' bind
2532 * names) of the execution context for script.
2534 * Using obj as the variables object is problematic if obj's parent (which is
2535 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2536 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2537 * created by assignment to an unbound id, 'x = 0', go in the last object on
2538 * the scope chain linked by parent.
2540 * ECMA calls that last scoping object the "global object", but note that many
2541 * embeddings have several such objects. ECMA requires that "global code" be
2542 * executed with the variables object equal to this global object. But these
2543 * JS API entry points provide freedom to execute code against a "sub-global",
2544 * i.e., a parented or scoped object, in which case the variables object will
2545 * differ from the last object on the scope chain, resulting in confusing and
2546 * non-ECMA explicit vs. implicit variable creation.
2548 * Caveat embedders: unless you already depend on this buggy variables object
2549 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2550 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2551 * someone may have set other options on cx already -- for each context in the
2552 * application, if you pass parented objects as the obj parameter, or may ever
2553 * pass such objects in the future.
2555 * Why a runtime option? The alternative is to add six or so new API entry
2556 * points with signatures matching the following six, and that doesn't seem
2557 * worth the code bloat cost. Such new entry points would probably have less
2558 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2559 * OTOH, can be more easily hacked into existing code that does not depend on
2560 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2561 * etc., entry points.
2563 extern JS_PUBLIC_API(JSBool
)
2564 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2567 * Execute either the function-defining prolog of a script, or the script's
2568 * main body, but not both.
2570 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2572 extern JS_PUBLIC_API(JSBool
)
2573 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2574 const char *bytes
, uintN length
,
2575 const char *filename
, uintN lineno
,
2578 extern JS_PUBLIC_API(JSBool
)
2579 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2580 JSPrincipals
*principals
,
2581 const char *bytes
, uintN length
,
2582 const char *filename
, uintN lineno
,
2585 extern JS_PUBLIC_API(JSBool
)
2586 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2587 const jschar
*chars
, uintN length
,
2588 const char *filename
, uintN lineno
,
2591 extern JS_PUBLIC_API(JSBool
)
2592 JS_EvaluateUCScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2593 JSPrincipals
*principals
,
2594 const jschar
*chars
, uintN length
,
2595 const char *filename
, uintN lineno
,
2596 jsval
*rval
, JSVersion version
);
2598 extern JS_PUBLIC_API(JSBool
)
2599 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2600 JSPrincipals
*principals
,
2601 const jschar
*chars
, uintN length
,
2602 const char *filename
, uintN lineno
,
2605 extern JS_PUBLIC_API(JSBool
)
2606 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2607 jsval
*argv
, jsval
*rval
);
2609 extern JS_PUBLIC_API(JSBool
)
2610 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2611 jsval
*argv
, jsval
*rval
);
2613 extern JS_PUBLIC_API(JSBool
)
2614 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2615 jsval
*argv
, jsval
*rval
);
2618 * These functions allow setting an operation callback that will be called
2619 * from the thread the context is associated with some time after any thread
2620 * triggered the callback using JS_TriggerOperationCallback(cx).
2622 * In a threadsafe build the engine internally triggers operation callbacks
2623 * under certain circumstances (i.e. GC and title transfer) to force the
2624 * context to yield its current request, which the engine always
2625 * automatically does immediately prior to calling the callback function.
2626 * The embedding should thus not rely on callbacks being triggered through
2627 * the external API only.
2629 * Important note: Additional callbacks can occur inside the callback handler
2630 * if it re-enters the JS engine. The embedding must ensure that the callback
2631 * is disconnected before attempting such re-entry.
2634 extern JS_PUBLIC_API(JSOperationCallback
)
2635 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
);
2637 extern JS_PUBLIC_API(JSOperationCallback
)
2638 JS_GetOperationCallback(JSContext
*cx
);
2640 extern JS_PUBLIC_API(void)
2641 JS_TriggerOperationCallback(JSContext
*cx
);
2643 extern JS_PUBLIC_API(void)
2644 JS_TriggerAllOperationCallbacks(JSRuntime
*rt
);
2646 extern JS_PUBLIC_API(JSBool
)
2647 JS_IsRunning(JSContext
*cx
);
2650 * Saving and restoring frame chains.
2652 * These two functions are used to set aside cx's call stack while that stack
2653 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2654 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2655 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2656 * matching JS_RestoreFrameChain calls.
2658 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2659 * return does not signify an error, and JS_RestoreFrameChain handles a null
2660 * frame pointer argument safely.
2662 extern JS_PUBLIC_API(JSStackFrame
*)
2663 JS_SaveFrameChain(JSContext
*cx
);
2665 extern JS_PUBLIC_API(void)
2666 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2668 /************************************************************************/
2673 * NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
2674 * on error (signified by null return), it leaves bytes owned by the caller.
2675 * So the caller must free bytes in the error case, if it has no use for them.
2676 * In contrast, all the JS_New*StringCopy* functions do not take ownership of
2677 * the character memory passed to them -- they copy it.
2679 extern JS_PUBLIC_API(JSString
*)
2680 JS_NewString(JSContext
*cx
, char *bytes
, size_t length
);
2682 extern JS_PUBLIC_API(JSString
*)
2683 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2685 extern JS_PUBLIC_API(JSString
*)
2686 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2688 extern JS_PUBLIC_API(JSString
*)
2689 JS_InternString(JSContext
*cx
, const char *s
);
2691 extern JS_PUBLIC_API(JSString
*)
2692 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
2694 extern JS_PUBLIC_API(JSString
*)
2695 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
2697 extern JS_PUBLIC_API(JSString
*)
2698 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
2700 extern JS_PUBLIC_API(JSString
*)
2701 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
2703 extern JS_PUBLIC_API(JSString
*)
2704 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
2706 extern JS_PUBLIC_API(char *)
2707 JS_GetStringBytes(JSString
*str
);
2709 extern JS_PUBLIC_API(jschar
*)
2710 JS_GetStringChars(JSString
*str
);
2712 extern JS_PUBLIC_API(size_t)
2713 JS_GetStringLength(JSString
*str
);
2715 extern JS_PUBLIC_API(const char *)
2716 JS_GetStringBytesZ(JSContext
*cx
, JSString
*str
);
2718 extern JS_PUBLIC_API(const jschar
*)
2719 JS_GetStringCharsZ(JSContext
*cx
, JSString
*str
);
2721 extern JS_PUBLIC_API(intN
)
2722 JS_CompareStrings(JSString
*str1
, JSString
*str2
);
2725 * This function is now obsolete and behaves the same as JS_NewUCString. Use
2726 * JS_NewUCString instead.
2728 extern JS_PUBLIC_API(JSString
*)
2729 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
2732 * Mutable string support. A string's characters are never mutable in this JS
2733 * implementation, but a dependent string is a substring of another dependent
2734 * or immutable string, and a rope is a lazily concatenated string that creates
2735 * its underlying buffer the first time it is accessed. Even after a rope
2736 * creates its underlying buffer, it still considered mutable. The direct data
2737 * members of the (opaque to API clients) JSString struct may be changed in a
2738 * single-threaded way for dependent strings and ropes.
2740 * Therefore mutable strings (ropes and dependent strings) cannot be used by
2741 * more than one thread at a time. You may call JS_MakeStringImmutable to
2742 * convert the string from a mutable string to an immutable (and therefore
2743 * thread-safe) string. The engine takes care of converting ropes and dependent
2744 * strings to immutable for you if you store strings in multi-threaded objects
2745 * using JS_SetProperty or kindred API entry points.
2747 * If you store a JSString pointer in a native data structure that is (safely)
2748 * accessible to multiple threads, you must call JS_MakeStringImmutable before
2749 * retiring the store.
2753 * Create a dependent string, i.e., a string that owns no character storage,
2754 * but that refers to a slice of another string's chars. Dependent strings
2755 * are mutable by definition, so the thread safety comments above apply.
2757 extern JS_PUBLIC_API(JSString
*)
2758 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
2762 * Concatenate two strings, possibly resulting in a rope.
2763 * See above for thread safety comments.
2765 extern JS_PUBLIC_API(JSString
*)
2766 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
2769 * Convert a dependent string into an independent one. This function does not
2770 * change the string's mutability, so the thread safety comments above apply.
2772 extern JS_PUBLIC_API(const jschar
*)
2773 JS_UndependString(JSContext
*cx
, JSString
*str
);
2776 * Convert a mutable string (either rope or dependent) into an immutable,
2779 extern JS_PUBLIC_API(JSBool
)
2780 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
2783 * Return JS_TRUE if C (char []) strings passed via the API and internally
2786 JS_PUBLIC_API(JSBool
)
2787 JS_CStringsAreUTF8(void);
2790 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
2791 * can never be changed. This API must be called before the first call to
2795 JS_SetCStringsAreUTF8(void);
2798 * Character encoding support.
2800 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
2801 * of the destination buffer before the call; on return, *dstlenp contains the
2802 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
2803 * stored. To determine the necessary destination buffer size, make a sizing
2804 * call that passes NULL for dst.
2806 * On errors, the functions report the error. In that case, *dstlenp contains
2807 * the number of characters or bytes transferred so far. If cx is NULL, no
2808 * error is reported on failure, and the functions simply return JS_FALSE.
2810 * NB: Neither function stores an additional zero byte or jschar after the
2811 * transcoded string.
2813 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
2814 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
2815 * errors if the character sequence is malformed. If UTF-8 support is
2816 * disabled, the functions deflate and inflate, respectively.
2818 JS_PUBLIC_API(JSBool
)
2819 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
2822 JS_PUBLIC_API(JSBool
)
2823 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
2827 * A variation on JS_EncodeCharacters where a null terminated string is
2828 * returned that you are expected to call JS_free on when done.
2830 JS_PUBLIC_API(char *)
2831 JS_EncodeString(JSContext
*cx
, JSString
*str
);
2833 /************************************************************************/
2837 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
2840 * JSON.stringify as specified by ES3.1 (draft)
2842 JS_PUBLIC_API(JSBool
)
2843 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
, jsval space
,
2844 JSONWriteCallback callback
, void *data
);
2847 * Retrieve a toJSON function. If found, set vp to its result.
2849 JS_PUBLIC_API(JSBool
)
2850 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
2853 * JSON.parse as specified by ES3.1 (draft)
2855 JS_PUBLIC_API(JSONParser
*)
2856 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
2858 JS_PUBLIC_API(JSBool
)
2859 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
2861 JS_PUBLIC_API(JSBool
)
2862 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
, jsval reviver
);
2864 /************************************************************************/
2867 * Locale specific string conversion and error message callbacks.
2869 struct JSLocaleCallbacks
{
2870 JSLocaleToUpperCase localeToUpperCase
;
2871 JSLocaleToLowerCase localeToLowerCase
;
2872 JSLocaleCompare localeCompare
;
2873 JSLocaleToUnicode localeToUnicode
;
2874 JSErrorCallback localeGetErrorMessage
;
2878 * Establish locale callbacks. The pointer must persist as long as the
2879 * JSContext. Passing NULL restores the default behaviour.
2881 extern JS_PUBLIC_API(void)
2882 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
2885 * Return the address of the current locale callbacks struct, which may
2888 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
2889 JS_GetLocaleCallbacks(JSContext
*cx
);
2891 /************************************************************************/
2898 * Report an exception represented by the sprintf-like conversion of format
2899 * and its arguments. This exception message string is passed to a pre-set
2900 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
2901 * the JSErrorReporter typedef).
2903 extern JS_PUBLIC_API(void)
2904 JS_ReportError(JSContext
*cx
, const char *format
, ...);
2907 * Use an errorNumber to retrieve the format string, args are char *
2909 extern JS_PUBLIC_API(void)
2910 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
2911 void *userRef
, const uintN errorNumber
, ...);
2914 * Use an errorNumber to retrieve the format string, args are jschar *
2916 extern JS_PUBLIC_API(void)
2917 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
2918 void *userRef
, const uintN errorNumber
, ...);
2921 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
2922 * Return true if there was no error trying to issue the warning, and if the
2923 * warning was not converted into an error due to the JSOPTION_WERROR option
2924 * being set, false otherwise.
2926 extern JS_PUBLIC_API(JSBool
)
2927 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
2929 extern JS_PUBLIC_API(JSBool
)
2930 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
2931 JSErrorCallback errorCallback
, void *userRef
,
2932 const uintN errorNumber
, ...);
2934 extern JS_PUBLIC_API(JSBool
)
2935 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
2936 JSErrorCallback errorCallback
, void *userRef
,
2937 const uintN errorNumber
, ...);
2940 * Complain when out of memory.
2942 extern JS_PUBLIC_API(void)
2943 JS_ReportOutOfMemory(JSContext
*cx
);
2946 * Complain when an allocation size overflows the maximum supported limit.
2948 extern JS_PUBLIC_API(void)
2949 JS_ReportAllocationOverflow(JSContext
*cx
);
2951 struct JSErrorReport
{
2952 const char *filename
; /* source file name, URL, etc., or null */
2953 uintN lineno
; /* source line number */
2954 const char *linebuf
; /* offending source line without final \n */
2955 const char *tokenptr
; /* pointer to error token in linebuf */
2956 const jschar
*uclinebuf
; /* unicode (original) line buffer */
2957 const jschar
*uctokenptr
; /* unicode (original) token pointer */
2958 uintN flags
; /* error/warning, etc. */
2959 uintN errorNumber
; /* the error number, e.g. see js.msg */
2960 const jschar
*ucmessage
; /* the (default) error message */
2961 const jschar
**messageArgs
; /* arguments for the error message */
2965 * JSErrorReport flag values. These may be freely composed.
2967 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
2968 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
2969 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
2970 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
2973 * This condition is an error in strict mode code, a warning if
2974 * JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
2975 * all. We check the strictness of the context's top frame's script;
2976 * where that isn't appropriate, the caller should do the right checks
2977 * itself instead of using this flag.
2979 #define JSREPORT_STRICT_MODE_ERROR 0x8
2982 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
2983 * has been thrown for this runtime error, and the host should ignore it.
2984 * Exception-aware hosts should also check for JS_IsExceptionPending if
2985 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
2988 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
2989 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
2990 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
2991 #define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) & \
2992 JSREPORT_STRICT_MODE_ERROR) != 0)
2994 extern JS_PUBLIC_API(JSErrorReporter
)
2995 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
2997 /************************************************************************/
3000 * Regular Expressions.
3002 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
3003 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
3004 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
3005 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
3006 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
3007 #define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
3009 extern JS_PUBLIC_API(JSObject
*)
3010 JS_NewRegExpObject(JSContext
*cx
, JSObject
*obj
, char *bytes
, size_t length
, uintN flags
);
3012 extern JS_PUBLIC_API(JSObject
*)
3013 JS_NewUCRegExpObject(JSContext
*cx
, JSObject
*obj
, jschar
*chars
, size_t length
, uintN flags
);
3015 extern JS_PUBLIC_API(void)
3016 JS_SetRegExpInput(JSContext
*cx
, JSObject
*obj
, JSString
*input
, JSBool multiline
);
3018 extern JS_PUBLIC_API(void)
3019 JS_ClearRegExpStatics(JSContext
*cx
, JSObject
*obj
);
3021 extern JS_PUBLIC_API(JSBool
)
3022 JS_ExecuteRegExp(JSContext
*cx
, JSObject
*obj
, JSObject
*reobj
, jschar
*chars
, size_t length
,
3023 size_t *indexp
, JSBool test
, jsval
*rval
);
3025 /* RegExp interface for clients without a global object. */
3027 extern JS_PUBLIC_API(JSObject
*)
3028 JS_NewRegExpObjectNoStatics(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
3030 extern JS_PUBLIC_API(JSObject
*)
3031 JS_NewUCRegExpObjectNoStatics(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
3033 extern JS_PUBLIC_API(JSBool
)
3034 JS_ExecuteRegExpNoStatics(JSContext
*cx
, JSObject
*reobj
, jschar
*chars
, size_t length
,
3035 size_t *indexp
, JSBool test
, jsval
*rval
);
3037 /************************************************************************/
3039 extern JS_PUBLIC_API(JSBool
)
3040 JS_IsExceptionPending(JSContext
*cx
);
3042 extern JS_PUBLIC_API(JSBool
)
3043 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
3045 extern JS_PUBLIC_API(void)
3046 JS_SetPendingException(JSContext
*cx
, jsval v
);
3048 extern JS_PUBLIC_API(void)
3049 JS_ClearPendingException(JSContext
*cx
);
3051 extern JS_PUBLIC_API(JSBool
)
3052 JS_ReportPendingException(JSContext
*cx
);
3055 * Save the current exception state. This takes a snapshot of cx's current
3056 * exception state without making any change to that state.
3058 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
3059 * (to restore that saved state, overriding any more recent state) or else to
3060 * JS_DropExceptionState (to free the state struct in case it is not correct
3061 * or desirable to restore it). Both Restore and Drop free the state struct,
3062 * so callers must stop using the pointer returned from Save after calling the
3063 * Release or Drop API.
3065 extern JS_PUBLIC_API(JSExceptionState
*)
3066 JS_SaveExceptionState(JSContext
*cx
);
3068 extern JS_PUBLIC_API(void)
3069 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3071 extern JS_PUBLIC_API(void)
3072 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3075 * If the given value is an exception object that originated from an error,
3076 * the exception will contain an error report struct, and this API will return
3077 * the address of that struct. Otherwise, it returns NULL. The lifetime of
3078 * the error report struct that might be returned is the same as the lifetime
3079 * of the exception object.
3081 extern JS_PUBLIC_API(JSErrorReport
*)
3082 JS_ErrorFromException(JSContext
*cx
, jsval v
);
3085 * Given a reported error's message and JSErrorReport struct pointer, throw
3086 * the corresponding exception on cx.
3088 extern JS_PUBLIC_API(JSBool
)
3089 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
3090 JSErrorReport
*reportp
);
3093 * Throws a StopIteration exception on cx.
3095 extern JS_PUBLIC_API(JSBool
)
3096 JS_ThrowStopIteration(JSContext
*cx
);
3099 * Associate the current thread with the given context. This is done
3100 * implicitly by JS_NewContext.
3102 * Returns the old thread id for this context, which should be treated as
3103 * an opaque value. This value is provided for comparison to 0, which
3104 * indicates that ClearContextThread has been called on this context
3105 * since the last SetContextThread, or non-0, which indicates the opposite.
3107 extern JS_PUBLIC_API(jsword
)
3108 JS_GetContextThread(JSContext
*cx
);
3110 extern JS_PUBLIC_API(jsword
)
3111 JS_SetContextThread(JSContext
*cx
);
3113 extern JS_PUBLIC_API(jsword
)
3114 JS_ClearContextThread(JSContext
*cx
);
3116 #ifdef MOZ_TRACE_JSCALLS
3117 typedef void (*JSFunctionCallback
)(const JSFunction
*fun
,
3118 const JSScript
*scr
,
3119 const JSContext
*cx
,
3122 extern JS_PUBLIC_API(void)
3123 JS_SetFunctionCallback(JSContext
*cx
, JSFunctionCallback fcb
);
3125 extern JS_PUBLIC_API(JSFunctionCallback
)
3126 JS_GetFunctionCallback(JSContext
*cx
);
3129 /************************************************************************/
3132 * JS_IsConstructing must be called from within a native given the
3133 * native's original cx and vp arguments. If JS_IsConstructing is true,
3134 * JS_THIS must not be used; the constructor should construct and return a
3135 * new object. Otherwise, the native is called as an ordinary function and
3136 * JS_THIS may be used.
3138 static JS_ALWAYS_INLINE JSBool
3139 JS_IsConstructing(JSContext
*cx
, const jsval
*vp
)
3144 JSObject
*callee
= JSVAL_TO_OBJECT(JS_CALLEE(cx
, vp
));
3145 if (JS_ObjectIsFunction(cx
, callee
)) {
3146 JSFunction
*fun
= JS_ValueToFunction(cx
, JS_CALLEE(cx
, vp
));
3147 JS_ASSERT((JS_GetFunctionFlags(fun
) & JSFUN_CONSTRUCTOR
) != 0);
3149 JS_ASSERT(JS_GET_CLASS(cx
, callee
)->construct
!= NULL
);
3153 l
.asBits
= JSVAL_BITS(vp
[1]);
3154 return JSVAL_IS_MAGIC_IMPL(l
);
3158 * In the case of a constructor called from JS_ConstructObject and
3159 * JS_InitClass where the class has the JSCLASS_CONSTRUCT_PROTOTYPE flag set,
3160 * the JS engine passes the constructor a non-standard 'this' object. In such
3161 * cases, the following query provides the additional information of whether a
3162 * special 'this' was supplied. E.g.:
3164 * JSBool foo_native(JSContext *cx, uintN argc, jsval *vp) {
3165 * JSObject *maybeThis;
3166 * if (JS_IsConstructing_PossiblyWithGivenThisObject(cx, vp, &maybeThis)) {
3167 * // native called as a constructor
3169 * // native called as a constructor with maybeThis as 'this'
3171 * // native called as function, maybeThis is still uninitialized
3175 * Note that embeddings do not need to use this query unless they use the
3176 * aforementioned API/flags.
3178 static JS_ALWAYS_INLINE JSBool
3179 JS_IsConstructing_PossiblyWithGivenThisObject(JSContext
*cx
, const jsval
*vp
,
3180 JSObject
**maybeThis
)
3186 JSObject
*callee
= JSVAL_TO_OBJECT(JS_CALLEE(cx
, vp
));
3187 if (JS_ObjectIsFunction(cx
, callee
)) {
3188 JSFunction
*fun
= JS_ValueToFunction(cx
, JS_CALLEE(cx
, vp
));
3189 JS_ASSERT((JS_GetFunctionFlags(fun
) & JSFUN_CONSTRUCTOR
) != 0);
3191 JS_ASSERT(JS_GET_CLASS(cx
, callee
)->construct
!= NULL
);
3195 l
.asBits
= JSVAL_BITS(vp
[1]);
3196 isCtor
= JSVAL_IS_MAGIC_IMPL(l
);
3198 *maybeThis
= MAGIC_JSVAL_TO_OBJECT_OR_NULL_IMPL(l
);
3203 * If a constructor does not have any static knowledge about the type of
3204 * object to create, it can request that the JS engine create a default new
3205 * 'this' object, as is done for non-constructor natives when called with new.
3207 extern JS_PUBLIC_API(JSObject
*)
3208 JS_NewObjectForConstructor(JSContext
*cx
, const jsval
*vp
);
3210 /************************************************************************/
3213 #define JS_GC_ZEAL 1
3217 extern JS_PUBLIC_API(void)
3218 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
3223 #endif /* jsapi_h___ */