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 static JS_ALWAYS_INLINE JSBool
330 JSID_IS_ZERO(jsid id
)
332 return JSID_BITS(id
) == 0;
335 JS_PUBLIC_API(JSBool
)
336 JS_StringHasBeenInterned(JSString
*str
);
338 /* A jsid may only hold an interned JSString. */
339 static JS_ALWAYS_INLINE jsid
340 INTERNED_STRING_TO_JSID(JSString
*str
)
344 JS_ASSERT(JS_StringHasBeenInterned(str
));
345 JS_ASSERT(((size_t)str
& JSID_TYPE_MASK
) == 0);
346 JSID_BITS(id
) = (size_t)str
;
350 static JS_ALWAYS_INLINE JSBool
353 return !!(JSID_BITS(id
) & JSID_TYPE_INT
);
356 static JS_ALWAYS_INLINE int32
359 JS_ASSERT(JSID_IS_INT(id
));
360 return ((int32
)JSID_BITS(id
)) >> 1;
364 * Note: when changing these values, verify that their use in
365 * js_CheckForStringIndex is still valid.
367 #define JSID_INT_MIN (-(1 << 30))
368 #define JSID_INT_MAX ((1 << 30) - 1)
370 static JS_ALWAYS_INLINE JSBool
371 INT_FITS_IN_JSID(int32 i
)
373 return ((jsuint
)(i
) - (jsuint
)JSID_INT_MIN
<=
374 (jsuint
)(JSID_INT_MAX
- JSID_INT_MIN
));
377 static JS_ALWAYS_INLINE jsid
381 JS_ASSERT(INT_FITS_IN_JSID(i
));
382 JSID_BITS(id
) = ((i
<< 1) | JSID_TYPE_INT
);
386 static JS_ALWAYS_INLINE JSBool
387 JSID_IS_OBJECT(jsid id
)
389 return (JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_OBJECT
&&
390 (size_t)JSID_BITS(id
) != JSID_TYPE_OBJECT
;
393 static JS_ALWAYS_INLINE JSObject
*
394 JSID_TO_OBJECT(jsid id
)
396 JS_ASSERT(JSID_IS_OBJECT(id
));
397 return (JSObject
*)(JSID_BITS(id
) & ~(size_t)JSID_TYPE_MASK
);
400 static JS_ALWAYS_INLINE jsid
401 OBJECT_TO_JSID(JSObject
*obj
)
404 JS_ASSERT(obj
!= NULL
);
405 JS_ASSERT(((size_t)obj
& JSID_TYPE_MASK
) == 0);
406 JSID_BITS(id
) = ((size_t)obj
| JSID_TYPE_OBJECT
);
410 static JS_ALWAYS_INLINE JSBool
411 JSID_IS_GCTHING(jsid id
)
413 return JSID_IS_STRING(id
) || JSID_IS_OBJECT(id
);
416 static JS_ALWAYS_INLINE
void *
417 JSID_TO_GCTHING(jsid id
)
419 return (void *)(JSID_BITS(id
) & ~(size_t)JSID_TYPE_MASK
);
423 * The magic XML namespace id is not a valid jsid. Global object classes in
424 * embeddings that enable JS_HAS_XML_SUPPORT (E4X) should handle this id.
427 static JS_ALWAYS_INLINE JSBool
428 JSID_IS_DEFAULT_XML_NAMESPACE(jsid id
)
430 JS_ASSERT_IF(((size_t)JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
,
431 JSID_BITS(id
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
);
432 return ((size_t)JSID_BITS(id
) == JSID_TYPE_DEFAULT_XML_NAMESPACE
);
435 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
436 extern JS_PUBLIC_DATA(jsid
) JS_DEFAULT_XML_NAMESPACE_ID
;
438 #define JS_DEFAULT_XML_NAMESPACE_ID ((jsid)JSID_TYPE_DEFAULT_XML_NAMESPACE)
442 * A void jsid is not a valid id and only arises as an exceptional API return
443 * value, such as in JS_NextProperty. Embeddings must not pass JSID_VOID into
444 * JSAPI entry points expecting a jsid and do not need to handle JSID_VOID in
445 * hooks receiving a jsid except when explicitly noted in the API contract.
448 static JS_ALWAYS_INLINE JSBool
449 JSID_IS_VOID(jsid id
)
451 JS_ASSERT_IF(((size_t)JSID_BITS(id
) & JSID_TYPE_MASK
) == JSID_TYPE_VOID
,
452 JSID_BITS(id
) == JSID_TYPE_VOID
);
453 return ((size_t)JSID_BITS(id
) == JSID_TYPE_VOID
);
456 static JS_ALWAYS_INLINE JSBool
457 JSID_IS_EMPTY(jsid id
)
459 return ((size_t)JSID_BITS(id
) == JSID_TYPE_OBJECT
);
464 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
465 extern JS_PUBLIC_DATA(jsid
) JSID_VOID
;
466 extern JS_PUBLIC_DATA(jsid
) JSID_EMPTY
;
468 # define JSID_VOID ((jsid)JSID_TYPE_VOID)
469 # define JSID_EMPTY ((jsid)JSID_TYPE_OBJECT)
472 /************************************************************************/
474 /* Lock and unlock the GC thing held by a jsval. */
475 #define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
476 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
478 #define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
479 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
482 /* Property attributes, set in JSPropertySpec and passed to API functions. */
483 #define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
484 #define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
485 #define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
486 #define JSPROP_GETTER 0x10 /* property holds getter function */
487 #define JSPROP_SETTER 0x20 /* property holds setter function */
488 #define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
489 property; don't copy the property on
490 set of the same-named property in an
491 object that delegates to a prototype
492 containing this property */
493 #define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
494 #define JSPROP_SHORTID 0x100 /* set in JSPropertyDescriptor.attrs
495 if getters/setters use a shortid */
497 /* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
498 #define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
499 #define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
501 #define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
503 /* 0x0100 is unused */
504 #define JSFUN_CONSTRUCTOR 0x0200 /* native that can be called as a ctor
505 without creating a this object */
507 #define JSFUN_FLAGS_MASK 0x07f8 /* overlay JSFUN_* attributes --
508 bits 12-15 are used internally to
509 flag interpreted functions */
511 #define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
512 instead of defaulting to class gsops
513 for property holding function */
516 * Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
517 * JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
518 * methods of a class prototype that are exposed as static methods taking an
519 * extra leading argument: the generic |this| parameter.
521 * If you set this flag in a JSFunctionSpec struct's flags initializer, then
522 * that struct must live at least as long as the native static method object
523 * created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
524 * JSFunctionSpec structs are allocated in static arrays.
526 #define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
529 * Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
530 * comment in jstypes.h regarding safe int64 usage.
532 extern JS_PUBLIC_API(int64
)
535 /* Don't want to export data, so provide accessors for non-inline jsvals. */
536 extern JS_PUBLIC_API(jsval
)
537 JS_GetNaNValue(JSContext
*cx
);
539 extern JS_PUBLIC_API(jsval
)
540 JS_GetNegativeInfinityValue(JSContext
*cx
);
542 extern JS_PUBLIC_API(jsval
)
543 JS_GetPositiveInfinityValue(JSContext
*cx
);
545 extern JS_PUBLIC_API(jsval
)
546 JS_GetEmptyStringValue(JSContext
*cx
);
548 extern JS_PUBLIC_API(JSString
*)
549 JS_GetEmptyString(JSRuntime
*rt
);
552 * Format is a string of the following characters (spaces are insignificant),
553 * specifying the tabulated type conversions:
556 * c uint16/jschar ECMA uint16, Unicode char
558 * u uint32 ECMA uint32
559 * j int32 Rounded int32 (coordinate)
560 * d jsdouble IEEE double
561 * I jsdouble Integral IEEE double
562 * S JSString * Unicode string, accessed by a JSString pointer
563 * W jschar * Unicode character vector, 0-terminated (W for wide)
564 * o JSObject * Object reference
565 * f JSFunction * Function private
566 * v jsval Argument value (no conversion)
567 * * N/A Skip this argument (no vararg)
568 * / N/A End of required arguments
570 * The variable argument list after format must consist of &b, &c, &s, e.g.,
571 * where those variables have the types given above. For the pointer types
572 * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
573 * to the JS runtime, not to the calling native code. The runtime promises
574 * to keep this memory valid so long as argv refers to allocated stack space
575 * (so long as the native function is active).
577 * Fewer arguments than format specifies may be passed only if there is a /
578 * in format after the last required argument specifier and argc is at least
579 * the number of required arguments. More arguments than format specifies
580 * may be passed without error; it is up to the caller to deal with trailing
581 * unconverted arguments.
583 extern JS_PUBLIC_API(JSBool
)
584 JS_ConvertArguments(JSContext
*cx
, uintN argc
, jsval
*argv
, const char *format
,
588 extern JS_PUBLIC_API(JSBool
)
589 JS_ConvertArgumentsVA(JSContext
*cx
, uintN argc
, jsval
*argv
,
590 const char *format
, va_list ap
);
593 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
596 * Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
597 * The handler function has this signature (see jspubtd.h):
599 * JSBool MyArgumentFormatter(JSContext *cx, const char *format,
600 * JSBool fromJS, jsval **vpp, va_list *app);
602 * It should return true on success, and return false after reporting an error
603 * or detecting an already-reported error.
605 * For a given format string, for example "AA", the formatter is called from
606 * JS_ConvertArgumentsVA like so:
608 * formatter(cx, "AA...", JS_TRUE, &sp, &ap);
610 * sp points into the arguments array on the JS stack, while ap points into
611 * the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
612 * the formatter to convert zero or more jsvals at sp to zero or more C values
613 * accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
614 * (via *app) to point past the converted arguments and their result pointers
617 * When called from JS_PushArgumentsVA, the formatter is invoked thus:
619 * formatter(cx, "AA...", JS_FALSE, &sp, &ap);
621 * where JS_FALSE for fromJS means to wrap the C values at ap according to the
622 * format specifier and store them at sp, updating ap and sp appropriately.
624 * The "..." after "AA" is the rest of the format string that was passed into
625 * JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
626 * in each Convert or PushArguments call is passed to the formatter, so that
627 * one such function may implement several formats, in order to share code.
629 * Remove just forgets about any handler associated with format. Add does not
630 * copy format, it points at the string storage allocated by the caller, which
631 * is typically a string constant. If format is in dynamic storage, it is up
632 * to the caller to keep the string alive until Remove is called.
634 extern JS_PUBLIC_API(JSBool
)
635 JS_AddArgumentFormatter(JSContext
*cx
, const char *format
,
636 JSArgumentFormatter formatter
);
638 extern JS_PUBLIC_API(void)
639 JS_RemoveArgumentFormatter(JSContext
*cx
, const char *format
);
641 #endif /* JS_ARGUMENT_FORMATTER_DEFINED */
643 extern JS_PUBLIC_API(JSBool
)
644 JS_ConvertValue(JSContext
*cx
, jsval v
, JSType type
, jsval
*vp
);
646 extern JS_PUBLIC_API(JSBool
)
647 JS_ValueToObject(JSContext
*cx
, jsval v
, JSObject
**objp
);
649 extern JS_PUBLIC_API(JSFunction
*)
650 JS_ValueToFunction(JSContext
*cx
, jsval v
);
652 extern JS_PUBLIC_API(JSFunction
*)
653 JS_ValueToConstructor(JSContext
*cx
, jsval v
);
655 extern JS_PUBLIC_API(JSString
*)
656 JS_ValueToString(JSContext
*cx
, jsval v
);
658 extern JS_PUBLIC_API(JSString
*)
659 JS_ValueToSource(JSContext
*cx
, jsval v
);
661 extern JS_PUBLIC_API(JSBool
)
662 JS_ValueToNumber(JSContext
*cx
, jsval v
, jsdouble
*dp
);
664 extern JS_PUBLIC_API(JSBool
)
665 JS_DoubleIsInt32(jsdouble d
, jsint
*ip
);
668 * Convert a value to a number, then to an int32, according to the ECMA rules
671 extern JS_PUBLIC_API(JSBool
)
672 JS_ValueToECMAInt32(JSContext
*cx
, jsval v
, int32
*ip
);
675 * Convert a value to a number, then to a uint32, according to the ECMA rules
678 extern JS_PUBLIC_API(JSBool
)
679 JS_ValueToECMAUint32(JSContext
*cx
, jsval v
, uint32
*ip
);
682 * Convert a value to a number, then to an int32 if it fits by rounding to
683 * nearest; but failing with an error report if the double is out of range
686 extern JS_PUBLIC_API(JSBool
)
687 JS_ValueToInt32(JSContext
*cx
, jsval v
, int32
*ip
);
690 * ECMA ToUint16, for mapping a jsval to a Unicode point.
692 extern JS_PUBLIC_API(JSBool
)
693 JS_ValueToUint16(JSContext
*cx
, jsval v
, uint16
*ip
);
695 extern JS_PUBLIC_API(JSBool
)
696 JS_ValueToBoolean(JSContext
*cx
, jsval v
, JSBool
*bp
);
698 extern JS_PUBLIC_API(JSType
)
699 JS_TypeOfValue(JSContext
*cx
, jsval v
);
701 extern JS_PUBLIC_API(const char *)
702 JS_GetTypeName(JSContext
*cx
, JSType type
);
704 extern JS_PUBLIC_API(JSBool
)
705 JS_StrictlyEqual(JSContext
*cx
, jsval v1
, jsval v2
, JSBool
*equal
);
707 extern JS_PUBLIC_API(JSBool
)
708 JS_SameValue(JSContext
*cx
, jsval v1
, jsval v2
, JSBool
*same
);
710 /************************************************************************/
713 * Initialization, locking, contexts, and memory allocation.
715 * It is important that the first runtime and first context be created in a
716 * single-threaded fashion, otherwise the behavior of the library is undefined.
717 * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
719 #define JS_NewRuntime JS_Init
720 #define JS_DestroyRuntime JS_Finish
721 #define JS_LockRuntime JS_Lock
722 #define JS_UnlockRuntime JS_Unlock
724 extern JS_PUBLIC_API(JSRuntime
*)
725 JS_NewRuntime(uint32 maxbytes
);
728 #define JS_CommenceRuntimeShutDown(rt) ((void) 0)
730 extern JS_PUBLIC_API(void)
731 JS_DestroyRuntime(JSRuntime
*rt
);
733 extern JS_PUBLIC_API(void)
736 JS_PUBLIC_API(void *)
737 JS_GetRuntimePrivate(JSRuntime
*rt
);
740 JS_SetRuntimePrivate(JSRuntime
*rt
, void *data
);
742 extern JS_PUBLIC_API(void)
743 JS_BeginRequest(JSContext
*cx
);
745 extern JS_PUBLIC_API(void)
746 JS_EndRequest(JSContext
*cx
);
748 /* Yield to pending GC operations, regardless of request depth */
749 extern JS_PUBLIC_API(void)
750 JS_YieldRequest(JSContext
*cx
);
752 extern JS_PUBLIC_API(jsrefcount
)
753 JS_SuspendRequest(JSContext
*cx
);
755 extern JS_PUBLIC_API(void)
756 JS_ResumeRequest(JSContext
*cx
, jsrefcount saveDepth
);
758 extern JS_PUBLIC_API(JSBool
)
759 JS_IsInRequest(JSContext
*cx
);
764 class JSAutoRequest
{
766 JSAutoRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
767 : mContext(cx
), mSaveDepth(0) {
768 JS_GUARD_OBJECT_NOTIFIER_INIT
;
769 JS_BeginRequest(mContext
);
772 JS_EndRequest(mContext
);
776 mSaveDepth
= JS_SuspendRequest(mContext
);
779 JS_ResumeRequest(mContext
, mSaveDepth
);
784 jsrefcount mSaveDepth
;
785 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
789 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
790 static void operator delete(void *, size_t) { };
794 class JSAutoSuspendRequest
{
796 JSAutoSuspendRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
)
797 : mContext(cx
), mSaveDepth(0) {
798 JS_GUARD_OBJECT_NOTIFIER_INIT
;
800 mSaveDepth
= JS_SuspendRequest(mContext
);
803 ~JSAutoSuspendRequest() {
809 JS_ResumeRequest(mContext
, mSaveDepth
);
816 jsrefcount mSaveDepth
;
817 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
821 static void *operator new(size_t) CPP_THROW_NEW
{ return 0; };
822 static void operator delete(void *, size_t) { };
826 class JSAutoCheckRequest
{
828 JSAutoCheckRequest(JSContext
*cx JS_GUARD_OBJECT_NOTIFIER_PARAM
) {
829 #if defined JS_THREADSAFE && defined DEBUG
831 JS_ASSERT(JS_IsInRequest(cx
));
833 JS_GUARD_OBJECT_NOTIFIER_INIT
;
836 ~JSAutoCheckRequest() {
837 #if defined JS_THREADSAFE && defined DEBUG
838 JS_ASSERT(JS_IsInRequest(mContext
));
844 #if defined JS_THREADSAFE && defined DEBUG
847 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
853 extern JS_PUBLIC_API(void)
854 JS_Lock(JSRuntime
*rt
);
856 extern JS_PUBLIC_API(void)
857 JS_Unlock(JSRuntime
*rt
);
859 extern JS_PUBLIC_API(JSContextCallback
)
860 JS_SetContextCallback(JSRuntime
*rt
, JSContextCallback cxCallback
);
862 extern JS_PUBLIC_API(JSContext
*)
863 JS_NewContext(JSRuntime
*rt
, size_t stackChunkSize
);
865 extern JS_PUBLIC_API(void)
866 JS_DestroyContext(JSContext
*cx
);
868 extern JS_PUBLIC_API(void)
869 JS_DestroyContextNoGC(JSContext
*cx
);
871 extern JS_PUBLIC_API(void)
872 JS_DestroyContextMaybeGC(JSContext
*cx
);
874 extern JS_PUBLIC_API(void *)
875 JS_GetContextPrivate(JSContext
*cx
);
877 extern JS_PUBLIC_API(void)
878 JS_SetContextPrivate(JSContext
*cx
, void *data
);
880 extern JS_PUBLIC_API(JSRuntime
*)
881 JS_GetRuntime(JSContext
*cx
);
883 extern JS_PUBLIC_API(JSContext
*)
884 JS_ContextIterator(JSRuntime
*rt
, JSContext
**iterp
);
886 extern JS_PUBLIC_API(JSVersion
)
887 JS_GetVersion(JSContext
*cx
);
889 extern JS_PUBLIC_API(JSVersion
)
890 JS_SetVersion(JSContext
*cx
, JSVersion version
);
892 extern JS_PUBLIC_API(const char *)
893 JS_VersionToString(JSVersion version
);
895 extern JS_PUBLIC_API(JSVersion
)
896 JS_StringToVersion(const char *string
);
899 * JS options are orthogonal to version, and may be freely composed with one
900 * another as well as with version.
902 * JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
903 * prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
905 #define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
906 #define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
907 #define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
908 the last object on its 'obj'
909 param's scope chain as the
910 ECMA 'variables object' */
911 #define JSOPTION_PRIVATE_IS_NSISUPPORTS \
912 JS_BIT(3) /* context private data points
913 to an nsISupports subclass */
914 #define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
915 promises to execute compiled
916 script once only; enables
917 compile-time scope chain
918 resolution of consts. */
919 #define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
920 option supported for the
921 XUL preprocessor and kindred
923 #define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
924 parse <!-- --> as a token,
925 not backward compatible with
926 the comment-hiding hack used
927 in HTML script tags. */
928 #define JSOPTION_DONT_REPORT_UNCAUGHT \
929 JS_BIT(8) /* When returning from the
930 outermost API call, prevent
931 uncaught exceptions from
932 being converted to error
935 #define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
936 regular expression which
937 backtracks more than n^3
938 times, where n is length
939 of the input string */
940 #define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
941 statement context per
942 ECMA-262 Edition 3. */
944 #define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
946 #define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
947 that a null rval out-param
948 will be passed to each call
949 to JS_ExecuteScript. */
950 #define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
951 contexts' global objects
952 (see JS_GetGlobalObject),
953 leaving that up to the
956 #define JSOPTION_METHODJIT JS_BIT(14) /* Whole-method JIT. */
957 #define JSOPTION_PROFILING JS_BIT(15) /* Profiler to make tracer/methodjit choices. */
958 #define JSOPTION_METHODJIT_ALWAYS \
959 JS_BIT(16) /* Always whole-method JIT,
960 don't tune at run-time. */
962 /* Options which reflect compile-time properties of scripts. */
963 #define JSCOMPILEOPTION_MASK (JSOPTION_XML | JSOPTION_ANONFUNFIX)
965 #define JSRUNOPTION_MASK (JS_BITMASK(17) & ~JSCOMPILEOPTION_MASK)
966 #define JSALLOPTION_MASK (JSCOMPILEOPTION_MASK | JSRUNOPTION_MASK)
968 extern JS_PUBLIC_API(uint32
)
969 JS_GetOptions(JSContext
*cx
);
971 extern JS_PUBLIC_API(uint32
)
972 JS_SetOptions(JSContext
*cx
, uint32 options
);
974 extern JS_PUBLIC_API(uint32
)
975 JS_ToggleOptions(JSContext
*cx
, uint32 options
);
977 extern JS_PUBLIC_API(const char *)
978 JS_GetImplementationVersion(void);
980 extern JS_PUBLIC_API(JSCompartmentCallback
)
981 JS_SetCompartmentCallback(JSRuntime
*rt
, JSCompartmentCallback callback
);
983 extern JS_PUBLIC_API(JSWrapObjectCallback
)
984 JS_SetWrapObjectCallbacks(JSRuntime
*rt
,
985 JSWrapObjectCallback callback
,
986 JSPreWrapCallback precallback
);
988 extern JS_PUBLIC_API(JSCrossCompartmentCall
*)
989 JS_EnterCrossCompartmentCall(JSContext
*cx
, JSObject
*target
);
991 extern JS_PUBLIC_API(JSCrossCompartmentCall
*)
992 JS_EnterCrossCompartmentCallScript(JSContext
*cx
, JSScript
*target
);
994 extern JS_PUBLIC_API(void)
995 JS_LeaveCrossCompartmentCall(JSCrossCompartmentCall
*call
);
997 extern JS_PUBLIC_API(void *)
998 JS_SetCompartmentPrivate(JSContext
*cx
, JSCompartment
*compartment
, void *data
);
1000 extern JS_PUBLIC_API(void *)
1001 JS_GetCompartmentPrivate(JSContext
*cx
, JSCompartment
*compartment
);
1003 extern JS_PUBLIC_API(JSBool
)
1004 JS_WrapObject(JSContext
*cx
, JSObject
**objp
);
1006 extern JS_PUBLIC_API(JSBool
)
1007 JS_WrapValue(JSContext
*cx
, jsval
*vp
);
1009 extern JS_PUBLIC_API(JSObject
*)
1010 JS_TransplantObject(JSContext
*cx
, JSObject
*origobj
, JSObject
*target
);
1012 extern JS_FRIEND_API(JSObject
*)
1013 js_TransplantObjectWithWrapper(JSContext
*cx
,
1015 JSObject
*origwrapper
,
1016 JSObject
*targetobj
,
1017 JSObject
*targetwrapper
);
1022 class JS_PUBLIC_API(JSAutoEnterCompartment
)
1024 JSCrossCompartmentCall
*call
;
1027 JSAutoEnterCompartment() : call(NULL
) {}
1029 bool enter(JSContext
*cx
, JSObject
*target
);
1031 bool enter(JSContext
*cx
, JSScript
*target
);
1033 void enterAndIgnoreErrors(JSContext
*cx
, JSObject
*target
);
1035 bool entered() const { return call
!= NULL
; }
1037 ~JSAutoEnterCompartment() {
1038 if (call
&& call
!= reinterpret_cast<JSCrossCompartmentCall
*>(1))
1039 JS_LeaveCrossCompartmentCall(call
);
1042 void swap(JSAutoEnterCompartment
&other
) {
1043 JSCrossCompartmentCall
*tmp
= call
;
1052 extern JS_PUBLIC_API(JSObject
*)
1053 JS_GetGlobalObject(JSContext
*cx
);
1055 extern JS_PUBLIC_API(void)
1056 JS_SetGlobalObject(JSContext
*cx
, JSObject
*obj
);
1059 * Initialize standard JS class constructors, prototypes, and any top-level
1060 * functions and constants associated with the standard classes (e.g. isNaN
1063 * NB: This sets cx's global object to obj if it was null.
1065 extern JS_PUBLIC_API(JSBool
)
1066 JS_InitStandardClasses(JSContext
*cx
, JSObject
*obj
);
1069 * Resolve id, which must contain either a string or an int, to a standard
1070 * class name in obj if possible, defining the class's constructor and/or
1071 * prototype and storing true in *resolved. If id does not name a standard
1072 * class or a top-level property induced by initializing a standard class,
1073 * store false in *resolved and just return true. Return false on error,
1074 * as usual for JSBool result-typed API entry points.
1076 * This API can be called directly from a global object class's resolve op,
1077 * to define standard classes lazily. The class's enumerate op should call
1078 * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
1079 * loops any classes not yet resolved lazily.
1081 extern JS_PUBLIC_API(JSBool
)
1082 JS_ResolveStandardClass(JSContext
*cx
, JSObject
*obj
, jsid id
,
1085 extern JS_PUBLIC_API(JSBool
)
1086 JS_EnumerateStandardClasses(JSContext
*cx
, JSObject
*obj
);
1089 * Enumerate any already-resolved standard class ids into ida, or into a new
1090 * JSIdArray if ida is null. Return the augmented array on success, null on
1091 * failure with ida (if it was non-null on entry) destroyed.
1093 extern JS_PUBLIC_API(JSIdArray
*)
1094 JS_EnumerateResolvedStandardClasses(JSContext
*cx
, JSObject
*obj
,
1097 extern JS_PUBLIC_API(JSBool
)
1098 JS_GetClassObject(JSContext
*cx
, JSObject
*obj
, JSProtoKey key
,
1101 extern JS_PUBLIC_API(JSObject
*)
1102 JS_GetScopeChain(JSContext
*cx
);
1104 extern JS_PUBLIC_API(JSObject
*)
1105 JS_GetGlobalForObject(JSContext
*cx
, JSObject
*obj
);
1107 extern JS_PUBLIC_API(JSObject
*)
1108 JS_GetGlobalForScopeChain(JSContext
*cx
);
1110 #ifdef JS_HAS_CTYPES
1112 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
1113 * object will be sealed.
1115 extern JS_PUBLIC_API(JSBool
)
1116 JS_InitCTypesClass(JSContext
*cx
, JSObject
*global
);
1119 * Convert a unicode string 'source' of length 'slen' to the platform native
1120 * charset, returning a null-terminated string allocated with JS_malloc. On
1121 * failure, this function should report an error.
1124 (* JSCTypesUnicodeToNativeFun
)(JSContext
*cx
, const jschar
*source
, size_t slen
);
1127 * Set of function pointers that ctypes can use for various internal functions.
1128 * See JS_SetCTypesCallbacks below. Providing NULL for a function is safe,
1129 * and will result in the applicable ctypes functionality not being available.
1131 struct JSCTypesCallbacks
{
1132 JSCTypesUnicodeToNativeFun unicodeToNative
;
1135 typedef struct JSCTypesCallbacks JSCTypesCallbacks
;
1138 * Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
1139 * pointer to static data that exists for the lifetime of 'ctypesObj', but it
1140 * may safely be altered after calling this function and without having
1141 * to call this function again.
1143 extern JS_PUBLIC_API(JSBool
)
1144 JS_SetCTypesCallbacks(JSContext
*cx
, JSObject
*ctypesObj
, JSCTypesCallbacks
*callbacks
);
1148 * Macros to hide interpreter stack layout details from a JSFastNative using
1149 * its jsval *vp parameter. The stack layout underlying invocation can't change
1150 * without breaking source and binary compatibility (argv[-2] is well-known to
1151 * be the callee jsval, and argv[-1] is as well known to be |this|).
1153 * Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
1154 * is the global object, so embeddings implementing fast natives *must* call
1155 * JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
1156 * which should propagate as a false return from native functions and hooks.
1158 * To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
1159 * handle a null obj parameter by returning false (throwing a TypeError if
1160 * given non-null argv), so most native functions that type-check their |this|
1161 * parameter need not add null checking.
1163 * NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
1164 * methods that may inspect their callee must defer setting their return value
1165 * until after any such possible inspection. Otherwise the return value will be
1166 * inspected instead of the callee function object.
1168 * WARNING: These are not (yet) mandatory macros, but new code outside of the
1169 * engine should use them. In the Mozilla 2.0 milestone their definitions may
1170 * change incompatibly.
1172 * N.B. constructors must not use JS_THIS, as no 'this' object has been created.
1175 #define JS_CALLEE(cx,vp) ((vp)[0])
1176 #define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
1177 #define JS_THIS_OBJECT(cx,vp) (JSVAL_TO_OBJECT(JS_THIS(cx,vp)))
1178 #define JS_ARGV(cx,vp) ((vp) + 2)
1179 #define JS_RVAL(cx,vp) (*(vp))
1180 #define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
1182 extern JS_PUBLIC_API(jsval
)
1183 JS_ComputeThis(JSContext
*cx
, jsval
*vp
);
1188 JS_THIS(JSContext
*cx
, jsval
*vp
)
1190 return JSVAL_IS_PRIMITIVE(vp
[1]) ? JS_ComputeThis(cx
, vp
) : vp
[1];
1195 * |this| is passed to functions in ES5 without change. Functions themselves
1196 * do any post-processing they desire to box |this|, compute the global object,
1197 * &c. Use this macro to retrieve a function's unboxed |this| value.
1199 * This macro must not be used in conjunction with JS_THIS or JS_THIS_OBJECT,
1200 * or vice versa. Either use the provided this value with this macro, or
1201 * compute the boxed this value using those.
1203 * N.B. constructors must not use JS_THIS_VALUE, as no 'this' object has been
1206 #define JS_THIS_VALUE(cx,vp) ((vp)[1])
1208 extern JS_PUBLIC_API(void *)
1209 JS_malloc(JSContext
*cx
, size_t nbytes
);
1211 extern JS_PUBLIC_API(void *)
1212 JS_realloc(JSContext
*cx
, void *p
, size_t nbytes
);
1214 extern JS_PUBLIC_API(void)
1215 JS_free(JSContext
*cx
, void *p
);
1217 extern JS_PUBLIC_API(void)
1218 JS_updateMallocCounter(JSContext
*cx
, size_t nbytes
);
1220 extern JS_PUBLIC_API(char *)
1221 JS_strdup(JSContext
*cx
, const char *s
);
1223 extern JS_PUBLIC_API(JSBool
)
1224 JS_NewNumberValue(JSContext
*cx
, jsdouble d
, jsval
*rval
);
1227 * A GC root is a pointer to a jsval, JSObject * or JSString * that itself
1228 * points into the GC heap. JS_AddValueRoot takes a pointer to a jsval and
1229 * JS_AddGCThingRoot takes a pointer to a JSObject * or JString *.
1231 * Note that, since JS_Add*Root stores the address of a variable (of type
1232 * jsval, JSString *, or JSObject *), that variable must live until
1233 * JS_Remove*Root is called to remove that variable. For example, after:
1235 * void some_function() {
1237 * JS_AddNamedRootedValue(cx, &v, "name");
1239 * the caller must perform
1241 * JS_RemoveRootedValue(cx, &v);
1243 * before some_function() returns.
1245 * Also, use JS_AddNamed*Root(cx, &structPtr->memberObj, "structPtr->memberObj")
1246 * in preference to JS_Add*Root(cx, &structPtr->memberObj), in order to identify
1247 * roots by their source callsites. This way, you can find the callsite while
1248 * debugging if you should fail to do JS_Remove*Root(cx, &structPtr->memberObj)
1249 * before freeing structPtr's memory.
1251 extern JS_PUBLIC_API(JSBool
)
1252 JS_AddValueRoot(JSContext
*cx
, jsval
*vp
);
1254 extern JS_PUBLIC_API(JSBool
)
1255 JS_AddStringRoot(JSContext
*cx
, JSString
**rp
);
1257 extern JS_PUBLIC_API(JSBool
)
1258 JS_AddObjectRoot(JSContext
*cx
, JSObject
**rp
);
1260 extern JS_PUBLIC_API(JSBool
)
1261 JS_AddGCThingRoot(JSContext
*cx
, void **rp
);
1263 #ifdef NAME_ALL_GC_ROOTS
1264 #define JS_DEFINE_TO_TOKEN(def) #def
1265 #define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
1266 #define JS_AddValueRoot(cx,vp) JS_AddNamedValueRoot((cx), (vp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1267 #define JS_AddStringRoot(cx,rp) JS_AddNamedStringRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1268 #define JS_AddObjectRoot(cx,rp) JS_AddNamedObjectRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1269 #define JS_AddGCThingRoot(cx,rp) JS_AddNamedGCThingRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
1272 extern JS_PUBLIC_API(JSBool
)
1273 JS_AddNamedValueRoot(JSContext
*cx
, jsval
*vp
, const char *name
);
1275 extern JS_PUBLIC_API(JSBool
)
1276 JS_AddNamedStringRoot(JSContext
*cx
, JSString
**rp
, const char *name
);
1278 extern JS_PUBLIC_API(JSBool
)
1279 JS_AddNamedObjectRoot(JSContext
*cx
, JSObject
**rp
, const char *name
);
1281 extern JS_PUBLIC_API(JSBool
)
1282 JS_AddNamedGCThingRoot(JSContext
*cx
, void **rp
, const char *name
);
1284 extern JS_PUBLIC_API(JSBool
)
1285 JS_RemoveValueRoot(JSContext
*cx
, jsval
*vp
);
1287 extern JS_PUBLIC_API(JSBool
)
1288 JS_RemoveStringRoot(JSContext
*cx
, JSString
**rp
);
1290 extern JS_PUBLIC_API(JSBool
)
1291 JS_RemoveObjectRoot(JSContext
*cx
, JSObject
**rp
);
1293 extern JS_PUBLIC_API(JSBool
)
1294 JS_RemoveGCThingRoot(JSContext
*cx
, void **rp
);
1296 /* TODO: remove these APIs */
1298 extern JS_FRIEND_API(JSBool
)
1299 js_AddRootRT(JSRuntime
*rt
, jsval
*vp
, const char *name
);
1301 extern JS_FRIEND_API(JSBool
)
1302 js_AddGCThingRootRT(JSRuntime
*rt
, void **rp
, const char *name
);
1304 extern JS_FRIEND_API(JSBool
)
1305 js_RemoveRoot(JSRuntime
*rt
, void *rp
);
1313 * Protecting non-jsval, non-JSObject *, non-JSString * values from collection
1315 * Most of the time, the garbage collector's conservative stack scanner works
1316 * behind the scenes, finding all live values and protecting them from being
1317 * collected. However, when JSAPI client code obtains a pointer to data the
1318 * scanner does not know about, owned by an object the scanner does know about,
1319 * Care Must Be Taken.
1321 * The scanner recognizes only a select set of types: pointers to JSObjects and
1322 * similar things (JSFunctions, and so on), pointers to JSStrings, and jsvals.
1323 * So while the scanner finds all live |JSString| pointers, it does not notice
1324 * |jschar| pointers.
1326 * So suppose we have:
1328 * void f(JSString *str) {
1329 * const jschar *ch = JS_GetStringCharsZ(str);
1330 * ... do stuff with ch, but no uses of str ...;
1333 * After the call to |JS_GetStringCharsZ|, there are no further uses of
1334 * |str|, which means that the compiler is within its rights to not store
1335 * it anywhere. But because the stack scanner will not notice |ch|, there
1336 * is no longer any live value in this frame that would keep the string
1337 * alive. If |str| is the last reference to that |JSString|, and the
1338 * collector runs while we are using |ch|, the string's array of |jschar|s
1339 * may be freed out from under us.
1341 * Note that there is only an issue when 1) we extract a thing X the scanner
1342 * doesn't recognize from 2) a thing Y the scanner does recognize, and 3) if Y
1343 * gets garbage-collected, then X gets freed. If we have code like this:
1345 * void g(JSObject *obj) {
1347 * JS_GetProperty(obj, "x", &x);
1348 * ... do stuff with x ...
1351 * there's no problem, because the value we've extracted, x, is a jsval, a
1352 * type that the conservative scanner recognizes.
1354 * Conservative GC frees us from the obligation to explicitly root the types it
1355 * knows about, but when we work with derived values like |ch|, we must root
1356 * their owners, as the derived value alone won't keep them alive.
1358 * A JS::Anchor is a kind of GC root that allows us to keep the owners of
1359 * derived values like |ch| alive throughout the Anchor's lifetime. We could
1360 * fix the above code as follows:
1362 * void f(JSString *str) {
1363 * JS::Anchor<JSString *> a_str(str);
1364 * const jschar *ch = JS_GetStringCharsZ(str);
1365 * ... do stuff with ch, but no uses of str ...;
1368 * This simply ensures that |str| will be live until |a_str| goes out of scope.
1369 * As long as we don't retain a pointer to the string's characters for longer
1370 * than that, we have avoided all garbage collection hazards.
1372 template<typename T
> class AnchorPermitted
;
1373 template<> class AnchorPermitted
<JSObject
*> { };
1374 template<> class AnchorPermitted
<const JSObject
*> { };
1375 template<> class AnchorPermitted
<JSFunction
*> { };
1376 template<> class AnchorPermitted
<const JSFunction
*> { };
1377 template<> class AnchorPermitted
<JSString
*> { };
1378 template<> class AnchorPermitted
<const JSString
*> { };
1379 template<> class AnchorPermitted
<jsval
> { };
1381 template<typename T
>
1382 class Anchor
: AnchorPermitted
<T
> {
1385 explicit Anchor(T t
) { hold
= t
; }
1387 T
&get() { return hold
; }
1388 void set(const T
&t
) { hold
= t
; }
1389 void clear() { hold
= 0; }
1392 /* Anchors should not be assigned or passed to functions. */
1393 Anchor(const Anchor
&);
1394 const Anchor
&operator=(const Anchor
&);
1398 template<typename T
>
1399 inline Anchor
<T
>::~Anchor() {
1401 * No code is generated for this. But because this is marked 'volatile', G++ will
1402 * assume it has important side-effects, and won't delete it. (G++ never looks at
1403 * the actual text and notices it's empty.) And because we have passed |hold| to
1404 * it, GCC will keep |hold| alive until this point.
1406 * The "memory" clobber operand ensures that G++ will not move prior memory
1407 * accesses after the asm --- it's a barrier. Unfortunately, it also means that
1408 * G++ will assume that all memory has changed after the asm, as it would for a
1409 * call to an unknown function. I don't know of a way to avoid that consequence.
1411 asm volatile("":: "g" (hold
) : "memory");
1414 template<typename T
>
1415 inline Anchor
<T
>::~Anchor() {
1417 * An adequate portable substitute, for non-structure types.
1419 * The compiler promises that, by the end of an expression statement, the
1420 * last-stored value to a volatile object is the same as it would be in an
1421 * unoptimized, direct implementation (the "abstract machine" whose behavior the
1422 * language spec describes). However, the compiler is still free to reorder
1423 * non-volatile accesses across this store --- which is what we must prevent. So
1424 * assigning the held value to a volatile variable, as we do here, is not enough.
1426 * In our case, however, garbage collection only occurs at function calls, so it
1427 * is sufficient to ensure that the destructor's store isn't moved earlier across
1428 * any function calls that could collect. It is hard to imagine the compiler
1429 * analyzing the program so thoroughly that it could prove that such motion was
1430 * safe. In practice, compilers treat calls to the collector as opaque operations
1431 * --- in particular, as operations which could access volatile variables, across
1432 * which this destructor must not be moved.
1434 * ("Objection, your honor! *Alleged* killer whale!")
1436 * The disadvantage of this approach is that it does generate code for the store.
1437 * We do need to use Anchors in some cases where cycles are tight.
1443 #ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
1445 * The default assignment operator for |struct C| has the signature:
1447 * C& C::operator=(const C&)
1449 * And in particular requires implicit conversion of |this| to type |C| for the return
1450 * value. But |volatile C| cannot thus be converted to |C|, so just doing |sink = hold| as
1451 * in the non-specialized version would fail to compile. Do the assignment on asBits
1452 * instead, since I don't think we want to give jsval_layout an assignment operator
1453 * returning |volatile jsval_layout|.
1456 inline Anchor
<jsval
>::~Anchor() {
1457 volatile jsval sink
;
1458 sink
.asBits
= hold
.asBits
;
1463 } /* namespace JS */
1469 * This symbol may be used by embedders to detect the change from the old
1470 * JS_AddRoot(JSContext *, void *) APIs to the new ones above.
1472 #define JS_TYPED_ROOTING_API
1474 /* Obsolete rooting APIs. */
1475 #define JS_ClearNewbornRoots(cx) ((void) 0)
1476 #define JS_EnterLocalRootScope(cx) (JS_TRUE)
1477 #define JS_LeaveLocalRootScope(cx) ((void) 0)
1478 #define JS_LeaveLocalRootScopeWithResult(cx, rval) ((void) 0)
1479 #define JS_ForgetLocalRoot(cx, thing) ((void) 0)
1481 typedef enum JSGCRootType
{
1482 JS_GC_ROOT_VALUE_PTR
,
1483 JS_GC_ROOT_GCTHING_PTR
1487 extern JS_PUBLIC_API(void)
1488 JS_DumpNamedRoots(JSRuntime
*rt
,
1489 void (*dump
)(const char *name
, void *rp
, JSGCRootType type
, void *data
),
1494 * Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
1495 * The root is pointed at by rp; if the root is unnamed, name is null; data is
1496 * supplied from the third parameter to JS_MapGCRoots.
1498 * The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
1499 * enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
1500 * in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
1501 * constants are flags; you can OR them together.
1503 * This function acquires and releases rt's GC lock around the mapping of the
1504 * roots table, so the map function should run to completion in as few cycles
1505 * as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
1506 * or any JS API entry point that acquires locks, without double-tripping or
1507 * deadlocking on the GC lock.
1509 * The JSGCRootType parameter indicates whether rp is a pointer to a Value
1510 * (which is obtained by '(Value *)rp') or a pointer to a GC-thing pointer
1511 * (which is obtained by '(void **)rp').
1513 * JS_MapGCRoots returns the count of roots that were successfully mapped.
1515 #define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
1516 #define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
1517 #define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
1520 (* JSGCRootMapFun
)(void *rp
, JSGCRootType type
, const char *name
, void *data
);
1522 extern JS_PUBLIC_API(uint32
)
1523 JS_MapGCRoots(JSRuntime
*rt
, JSGCRootMapFun map
, void *data
);
1525 extern JS_PUBLIC_API(JSBool
)
1526 JS_LockGCThing(JSContext
*cx
, void *thing
);
1528 extern JS_PUBLIC_API(JSBool
)
1529 JS_LockGCThingRT(JSRuntime
*rt
, void *thing
);
1531 extern JS_PUBLIC_API(JSBool
)
1532 JS_UnlockGCThing(JSContext
*cx
, void *thing
);
1534 extern JS_PUBLIC_API(JSBool
)
1535 JS_UnlockGCThingRT(JSRuntime
*rt
, void *thing
);
1538 * Register externally maintained GC roots.
1540 * traceOp: the trace operation. For each root the implementation should call
1541 * JS_CallTracer whenever the root contains a traceable thing.
1542 * data: the data argument to pass to each invocation of traceOp.
1544 extern JS_PUBLIC_API(void)
1545 JS_SetExtraGCRoots(JSRuntime
*rt
, JSTraceDataOp traceOp
, void *data
);
1548 * For implementors of JSMarkOp. All new code should implement JSTraceOp
1551 extern JS_PUBLIC_API(void)
1552 JS_MarkGCThing(JSContext
*cx
, jsval v
, const char *name
, void *arg
);
1555 * JS_CallTracer API and related macros for implementors of JSTraceOp, to
1556 * enumerate all references to traceable things reachable via a property or
1557 * other strong ref identified for debugging purposes by name or index or
1558 * a naming callback.
1560 * By definition references to traceable things include non-null pointers
1561 * to JSObject, JSString and jsdouble and corresponding jsvals.
1563 * See the JSTraceOp typedef in jspubtd.h.
1566 /* Trace kinds to pass to JS_Tracing. */
1567 #define JSTRACE_OBJECT 0
1568 #define JSTRACE_STRING 1
1571 * Use the following macros to check if a particular jsval is a traceable
1572 * thing and to extract the thing and its kind to pass to JS_CallTracer.
1574 static JS_ALWAYS_INLINE JSBool
1575 JSVAL_IS_TRACEABLE(jsval v
)
1578 l
.asBits
= JSVAL_BITS(v
);
1579 return JSVAL_IS_TRACEABLE_IMPL(l
);
1582 static JS_ALWAYS_INLINE
void *
1583 JSVAL_TO_TRACEABLE(jsval v
)
1585 return JSVAL_TO_GCTHING(v
);
1588 static JS_ALWAYS_INLINE uint32
1589 JSVAL_TRACE_KIND(jsval v
)
1592 JS_ASSERT(JSVAL_IS_GCTHING(v
));
1593 l
.asBits
= JSVAL_BITS(v
);
1594 return JSVAL_TRACE_KIND_IMPL(l
);
1599 JSTraceCallback callback
;
1600 JSTraceNamePrinter debugPrinter
;
1601 const void *debugPrintArg
;
1602 size_t debugPrintIndex
;
1606 * The method to call on each reference to a traceable thing stored in a
1607 * particular JSObject or other runtime structure. With DEBUG defined the
1608 * caller before calling JS_CallTracer must initialize JSTracer fields
1609 * describing the reference using the macros below.
1611 extern JS_PUBLIC_API(void)
1612 JS_CallTracer(JSTracer
*trc
, void *thing
, uint32 kind
);
1615 * Set debugging information about a reference to a traceable thing to prepare
1616 * for the following call to JS_CallTracer.
1618 * When printer is null, arg must be const char * or char * C string naming
1619 * the reference and index must be either (size_t)-1 indicating that the name
1620 * alone describes the reference or it must be an index into some array vector
1621 * that stores the reference.
1623 * When printer callback is not null, the arg and index arguments are
1624 * available to the callback as debugPrinterArg and debugPrintIndex fields
1627 * The storage for name or callback's arguments needs to live only until
1628 * the following call to JS_CallTracer returns.
1631 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1633 (trc)->debugPrinter = (printer); \
1634 (trc)->debugPrintArg = (arg); \
1635 (trc)->debugPrintIndex = (index); \
1638 # define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
1644 * Convenience macro to describe the argument of JS_CallTracer using C string
1647 # define JS_SET_TRACING_INDEX(trc, name, index) \
1648 JS_SET_TRACING_DETAILS(trc, NULL, name, index)
1651 * Convenience macro to describe the argument of JS_CallTracer using C string.
1653 # define JS_SET_TRACING_NAME(trc, name) \
1654 JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
1657 * Convenience macro to invoke JS_CallTracer using C string as the name for
1658 * the reference to a traceable thing.
1660 # define JS_CALL_TRACER(trc, thing, kind, name) \
1662 JS_SET_TRACING_NAME(trc, name); \
1663 JS_CallTracer((trc), (thing), (kind)); \
1667 * Convenience macros to invoke JS_CallTracer when jsval represents a
1668 * reference to a traceable thing.
1670 #define JS_CALL_VALUE_TRACER(trc, val, name) \
1672 if (JSVAL_IS_TRACEABLE(val)) { \
1673 JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
1674 JSVAL_TRACE_KIND(val), name); \
1678 #define JS_CALL_OBJECT_TRACER(trc, object, name) \
1680 JSObject *obj_ = (object); \
1682 JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
1685 #define JS_CALL_STRING_TRACER(trc, string, name) \
1687 JSString *str_ = (string); \
1689 JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
1693 * API for JSTraceCallback implementations.
1695 # define JS_TRACER_INIT(trc, cx_, callback_) \
1697 (trc)->context = (cx_); \
1698 (trc)->callback = (callback_); \
1699 (trc)->debugPrinter = NULL; \
1700 (trc)->debugPrintArg = NULL; \
1701 (trc)->debugPrintIndex = (size_t)-1; \
1704 extern JS_PUBLIC_API(void)
1705 JS_TraceChildren(JSTracer
*trc
, void *thing
, uint32 kind
);
1707 extern JS_PUBLIC_API(void)
1708 JS_TraceRuntime(JSTracer
*trc
);
1712 extern JS_PUBLIC_API(void)
1713 JS_PrintTraceThingInfo(char *buf
, size_t bufsize
, JSTracer
*trc
,
1714 void *thing
, uint32 kind
, JSBool includeDetails
);
1717 * DEBUG-only method to dump the object graph of heap-allocated things.
1719 * fp: file for the dump output.
1720 * start: when non-null, dump only things reachable from start
1721 * thing. Otherwise dump all things reachable from the
1723 * startKind: trace kind of start if start is not null. Must be 0 when
1725 * thingToFind: dump only paths in the object graph leading to thingToFind
1727 * maxDepth: the upper bound on the number of edges to descend from the
1729 * thingToIgnore: thing to ignore during the graph traversal when non-null.
1731 extern JS_PUBLIC_API(JSBool
)
1732 JS_DumpHeap(JSContext
*cx
, FILE *fp
, void* startThing
, uint32 startKind
,
1733 void *thingToFind
, size_t maxDepth
, void *thingToIgnore
);
1738 * Garbage collector API.
1740 extern JS_PUBLIC_API(void)
1741 JS_GC(JSContext
*cx
);
1743 extern JS_PUBLIC_API(void)
1744 JS_MaybeGC(JSContext
*cx
);
1746 extern JS_PUBLIC_API(JSGCCallback
)
1747 JS_SetGCCallback(JSContext
*cx
, JSGCCallback cb
);
1749 extern JS_PUBLIC_API(JSGCCallback
)
1750 JS_SetGCCallbackRT(JSRuntime
*rt
, JSGCCallback cb
);
1752 extern JS_PUBLIC_API(JSBool
)
1753 JS_IsGCMarkingTracer(JSTracer
*trc
);
1755 extern JS_PUBLIC_API(JSBool
)
1756 JS_IsAboutToBeFinalized(JSContext
*cx
, void *thing
);
1758 typedef enum JSGCParamKey
{
1759 /* Maximum nominal heap before last ditch GC. */
1762 /* Number of JS_malloc bytes before last ditch GC. */
1763 JSGC_MAX_MALLOC_BYTES
= 1,
1765 /* Hoard stackPools for this long, in ms, default is 30 seconds. */
1766 JSGC_STACKPOOL_LIFESPAN
= 2,
1769 * The factor that defines when the GC is invoked. The factor is a
1770 * percent of the memory allocated by the GC after the last run of
1771 * the GC. When the current memory allocated by the GC is more than
1772 * this percent then the GC is invoked. The factor cannot be less
1773 * than 100 since the current memory allocated by the GC cannot be less
1774 * than the memory allocated after the last run of the GC.
1776 JSGC_TRIGGER_FACTOR
= 3,
1778 /* Amount of bytes allocated by the GC. */
1781 /* Number of times when GC was invoked. */
1784 /* Max size of the code cache in bytes. */
1785 JSGC_MAX_CODE_CACHE_BYTES
= 6,
1787 /* Select GC mode. */
1791 typedef enum JSGCMode
{
1792 /* Perform only global GCs. */
1793 JSGC_MODE_GLOBAL
= 0,
1795 /* Perform per-compartment GCs until too much garbage has accumulated. */
1796 JSGC_MODE_COMPARTMENT
= 1
1799 extern JS_PUBLIC_API(void)
1800 JS_SetGCParameter(JSRuntime
*rt
, JSGCParamKey key
, uint32 value
);
1802 extern JS_PUBLIC_API(uint32
)
1803 JS_GetGCParameter(JSRuntime
*rt
, JSGCParamKey key
);
1805 extern JS_PUBLIC_API(void)
1806 JS_SetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
, uint32 value
);
1808 extern JS_PUBLIC_API(uint32
)
1809 JS_GetGCParameterForThread(JSContext
*cx
, JSGCParamKey key
);
1812 * Flush the code cache for the current thread. The operation might be
1813 * delayed if the cache cannot be flushed currently because native
1814 * code is currently executing.
1817 extern JS_PUBLIC_API(void)
1818 JS_FlushCaches(JSContext
*cx
);
1821 * Add a finalizer for external strings created by JS_NewExternalString (see
1822 * below) using a type-code returned from this function, and that understands
1823 * how to free or release the memory pointed at by JS_GetStringChars(str).
1825 * Return a nonnegative type index if there is room for finalizer in the
1826 * global GC finalizers table, else return -1. If the engine is compiled
1827 * JS_THREADSAFE and used in a multi-threaded environment, this function must
1828 * be invoked on the primordial thread only, at startup -- or else the entire
1829 * program must single-thread itself while loading a module that calls this
1832 extern JS_PUBLIC_API(intN
)
1833 JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1836 * Remove finalizer from the global GC finalizers table, returning its type
1837 * code if found, -1 if not found.
1839 * As with JS_AddExternalStringFinalizer, there is a threading restriction
1840 * if you compile the engine JS_THREADSAFE: this function may be called for a
1841 * given finalizer pointer on only one thread; different threads may call to
1842 * remove distinct finalizers safely.
1844 * You must ensure that all strings with finalizer's type have been collected
1845 * before calling this function. Otherwise, string data will be leaked by the
1846 * GC, for want of a finalizer to call.
1848 extern JS_PUBLIC_API(intN
)
1849 JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer
);
1852 * Create a new JSString whose chars member refers to external memory, i.e.,
1853 * memory requiring spe, type-specific finalization. The type code must
1854 * be a nonnegative return value from JS_AddExternalStringFinalizer.
1856 extern JS_PUBLIC_API(JSString
*)
1857 JS_NewExternalString(JSContext
*cx
, jschar
*chars
, size_t length
, intN type
);
1860 * Returns the external-string finalizer index for this string, or -1 if it is
1861 * an "internal" (native to JS engine) string.
1863 extern JS_PUBLIC_API(intN
)
1864 JS_GetExternalStringGCType(JSRuntime
*rt
, JSString
*str
);
1867 * Deprecated. Use JS_SetNativeStackQuoata instead.
1869 extern JS_PUBLIC_API(void)
1870 JS_SetThreadStackLimit(JSContext
*cx
, jsuword limitAddr
);
1873 * Set the size of the native stack that should not be exceed. To disable
1874 * stack size checking pass 0.
1876 extern JS_PUBLIC_API(void)
1877 JS_SetNativeStackQuota(JSContext
*cx
, size_t stackSize
);
1881 * Set the quota on the number of bytes that stack-like data structures can
1882 * use when the runtime compiles and executes scripts. These structures
1883 * consume heap space, so JS_SetThreadStackLimit does not bound their size.
1884 * The default quota is 128MB which is very generous.
1886 * The function must be called before any script compilation or execution API
1887 * calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
1890 extern JS_PUBLIC_API(void)
1891 JS_SetScriptStackQuota(JSContext
*cx
, size_t quota
);
1893 #define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x8000000)
1895 /************************************************************************/
1898 * Classes, objects, and properties.
1900 typedef void (*JSClassInternal
)();
1902 /* For detailed comments on the function pointer types, see jspubtd.h. */
1907 /* Mandatory non-null function pointer members. */
1908 JSPropertyOp addProperty
;
1909 JSPropertyOp delProperty
;
1910 JSPropertyOp getProperty
;
1911 JSStrictPropertyOp setProperty
;
1912 JSEnumerateOp enumerate
;
1913 JSResolveOp resolve
;
1914 JSConvertOp convert
;
1915 JSFinalizeOp finalize
;
1917 /* Optionally non-null members start here. */
1918 JSClassInternal reserved0
;
1919 JSCheckAccessOp checkAccess
;
1922 JSXDRObjectOp xdrObject
;
1923 JSHasInstanceOp hasInstance
;
1926 JSClassInternal reserved1
;
1930 #define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
1931 #define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
1932 #define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
1933 #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
1934 #define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
1935 object in prototype chain
1936 passed in via *objp in/out
1938 #define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
1940 #define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
1943 * To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
1944 * JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
1945 * n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
1947 #define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
1948 #define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
1949 #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
1950 #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
1951 << JSCLASS_RESERVED_SLOTS_SHIFT)
1952 #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
1953 >> JSCLASS_RESERVED_SLOTS_SHIFT) \
1954 & JSCLASS_RESERVED_SLOTS_MASK)
1956 #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
1957 JSCLASS_RESERVED_SLOTS_WIDTH)
1959 #define JSCLASS_INTERNAL_FLAG1 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
1960 #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
1961 #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
1963 /* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
1964 #define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
1965 #define JSCLASS_INTERNAL_FLAG2 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+4))
1967 /* Indicate whether the proto or ctor should be frozen. */
1968 #define JSCLASS_FREEZE_PROTO (1<<(JSCLASS_HIGH_FLAGS_SHIFT+5))
1969 #define JSCLASS_FREEZE_CTOR (1<<(JSCLASS_HIGH_FLAGS_SHIFT+6))
1971 /* Additional global reserved slots, beyond those for standard prototypes. */
1972 #define JSRESERVED_GLOBAL_SLOTS_COUNT 6
1973 #define JSRESERVED_GLOBAL_THIS (JSProto_LIMIT * 3)
1974 #define JSRESERVED_GLOBAL_THROWTYPEERROR (JSRESERVED_GLOBAL_THIS + 1)
1975 #define JSRESERVED_GLOBAL_REGEXP_STATICS (JSRESERVED_GLOBAL_THROWTYPEERROR + 1)
1976 #define JSRESERVED_GLOBAL_FUNCTION_NS (JSRESERVED_GLOBAL_REGEXP_STATICS + 1)
1977 #define JSRESERVED_GLOBAL_EVAL_ALLOWED (JSRESERVED_GLOBAL_FUNCTION_NS + 1)
1978 #define JSRESERVED_GLOBAL_FLAGS (JSRESERVED_GLOBAL_EVAL_ALLOWED + 1)
1981 #define JSGLOBAL_FLAGS_CLEARED 0x1
1984 * ECMA-262 requires that most constructors used internally create objects
1985 * with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
1986 * member initial value. The "original ... value" verbiage is there because
1987 * in ECMA-262, global properties naming class objects are read/write and
1988 * deleteable, for the most part.
1990 * Implementing this efficiently requires that global objects have classes
1991 * with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was
1992 * prevously allowed, but is now an ES5 violation and thus unsupported.
1994 #define JSCLASS_GLOBAL_FLAGS \
1995 (JSCLASS_IS_GLOBAL | \
1996 JSCLASS_HAS_RESERVED_SLOTS(JSRESERVED_GLOBAL_THIS + JSRESERVED_GLOBAL_SLOTS_COUNT))
1998 /* Fast access to the original value of each standard class's prototype. */
1999 #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
2000 #define JSCLASS_CACHED_PROTO_WIDTH 8
2001 #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
2002 #define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
2003 #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
2005 >> JSCLASS_CACHED_PROTO_SHIFT) \
2006 & JSCLASS_CACHED_PROTO_MASK))
2008 /* Initializer for unused members of statically initialized JSClass structs. */
2009 #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}
2010 #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,JSCLASS_NO_INTERNAL_MEMBERS
2014 jsid vector
[1]; /* actually, length jsid words */
2017 extern JS_PUBLIC_API(void)
2018 JS_DestroyIdArray(JSContext
*cx
, JSIdArray
*ida
);
2020 extern JS_PUBLIC_API(JSBool
)
2021 JS_ValueToId(JSContext
*cx
, jsval v
, jsid
*idp
);
2023 extern JS_PUBLIC_API(JSBool
)
2024 JS_IdToValue(JSContext
*cx
, jsid id
, jsval
*vp
);
2027 * JSNewResolveOp flag bits.
2029 #define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
2030 #define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
2031 #define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
2032 #define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
2033 #define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
2034 #define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
2036 extern JS_PUBLIC_API(JSBool
)
2037 JS_PropertyStub(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2039 extern JS_PUBLIC_API(JSBool
)
2040 JS_StrictPropertyStub(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool strict
, jsval
*vp
);
2042 extern JS_PUBLIC_API(JSBool
)
2043 JS_EnumerateStub(JSContext
*cx
, JSObject
*obj
);
2045 extern JS_PUBLIC_API(JSBool
)
2046 JS_ResolveStub(JSContext
*cx
, JSObject
*obj
, jsid id
);
2048 extern JS_PUBLIC_API(JSBool
)
2049 JS_ConvertStub(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
);
2051 extern JS_PUBLIC_API(void)
2052 JS_FinalizeStub(JSContext
*cx
, JSObject
*obj
);
2054 struct JSConstDoubleSpec
{
2062 * To define an array element rather than a named property member, cast the
2063 * element's index to (const char *) and initialize name with it, and set the
2064 * JSPROP_INDEX bit in flags.
2066 struct JSPropertySpec
{
2070 JSPropertyOp getter
;
2071 JSStrictPropertyOp setter
;
2074 struct JSFunctionSpec
{
2082 * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
2083 * that's passed to JS_DefineFunctions or JS_InitClass.
2085 #define JS_FS_END JS_FS(NULL,NULL,0,0)
2088 * Initializer macros for a JSFunctionSpec array element. JS_FN (whose name
2089 * pays homage to the old JSNative/JSFastNative split) simply adds the flag
2092 #define JS_FS(name,call,nargs,flags) \
2093 {name, call, nargs, flags}
2094 #define JS_FN(name,call,nargs,flags) \
2095 {name, call, nargs, (flags) | JSFUN_STUB_GSOPS}
2097 extern JS_PUBLIC_API(JSObject
*)
2098 JS_InitClass(JSContext
*cx
, JSObject
*obj
, JSObject
*parent_proto
,
2099 JSClass
*clasp
, JSNative constructor
, uintN nargs
,
2100 JSPropertySpec
*ps
, JSFunctionSpec
*fs
,
2101 JSPropertySpec
*static_ps
, JSFunctionSpec
*static_fs
);
2103 #ifdef JS_THREADSAFE
2104 extern JS_PUBLIC_API(JSClass
*)
2105 JS_GetClass(JSContext
*cx
, JSObject
*obj
);
2107 #define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
2109 extern JS_PUBLIC_API(JSClass
*)
2110 JS_GetClass(JSObject
*obj
);
2112 #define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
2115 extern JS_PUBLIC_API(JSBool
)
2116 JS_InstanceOf(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
, jsval
*argv
);
2118 extern JS_PUBLIC_API(JSBool
)
2119 JS_HasInstance(JSContext
*cx
, JSObject
*obj
, jsval v
, JSBool
*bp
);
2121 extern JS_PUBLIC_API(void *)
2122 JS_GetPrivate(JSContext
*cx
, JSObject
*obj
);
2124 extern JS_PUBLIC_API(JSBool
)
2125 JS_SetPrivate(JSContext
*cx
, JSObject
*obj
, void *data
);
2127 extern JS_PUBLIC_API(void *)
2128 JS_GetInstancePrivate(JSContext
*cx
, JSObject
*obj
, JSClass
*clasp
,
2131 extern JS_PUBLIC_API(JSObject
*)
2132 JS_GetPrototype(JSContext
*cx
, JSObject
*obj
);
2134 extern JS_PUBLIC_API(JSBool
)
2135 JS_SetPrototype(JSContext
*cx
, JSObject
*obj
, JSObject
*proto
);
2137 extern JS_PUBLIC_API(JSObject
*)
2138 JS_GetParent(JSContext
*cx
, JSObject
*obj
);
2140 extern JS_PUBLIC_API(JSBool
)
2141 JS_SetParent(JSContext
*cx
, JSObject
*obj
, JSObject
*parent
);
2143 extern JS_PUBLIC_API(JSObject
*)
2144 JS_GetConstructor(JSContext
*cx
, JSObject
*proto
);
2147 * Get a unique identifier for obj, good for the lifetime of obj (even if it
2148 * is moved by a copying GC). Return false on failure (likely out of memory),
2149 * and true with *idp containing the unique id on success.
2151 extern JS_PUBLIC_API(JSBool
)
2152 JS_GetObjectId(JSContext
*cx
, JSObject
*obj
, jsid
*idp
);
2154 extern JS_PUBLIC_API(JSObject
*)
2155 JS_NewGlobalObject(JSContext
*cx
, JSClass
*clasp
);
2157 extern JS_PUBLIC_API(JSObject
*)
2158 JS_NewCompartmentAndGlobalObject(JSContext
*cx
, JSClass
*clasp
, JSPrincipals
*principals
);
2160 extern JS_PUBLIC_API(JSObject
*)
2161 JS_NewObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
, JSObject
*parent
);
2163 /* Queries the [[Extensible]] property of the object. */
2164 extern JS_PUBLIC_API(JSBool
)
2165 JS_IsExtensible(JSObject
*obj
);
2168 * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
2169 * proto if proto's actual parameter value is null.
2171 extern JS_PUBLIC_API(JSObject
*)
2172 JS_NewObjectWithGivenProto(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
2176 * Freeze obj, and all objects it refers to, recursively. This will not recurse
2177 * through non-extensible objects, on the assumption that those are already
2180 extern JS_PUBLIC_API(JSBool
)
2181 JS_DeepFreezeObject(JSContext
*cx
, JSObject
*obj
);
2184 * Freezes an object; see ES5's Object.freeze(obj) method.
2186 extern JS_PUBLIC_API(JSBool
)
2187 JS_FreezeObject(JSContext
*cx
, JSObject
*obj
);
2189 extern JS_PUBLIC_API(JSObject
*)
2190 JS_ConstructObject(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
2193 extern JS_PUBLIC_API(JSObject
*)
2194 JS_ConstructObjectWithArguments(JSContext
*cx
, JSClass
*clasp
, JSObject
*proto
,
2195 JSObject
*parent
, uintN argc
, jsval
*argv
);
2197 extern JS_PUBLIC_API(JSObject
*)
2198 JS_New(JSContext
*cx
, JSObject
*ctor
, uintN argc
, jsval
*argv
);
2200 extern JS_PUBLIC_API(JSObject
*)
2201 JS_DefineObject(JSContext
*cx
, JSObject
*obj
, const char *name
, JSClass
*clasp
,
2202 JSObject
*proto
, uintN attrs
);
2204 extern JS_PUBLIC_API(JSBool
)
2205 JS_DefineConstDoubles(JSContext
*cx
, JSObject
*obj
, JSConstDoubleSpec
*cds
);
2207 extern JS_PUBLIC_API(JSBool
)
2208 JS_DefineProperties(JSContext
*cx
, JSObject
*obj
, JSPropertySpec
*ps
);
2210 extern JS_PUBLIC_API(JSBool
)
2211 JS_DefineProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval value
,
2212 JSPropertyOp getter
, JSStrictPropertyOp setter
, uintN attrs
);
2214 extern JS_PUBLIC_API(JSBool
)
2215 JS_DefinePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval value
,
2216 JSPropertyOp getter
, JSStrictPropertyOp setter
, uintN attrs
);
2218 extern JS_PUBLIC_API(JSBool
)
2219 JS_DefineOwnProperty(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval descriptor
, JSBool
*bp
);
2222 * Determine the attributes (JSPROP_* flags) of a property on a given object.
2224 * If the object does not have a property by that name, *foundp will be
2225 * JS_FALSE and the value of *attrsp is undefined.
2227 extern JS_PUBLIC_API(JSBool
)
2228 JS_GetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
2229 uintN
*attrsp
, JSBool
*foundp
);
2232 * The same, but if the property is native, return its getter and setter via
2233 * *getterp and *setterp, respectively (and only if the out parameter pointer
2236 extern JS_PUBLIC_API(JSBool
)
2237 JS_GetPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
2239 uintN
*attrsp
, JSBool
*foundp
,
2240 JSPropertyOp
*getterp
,
2241 JSStrictPropertyOp
*setterp
);
2243 extern JS_PUBLIC_API(JSBool
)
2244 JS_GetPropertyAttrsGetterAndSetterById(JSContext
*cx
, JSObject
*obj
,
2246 uintN
*attrsp
, JSBool
*foundp
,
2247 JSPropertyOp
*getterp
,
2248 JSStrictPropertyOp
*setterp
);
2251 * Set the attributes of a property on a given object.
2253 * If the object does not have a property by that name, *foundp will be
2254 * JS_FALSE and nothing will be altered.
2256 extern JS_PUBLIC_API(JSBool
)
2257 JS_SetPropertyAttributes(JSContext
*cx
, JSObject
*obj
, const char *name
,
2258 uintN attrs
, JSBool
*foundp
);
2260 extern JS_PUBLIC_API(JSBool
)
2261 JS_DefinePropertyWithTinyId(JSContext
*cx
, JSObject
*obj
, const char *name
,
2262 int8 tinyid
, jsval value
,
2263 JSPropertyOp getter
, JSStrictPropertyOp setter
,
2266 extern JS_PUBLIC_API(JSBool
)
2267 JS_AliasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
2270 extern JS_PUBLIC_API(JSBool
)
2271 JS_AlreadyHasOwnProperty(JSContext
*cx
, JSObject
*obj
, const char *name
,
2274 extern JS_PUBLIC_API(JSBool
)
2275 JS_AlreadyHasOwnPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
,
2278 extern JS_PUBLIC_API(JSBool
)
2279 JS_HasProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, JSBool
*foundp
);
2281 extern JS_PUBLIC_API(JSBool
)
2282 JS_HasPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSBool
*foundp
);
2284 extern JS_PUBLIC_API(JSBool
)
2285 JS_LookupProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2287 extern JS_PUBLIC_API(JSBool
)
2288 JS_LookupPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2290 extern JS_PUBLIC_API(JSBool
)
2291 JS_LookupPropertyWithFlags(JSContext
*cx
, JSObject
*obj
, const char *name
,
2292 uintN flags
, jsval
*vp
);
2294 extern JS_PUBLIC_API(JSBool
)
2295 JS_LookupPropertyWithFlagsById(JSContext
*cx
, JSObject
*obj
, jsid id
,
2296 uintN flags
, JSObject
**objp
, jsval
*vp
);
2298 struct JSPropertyDescriptor
{
2301 JSPropertyOp getter
;
2302 JSStrictPropertyOp setter
;
2308 * Like JS_GetPropertyAttrsGetterAndSetterById but will return a property on
2309 * an object on the prototype chain (returned in objp). If data->obj is null,
2310 * then this property was not found on the prototype chain.
2312 extern JS_PUBLIC_API(JSBool
)
2313 JS_GetPropertyDescriptorById(JSContext
*cx
, JSObject
*obj
, jsid id
, uintN flags
,
2314 JSPropertyDescriptor
*desc
);
2316 extern JS_PUBLIC_API(JSBool
)
2317 JS_GetOwnPropertyDescriptor(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2319 extern JS_PUBLIC_API(JSBool
)
2320 JS_GetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2322 extern JS_PUBLIC_API(JSBool
)
2323 JS_GetPropertyDefault(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval def
, jsval
*vp
);
2325 extern JS_PUBLIC_API(JSBool
)
2326 JS_GetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2328 extern JS_PUBLIC_API(JSBool
)
2329 JS_GetPropertyByIdDefault(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval def
, jsval
*vp
);
2331 extern JS_PUBLIC_API(JSBool
)
2332 JS_GetMethodById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSObject
**objp
,
2335 extern JS_PUBLIC_API(JSBool
)
2336 JS_GetMethod(JSContext
*cx
, JSObject
*obj
, const char *name
, JSObject
**objp
,
2339 extern JS_PUBLIC_API(JSBool
)
2340 JS_SetProperty(JSContext
*cx
, JSObject
*obj
, const char *name
, jsval
*vp
);
2342 extern JS_PUBLIC_API(JSBool
)
2343 JS_SetPropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*vp
);
2345 extern JS_PUBLIC_API(JSBool
)
2346 JS_DeleteProperty(JSContext
*cx
, JSObject
*obj
, const char *name
);
2348 extern JS_PUBLIC_API(JSBool
)
2349 JS_DeleteProperty2(JSContext
*cx
, JSObject
*obj
, const char *name
,
2352 extern JS_PUBLIC_API(JSBool
)
2353 JS_DeletePropertyById(JSContext
*cx
, JSObject
*obj
, jsid id
);
2355 extern JS_PUBLIC_API(JSBool
)
2356 JS_DeletePropertyById2(JSContext
*cx
, JSObject
*obj
, jsid id
, jsval
*rval
);
2358 extern JS_PUBLIC_API(JSBool
)
2359 JS_DefineUCProperty(JSContext
*cx
, JSObject
*obj
,
2360 const jschar
*name
, size_t namelen
, jsval value
,
2361 JSPropertyOp getter
, JSStrictPropertyOp setter
,
2365 * Determine the attributes (JSPROP_* flags) of a property on a given object.
2367 * If the object does not have a property by that name, *foundp will be
2368 * JS_FALSE and the value of *attrsp is undefined.
2370 extern JS_PUBLIC_API(JSBool
)
2371 JS_GetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
2372 const jschar
*name
, size_t namelen
,
2373 uintN
*attrsp
, JSBool
*foundp
);
2376 * The same, but if the property is native, return its getter and setter via
2377 * *getterp and *setterp, respectively (and only if the out parameter pointer
2380 extern JS_PUBLIC_API(JSBool
)
2381 JS_GetUCPropertyAttrsGetterAndSetter(JSContext
*cx
, JSObject
*obj
,
2382 const jschar
*name
, size_t namelen
,
2383 uintN
*attrsp
, JSBool
*foundp
,
2384 JSPropertyOp
*getterp
,
2385 JSStrictPropertyOp
*setterp
);
2388 * Set the attributes of a property on a given object.
2390 * If the object does not have a property by that name, *foundp will be
2391 * JS_FALSE and nothing will be altered.
2393 extern JS_PUBLIC_API(JSBool
)
2394 JS_SetUCPropertyAttributes(JSContext
*cx
, JSObject
*obj
,
2395 const jschar
*name
, size_t namelen
,
2396 uintN attrs
, JSBool
*foundp
);
2399 extern JS_PUBLIC_API(JSBool
)
2400 JS_DefineUCPropertyWithTinyId(JSContext
*cx
, JSObject
*obj
,
2401 const jschar
*name
, size_t namelen
,
2402 int8 tinyid
, jsval value
,
2403 JSPropertyOp getter
, JSStrictPropertyOp setter
,
2406 extern JS_PUBLIC_API(JSBool
)
2407 JS_AlreadyHasOwnUCProperty(JSContext
*cx
, JSObject
*obj
, const jschar
*name
,
2408 size_t namelen
, JSBool
*foundp
);
2410 extern JS_PUBLIC_API(JSBool
)
2411 JS_HasUCProperty(JSContext
*cx
, JSObject
*obj
,
2412 const jschar
*name
, size_t namelen
,
2415 extern JS_PUBLIC_API(JSBool
)
2416 JS_LookupUCProperty(JSContext
*cx
, JSObject
*obj
,
2417 const jschar
*name
, size_t namelen
,
2420 extern JS_PUBLIC_API(JSBool
)
2421 JS_GetUCProperty(JSContext
*cx
, JSObject
*obj
,
2422 const jschar
*name
, size_t namelen
,
2425 extern JS_PUBLIC_API(JSBool
)
2426 JS_SetUCProperty(JSContext
*cx
, JSObject
*obj
,
2427 const jschar
*name
, size_t namelen
,
2430 extern JS_PUBLIC_API(JSBool
)
2431 JS_DeleteUCProperty2(JSContext
*cx
, JSObject
*obj
,
2432 const jschar
*name
, size_t namelen
,
2435 extern JS_PUBLIC_API(JSObject
*)
2436 JS_NewArrayObject(JSContext
*cx
, jsint length
, jsval
*vector
);
2438 extern JS_PUBLIC_API(JSBool
)
2439 JS_IsArrayObject(JSContext
*cx
, JSObject
*obj
);
2441 extern JS_PUBLIC_API(JSBool
)
2442 JS_GetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
2444 extern JS_PUBLIC_API(JSBool
)
2445 JS_SetArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint length
);
2447 extern JS_PUBLIC_API(JSBool
)
2448 JS_HasArrayLength(JSContext
*cx
, JSObject
*obj
, jsuint
*lengthp
);
2450 extern JS_PUBLIC_API(JSBool
)
2451 JS_DefineElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval value
,
2452 JSPropertyOp getter
, JSStrictPropertyOp setter
, uintN attrs
);
2454 extern JS_PUBLIC_API(JSBool
)
2455 JS_AliasElement(JSContext
*cx
, JSObject
*obj
, const char *name
, jsint alias
);
2457 extern JS_PUBLIC_API(JSBool
)
2458 JS_AlreadyHasOwnElement(JSContext
*cx
, JSObject
*obj
, jsint index
,
2461 extern JS_PUBLIC_API(JSBool
)
2462 JS_HasElement(JSContext
*cx
, JSObject
*obj
, jsint index
, JSBool
*foundp
);
2464 extern JS_PUBLIC_API(JSBool
)
2465 JS_LookupElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2467 extern JS_PUBLIC_API(JSBool
)
2468 JS_GetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2470 extern JS_PUBLIC_API(JSBool
)
2471 JS_SetElement(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*vp
);
2473 extern JS_PUBLIC_API(JSBool
)
2474 JS_DeleteElement(JSContext
*cx
, JSObject
*obj
, jsint index
);
2476 extern JS_PUBLIC_API(JSBool
)
2477 JS_DeleteElement2(JSContext
*cx
, JSObject
*obj
, jsint index
, jsval
*rval
);
2479 extern JS_PUBLIC_API(void)
2480 JS_ClearScope(JSContext
*cx
, JSObject
*obj
);
2482 extern JS_PUBLIC_API(JSIdArray
*)
2483 JS_Enumerate(JSContext
*cx
, JSObject
*obj
);
2486 * Create an object to iterate over enumerable properties of obj, in arbitrary
2487 * property definition order. NB: This differs from longstanding for..in loop
2488 * order, which uses order of property definition in obj.
2490 extern JS_PUBLIC_API(JSObject
*)
2491 JS_NewPropertyIterator(JSContext
*cx
, JSObject
*obj
);
2494 * Return true on success with *idp containing the id of the next enumerable
2495 * property to visit using iterobj, or JSID_IS_VOID if there is no such property
2496 * left to visit. Return false on error.
2498 extern JS_PUBLIC_API(JSBool
)
2499 JS_NextProperty(JSContext
*cx
, JSObject
*iterobj
, jsid
*idp
);
2501 extern JS_PUBLIC_API(JSBool
)
2502 JS_CheckAccess(JSContext
*cx
, JSObject
*obj
, jsid id
, JSAccessMode mode
,
2503 jsval
*vp
, uintN
*attrsp
);
2505 extern JS_PUBLIC_API(JSBool
)
2506 JS_GetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval
*vp
);
2508 extern JS_PUBLIC_API(JSBool
)
2509 JS_SetReservedSlot(JSContext
*cx
, JSObject
*obj
, uint32 index
, jsval v
);
2511 /************************************************************************/
2514 * Security protocol.
2516 struct JSPrincipals
{
2519 /* XXX unspecified and unused by Mozilla code -- can we remove these? */
2520 void * (* getPrincipalArray
)(JSContext
*cx
, JSPrincipals
*);
2521 JSBool (* globalPrivilegesEnabled
)(JSContext
*cx
, JSPrincipals
*);
2523 /* Don't call "destroy"; use reference counting macros below. */
2524 jsrefcount refcount
;
2526 void (* destroy
)(JSContext
*cx
, JSPrincipals
*);
2527 JSBool (* subsume
)(JSPrincipals
*, JSPrincipals
*);
2530 #ifdef JS_THREADSAFE
2531 #define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
2532 #define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
2534 extern JS_PUBLIC_API(jsrefcount
)
2535 JS_HoldPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2537 extern JS_PUBLIC_API(jsrefcount
)
2538 JS_DropPrincipals(JSContext
*cx
, JSPrincipals
*principals
);
2541 #define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
2542 #define JSPRINCIPALS_DROP(cx, principals) \
2543 ((--(principals)->refcount == 0) \
2544 ? ((*(principals)->destroy)((cx), (principals)), 0) \
2545 : (principals)->refcount)
2549 struct JSSecurityCallbacks
{
2550 JSCheckAccessOp checkObjectAccess
;
2551 JSPrincipalsTranscoder principalsTranscoder
;
2552 JSObjectPrincipalsFinder findObjectPrincipals
;
2553 JSCSPEvalChecker contentSecurityPolicyAllows
;
2556 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2557 JS_SetRuntimeSecurityCallbacks(JSRuntime
*rt
, JSSecurityCallbacks
*callbacks
);
2559 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2560 JS_GetRuntimeSecurityCallbacks(JSRuntime
*rt
);
2562 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2563 JS_SetContextSecurityCallbacks(JSContext
*cx
, JSSecurityCallbacks
*callbacks
);
2565 extern JS_PUBLIC_API(JSSecurityCallbacks
*)
2566 JS_GetSecurityCallbacks(JSContext
*cx
);
2568 /************************************************************************/
2571 * Functions and scripts.
2573 extern JS_PUBLIC_API(JSFunction
*)
2574 JS_NewFunction(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
2575 JSObject
*parent
, const char *name
);
2578 * Create the function with the name given by the id. JSID_IS_STRING(id) must
2581 extern JS_PUBLIC_API(JSFunction
*)
2582 JS_NewFunctionById(JSContext
*cx
, JSNative call
, uintN nargs
, uintN flags
,
2583 JSObject
*parent
, jsid id
);
2585 extern JS_PUBLIC_API(JSObject
*)
2586 JS_GetFunctionObject(JSFunction
*fun
);
2589 * Return the function's identifier as a JSString, or null if fun is unnamed.
2590 * The returned string lives as long as fun, so you don't need to root a saved
2591 * reference to it if fun is well-connected or rooted, and provided you bound
2592 * the use of the saved reference by fun's lifetime.
2594 extern JS_PUBLIC_API(JSString
*)
2595 JS_GetFunctionId(JSFunction
*fun
);
2598 * Return JSFUN_* flags for fun.
2600 extern JS_PUBLIC_API(uintN
)
2601 JS_GetFunctionFlags(JSFunction
*fun
);
2604 * Return the arity (length) of fun.
2606 extern JS_PUBLIC_API(uint16
)
2607 JS_GetFunctionArity(JSFunction
*fun
);
2610 * Infallible predicate to test whether obj is a function object (faster than
2611 * comparing obj's class name to "Function", but equivalent unless someone has
2612 * overwritten the "Function" identifier with a different constructor and then
2613 * created instances using that constructor that might be passed in as obj).
2615 extern JS_PUBLIC_API(JSBool
)
2616 JS_ObjectIsFunction(JSContext
*cx
, JSObject
*obj
);
2618 extern JS_PUBLIC_API(JSBool
)
2619 JS_ObjectIsCallable(JSContext
*cx
, JSObject
*obj
);
2621 extern JS_PUBLIC_API(JSBool
)
2622 JS_DefineFunctions(JSContext
*cx
, JSObject
*obj
, JSFunctionSpec
*fs
);
2624 extern JS_PUBLIC_API(JSFunction
*)
2625 JS_DefineFunction(JSContext
*cx
, JSObject
*obj
, const char *name
, JSNative call
,
2626 uintN nargs
, uintN attrs
);
2628 extern JS_PUBLIC_API(JSFunction
*)
2629 JS_DefineUCFunction(JSContext
*cx
, JSObject
*obj
,
2630 const jschar
*name
, size_t namelen
, JSNative call
,
2631 uintN nargs
, uintN attrs
);
2633 extern JS_PUBLIC_API(JSFunction
*)
2634 JS_DefineFunctionById(JSContext
*cx
, JSObject
*obj
, jsid id
, JSNative call
,
2635 uintN nargs
, uintN attrs
);
2637 extern JS_PUBLIC_API(JSObject
*)
2638 JS_CloneFunctionObject(JSContext
*cx
, JSObject
*funobj
, JSObject
*parent
);
2641 * Given a buffer, return JS_FALSE if the buffer might become a valid
2642 * javascript statement with the addition of more lines. Otherwise return
2643 * JS_TRUE. The intent is to support interactive compilation - accumulate
2644 * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
2647 extern JS_PUBLIC_API(JSBool
)
2648 JS_BufferIsCompilableUnit(JSContext
*cx
, JSObject
*obj
,
2649 const char *bytes
, size_t length
);
2652 * The JSScript objects returned by the following functions refer to string and
2653 * other kinds of literals, including doubles and RegExp objects. These
2654 * literals are vulnerable to garbage collection; to root script objects and
2655 * prevent literals from being collected, create a rootable object using
2656 * JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
2658 extern JS_PUBLIC_API(JSScript
*)
2659 JS_CompileScript(JSContext
*cx
, JSObject
*obj
,
2660 const char *bytes
, size_t length
,
2661 const char *filename
, uintN lineno
);
2663 extern JS_PUBLIC_API(JSScript
*)
2664 JS_CompileScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2665 JSPrincipals
*principals
,
2666 const char *bytes
, size_t length
,
2667 const char *filename
, uintN lineno
);
2669 extern JS_PUBLIC_API(JSScript
*)
2670 JS_CompileScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2671 JSPrincipals
*principals
,
2672 const char *bytes
, size_t length
,
2673 const char *filename
, uintN lineno
,
2676 extern JS_PUBLIC_API(JSScript
*)
2677 JS_CompileUCScript(JSContext
*cx
, JSObject
*obj
,
2678 const jschar
*chars
, size_t length
,
2679 const char *filename
, uintN lineno
);
2681 extern JS_PUBLIC_API(JSScript
*)
2682 JS_CompileUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2683 JSPrincipals
*principals
,
2684 const jschar
*chars
, size_t length
,
2685 const char *filename
, uintN lineno
);
2687 extern JS_PUBLIC_API(JSScript
*)
2688 JS_CompileUCScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2689 JSPrincipals
*principals
,
2690 const jschar
*chars
, size_t length
,
2691 const char *filename
, uintN lineno
,
2694 extern JS_PUBLIC_API(JSScript
*)
2695 JS_CompileFile(JSContext
*cx
, JSObject
*obj
, const char *filename
);
2697 extern JS_PUBLIC_API(JSScript
*)
2698 JS_CompileFileHandle(JSContext
*cx
, JSObject
*obj
, const char *filename
,
2701 extern JS_PUBLIC_API(JSScript
*)
2702 JS_CompileFileHandleForPrincipals(JSContext
*cx
, JSObject
*obj
,
2703 const char *filename
, FILE *fh
,
2704 JSPrincipals
*principals
);
2706 extern JS_PUBLIC_API(JSScript
*)
2707 JS_CompileFileHandleForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2708 const char *filename
, FILE *fh
,
2709 JSPrincipals
*principals
,
2713 * NB: you must use JS_NewScriptObject and root a pointer to its return value
2714 * in order to keep a JSScript and its atoms safe from garbage collection after
2715 * creating the script via JS_Compile* and before a JS_ExecuteScript* call.
2716 * E.g., and without error checks:
2718 * JSScript *script = JS_CompileFile(cx, global, filename);
2719 * JSObject *scrobj = JS_NewScriptObject(cx, script);
2720 * JS_AddNamedObjectRoot(cx, &scrobj, "scrobj");
2723 * JS_ExecuteScript(cx, global, script, &result);
2725 * } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2726 * JS_RemoveObjectRoot(cx, &scrobj);
2728 extern JS_PUBLIC_API(JSObject
*)
2729 JS_NewScriptObject(JSContext
*cx
, JSScript
*script
);
2732 * Infallible getter for a script's object. If JS_NewScriptObject has not been
2733 * called on script yet, the return value will be null.
2735 extern JS_PUBLIC_API(JSObject
*)
2736 JS_GetScriptObject(JSScript
*script
);
2738 extern JS_PUBLIC_API(void)
2739 JS_DestroyScript(JSContext
*cx
, JSScript
*script
);
2741 extern JS_PUBLIC_API(JSFunction
*)
2742 JS_CompileFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2743 uintN nargs
, const char **argnames
,
2744 const char *bytes
, size_t length
,
2745 const char *filename
, uintN lineno
);
2747 extern JS_PUBLIC_API(JSFunction
*)
2748 JS_CompileFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2749 JSPrincipals
*principals
, const char *name
,
2750 uintN nargs
, const char **argnames
,
2751 const char *bytes
, size_t length
,
2752 const char *filename
, uintN lineno
);
2754 extern JS_PUBLIC_API(JSFunction
*)
2755 JS_CompileUCFunction(JSContext
*cx
, JSObject
*obj
, const char *name
,
2756 uintN nargs
, const char **argnames
,
2757 const jschar
*chars
, size_t length
,
2758 const char *filename
, uintN lineno
);
2760 extern JS_PUBLIC_API(JSFunction
*)
2761 JS_CompileUCFunctionForPrincipals(JSContext
*cx
, JSObject
*obj
,
2762 JSPrincipals
*principals
, const char *name
,
2763 uintN nargs
, const char **argnames
,
2764 const jschar
*chars
, size_t length
,
2765 const char *filename
, uintN lineno
);
2767 extern JS_PUBLIC_API(JSFunction
*)
2768 JS_CompileUCFunctionForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2769 JSPrincipals
*principals
, const char *name
,
2770 uintN nargs
, const char **argnames
,
2771 const jschar
*chars
, size_t length
,
2772 const char *filename
, uintN lineno
,
2775 extern JS_PUBLIC_API(JSString
*)
2776 JS_DecompileScript(JSContext
*cx
, JSScript
*script
, const char *name
,
2780 * API extension: OR this into indent to avoid pretty-printing the decompiled
2781 * source resulting from JS_DecompileFunction{,Body}.
2783 #define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
2785 extern JS_PUBLIC_API(JSString
*)
2786 JS_DecompileFunction(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2788 extern JS_PUBLIC_API(JSString
*)
2789 JS_DecompileFunctionBody(JSContext
*cx
, JSFunction
*fun
, uintN indent
);
2792 * NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
2793 * parameter as the initial scope chain header, the 'this' keyword value, and
2794 * the variables object (ECMA parlance for where 'var' and 'function' bind
2795 * names) of the execution context for script.
2797 * Using obj as the variables object is problematic if obj's parent (which is
2798 * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
2799 * this case, variables created by 'var x = 0', e.g., go in obj, but variables
2800 * created by assignment to an unbound id, 'x = 0', go in the last object on
2801 * the scope chain linked by parent.
2803 * ECMA calls that last scoping object the "global object", but note that many
2804 * embeddings have several such objects. ECMA requires that "global code" be
2805 * executed with the variables object equal to this global object. But these
2806 * JS API entry points provide freedom to execute code against a "sub-global",
2807 * i.e., a parented or scoped object, in which case the variables object will
2808 * differ from the last object on the scope chain, resulting in confusing and
2809 * non-ECMA explicit vs. implicit variable creation.
2811 * Caveat embedders: unless you already depend on this buggy variables object
2812 * binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
2813 * JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
2814 * someone may have set other options on cx already -- for each context in the
2815 * application, if you pass parented objects as the obj parameter, or may ever
2816 * pass such objects in the future.
2818 * Why a runtime option? The alternative is to add six or so new API entry
2819 * points with signatures matching the following six, and that doesn't seem
2820 * worth the code bloat cost. Such new entry points would probably have less
2821 * obvious names, too, so would not tend to be used. The JS_SetOption call,
2822 * OTOH, can be more easily hacked into existing code that does not depend on
2823 * the bug; such code can continue to use the familiar JS_EvaluateScript,
2824 * etc., entry points.
2826 extern JS_PUBLIC_API(JSBool
)
2827 JS_ExecuteScript(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
);
2829 extern JS_PUBLIC_API(JSBool
)
2830 JS_ExecuteScriptVersion(JSContext
*cx
, JSObject
*obj
, JSScript
*script
, jsval
*rval
,
2834 * Execute either the function-defining prolog of a script, or the script's
2835 * main body, but not both.
2837 typedef enum JSExecPart
{ JSEXEC_PROLOG
, JSEXEC_MAIN
} JSExecPart
;
2839 extern JS_PUBLIC_API(JSBool
)
2840 JS_EvaluateScript(JSContext
*cx
, JSObject
*obj
,
2841 const char *bytes
, uintN length
,
2842 const char *filename
, uintN lineno
,
2845 extern JS_PUBLIC_API(JSBool
)
2846 JS_EvaluateScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2847 JSPrincipals
*principals
,
2848 const char *bytes
, uintN length
,
2849 const char *filename
, uintN lineno
,
2852 extern JS_PUBLIC_API(JSBool
)
2853 JS_EvaluateScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2854 JSPrincipals
*principals
,
2855 const char *bytes
, uintN length
,
2856 const char *filename
, uintN lineno
,
2857 jsval
*rval
, JSVersion version
);
2859 extern JS_PUBLIC_API(JSBool
)
2860 JS_EvaluateUCScript(JSContext
*cx
, JSObject
*obj
,
2861 const jschar
*chars
, uintN length
,
2862 const char *filename
, uintN lineno
,
2865 extern JS_PUBLIC_API(JSBool
)
2866 JS_EvaluateUCScriptForPrincipalsVersion(JSContext
*cx
, JSObject
*obj
,
2867 JSPrincipals
*principals
,
2868 const jschar
*chars
, uintN length
,
2869 const char *filename
, uintN lineno
,
2870 jsval
*rval
, JSVersion version
);
2872 extern JS_PUBLIC_API(JSBool
)
2873 JS_EvaluateUCScriptForPrincipals(JSContext
*cx
, JSObject
*obj
,
2874 JSPrincipals
*principals
,
2875 const jschar
*chars
, uintN length
,
2876 const char *filename
, uintN lineno
,
2879 extern JS_PUBLIC_API(JSBool
)
2880 JS_CallFunction(JSContext
*cx
, JSObject
*obj
, JSFunction
*fun
, uintN argc
,
2881 jsval
*argv
, jsval
*rval
);
2883 extern JS_PUBLIC_API(JSBool
)
2884 JS_CallFunctionName(JSContext
*cx
, JSObject
*obj
, const char *name
, uintN argc
,
2885 jsval
*argv
, jsval
*rval
);
2887 extern JS_PUBLIC_API(JSBool
)
2888 JS_CallFunctionValue(JSContext
*cx
, JSObject
*obj
, jsval fval
, uintN argc
,
2889 jsval
*argv
, jsval
*rval
);
2897 Call(JSContext
*cx
, JSObject
*thisObj
, JSFunction
*fun
, uintN argc
, jsval
*argv
, jsval
*rval
) {
2898 return !!JS_CallFunction(cx
, thisObj
, fun
, argc
, argv
, rval
);
2902 Call(JSContext
*cx
, JSObject
*thisObj
, const char *name
, uintN argc
, jsval
*argv
, jsval
*rval
) {
2903 return !!JS_CallFunctionName(cx
, thisObj
, name
, argc
, argv
, rval
);
2907 Call(JSContext
*cx
, JSObject
*thisObj
, jsval fun
, uintN argc
, jsval
*argv
, jsval
*rval
) {
2908 return !!JS_CallFunctionValue(cx
, thisObj
, fun
, argc
, argv
, rval
);
2911 extern JS_PUBLIC_API(bool)
2912 Call(JSContext
*cx
, jsval thisv
, jsval fun
, uintN argc
, jsval
*argv
, jsval
*rval
);
2915 Call(JSContext
*cx
, jsval thisv
, JSObject
*funObj
, uintN argc
, jsval
*argv
, jsval
*rval
) {
2916 return Call(cx
, thisv
, OBJECT_TO_JSVAL(funObj
), argc
, argv
, rval
);
2919 } /* namespace js */
2922 #endif /* __cplusplus */
2925 * These functions allow setting an operation callback that will be called
2926 * from the thread the context is associated with some time after any thread
2927 * triggered the callback using JS_TriggerOperationCallback(cx).
2929 * In a threadsafe build the engine internally triggers operation callbacks
2930 * under certain circumstances (i.e. GC and title transfer) to force the
2931 * context to yield its current request, which the engine always
2932 * automatically does immediately prior to calling the callback function.
2933 * The embedding should thus not rely on callbacks being triggered through
2934 * the external API only.
2936 * Important note: Additional callbacks can occur inside the callback handler
2937 * if it re-enters the JS engine. The embedding must ensure that the callback
2938 * is disconnected before attempting such re-entry.
2941 extern JS_PUBLIC_API(JSOperationCallback
)
2942 JS_SetOperationCallback(JSContext
*cx
, JSOperationCallback callback
);
2944 extern JS_PUBLIC_API(JSOperationCallback
)
2945 JS_GetOperationCallback(JSContext
*cx
);
2947 extern JS_PUBLIC_API(void)
2948 JS_TriggerOperationCallback(JSContext
*cx
);
2950 extern JS_PUBLIC_API(void)
2951 JS_TriggerAllOperationCallbacks(JSRuntime
*rt
);
2953 extern JS_PUBLIC_API(JSBool
)
2954 JS_IsRunning(JSContext
*cx
);
2957 * Saving and restoring frame chains.
2959 * These two functions are used to set aside cx's call stack while that stack
2960 * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
2961 * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
2962 * must be balanced and all nested calls to JS_SaveFrameChain must have had
2963 * matching JS_RestoreFrameChain calls.
2965 * JS_SaveFrameChain deals with cx not having any code running on it. A null
2966 * return does not signify an error, and JS_RestoreFrameChain handles a null
2967 * frame pointer argument safely.
2969 extern JS_PUBLIC_API(JSStackFrame
*)
2970 JS_SaveFrameChain(JSContext
*cx
);
2972 extern JS_PUBLIC_API(void)
2973 JS_RestoreFrameChain(JSContext
*cx
, JSStackFrame
*fp
);
2975 /************************************************************************/
2980 * NB: JS_NewUCString takes ownership of bytes on success, avoiding a copy;
2981 * but on error (signified by null return), it leaves chars owned by the
2982 * caller. So the caller must free bytes in the error case, if it has no use
2983 * for them. In contrast, all the JS_New*StringCopy* functions do not take
2984 * ownership of the character memory passed to them -- they copy it.
2986 extern JS_PUBLIC_API(JSString
*)
2987 JS_NewStringCopyN(JSContext
*cx
, const char *s
, size_t n
);
2989 extern JS_PUBLIC_API(JSString
*)
2990 JS_NewStringCopyZ(JSContext
*cx
, const char *s
);
2992 extern JS_PUBLIC_API(JSString
*)
2993 JS_InternJSString(JSContext
*cx
, JSString
*str
);
2995 extern JS_PUBLIC_API(JSString
*)
2996 JS_InternString(JSContext
*cx
, const char *s
);
2998 extern JS_PUBLIC_API(JSString
*)
2999 JS_NewUCString(JSContext
*cx
, jschar
*chars
, size_t length
);
3001 extern JS_PUBLIC_API(JSString
*)
3002 JS_NewUCStringCopyN(JSContext
*cx
, const jschar
*s
, size_t n
);
3004 extern JS_PUBLIC_API(JSString
*)
3005 JS_NewUCStringCopyZ(JSContext
*cx
, const jschar
*s
);
3007 extern JS_PUBLIC_API(JSString
*)
3008 JS_InternUCStringN(JSContext
*cx
, const jschar
*s
, size_t length
);
3010 extern JS_PUBLIC_API(JSString
*)
3011 JS_InternUCString(JSContext
*cx
, const jschar
*s
);
3013 extern JS_PUBLIC_API(JSBool
)
3014 JS_CompareStrings(JSContext
*cx
, JSString
*str1
, JSString
*str2
, int32
*result
);
3016 extern JS_PUBLIC_API(JSBool
)
3017 JS_StringEqualsAscii(JSContext
*cx
, JSString
*str
, const char *asciiBytes
, JSBool
*match
);
3019 extern JS_PUBLIC_API(size_t)
3020 JS_PutEscapedString(JSContext
*cx
, char *buffer
, size_t size
, JSString
*str
, char quote
);
3022 extern JS_PUBLIC_API(JSBool
)
3023 JS_FileEscapedString(FILE *fp
, JSString
*str
, char quote
);
3026 * Extracting string characters and length.
3028 * While getting the length of a string is infallible, getting the chars can
3029 * fail. As indicated by the lack of a JSContext parameter, there are two
3030 * special cases where getting the chars is infallible:
3032 * The first case is interned strings, i.e., strings from JS_InternString or
3033 * JSID_TO_STRING(id), using JS_GetInternedStringChars*.
3035 * The second case is "flat" strings that have been explicitly prepared in a
3036 * fallible context by JS_FlattenString. To catch errors, a separate opaque
3037 * JSFlatString type is returned by JS_FlattenString and expected by
3038 * JS_GetFlatStringChars. Note, though, that this is purely a syntactic
3039 * distinction: the input and output of JS_FlattenString are the same actual
3040 * GC-thing so only one needs to be rooted. If a JSString is known to be flat,
3041 * JS_ASSERT_STRING_IS_FLAT can be used to make a debug-checked cast. Example:
3043 * // in a fallible context
3044 * JSFlatString *fstr = JS_FlattenString(cx, str);
3047 * JS_ASSERT(fstr == JS_ASSERT_STRING_IS_FLAT(str));
3049 * // in an infallible context, for the same 'str'
3050 * const jschar *chars = JS_GetFlatStringChars(fstr)
3053 * The CharsZ APIs guarantee that the returned array has a null character at
3054 * chars[length]. This can require additional copying so clients should prefer
3055 * APIs without CharsZ if possible. The infallible functions also return
3056 * null-terminated arrays. (There is no additional cost or non-Z alternative
3057 * for the infallible functions, so 'Z' is left out of the identifier.)
3060 extern JS_PUBLIC_API(size_t)
3061 JS_GetStringLength(JSString
*str
);
3063 extern JS_PUBLIC_API(const jschar
*)
3064 JS_GetStringCharsAndLength(JSContext
*cx
, JSString
*str
, size_t *length
);
3066 extern JS_PUBLIC_API(const jschar
*)
3067 JS_GetInternedStringChars(JSString
*str
);
3069 extern JS_PUBLIC_API(const jschar
*)
3070 JS_GetInternedStringCharsAndLength(JSString
*str
, size_t *length
);
3072 extern JS_PUBLIC_API(const jschar
*)
3073 JS_GetStringCharsZ(JSContext
*cx
, JSString
*str
);
3075 extern JS_PUBLIC_API(const jschar
*)
3076 JS_GetStringCharsZAndLength(JSContext
*cx
, JSString
*str
, size_t *length
);
3078 extern JS_PUBLIC_API(JSFlatString
*)
3079 JS_FlattenString(JSContext
*cx
, JSString
*str
);
3081 extern JS_PUBLIC_API(const jschar
*)
3082 JS_GetFlatStringChars(JSFlatString
*str
);
3084 static JS_ALWAYS_INLINE JSFlatString
*
3085 JSID_TO_FLAT_STRING(jsid id
)
3087 JS_ASSERT(JSID_IS_STRING(id
));
3088 return (JSFlatString
*)(JSID_BITS(id
));
3091 static JS_ALWAYS_INLINE JSFlatString
*
3092 JS_ASSERT_STRING_IS_FLAT(JSString
*str
)
3094 JS_ASSERT(JS_GetFlatStringChars((JSFlatString
*)str
));
3095 return (JSFlatString
*)str
;
3098 static JS_ALWAYS_INLINE JSString
*
3099 JS_FORGET_STRING_FLATNESS(JSFlatString
*fstr
)
3101 return (JSString
*)fstr
;
3105 * Additional APIs that avoid fallibility when given a flat string.
3108 extern JS_PUBLIC_API(JSBool
)
3109 JS_FlatStringEqualsAscii(JSFlatString
*str
, const char *asciiBytes
);
3111 extern JS_PUBLIC_API(size_t)
3112 JS_PutEscapedFlatString(char *buffer
, size_t size
, JSFlatString
*str
, char quote
);
3115 * This function is now obsolete and behaves the same as JS_NewUCString. Use
3116 * JS_NewUCString instead.
3118 extern JS_PUBLIC_API(JSString
*)
3119 JS_NewGrowableString(JSContext
*cx
, jschar
*chars
, size_t length
);
3122 * Mutable string support. A string's characters are never mutable in this JS
3123 * implementation, but a dependent string is a substring of another dependent
3124 * or immutable string, and a rope is a lazily concatenated string that creates
3125 * its underlying buffer the first time it is accessed. Even after a rope
3126 * creates its underlying buffer, it still considered mutable. The direct data
3127 * members of the (opaque to API clients) JSString struct may be changed in a
3128 * single-threaded way for dependent strings and ropes.
3130 * Therefore mutable strings (ropes and dependent strings) cannot be used by
3131 * more than one thread at a time. You may call JS_MakeStringImmutable to
3132 * convert the string from a mutable string to an immutable (and therefore
3133 * thread-safe) string. The engine takes care of converting ropes and dependent
3134 * strings to immutable for you if you store strings in multi-threaded objects
3135 * using JS_SetProperty or kindred API entry points.
3137 * If you store a JSString pointer in a native data structure that is (safely)
3138 * accessible to multiple threads, you must call JS_MakeStringImmutable before
3139 * retiring the store.
3143 * Create a dependent string, i.e., a string that owns no character storage,
3144 * but that refers to a slice of another string's chars. Dependent strings
3145 * are mutable by definition, so the thread safety comments above apply.
3147 extern JS_PUBLIC_API(JSString
*)
3148 JS_NewDependentString(JSContext
*cx
, JSString
*str
, size_t start
,
3152 * Concatenate two strings, possibly resulting in a rope.
3153 * See above for thread safety comments.
3155 extern JS_PUBLIC_API(JSString
*)
3156 JS_ConcatStrings(JSContext
*cx
, JSString
*left
, JSString
*right
);
3159 * Convert a dependent string into an independent one. This function does not
3160 * change the string's mutability, so the thread safety comments above apply.
3162 extern JS_PUBLIC_API(const jschar
*)
3163 JS_UndependString(JSContext
*cx
, JSString
*str
);
3166 * Convert a mutable string (either rope or dependent) into an immutable,
3169 extern JS_PUBLIC_API(JSBool
)
3170 JS_MakeStringImmutable(JSContext
*cx
, JSString
*str
);
3173 * Return JS_TRUE if C (char []) strings passed via the API and internally
3176 JS_PUBLIC_API(JSBool
)
3177 JS_CStringsAreUTF8(void);
3180 * Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
3181 * can never be changed. This API must be called before the first call to
3185 JS_SetCStringsAreUTF8(void);
3188 * Character encoding support.
3190 * For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
3191 * of the destination buffer before the call; on return, *dstlenp contains the
3192 * number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
3193 * stored. To determine the necessary destination buffer size, make a sizing
3194 * call that passes NULL for dst.
3196 * On errors, the functions report the error. In that case, *dstlenp contains
3197 * the number of characters or bytes transferred so far. If cx is NULL, no
3198 * error is reported on failure, and the functions simply return JS_FALSE.
3200 * NB: Neither function stores an additional zero byte or jschar after the
3201 * transcoded string.
3203 * If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
3204 * UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
3205 * errors if the character sequence is malformed. If UTF-8 support is
3206 * disabled, the functions deflate and inflate, respectively.
3208 JS_PUBLIC_API(JSBool
)
3209 JS_EncodeCharacters(JSContext
*cx
, const jschar
*src
, size_t srclen
, char *dst
,
3212 JS_PUBLIC_API(JSBool
)
3213 JS_DecodeBytes(JSContext
*cx
, const char *src
, size_t srclen
, jschar
*dst
,
3217 * A variation on JS_EncodeCharacters where a null terminated string is
3218 * returned that you are expected to call JS_free on when done.
3220 JS_PUBLIC_API(char *)
3221 JS_EncodeString(JSContext
*cx
, JSString
*str
);
3224 * Get number of bytes in the string encoding (without accounting for a
3225 * terminating zero bytes. The function returns (size_t) -1 if the string
3226 * can not be encoded into bytes and reports an error using cx accordingly.
3228 JS_PUBLIC_API(size_t)
3229 JS_GetStringEncodingLength(JSContext
*cx
, JSString
*str
);
3232 * Encode string into a buffer. The function does not stores an additional
3233 * zero byte. The function returns (size_t) -1 if the string can not be
3234 * encoded into bytes with no error reported. Otherwise it returns the number
3235 * of bytes that are necessary to encode the string. If that exceeds the
3236 * length parameter, the string will be cut and only length bytes will be
3237 * written into the buffer.
3239 * If JS_CStringsAreUTF8() is true, the string does not fit into the buffer
3240 * and the the first length bytes ends in the middle of utf-8 encoding for
3241 * some character, then such partial utf-8 encoding is replaced by zero bytes.
3242 * This way the result always represents the valid UTF-8 sequence.
3244 JS_PUBLIC_API(size_t)
3245 JS_EncodeStringToBuffer(JSString
*str
, char *buffer
, size_t length
);
3249 class JSAutoByteString
{
3251 JSAutoByteString(JSContext
*cx
, JSString
*str JS_GUARD_OBJECT_NOTIFIER_PARAM
)
3252 : mBytes(JS_EncodeString(cx
, str
)) {
3254 JS_GUARD_OBJECT_NOTIFIER_INIT
;
3257 JSAutoByteString(JS_GUARD_OBJECT_NOTIFIER_PARAM0
)
3259 JS_GUARD_OBJECT_NOTIFIER_INIT
;
3262 ~JSAutoByteString() {
3266 /* Take ownership of the given byte array. */
3267 void initBytes(char *bytes
) {
3272 char *encode(JSContext
*cx
, JSString
*str
) {
3275 mBytes
= JS_EncodeString(cx
, str
);
3288 bool operator!() const {
3294 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
3296 /* Copy and assignment are not supported. */
3297 JSAutoByteString(const JSAutoByteString
&another
);
3298 JSAutoByteString
&operator=(const JSAutoByteString
&another
);
3303 /************************************************************************/
3307 typedef JSBool (* JSONWriteCallback
)(const jschar
*buf
, uint32 len
, void *data
);
3310 * JSON.stringify as specified by ES3.1 (draft)
3312 JS_PUBLIC_API(JSBool
)
3313 JS_Stringify(JSContext
*cx
, jsval
*vp
, JSObject
*replacer
, jsval space
,
3314 JSONWriteCallback callback
, void *data
);
3317 * Retrieve a toJSON function. If found, set vp to its result.
3319 JS_PUBLIC_API(JSBool
)
3320 JS_TryJSON(JSContext
*cx
, jsval
*vp
);
3323 * JSON.parse as specified by ES3.1 (draft)
3325 JS_PUBLIC_API(JSONParser
*)
3326 JS_BeginJSONParse(JSContext
*cx
, jsval
*vp
);
3328 JS_PUBLIC_API(JSBool
)
3329 JS_ConsumeJSONText(JSContext
*cx
, JSONParser
*jp
, const jschar
*data
, uint32 len
);
3331 JS_PUBLIC_API(JSBool
)
3332 JS_FinishJSONParse(JSContext
*cx
, JSONParser
*jp
, jsval reviver
);
3334 /************************************************************************/
3336 /* API for the HTML5 internal structured cloning algorithm. */
3338 /* The maximum supported structured-clone serialization format version. */
3339 #define JS_STRUCTURED_CLONE_VERSION 1
3341 struct JSStructuredCloneCallbacks
{
3342 ReadStructuredCloneOp read
;
3343 WriteStructuredCloneOp write
;
3344 StructuredCloneErrorOp reportError
;
3347 JS_PUBLIC_API(JSBool
)
3348 JS_ReadStructuredClone(JSContext
*cx
, const uint64
*data
, size_t nbytes
,
3349 uint32 version
, jsval
*vp
,
3350 const JSStructuredCloneCallbacks
*optionalCallbacks
,
3353 /* Note: On success, the caller is responsible for calling js_free(*datap). */
3354 JS_PUBLIC_API(JSBool
)
3355 JS_WriteStructuredClone(JSContext
*cx
, jsval v
, uint64
**datap
, size_t *nbytesp
,
3356 const JSStructuredCloneCallbacks
*optionalCallbacks
,
3359 JS_PUBLIC_API(JSBool
)
3360 JS_StructuredClone(JSContext
*cx
, jsval v
, jsval
*vp
,
3361 const JSStructuredCloneCallbacks
*optionalCallbacks
,
3365 /* RAII sugar for JS_WriteStructuredClone. */
3366 class JSAutoStructuredCloneBuffer
{
3373 JSAutoStructuredCloneBuffer()
3374 : cx_(NULL
), data_(NULL
), nbytes_(0), version_(JS_STRUCTURED_CLONE_VERSION
) {}
3376 ~JSAutoStructuredCloneBuffer() { clear(); }
3378 JSContext
*cx() const { return cx_
; }
3379 uint64
*data() const { return data_
; }
3380 size_t nbytes() const { return nbytes_
; }
3382 void clear(JSContext
*cx
=NULL
) {
3396 * Adopt some memory. It will be automatically freed by the destructor.
3397 * data must have been allocated using JS_malloc.
3399 void adopt(JSContext
*cx
, uint64
*data
, size_t nbytes
,
3400 uint32 version
=JS_STRUCTURED_CLONE_VERSION
) {
3409 * Remove the buffer so that it will not be automatically freed.
3410 * After this, the caller is responsible for calling JS_free(*datap).
3412 void steal(uint64
**datap
, size_t *nbytesp
, JSContext
**cxp
=NULL
,
3413 uint32
*versionp
=NULL
) {
3419 *versionp
= version_
;
3427 bool read(jsval
*vp
, JSContext
*cx
=NULL
,
3428 const JSStructuredCloneCallbacks
*optionalCallbacks
=NULL
,
3429 void *closure
=NULL
) const {
3434 return !!JS_ReadStructuredClone(cx
, data_
, nbytes_
, version_
, vp
,
3435 optionalCallbacks
, closure
);
3438 bool write(JSContext
*cx
, jsval v
,
3439 const JSStructuredCloneCallbacks
*optionalCallbacks
=NULL
,
3440 void *closure
=NULL
) {
3443 bool ok
= !!JS_WriteStructuredClone(cx
, v
, &data_
, &nbytes_
,
3444 optionalCallbacks
, closure
);
3448 version_
= JS_STRUCTURED_CLONE_VERSION
;
3454 * Swap ownership with another JSAutoStructuredCloneBuffer.
3456 void swap(JSAutoStructuredCloneBuffer
&other
) {
3457 JSContext
*cx
= other
.cx_
;
3458 uint64
*data
= other
.data_
;
3459 size_t nbytes
= other
.nbytes_
;
3460 uint32 version
= other
.version_
;
3462 other
.cx_
= this->cx_
;
3463 other
.data_
= this->data_
;
3464 other
.nbytes_
= this->nbytes_
;
3465 other
.version_
= this->version_
;
3469 this->nbytes_
= nbytes
;
3470 this->version_
= version
;
3474 /* Copy and assignment are not supported. */
3475 JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer
&other
);
3476 JSAutoStructuredCloneBuffer
&operator=(const JSAutoStructuredCloneBuffer
&other
);
3480 /* API for implementing custom serialization behavior (for ImageData, File, etc.) */
3482 /* The range of tag values the application may use for its own custom object types. */
3483 #define JS_SCTAG_USER_MIN ((uint32) 0xFFFF8000)
3484 #define JS_SCTAG_USER_MAX ((uint32) 0xFFFFFFFF)
3486 #define JS_SCERR_RECURSION 0
3489 JS_SetStructuredCloneCallbacks(JSRuntime
*rt
, const JSStructuredCloneCallbacks
*callbacks
);
3491 JS_PUBLIC_API(JSBool
)
3492 JS_ReadUint32Pair(JSStructuredCloneReader
*r
, uint32
*p1
, uint32
*p2
);
3494 JS_PUBLIC_API(JSBool
)
3495 JS_ReadBytes(JSStructuredCloneReader
*r
, void *p
, size_t len
);
3497 JS_PUBLIC_API(JSBool
)
3498 JS_WriteUint32Pair(JSStructuredCloneWriter
*w
, uint32 tag
, uint32 data
);
3500 JS_PUBLIC_API(JSBool
)
3501 JS_WriteBytes(JSStructuredCloneWriter
*w
, const void *p
, size_t len
);
3503 /************************************************************************/
3506 * Locale specific string conversion and error message callbacks.
3508 struct JSLocaleCallbacks
{
3509 JSLocaleToUpperCase localeToUpperCase
;
3510 JSLocaleToLowerCase localeToLowerCase
;
3511 JSLocaleCompare localeCompare
;
3512 JSLocaleToUnicode localeToUnicode
;
3513 JSErrorCallback localeGetErrorMessage
;
3517 * Establish locale callbacks. The pointer must persist as long as the
3518 * JSContext. Passing NULL restores the default behaviour.
3520 extern JS_PUBLIC_API(void)
3521 JS_SetLocaleCallbacks(JSContext
*cx
, JSLocaleCallbacks
*callbacks
);
3524 * Return the address of the current locale callbacks struct, which may
3527 extern JS_PUBLIC_API(JSLocaleCallbacks
*)
3528 JS_GetLocaleCallbacks(JSContext
*cx
);
3530 /************************************************************************/
3537 * Report an exception represented by the sprintf-like conversion of format
3538 * and its arguments. This exception message string is passed to a pre-set
3539 * JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
3540 * the JSErrorReporter typedef).
3542 extern JS_PUBLIC_API(void)
3543 JS_ReportError(JSContext
*cx
, const char *format
, ...);
3546 * Use an errorNumber to retrieve the format string, args are char *
3548 extern JS_PUBLIC_API(void)
3549 JS_ReportErrorNumber(JSContext
*cx
, JSErrorCallback errorCallback
,
3550 void *userRef
, const uintN errorNumber
, ...);
3553 * Use an errorNumber to retrieve the format string, args are jschar *
3555 extern JS_PUBLIC_API(void)
3556 JS_ReportErrorNumberUC(JSContext
*cx
, JSErrorCallback errorCallback
,
3557 void *userRef
, const uintN errorNumber
, ...);
3560 * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
3561 * Return true if there was no error trying to issue the warning, and if the
3562 * warning was not converted into an error due to the JSOPTION_WERROR option
3563 * being set, false otherwise.
3565 extern JS_PUBLIC_API(JSBool
)
3566 JS_ReportWarning(JSContext
*cx
, const char *format
, ...);
3568 extern JS_PUBLIC_API(JSBool
)
3569 JS_ReportErrorFlagsAndNumber(JSContext
*cx
, uintN flags
,
3570 JSErrorCallback errorCallback
, void *userRef
,
3571 const uintN errorNumber
, ...);
3573 extern JS_PUBLIC_API(JSBool
)
3574 JS_ReportErrorFlagsAndNumberUC(JSContext
*cx
, uintN flags
,
3575 JSErrorCallback errorCallback
, void *userRef
,
3576 const uintN errorNumber
, ...);
3579 * Complain when out of memory.
3581 extern JS_PUBLIC_API(void)
3582 JS_ReportOutOfMemory(JSContext
*cx
);
3585 * Complain when an allocation size overflows the maximum supported limit.
3587 extern JS_PUBLIC_API(void)
3588 JS_ReportAllocationOverflow(JSContext
*cx
);
3590 struct JSErrorReport
{
3591 const char *filename
; /* source file name, URL, etc., or null */
3592 uintN lineno
; /* source line number */
3593 const char *linebuf
; /* offending source line without final \n */
3594 const char *tokenptr
; /* pointer to error token in linebuf */
3595 const jschar
*uclinebuf
; /* unicode (original) line buffer */
3596 const jschar
*uctokenptr
; /* unicode (original) token pointer */
3597 uintN flags
; /* error/warning, etc. */
3598 uintN errorNumber
; /* the error number, e.g. see js.msg */
3599 const jschar
*ucmessage
; /* the (default) error message */
3600 const jschar
**messageArgs
; /* arguments for the error message */
3604 * JSErrorReport flag values. These may be freely composed.
3606 #define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
3607 #define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
3608 #define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
3609 #define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
3612 * This condition is an error in strict mode code, a warning if
3613 * JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
3614 * all. We check the strictness of the context's top frame's script;
3615 * where that isn't appropriate, the caller should do the right checks
3616 * itself instead of using this flag.
3618 #define JSREPORT_STRICT_MODE_ERROR 0x8
3621 * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
3622 * has been thrown for this runtime error, and the host should ignore it.
3623 * Exception-aware hosts should also check for JS_IsExceptionPending if
3624 * JS_ExecuteScript returns failure, and signal or propagate the exception, as
3627 #define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
3628 #define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
3629 #define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
3630 #define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) & \
3631 JSREPORT_STRICT_MODE_ERROR) != 0)
3633 extern JS_PUBLIC_API(JSErrorReporter
)
3634 JS_SetErrorReporter(JSContext
*cx
, JSErrorReporter er
);
3636 /************************************************************************/
3642 extern JS_PUBLIC_API(JSObject
*)
3643 JS_NewDateObject(JSContext
*cx
, int year
, int mon
, int mday
, int hour
, int min
, int sec
);
3645 extern JS_PUBLIC_API(JSObject
*)
3646 JS_NewDateObjectMsec(JSContext
*cx
, jsdouble msec
);
3649 * Infallible predicate to test whether obj is a date object.
3651 extern JS_PUBLIC_API(JSBool
)
3652 JS_ObjectIsDate(JSContext
*cx
, JSObject
*obj
);
3654 /************************************************************************/
3657 * Regular Expressions.
3659 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
3660 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
3661 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
3662 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
3663 #define JSREG_FLAT 0x10 /* parse as a flat regexp */
3664 #define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
3666 extern JS_PUBLIC_API(JSObject
*)
3667 JS_NewRegExpObject(JSContext
*cx
, JSObject
*obj
, char *bytes
, size_t length
, uintN flags
);
3669 extern JS_PUBLIC_API(JSObject
*)
3670 JS_NewUCRegExpObject(JSContext
*cx
, JSObject
*obj
, jschar
*chars
, size_t length
, uintN flags
);
3672 extern JS_PUBLIC_API(void)
3673 JS_SetRegExpInput(JSContext
*cx
, JSObject
*obj
, JSString
*input
, JSBool multiline
);
3675 extern JS_PUBLIC_API(void)
3676 JS_ClearRegExpStatics(JSContext
*cx
, JSObject
*obj
);
3678 extern JS_PUBLIC_API(JSBool
)
3679 JS_ExecuteRegExp(JSContext
*cx
, JSObject
*obj
, JSObject
*reobj
, jschar
*chars
, size_t length
,
3680 size_t *indexp
, JSBool test
, jsval
*rval
);
3682 /* RegExp interface for clients without a global object. */
3684 extern JS_PUBLIC_API(JSObject
*)
3685 JS_NewRegExpObjectNoStatics(JSContext
*cx
, char *bytes
, size_t length
, uintN flags
);
3687 extern JS_PUBLIC_API(JSObject
*)
3688 JS_NewUCRegExpObjectNoStatics(JSContext
*cx
, jschar
*chars
, size_t length
, uintN flags
);
3690 extern JS_PUBLIC_API(JSBool
)
3691 JS_ExecuteRegExpNoStatics(JSContext
*cx
, JSObject
*reobj
, jschar
*chars
, size_t length
,
3692 size_t *indexp
, JSBool test
, jsval
*rval
);
3694 /************************************************************************/
3696 extern JS_PUBLIC_API(JSBool
)
3697 JS_IsExceptionPending(JSContext
*cx
);
3699 extern JS_PUBLIC_API(JSBool
)
3700 JS_GetPendingException(JSContext
*cx
, jsval
*vp
);
3702 extern JS_PUBLIC_API(void)
3703 JS_SetPendingException(JSContext
*cx
, jsval v
);
3705 extern JS_PUBLIC_API(void)
3706 JS_ClearPendingException(JSContext
*cx
);
3708 extern JS_PUBLIC_API(JSBool
)
3709 JS_ReportPendingException(JSContext
*cx
);
3712 * Save the current exception state. This takes a snapshot of cx's current
3713 * exception state without making any change to that state.
3715 * The returned state pointer MUST be passed later to JS_RestoreExceptionState
3716 * (to restore that saved state, overriding any more recent state) or else to
3717 * JS_DropExceptionState (to free the state struct in case it is not correct
3718 * or desirable to restore it). Both Restore and Drop free the state struct,
3719 * so callers must stop using the pointer returned from Save after calling the
3720 * Release or Drop API.
3722 extern JS_PUBLIC_API(JSExceptionState
*)
3723 JS_SaveExceptionState(JSContext
*cx
);
3725 extern JS_PUBLIC_API(void)
3726 JS_RestoreExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3728 extern JS_PUBLIC_API(void)
3729 JS_DropExceptionState(JSContext
*cx
, JSExceptionState
*state
);
3732 * If the given value is an exception object that originated from an error,
3733 * the exception will contain an error report struct, and this API will return
3734 * the address of that struct. Otherwise, it returns NULL. The lifetime of
3735 * the error report struct that might be returned is the same as the lifetime
3736 * of the exception object.
3738 extern JS_PUBLIC_API(JSErrorReport
*)
3739 JS_ErrorFromException(JSContext
*cx
, jsval v
);
3742 * Given a reported error's message and JSErrorReport struct pointer, throw
3743 * the corresponding exception on cx.
3745 extern JS_PUBLIC_API(JSBool
)
3746 JS_ThrowReportedError(JSContext
*cx
, const char *message
,
3747 JSErrorReport
*reportp
);
3750 * Throws a StopIteration exception on cx.
3752 extern JS_PUBLIC_API(JSBool
)
3753 JS_ThrowStopIteration(JSContext
*cx
);
3756 * Associate the current thread with the given context. This is done
3757 * implicitly by JS_NewContext.
3759 * Returns the old thread id for this context, which should be treated as
3760 * an opaque value. This value is provided for comparison to 0, which
3761 * indicates that ClearContextThread has been called on this context
3762 * since the last SetContextThread, or non-0, which indicates the opposite.
3764 extern JS_PUBLIC_API(jsword
)
3765 JS_GetContextThread(JSContext
*cx
);
3767 extern JS_PUBLIC_API(jsword
)
3768 JS_SetContextThread(JSContext
*cx
);
3770 extern JS_PUBLIC_API(jsword
)
3771 JS_ClearContextThread(JSContext
*cx
);
3773 #ifdef MOZ_TRACE_JSCALLS
3774 typedef void (*JSFunctionCallback
)(const JSFunction
*fun
,
3775 const JSScript
*scr
,
3776 const JSContext
*cx
,
3780 * The callback is expected to be quick and noninvasive. It should not
3781 * trigger interrupts, turn on debugging, or produce uncaught JS
3782 * exceptions. The state of the stack and registers in the context
3783 * cannot be relied upon, since this callback may be invoked directly
3784 * from either JIT. The 'entering' field means we are entering a
3785 * function if it is positive, leaving a function if it is zero or
3788 extern JS_PUBLIC_API(void)
3789 JS_SetFunctionCallback(JSContext
*cx
, JSFunctionCallback fcb
);
3791 extern JS_PUBLIC_API(JSFunctionCallback
)
3792 JS_GetFunctionCallback(JSContext
*cx
);
3795 /************************************************************************/
3798 * JS_IsConstructing must be called from within a native given the
3799 * native's original cx and vp arguments. If JS_IsConstructing is true,
3800 * JS_THIS must not be used; the constructor should construct and return a
3801 * new object. Otherwise, the native is called as an ordinary function and
3802 * JS_THIS may be used.
3804 static JS_ALWAYS_INLINE JSBool
3805 JS_IsConstructing(JSContext
*cx
, const jsval
*vp
)
3810 JSObject
*callee
= JSVAL_TO_OBJECT(JS_CALLEE(cx
, vp
));
3811 if (JS_ObjectIsFunction(cx
, callee
)) {
3812 JSFunction
*fun
= JS_ValueToFunction(cx
, JS_CALLEE(cx
, vp
));
3813 JS_ASSERT((JS_GetFunctionFlags(fun
) & JSFUN_CONSTRUCTOR
) != 0);
3815 JS_ASSERT(JS_GET_CLASS(cx
, callee
)->construct
!= NULL
);
3819 l
.asBits
= JSVAL_BITS(vp
[1]);
3820 return JSVAL_IS_MAGIC_IMPL(l
);
3824 * In the case of a constructor called from JS_ConstructObject and
3825 * JS_InitClass where the class has the JSCLASS_CONSTRUCT_PROTOTYPE flag set,
3826 * the JS engine passes the constructor a non-standard 'this' object. In such
3827 * cases, the following query provides the additional information of whether a
3828 * special 'this' was supplied. E.g.:
3830 * JSBool foo_native(JSContext *cx, uintN argc, jsval *vp) {
3831 * JSObject *maybeThis;
3832 * if (JS_IsConstructing_PossiblyWithGivenThisObject(cx, vp, &maybeThis)) {
3833 * // native called as a constructor
3835 * // native called as a constructor with maybeThis as 'this'
3837 * // native called as function, maybeThis is still uninitialized
3841 * Note that embeddings do not need to use this query unless they use the
3842 * aforementioned API/flags.
3844 static JS_ALWAYS_INLINE JSBool
3845 JS_IsConstructing_PossiblyWithGivenThisObject(JSContext
*cx
, const jsval
*vp
,
3846 JSObject
**maybeThis
)
3852 JSObject
*callee
= JSVAL_TO_OBJECT(JS_CALLEE(cx
, vp
));
3853 if (JS_ObjectIsFunction(cx
, callee
)) {
3854 JSFunction
*fun
= JS_ValueToFunction(cx
, JS_CALLEE(cx
, vp
));
3855 JS_ASSERT((JS_GetFunctionFlags(fun
) & JSFUN_CONSTRUCTOR
) != 0);
3857 JS_ASSERT(JS_GET_CLASS(cx
, callee
)->construct
!= NULL
);
3861 l
.asBits
= JSVAL_BITS(vp
[1]);
3862 isCtor
= JSVAL_IS_MAGIC_IMPL(l
);
3864 *maybeThis
= MAGIC_JSVAL_TO_OBJECT_OR_NULL_IMPL(l
);
3869 * If a constructor does not have any static knowledge about the type of
3870 * object to create, it can request that the JS engine create a default new
3871 * 'this' object, as is done for non-constructor natives when called with new.
3873 extern JS_PUBLIC_API(JSObject
*)
3874 JS_NewObjectForConstructor(JSContext
*cx
, const jsval
*vp
);
3876 /************************************************************************/
3879 #define JS_GC_ZEAL 1
3883 extern JS_PUBLIC_API(void)
3884 JS_SetGCZeal(JSContext
*cx
, uint8 zeal
);
3889 #endif /* jsapi_h___ */