gecko-dev/js/src/jsapi.h

2829 lines
102 KiB
C
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
1998-03-28 02:44:41 +00:00
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
1998-03-28 02:44:41 +00:00
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
1998-03-28 02:44:41 +00:00
*
2001-09-20 00:02:59 +00:00
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
1998-03-28 02:44:41 +00:00
#ifndef jsapi_h___
#define jsapi_h___
/*
* JavaScript API.
*/
#include <stddef.h>
#include <stdio.h>
#include "js-config.h"
1998-03-28 02:44:41 +00:00
#include "jspubtd.h"
#include "jsutil.h"
1998-03-28 02:44:41 +00:00
JS_BEGIN_EXTERN_C
1998-03-28 02:44:41 +00:00
/*
* Type tags stored in the low bits of a jsval.
*/
typedef enum jsvaltag {
JSVAL_OBJECT = 0x0, /* untagged reference to object */
JSVAL_INT = 0x1, /* tagged 31-bit integer value */
JSVAL_DOUBLE = 0x2, /* tagged reference to double */
JSVAL_STRING = 0x4, /* tagged reference to string */
JSVAL_SPECIAL = 0x6 /* tagged boolean or private value */
} jsvaltag;
1998-03-28 02:44:41 +00:00
/* Type tag bitfield length and derived macros. */
#define JSVAL_TAGBITS 3
#define JSVAL_TAGMASK ((jsval) JS_BITMASK(JSVAL_TAGBITS))
#define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)
1998-03-28 02:44:41 +00:00
/* Not a function, because we have static asserts that use it */
#define JSVAL_TAG(v) ((jsvaltag)((v) & JSVAL_TAGMASK))
/* Not a function, because we have static asserts that use it */
#define JSVAL_SETTAG(v, t) ((v) | (t))
static JS_ALWAYS_INLINE jsval
JSVAL_CLRTAG(jsval v)
{
return v & ~(jsval)JSVAL_TAGMASK;
}
/*
* Well-known JS values. The extern'd variables are initialized when the
* first JSContext is created by JS_NewContext (see below).
*/
#define JSVAL_NULL ((jsval) 0)
#define JSVAL_ZERO INT_TO_JSVAL(0)
#define JSVAL_ONE INT_TO_JSVAL(1)
#define JSVAL_FALSE SPECIAL_TO_JSVAL(JS_FALSE)
#define JSVAL_TRUE SPECIAL_TO_JSVAL(JS_TRUE)
#define JSVAL_VOID SPECIAL_TO_JSVAL(2)
/*
* A "special" value is a 29-bit (for 32-bit jsval) or 61-bit (for 64-bit jsval)
* value whose tag is JSVAL_SPECIAL. These values include the booleans 0 and 1.
*
* JSVAL_VOID is a non-boolean special value, but embedders MUST NOT rely on
* this. All other possible special values are implementation-reserved
* and MUST NOT be constructed by any embedding of SpiderMonkey.
*/
#define JSVAL_TO_SPECIAL(v) ((JSBool) ((v) >> JSVAL_TAGBITS))
#define SPECIAL_TO_JSVAL(b) \
JSVAL_SETTAG((jsval) (b) << JSVAL_TAGBITS, JSVAL_SPECIAL)
1998-03-28 02:44:41 +00:00
/* Predicates for type testing. */
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_OBJECT(jsval v)
{
return JSVAL_TAG(v) == JSVAL_OBJECT;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_INT(jsval v)
{
return (JSBool)(v & JSVAL_INT);
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_DOUBLE(jsval v)
{
return JSVAL_TAG(v) == JSVAL_DOUBLE;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_NUMBER(jsval v)
{
return JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v);
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_STRING(jsval v)
{
return JSVAL_TAG(v) == JSVAL_STRING;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_SPECIAL(jsval v)
{
return JSVAL_TAG(v) == JSVAL_SPECIAL;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_BOOLEAN(jsval v)
{
return (v & ~((jsval)1 << JSVAL_TAGBITS)) == JSVAL_SPECIAL;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_NULL(jsval v)
{
return v == JSVAL_NULL;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_VOID(jsval v)
{
return v == JSVAL_VOID;
}
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_PRIMITIVE(jsval v)
{
return !JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v);
}
1998-03-28 02:44:41 +00:00
/* Objects, strings, and doubles are GC'ed. */
static JS_ALWAYS_INLINE JSBool
JSVAL_IS_GCTHING(jsval v)
{
return !(v & JSVAL_INT) && JSVAL_TAG(v) != JSVAL_SPECIAL;
}
static JS_ALWAYS_INLINE void *
JSVAL_TO_GCTHING(jsval v)
{
JS_ASSERT(JSVAL_IS_GCTHING(v));
return (void *) JSVAL_CLRTAG(v);
}
static JS_ALWAYS_INLINE JSObject *
JSVAL_TO_OBJECT(jsval v)
{
JS_ASSERT(JSVAL_IS_OBJECT(v));
return (JSObject *) JSVAL_TO_GCTHING(v);
}
static JS_ALWAYS_INLINE jsdouble *
JSVAL_TO_DOUBLE(jsval v)
{
JS_ASSERT(JSVAL_IS_DOUBLE(v));
return (jsdouble *) JSVAL_TO_GCTHING(v);
}
static JS_ALWAYS_INLINE JSString *
JSVAL_TO_STRING(jsval v)
{
JS_ASSERT(JSVAL_IS_STRING(v));
return (JSString *) JSVAL_TO_GCTHING(v);
}
static JS_ALWAYS_INLINE jsval
OBJECT_TO_JSVAL(JSObject *obj)
{
JS_ASSERT(((jsval) obj & JSVAL_TAGMASK) == JSVAL_OBJECT);
return (jsval) obj;
}
static JS_ALWAYS_INLINE jsval
DOUBLE_TO_JSVAL(jsdouble *dp)
{
JS_ASSERT(((jsword) dp & JSVAL_TAGMASK) == 0);
return JSVAL_SETTAG((jsval) dp, JSVAL_DOUBLE);
}
static JS_ALWAYS_INLINE jsval
STRING_TO_JSVAL(JSString *str)
{
return JSVAL_SETTAG((jsval) str, JSVAL_STRING);
}
1998-03-28 02:44:41 +00:00
/* Lock and unlock the GC thing held by a jsval. */
#define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \
2000-09-08 21:24:14 +00:00
? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \
: JS_TRUE)
1998-03-28 02:44:41 +00:00
#define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \
2000-09-08 21:24:14 +00:00
? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \
: JS_TRUE)
1998-03-28 02:44:41 +00:00
/* Domain limits for the jsval int type. */
#define JSVAL_INT_BITS 31
1998-03-28 02:44:41 +00:00
#define JSVAL_INT_POW2(n) ((jsval)1 << (n))
2008-05-24 02:14:05 +00:00
#define JSVAL_INT_MIN (-JSVAL_INT_POW2(30))
1998-03-28 02:44:41 +00:00
#define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)
/* Not a function, because we have static asserts that use it */
#define INT_FITS_IN_JSVAL(i) ((jsuint)(i) - (jsuint)JSVAL_INT_MIN <= \
(jsuint)(JSVAL_INT_MAX - JSVAL_INT_MIN))
static JS_ALWAYS_INLINE jsint
JSVAL_TO_INT(jsval v)
{
JS_ASSERT(JSVAL_IS_INT(v));
return (jsint) v >> 1;
}
/* Not a function, because we have static asserts that use it */
#define INT_TO_JSVAL_CONSTEXPR(i) (((jsval)(i) << 1) | JSVAL_INT)
static JS_ALWAYS_INLINE jsval
INT_TO_JSVAL(jsint i)
{
JS_ASSERT(INT_FITS_IN_JSVAL(i));
return INT_TO_JSVAL_CONSTEXPR(i);
}
/* Convert between boolean and jsval, asserting that inputs are valid. */
static JS_ALWAYS_INLINE JSBool
JSVAL_TO_BOOLEAN(jsval v)
{
JS_ASSERT(v == JSVAL_TRUE || v == JSVAL_FALSE);
return JSVAL_TO_SPECIAL(v);
}
static JS_ALWAYS_INLINE jsval
BOOLEAN_TO_JSVAL(JSBool b)
{
JS_ASSERT(b == JS_TRUE || b == JS_FALSE);
return SPECIAL_TO_JSVAL(b);
}
1998-03-28 02:44:41 +00:00
/* A private data pointer (2-byte-aligned) can be stored as an int jsval. */
#define JSVAL_TO_PRIVATE(v) ((void *)((v) & ~JSVAL_INT))
#define PRIVATE_TO_JSVAL(p) ((jsval)(ptrdiff_t)(p) | JSVAL_INT)
1998-03-28 02:44:41 +00:00
/* Property attributes, set in JSPropertySpec and passed to API functions. */
1998-03-28 02:44:41 +00:00
#define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */
#define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */
1998-03-28 02:44:41 +00:00
#define JSPROP_PERMANENT 0x04 /* property cannot be deleted */
- Bumped default compile-time JS version from 1.4 to 1.5. - Add JS1.5 getter/setter support in all its glory: * getter function SN() {return ++x} at top-level or as a closure binds an SN property getter than returns the incremented value of x. Likewise for setter function SN(y) {return y = x}. * getters and setters may be defined in an object literal: o = {p getter:function() {return ++this.x}, p setter:function(y){return this.x = y}, x:42}; * getter= and setter= operators (compound tokens) may be used to bind getter and setter properties dynamically: o = new Object; o.p getter= function() {return ++this.x}; o.p setter= function(y){return this.x = y}; o.x = 42; Waldemar is concerned that this form will collide semantically with JS2, so I am not committing to keeping it in JS1.5. I'd like to check my code in ASAP so shaver can use it, and I'd also like to see this form get used (or not) during Mozilla betas. Caveat emptor, and if you find this "dynamic" or "imperative" form necessary and hard to substitute, please let me know. If this proves important to users, then I think JS1.5 should keep it. - Cleaned up property flags (in a binary-incompatible fashion -- who cares?) by eliminating JSPROP_ASSIGNHACK and JSPROP_TINYIDHACK. - Added JS_DONT_PRETTY_PRINT flag to be ORed with the indent argument to the several JS_Decompile*() API calls. This avoids any newlines or identation in the decompiled string. - Improved and extended (for getter/setter non-reservation) scanner lookahead by using a circular (power-of-2 sized) token buffer. - Fix ECMA Edition 3 deviation where function f(){function g(){}} bound f.g by mistake (it should arrange to make a closure named g in activations of f, but it should not bind a property of function f).
1999-09-21 00:13:48 +00:00
#define JSPROP_GETTER 0x10 /* property holds getter function */
#define JSPROP_SETTER 0x20 /* property holds setter function */
#define JSPROP_SHARED 0x40 /* don't allocate a value slot for this
property; don't copy the property on
set of the same-named property in an
object that delegates to a prototype
containing this property */
- Bumped default compile-time JS version from 1.4 to 1.5. - Add JS1.5 getter/setter support in all its glory: * getter function SN() {return ++x} at top-level or as a closure binds an SN property getter than returns the incremented value of x. Likewise for setter function SN(y) {return y = x}. * getters and setters may be defined in an object literal: o = {p getter:function() {return ++this.x}, p setter:function(y){return this.x = y}, x:42}; * getter= and setter= operators (compound tokens) may be used to bind getter and setter properties dynamically: o = new Object; o.p getter= function() {return ++this.x}; o.p setter= function(y){return this.x = y}; o.x = 42; Waldemar is concerned that this form will collide semantically with JS2, so I am not committing to keeping it in JS1.5. I'd like to check my code in ASAP so shaver can use it, and I'd also like to see this form get used (or not) during Mozilla betas. Caveat emptor, and if you find this "dynamic" or "imperative" form necessary and hard to substitute, please let me know. If this proves important to users, then I think JS1.5 should keep it. - Cleaned up property flags (in a binary-incompatible fashion -- who cares?) by eliminating JSPROP_ASSIGNHACK and JSPROP_TINYIDHACK. - Added JS_DONT_PRETTY_PRINT flag to be ORed with the indent argument to the several JS_Decompile*() API calls. This avoids any newlines or identation in the decompiled string. - Improved and extended (for getter/setter non-reservation) scanner lookahead by using a circular (power-of-2 sized) token buffer. - Fix ECMA Edition 3 deviation where function f(){function g(){}} bound f.g by mistake (it should arrange to make a closure named g in activations of f, but it should not bind a property of function f).
1999-09-21 00:13:48 +00:00
#define JSPROP_INDEX 0x80 /* name is actually (jsint) index */
#define JSPROP_SHORTID 0x100 /* set in JSPropertyDescriptor.attrs
if getters/setters use a shortid */
1998-03-28 02:44:41 +00:00
/* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */
#define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */
- Bumped default compile-time JS version from 1.4 to 1.5. - Add JS1.5 getter/setter support in all its glory: * getter function SN() {return ++x} at top-level or as a closure binds an SN property getter than returns the incremented value of x. Likewise for setter function SN(y) {return y = x}. * getters and setters may be defined in an object literal: o = {p getter:function() {return ++this.x}, p setter:function(y){return this.x = y}, x:42}; * getter= and setter= operators (compound tokens) may be used to bind getter and setter properties dynamically: o = new Object; o.p getter= function() {return ++this.x}; o.p setter= function(y){return this.x = y}; o.x = 42; Waldemar is concerned that this form will collide semantically with JS2, so I am not committing to keeping it in JS1.5. I'd like to check my code in ASAP so shaver can use it, and I'd also like to see this form get used (or not) during Mozilla betas. Caveat emptor, and if you find this "dynamic" or "imperative" form necessary and hard to substitute, please let me know. If this proves important to users, then I think JS1.5 should keep it. - Cleaned up property flags (in a binary-incompatible fashion -- who cares?) by eliminating JSPROP_ASSIGNHACK and JSPROP_TINYIDHACK. - Added JS_DONT_PRETTY_PRINT flag to be ORed with the indent argument to the several JS_Decompile*() API calls. This avoids any newlines or identation in the decompiled string. - Improved and extended (for getter/setter non-reservation) scanner lookahead by using a circular (power-of-2 sized) token buffer. - Fix ECMA Edition 3 deviation where function f(){function g(){}} bound f.g by mistake (it should arrange to make a closure named g in activations of f, but it should not bind a property of function f).
1999-09-21 00:13:48 +00:00
#define JSFUN_GETTER JSPROP_GETTER
#define JSFUN_SETTER JSPROP_SETTER
1998-03-28 02:44:41 +00:00
#define JSFUN_BOUND_METHOD 0x40 /* bind this to fun->object's parent */
#define JSFUN_HEAVYWEIGHT 0x80 /* activation requires a Call object */
#define JSFUN_DISJOINT_FLAGS(f) ((f) & 0x0f)
#define JSFUN_GSFLAGS(f) ((f) & (JSFUN_GETTER | JSFUN_SETTER))
#define JSFUN_GETTER_TEST(f) ((f) & JSFUN_GETTER)
#define JSFUN_SETTER_TEST(f) ((f) & JSFUN_SETTER)
#define JSFUN_BOUND_METHOD_TEST(f) ((f) & JSFUN_BOUND_METHOD)
#define JSFUN_HEAVYWEIGHT_TEST(f) ((f) & JSFUN_HEAVYWEIGHT)
#define JSFUN_GSFLAG2ATTR(f) JSFUN_GSFLAGS(f)
#define JSFUN_THISP_FLAGS(f) (f)
#define JSFUN_THISP_TEST(f,t) ((f) & t)
#define JSFUN_THISP_STRING 0x0100 /* |this| may be a primitive string */
#define JSFUN_THISP_NUMBER 0x0200 /* |this| may be a primitive number */
#define JSFUN_THISP_BOOLEAN 0x0400 /* |this| may be a primitive boolean */
#define JSFUN_THISP_PRIMITIVE 0x0700 /* |this| may be any primitive value */
#define JSFUN_FAST_NATIVE 0x0800 /* JSFastNative needs no JSStackFrame */
#define JSFUN_FLAGS_MASK 0x0ff8 /* overlay JSFUN_* attributes --
bits 12-15 are used internally to
flag interpreted functions */
1998-03-28 02:44:41 +00:00
#define JSFUN_STUB_GSOPS 0x1000 /* use JS_PropertyStub getter/setter
instead of defaulting to class gsops
for property holding function */
/*
* Re-use JSFUN_LAMBDA, which applies only to scripted functions, for use in
* JSFunctionSpec arrays that specify generic native prototype methods, i.e.,
* methods of a class prototype that are exposed as static methods taking an
* extra leading argument: the generic |this| parameter.
*
* If you set this flag in a JSFunctionSpec struct's flags initializer, then
* that struct must live at least as long as the native static method object
* created due to this flag by JS_DefineFunctions or JS_InitClass. Typically
* JSFunctionSpec structs are allocated in static arrays.
*/
#define JSFUN_GENERIC_NATIVE JSFUN_LAMBDA
/*
* Microseconds since the epoch, midnight, January 1, 1970 UTC. See the
* comment in jstypes.h regarding safe int64 usage.
*/
extern JS_PUBLIC_API(int64)
JS_Now(void);
/* Don't want to export data, so provide accessors for non-inline jsvals. */
extern JS_PUBLIC_API(jsval)
1998-03-28 02:44:41 +00:00
JS_GetNaNValue(JSContext *cx);
extern JS_PUBLIC_API(jsval)
1998-03-28 02:44:41 +00:00
JS_GetNegativeInfinityValue(JSContext *cx);
extern JS_PUBLIC_API(jsval)
1998-03-28 02:44:41 +00:00
JS_GetPositiveInfinityValue(JSContext *cx);
extern JS_PUBLIC_API(jsval)
1998-03-28 02:44:41 +00:00
JS_GetEmptyStringValue(JSContext *cx);
/*
* Format is a string of the following characters (spaces are insignificant),
* specifying the tabulated type conversions:
*
* b JSBool Boolean
* c uint16/jschar ECMA uint16, Unicode char
* i int32 ECMA int32
* u uint32 ECMA uint32
* j int32 Rounded int32 (coordinate)
* d jsdouble IEEE double
* I jsdouble Integral IEEE double
* s char * C string
* S JSString * Unicode string, accessed by a JSString pointer
* W jschar * Unicode character vector, 0-terminated (W for wide)
* o JSObject * Object reference
* f JSFunction * Function private
* v jsval Argument value (no conversion)
* * N/A Skip this argument (no vararg)
* / N/A End of required arguments
*
* The variable argument list after format must consist of &b, &c, &s, e.g.,
* where those variables have the types given above. For the pointer types
* char *, JSString *, and JSObject *, the pointed-at memory returned belongs
* to the JS runtime, not to the calling native code. The runtime promises
* to keep this memory valid so long as argv refers to allocated stack space
* (so long as the native function is active).
*
* Fewer arguments than format specifies may be passed only if there is a /
* in format after the last required argument specifier and argc is at least
* the number of required arguments. More arguments than format specifies
* may be passed without error; it is up to the caller to deal with trailing
* unconverted arguments.
*/
extern JS_PUBLIC_API(JSBool)
JS_ConvertArguments(JSContext *cx, uintN argc, jsval *argv, const char *format,
2000-09-08 21:24:14 +00:00
...);
#ifdef va_start
extern JS_PUBLIC_API(JSBool)
JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv,
2000-09-08 21:24:14 +00:00
const char *format, va_list ap);
#endif
#ifdef JS_ARGUMENT_FORMATTER_DEFINED
/*
* Add and remove a format string handler for JS_{Convert,Push}Arguments{,VA}.
* The handler function has this signature (see jspubtd.h):
*
* JSBool MyArgumentFormatter(JSContext *cx, const char *format,
* JSBool fromJS, jsval **vpp, va_list *app);
*
* It should return true on success, and return false after reporting an error
* or detecting an already-reported error.
*
* For a given format string, for example "AA", the formatter is called from
* JS_ConvertArgumentsVA like so:
*
* formatter(cx, "AA...", JS_TRUE, &sp, &ap);
*
* sp points into the arguments array on the JS stack, while ap points into
* the stdarg.h va_list on the C stack. The JS_TRUE passed for fromJS tells
* the formatter to convert zero or more jsvals at sp to zero or more C values
* accessed via pointers-to-values at ap, updating both sp (via *vpp) and ap
* (via *app) to point past the converted arguments and their result pointers
* on the C stack.
*
* When called from JS_PushArgumentsVA, the formatter is invoked thus:
*
* formatter(cx, "AA...", JS_FALSE, &sp, &ap);
*
* where JS_FALSE for fromJS means to wrap the C values at ap according to the
* format specifier and store them at sp, updating ap and sp appropriately.
*
* The "..." after "AA" is the rest of the format string that was passed into
* JS_{Convert,Push}Arguments{,VA}. The actual format trailing substring used
* in each Convert or PushArguments call is passed to the formatter, so that
* one such function may implement several formats, in order to share code.
*
* Remove just forgets about any handler associated with format. Add does not
* copy format, it points at the string storage allocated by the caller, which
* is typically a string constant. If format is in dynamic storage, it is up
* to the caller to keep the string alive until Remove is called.
*/
extern JS_PUBLIC_API(JSBool)
JS_AddArgumentFormatter(JSContext *cx, const char *format,
2000-09-08 21:24:14 +00:00
JSArgumentFormatter formatter);
extern JS_PUBLIC_API(void)
JS_RemoveArgumentFormatter(JSContext *cx, const char *format);
#endif /* JS_ARGUMENT_FORMATTER_DEFINED */
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ConvertValue(JSContext *cx, jsval v, JSType type, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_ValueToFunction(JSContext *cx, jsval v);
extern JS_PUBLIC_API(JSFunction *)
JS_ValueToConstructor(JSContext *cx, jsval v);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_ValueToString(JSContext *cx, jsval v);
extern JS_PUBLIC_API(JSString *)
JS_ValueToSource(JSContext *cx, jsval v);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
extern JS_PUBLIC_API(JSBool)
JS_DoubleIsInt32(jsdouble d, jsint *ip);
1998-03-28 02:44:41 +00:00
/*
* Convert a value to a number, then to an int32, according to the ECMA rules
* for ToInt32.
*/
extern JS_PUBLIC_API(JSBool)
JS_ValueToECMAInt32(JSContext *cx, jsval v, int32 *ip);
/*
* Convert a value to a number, then to a uint32, according to the ECMA rules
* for ToUint32.
1998-03-28 02:44:41 +00:00
*/
extern JS_PUBLIC_API(JSBool)
JS_ValueToECMAUint32(JSContext *cx, jsval v, uint32 *ip);
/*
* Convert a value to a number, then to an int32 if it fits by rounding to
* nearest; but failing with an error report if the double is out of range
* or unordered.
*/
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ValueToInt32(JSContext *cx, jsval v, int32 *ip);
/*
* ECMA ToUint16, for mapping a jsval to a Unicode point.
*/
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ValueToUint16(JSContext *cx, jsval v, uint16 *ip);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
extern JS_PUBLIC_API(JSType)
1998-03-28 02:44:41 +00:00
JS_TypeOfValue(JSContext *cx, jsval v);
extern JS_PUBLIC_API(const char *)
1998-03-28 02:44:41 +00:00
JS_GetTypeName(JSContext *cx, JSType type);
extern JS_PUBLIC_API(JSBool)
JS_StrictlyEqual(JSContext *cx, jsval v1, jsval v2);
extern JS_PUBLIC_API(JSBool)
JS_SameValue(JSContext *cx, jsval v1, jsval v2);
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Initialization, locking, contexts, and memory allocation.
*
* It is important that the first runtime and first context be created in a
* single-threaded fashion, otherwise the behavior of the library is undefined.
* See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
1998-03-28 02:44:41 +00:00
*/
#define JS_NewRuntime JS_Init
#define JS_DestroyRuntime JS_Finish
#define JS_LockRuntime JS_Lock
#define JS_UnlockRuntime JS_Unlock
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSRuntime *)
JS_NewRuntime(uint32 maxbytes);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(void)
JS_CommenceRuntimeShutDown(JSRuntime *rt);
extern JS_PUBLIC_API(void)
JS_DestroyRuntime(JSRuntime *rt);
extern JS_PUBLIC_API(void)
JS_ShutDown(void);
JS_PUBLIC_API(void *)
JS_GetRuntimePrivate(JSRuntime *rt);
JS_PUBLIC_API(void)
JS_SetRuntimePrivate(JSRuntime *rt, void *data);
extern JS_PUBLIC_API(void)
JS_BeginRequest(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_EndRequest(JSContext *cx);
/* Yield to pending GC operations, regardless of request depth */
extern JS_PUBLIC_API(void)
JS_YieldRequest(JSContext *cx);
All this r=mccabe, r=beard, and sr=jband -- many thanks to all who helped, especially to jband for his great stress-test setup and particularly helpful (in terms of reproducing bugs in draft patches) MP and laptop machines. - Radical(*) object (scope) locking optimization: don't lock if a scope is accessed on the context that exclusively owns it (initially, the context on which the scope was created). Once a scope becomes shared among more than one owner-context, give it the usual thin or fat lock, per existing jslock.c code. I did this at the memory cost of another word per JSScope, ownercx, which raised scope size from 12 to 13 words if !DEBUG. I also added a linked list head pointer, rt->scopeSharingTodo, and a scopeSharingDone condition variable to JSRuntime, and a scopeToShare pointer to JSContext that's necessary for deadlock avoidance. The rt->scopeSharingTodo list links JSScopes through the scope->u.link union arm, which overlays the pre-existing scope->count (now u.count) member. This list holds scopes still exclusively owned by a context, but wanted by js_LockScope calls active on other threads. Those calls wait on the rt->scopeSharingDone condition, which is notified every time an owner-context ends the request running on it, in which code active on that context may be using scope freely until end of request. The code that waits on rt->scopeSharingDone must first suspend any and all requests active on the calling context, and resume those contexts after the wait is notified. This means a GC could happen while the thread locking a scope owned by another thread's context blocks; all calls to JS_LOCK_OBJ must therefore first home fp->sp above any live operands, e.g. The interpreter takes care to do that already. To avoid AB-BA deadlocks, if a js_LockScope attempt on one context finds that the owner-context of the scope is already waiting on a scope owned by the current context (or indirectly depending on such a scope lock), the attempt converts the scope from lock-free exclusive ownership to shared ownership (thin or fat lock). - Fix js_SetupLocks and the js_LockGlobal/js_UnlockGlobal code to avoid divmod instruction costs, strength-reducing to bit-mask instructions. - The radical lock-free scope change required care in handling the 0=>1 and 1=>0 transitions of cx->requestDepth, which was till now thread-local because part of the JSContext not manipulated by other threads. It's still updated only by cx's thread, but it is read by other threads in the course of attempting to claim exclusive ownership of a scope for more lock-free JS object operations. - The JS_SuspendRequest and JS_ResumeRequest APIs have changed incompatibly to require their caller to save and restore the requestCount found when JS_SuspendRequest is called. This is necessary to avoid deadlock; sorry for the incompatible change. - Fixed various nits in jslock.[ch], including using Init/Finish rather than New/Destroy for the methods that take a JSThinLock and initialize and finish/free its members. Another example: JS_ATOMIC_ADDREF is now JS_ATOMIC_INCREMENT and JS_ATOMIC_DECREMENT, so the two cases can be mapped to PR_AtomicIncrement and PR_AtomicDecrement. This entailed changing jsrefcount from jsword to int32 (PRInt32). - No need to use JS_ATOMIC_INCREMENT on JSScopeProperty.nrefs, as it is always and everywhere protected by the property's JSScope.lock. - Cleaned up gratuitous casts in jscntxt.c by using &cx->links, etc. - The lock used for mutual exclusion around both request begin and end vs. GC synchronization is rt->gcLock, and this lock now also protects all scope->ownercx pointer changes from non-null (exclusive) to null (shared), the rt->scopeSharingTodo/scope->u.link list operations, and of course the rt->scopeSharingDone condition. But this means that js_GC cannot hold rt->gcLock across the bulk of its body, in particular the mark phase, during which JS_GetPrivate calls, e.g., may need to "promote" scope locks from lock-free to thin or fat, because doing so would double-trip. There never was any good reason to hold rt->gcLock so long, of course -- locks are for mutual exclusion, not for waiting or notifying a thread -- those operations require a condition, rt->gcDone, which we already use along with rt->gcLevel to keep racing GC attempts at bay. So now that rt->gcLock does not protect the mark phase, the enumeration of rt->gcRootsHash can race badly with JS_RemoveRootRT, an API that may legitimately be called outside of a request, without even a context. It turns out that people may be cheating on the request model even with JS_AddRoot, JS_AddNamedRoot, and JS_RemoveRoot calls, so we must make all of those interlock with the GC using gcLevel and gcDone, unless they are called on the gcThread. Also, since bug 49816 was fixed, there has been no need for a separate finalize phase, or for rt->gcFinalVec. Finalizers can no longer allocate newborn GC-things that might be swept (because unmarked), or double-trip on rt->gcLock (which is no longer held). So js_GC finalizes as it sweeps, just as it did in days of old. - I added comments to jslock.h making it plain that callers of JS_LOCK_OBJ and JS_UNLOCK_OBJ must either be implementations of js_ObjectOps hooks, or code reachable only from those hooks; or else must be predicated on OBJ_IS_NATIVE tests. It turns out jsinterp.c's CACHED_GET and CACHED_SET macros neglected to do such tests, limiting the ability of JS embeddings to implement JSObjectOps with their own non-JSScope JSObjectMap subclass. Fixed, small performance hit that the lock-free optimization should more than make up for. - jslock.c now gives a #error if you try to compile it on a platform that lacks a compare-and-swap instruction. The #error says to use NSPR locks. Before this change, some platforms would emulate compare-and-swap using a global PRLock, which is always worse in runtime than using per-scope PRLocks.
2000-12-04 02:43:31 +00:00
extern JS_PUBLIC_API(jsrefcount)
JS_SuspendRequest(JSContext *cx);
extern JS_PUBLIC_API(void)
All this r=mccabe, r=beard, and sr=jband -- many thanks to all who helped, especially to jband for his great stress-test setup and particularly helpful (in terms of reproducing bugs in draft patches) MP and laptop machines. - Radical(*) object (scope) locking optimization: don't lock if a scope is accessed on the context that exclusively owns it (initially, the context on which the scope was created). Once a scope becomes shared among more than one owner-context, give it the usual thin or fat lock, per existing jslock.c code. I did this at the memory cost of another word per JSScope, ownercx, which raised scope size from 12 to 13 words if !DEBUG. I also added a linked list head pointer, rt->scopeSharingTodo, and a scopeSharingDone condition variable to JSRuntime, and a scopeToShare pointer to JSContext that's necessary for deadlock avoidance. The rt->scopeSharingTodo list links JSScopes through the scope->u.link union arm, which overlays the pre-existing scope->count (now u.count) member. This list holds scopes still exclusively owned by a context, but wanted by js_LockScope calls active on other threads. Those calls wait on the rt->scopeSharingDone condition, which is notified every time an owner-context ends the request running on it, in which code active on that context may be using scope freely until end of request. The code that waits on rt->scopeSharingDone must first suspend any and all requests active on the calling context, and resume those contexts after the wait is notified. This means a GC could happen while the thread locking a scope owned by another thread's context blocks; all calls to JS_LOCK_OBJ must therefore first home fp->sp above any live operands, e.g. The interpreter takes care to do that already. To avoid AB-BA deadlocks, if a js_LockScope attempt on one context finds that the owner-context of the scope is already waiting on a scope owned by the current context (or indirectly depending on such a scope lock), the attempt converts the scope from lock-free exclusive ownership to shared ownership (thin or fat lock). - Fix js_SetupLocks and the js_LockGlobal/js_UnlockGlobal code to avoid divmod instruction costs, strength-reducing to bit-mask instructions. - The radical lock-free scope change required care in handling the 0=>1 and 1=>0 transitions of cx->requestDepth, which was till now thread-local because part of the JSContext not manipulated by other threads. It's still updated only by cx's thread, but it is read by other threads in the course of attempting to claim exclusive ownership of a scope for more lock-free JS object operations. - The JS_SuspendRequest and JS_ResumeRequest APIs have changed incompatibly to require their caller to save and restore the requestCount found when JS_SuspendRequest is called. This is necessary to avoid deadlock; sorry for the incompatible change. - Fixed various nits in jslock.[ch], including using Init/Finish rather than New/Destroy for the methods that take a JSThinLock and initialize and finish/free its members. Another example: JS_ATOMIC_ADDREF is now JS_ATOMIC_INCREMENT and JS_ATOMIC_DECREMENT, so the two cases can be mapped to PR_AtomicIncrement and PR_AtomicDecrement. This entailed changing jsrefcount from jsword to int32 (PRInt32). - No need to use JS_ATOMIC_INCREMENT on JSScopeProperty.nrefs, as it is always and everywhere protected by the property's JSScope.lock. - Cleaned up gratuitous casts in jscntxt.c by using &cx->links, etc. - The lock used for mutual exclusion around both request begin and end vs. GC synchronization is rt->gcLock, and this lock now also protects all scope->ownercx pointer changes from non-null (exclusive) to null (shared), the rt->scopeSharingTodo/scope->u.link list operations, and of course the rt->scopeSharingDone condition. But this means that js_GC cannot hold rt->gcLock across the bulk of its body, in particular the mark phase, during which JS_GetPrivate calls, e.g., may need to "promote" scope locks from lock-free to thin or fat, because doing so would double-trip. There never was any good reason to hold rt->gcLock so long, of course -- locks are for mutual exclusion, not for waiting or notifying a thread -- those operations require a condition, rt->gcDone, which we already use along with rt->gcLevel to keep racing GC attempts at bay. So now that rt->gcLock does not protect the mark phase, the enumeration of rt->gcRootsHash can race badly with JS_RemoveRootRT, an API that may legitimately be called outside of a request, without even a context. It turns out that people may be cheating on the request model even with JS_AddRoot, JS_AddNamedRoot, and JS_RemoveRoot calls, so we must make all of those interlock with the GC using gcLevel and gcDone, unless they are called on the gcThread. Also, since bug 49816 was fixed, there has been no need for a separate finalize phase, or for rt->gcFinalVec. Finalizers can no longer allocate newborn GC-things that might be swept (because unmarked), or double-trip on rt->gcLock (which is no longer held). So js_GC finalizes as it sweeps, just as it did in days of old. - I added comments to jslock.h making it plain that callers of JS_LOCK_OBJ and JS_UNLOCK_OBJ must either be implementations of js_ObjectOps hooks, or code reachable only from those hooks; or else must be predicated on OBJ_IS_NATIVE tests. It turns out jsinterp.c's CACHED_GET and CACHED_SET macros neglected to do such tests, limiting the ability of JS embeddings to implement JSObjectOps with their own non-JSScope JSObjectMap subclass. Fixed, small performance hit that the lock-free optimization should more than make up for. - jslock.c now gives a #error if you try to compile it on a platform that lacks a compare-and-swap instruction. The #error says to use NSPR locks. Before this change, some platforms would emulate compare-and-swap using a global PRLock, which is always worse in runtime than using per-scope PRLocks.
2000-12-04 02:43:31 +00:00
JS_ResumeRequest(JSContext *cx, jsrefcount saveDepth);
extern JS_PUBLIC_API(void)
JS_TransferRequest(JSContext *cx, JSContext *another);
#ifdef __cplusplus
JS_END_EXTERN_C
class JSAutoRequest {
public:
JSAutoRequest(JSContext *cx JS_GUARD_OBJECT_NOTIFIER_PARAM)
: mContext(cx), mSaveDepth(0) {
JS_GUARD_OBJECT_NOTIFIER_INIT;
JS_BeginRequest(mContext);
}
~JSAutoRequest() {
JS_EndRequest(mContext);
}
void suspend() {
mSaveDepth = JS_SuspendRequest(mContext);
}
void resume() {
JS_ResumeRequest(mContext, mSaveDepth);
}
protected:
JSContext *mContext;
jsrefcount mSaveDepth;
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
#if 0
private:
static void *operator new(size_t) CPP_THROW_NEW { return 0; };
static void operator delete(void *, size_t) { };
#endif
};
class JSAutoSuspendRequest {
public:
JSAutoSuspendRequest(JSContext *cx JS_GUARD_OBJECT_NOTIFIER_PARAM)
: mContext(cx), mSaveDepth(0) {
JS_GUARD_OBJECT_NOTIFIER_INIT;
if (mContext) {
mSaveDepth = JS_SuspendRequest(mContext);
}
}
~JSAutoSuspendRequest() {
resume();
}
void resume() {
if (mContext) {
JS_ResumeRequest(mContext, mSaveDepth);
mContext = 0;
}
}
protected:
JSContext *mContext;
jsrefcount mSaveDepth;
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
#if 0
private:
static void *operator new(size_t) CPP_THROW_NEW { return 0; };
static void operator delete(void *, size_t) { };
#endif
};
class JSAutoTransferRequest
{
public:
JSAutoTransferRequest(JSContext* cx1, JSContext* cx2)
: cx1(cx1), cx2(cx2) {
if(cx1 != cx2)
JS_TransferRequest(cx1, cx2);
}
~JSAutoTransferRequest() {
if(cx1 != cx2)
JS_TransferRequest(cx2, cx1);
}
private:
JSContext* const cx1;
JSContext* const cx2;
/* Not copyable. */
JSAutoTransferRequest(JSAutoTransferRequest &);
void operator =(JSAutoTransferRequest&);
};
JS_BEGIN_EXTERN_C
#endif
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_Lock(JSRuntime *rt);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_Unlock(JSRuntime *rt);
extern JS_PUBLIC_API(JSContextCallback)
JS_SetContextCallback(JSRuntime *rt, JSContextCallback cxCallback);
extern JS_PUBLIC_API(JSContext *)
JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_DestroyContext(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_DestroyContextNoGC(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_DestroyContextMaybeGC(JSContext *cx);
extern JS_PUBLIC_API(void *)
JS_GetContextPrivate(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_SetContextPrivate(JSContext *cx, void *data);
extern JS_PUBLIC_API(JSRuntime *)
1998-03-28 02:44:41 +00:00
JS_GetRuntime(JSContext *cx);
extern JS_PUBLIC_API(JSContext *)
1998-03-28 02:44:41 +00:00
JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
extern JS_PUBLIC_API(JSVersion)
1998-03-28 02:44:41 +00:00
JS_GetVersion(JSContext *cx);
extern JS_PUBLIC_API(JSVersion)
1998-03-28 02:44:41 +00:00
JS_SetVersion(JSContext *cx, JSVersion version);
extern JS_PUBLIC_API(const char *)
JS_VersionToString(JSVersion version);
extern JS_PUBLIC_API(JSVersion)
JS_StringToVersion(const char *string);
/*
* JS options are orthogonal to version, and may be freely composed with one
* another as well as with version.
*
* JSOPTION_VAROBJFIX is recommended -- see the comments associated with the
* prototypes for JS_ExecuteScript, JS_EvaluateScript, etc.
*/
#define JSOPTION_STRICT JS_BIT(0) /* warn on dubious practice */
#define JSOPTION_WERROR JS_BIT(1) /* convert warning to error */
#define JSOPTION_VAROBJFIX JS_BIT(2) /* make JS_EvaluateScript use
the last object on its 'obj'
param's scope chain as the
ECMA 'variables object' */
#define JSOPTION_PRIVATE_IS_NSISUPPORTS \
JS_BIT(3) /* context private data points
to an nsISupports subclass */
#define JSOPTION_COMPILE_N_GO JS_BIT(4) /* caller of JS_Compile*Script
promises to execute compiled
script once only; enables
compile-time scope chain
resolution of consts. */
#define JSOPTION_ATLINE JS_BIT(5) /* //@line number ["filename"]
option supported for the
XUL preprocessor and kindred
beasts. */
#define JSOPTION_XML JS_BIT(6) /* EMCAScript for XML support:
parse <!-- --> as a token,
not backward compatible with
the comment-hiding hack used
in HTML script tags. */
#define JSOPTION_DONT_REPORT_UNCAUGHT \
JS_BIT(8) /* When returning from the
outermost API call, prevent
uncaught exceptions from
being converted to error
reports */
#define JSOPTION_RELIMIT JS_BIT(9) /* Throw exception on any
regular expression which
backtracks more than n^3
times, where n is length
of the input string */
#define JSOPTION_ANONFUNFIX JS_BIT(10) /* Disallow function () {} in
statement context per
ECMA-262 Edition 3. */
#define JSOPTION_JIT JS_BIT(11) /* Enable JIT compilation. */
#define JSOPTION_NO_SCRIPT_RVAL JS_BIT(12) /* A promise to the compiler
that a null rval out-param
will be passed to each call
to JS_ExecuteScript. */
#define JSOPTION_UNROOTED_GLOBAL JS_BIT(13) /* The GC will not root the
contexts' global objects
(see JS_GetGlobalObject),
leaving that up to the
embedding. */
extern JS_PUBLIC_API(uint32)
JS_GetOptions(JSContext *cx);
extern JS_PUBLIC_API(uint32)
JS_SetOptions(JSContext *cx, uint32 options);
extern JS_PUBLIC_API(uint32)
JS_ToggleOptions(JSContext *cx, uint32 options);
extern JS_PUBLIC_API(const char *)
JS_GetImplementationVersion(void);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetGlobalObject(JSContext *cx);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_SetGlobalObject(JSContext *cx, JSObject *obj);
/*
* Initialize standard JS class constructors, prototypes, and any top-level
* functions and constants associated with the standard classes (e.g. isNaN
* for Number).
*
* NB: This sets cx's global object to obj if it was null.
*/
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_InitStandardClasses(JSContext *cx, JSObject *obj);
/*
* Resolve id, which must contain either a string or an int, to a standard
* class name in obj if possible, defining the class's constructor and/or
* prototype and storing true in *resolved. If id does not name a standard
* class or a top-level property induced by initializing a standard class,
* store false in *resolved and just return true. Return false on error,
* as usual for JSBool result-typed API entry points.
*
* This API can be called directly from a global object class's resolve op,
* to define standard classes lazily. The class's enumerate op should call
* JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
* loops any classes not yet resolved lazily.
*/
extern JS_PUBLIC_API(JSBool)
JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
JSBool *resolved);
extern JS_PUBLIC_API(JSBool)
JS_EnumerateStandardClasses(JSContext *cx, JSObject *obj);
/*
* Enumerate any already-resolved standard class ids into ida, or into a new
* JSIdArray if ida is null. Return the augmented array on success, null on
* failure with ida (if it was non-null on entry) destroyed.
*/
extern JS_PUBLIC_API(JSIdArray *)
JS_EnumerateResolvedStandardClasses(JSContext *cx, JSObject *obj,
JSIdArray *ida);
extern JS_PUBLIC_API(JSBool)
JS_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
JSObject **objp);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetScopeChain(JSContext *cx);
extern JS_PUBLIC_API(JSObject *)
JS_GetGlobalForObject(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSObject *)
JS_GetGlobalForScopeChain(JSContext *cx);
#ifdef JS_HAS_CTYPES
/*
* Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
* object will be sealed.
*/
extern JS_PUBLIC_API(JSBool)
JS_InitCTypesClass(JSContext *cx, JSObject *global);
#endif
/*
* Macros to hide interpreter stack layout details from a JSFastNative using
* its jsval *vp parameter. The stack layout underlying invocation can't change
* without breaking source and binary compatibility (argv[-2] is well-known to
* be the callee jsval, and argv[-1] is as well known to be |this|).
*
* Note well: However, argv[-1] may be JSVAL_NULL where with slow natives it
* is the global object, so embeddings implementing fast natives *must* call
* JS_THIS or JS_THIS_OBJECT and test for failure indicated by a null return,
* which should propagate as a false return from native functions and hooks.
*
* To reduce boilerplace checks, JS_InstanceOf and JS_GetInstancePrivate now
* handle a null obj parameter by returning false (throwing a TypeError if
* given non-null argv), so most native functions that type-check their |this|
* parameter need not add null checking.
*
* NB: there is an anti-dependency between JS_CALLEE and JS_SET_RVAL: native
* methods that may inspect their callee must defer setting their return value
* until after any such possible inspection. Otherwise the return value will be
* inspected instead of the callee function object.
*
* WARNING: These are not (yet) mandatory macros, but new code outside of the
* engine should use them. In the Mozilla 2.0 milestone their definitions may
* change incompatibly.
*/
#define JS_CALLEE(cx,vp) ((vp)[0])
#define JS_ARGV_CALLEE(argv) ((argv)[-2])
#define JS_THIS(cx,vp) JS_ComputeThis(cx, vp)
#define JS_THIS_OBJECT(cx,vp) ((JSObject *) JS_THIS(cx,vp))
#define JS_ARGV(cx,vp) ((vp) + 2)
#define JS_RVAL(cx,vp) (*(vp))
#define JS_SET_RVAL(cx,vp,v) (*(vp) = (v))
extern JS_PUBLIC_API(jsval)
JS_ComputeThis(JSContext *cx, jsval *vp);
extern JS_PUBLIC_API(void *)
1998-03-28 02:44:41 +00:00
JS_malloc(JSContext *cx, size_t nbytes);
extern JS_PUBLIC_API(void *)
1998-03-28 02:44:41 +00:00
JS_realloc(JSContext *cx, void *p, size_t nbytes);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_free(JSContext *cx, void *p);
2009-10-29 18:48:18 +00:00
extern JS_PUBLIC_API(void)
JS_updateMallocCounter(JSContext *cx, size_t nbytes);
extern JS_PUBLIC_API(char *)
1998-03-28 02:44:41 +00:00
JS_strdup(JSContext *cx, const char *s);
extern JS_PUBLIC_API(jsdouble *)
1998-03-28 02:44:41 +00:00
JS_NewDouble(JSContext *cx, jsdouble d);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_NewNumberValue(JSContext *cx, jsdouble d, jsval *rval);
/*
2010-06-08 00:05:02 +00:00
* A GC root is a pointer to a jsval, JSObject * or JSString * that itself
* points into the GC heap. JS_AddValueRoot takes a pointer to a jsval and
* JS_AddGCThingRoot takes a pointer to a JSObject * or JString *.
*
2010-06-08 00:05:02 +00:00
* Note that, since JS_Add*Root stores the address of a variable (of type
* jsval, JSString *, or JSObject *), that variable must live until
* JS_Remove*Root is called to remove that variable. For example, after:
*
2010-06-08 00:05:02 +00:00
* void some_function() {
* jsval v;
* JS_AddNamedRootedValue(cx, &v, "name");
*
* the caller must perform
*
* JS_RemoveRootedValue(cx, &v);
*
* before some_function() returns.
*
* Also, use JS_AddNamed*Root(cx, &structPtr->memberObj, "structPtr->memberObj")
* in preference to JS_Add*Root(cx, &structPtr->memberObj), in order to identify
* roots by their source callsites. This way, you can find the callsite while
2010-06-08 00:05:02 +00:00
* debugging if you should fail to do JS_Remove*Root(cx, &structPtr->memberObj)
* before freeing structPtr's memory.
*/
extern JS_PUBLIC_API(JSBool)
2010-06-08 00:05:02 +00:00
JS_AddValueRoot(JSContext *cx, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_AddStringRoot(JSContext *cx, JSString **rp);
extern JS_PUBLIC_API(JSBool)
JS_AddObjectRoot(JSContext *cx, JSObject **rp);
extern JS_PUBLIC_API(JSBool)
JS_AddDoubleRoot(JSContext *cx, jsdouble **rp);
extern JS_PUBLIC_API(JSBool)
JS_AddGCThingRoot(JSContext *cx, void **rp);
1998-03-28 02:44:41 +00:00
#ifdef NAME_ALL_GC_ROOTS
#define JS_DEFINE_TO_TOKEN(def) #def
#define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
2010-06-08 00:05:02 +00:00
#define JS_AddValueRoot(cx,vp) JS_AddNamedValueRoot((cx), (vp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#define JS_AddStringRoot(cx,rp) JS_AddNamedStringRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#define JS_AddObjectRoot(cx,rp) JS_AddNamedObjectRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#define JS_AddDoubleRoot(cx,rp) JS_AddNamedDoubleRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#define JS_AddGCThingRoot(cx,rp) JS_AddNamedGCThingRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#endif
extern JS_PUBLIC_API(JSBool)
2010-06-08 00:05:02 +00:00
JS_AddNamedValueRoot(JSContext *cx, jsval *vp, const char *name);
extern JS_PUBLIC_API(JSBool)
JS_AddNamedStringRoot(JSContext *cx, JSString **rp, const char *name);
extern JS_PUBLIC_API(JSBool)
JS_AddNamedObjectRoot(JSContext *cx, JSObject **rp, const char *name);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
2010-06-08 00:05:02 +00:00
JS_AddNamedDoubleRoot(JSContext *cx, jsdouble **rp, const char *name);
extern JS_PUBLIC_API(JSBool)
2010-06-08 00:05:02 +00:00
JS_AddNamedGCThingRoot(JSContext *cx, void **rp, const char *name);
extern JS_PUBLIC_API(JSBool)
2010-06-08 00:05:02 +00:00
JS_RemoveValueRoot(JSContext *cx, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_RemoveStringRoot(JSContext *cx, JSString **rp);
extern JS_PUBLIC_API(JSBool)
JS_RemoveObjectRoot(JSContext *cx, JSObject **rp);
extern JS_PUBLIC_API(JSBool)
JS_RemoveDoubleRoot(JSContext *cx, jsdouble **rp);
extern JS_PUBLIC_API(JSBool)
JS_RemoveGCThingRoot(JSContext *cx, void **rp);
/* TODO: remove these APIs */
extern JS_FRIEND_API(JSBool)
js_AddRootRT(JSRuntime *rt, jsval *vp, const char *name);
extern JS_FRIEND_API(JSBool)
js_AddGCThingRootRT(JSRuntime *rt, void **rp, const char *name);
extern JS_FRIEND_API(JSBool)
js_RemoveRoot(JSRuntime *rt, void *rp);
/*
* This symbol may be used by embedders to detect the change from the old
* JS_AddRoot(JSContext *, void *) APIs to the new ones above.
*/
#define JS_TYPED_ROOTING_API
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(void)
JS_ClearNewbornRoots(JSContext *cx);
/* Obsolete rooting APIs. */
#define JS_EnterLocalRootScope(cx) (true)
#define JS_LeaveLocalRootScope(cx) ((void) 0)
#define JS_LeaveLocalRootScopeWithResult(cx, rval) ((void) 0)
#define JS_ForgetLocalRoot(cx, thing) ((void) 0)
1998-03-28 02:44:41 +00:00
#ifdef DEBUG
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_DumpNamedRoots(JSRuntime *rt,
2000-09-08 21:24:14 +00:00
void (*dump)(const char *name, void *rp, void *data),
void *data);
1998-03-28 02:44:41 +00:00
#endif
/*
* Call JS_MapGCRoots to map the GC's roots table using map(rp, name, data).
* The root is pointed at by rp; if the root is unnamed, name is null; data is
* supplied from the third parameter to JS_MapGCRoots.
*
* The map function should return JS_MAP_GCROOT_REMOVE to cause the currently
* enumerated root to be removed. To stop enumeration, set JS_MAP_GCROOT_STOP
* in the return value. To keep on mapping, return JS_MAP_GCROOT_NEXT. These
* constants are flags; you can OR them together.
*
* This function acquires and releases rt's GC lock around the mapping of the
* roots table, so the map function should run to completion in as few cycles
* as possible. Of course, map cannot call JS_GC, JS_MaybeGC, JS_BeginRequest,
* or any JS API entry point that acquires locks, without double-tripping or
* deadlocking on the GC lock.
*
* JS_MapGCRoots returns the count of roots that were successfully mapped.
*/
#define JS_MAP_GCROOT_NEXT 0 /* continue mapping entries */
#define JS_MAP_GCROOT_STOP 1 /* stop mapping entries */
#define JS_MAP_GCROOT_REMOVE 2 /* remove and free the current entry */
typedef intN
(* JSGCRootMapFun)(void *rp, const char *name, void *data);
extern JS_PUBLIC_API(uint32)
JS_MapGCRoots(JSRuntime *rt, JSGCRootMapFun map, void *data);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_LockGCThing(JSContext *cx, void *thing);
extern JS_PUBLIC_API(JSBool)
JS_LockGCThingRT(JSRuntime *rt, void *thing);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_UnlockGCThing(JSContext *cx, void *thing);
extern JS_PUBLIC_API(JSBool)
JS_UnlockGCThingRT(JSRuntime *rt, void *thing);
/*
* Register externally maintained GC roots.
*
* traceOp: the trace operation. For each root the implementation should call
* JS_CallTracer whenever the root contains a traceable thing.
* data: the data argument to pass to each invocation of traceOp.
*/
extern JS_PUBLIC_API(void)
JS_SetExtraGCRoots(JSRuntime *rt, JSTraceDataOp traceOp, void *data);
/*
* For implementors of JSMarkOp. All new code should implement JSTraceOp
* instead.
*/
extern JS_PUBLIC_API(void)
JS_MarkGCThing(JSContext *cx, void *thing, const char *name, void *arg);
/*
* JS_CallTracer API and related macros for implementors of JSTraceOp, to
* enumerate all references to traceable things reachable via a property or
* other strong ref identified for debugging purposes by name or index or
2007-06-10 22:08:17 +00:00
* a naming callback.
*
* By definition references to traceable things include non-null pointers
* to JSObject, JSString and jsdouble and corresponding jsvals.
*
* See the JSTraceOp typedef in jspubtd.h.
*/
/* Trace kinds to pass to JS_Tracing. */
#define JSTRACE_OBJECT 0
#define JSTRACE_DOUBLE 1
#define JSTRACE_STRING 2
/*
* Use the following macros to check if a particular jsval is a traceable
* thing and to extract the thing and its kind to pass to JS_CallTracer.
*/
#define JSVAL_IS_TRACEABLE(v) (JSVAL_IS_GCTHING(v) && !JSVAL_IS_NULL(v))
#define JSVAL_TO_TRACEABLE(v) (JSVAL_TO_GCTHING(v))
#define JSVAL_TRACE_KIND(v) (JSVAL_TAG(v) >> 1)
struct JSTracer {
JSContext *context;
JSTraceCallback callback;
JSTraceNamePrinter debugPrinter;
const void *debugPrintArg;
size_t debugPrintIndex;
};
/*
2007-06-10 22:08:17 +00:00
* The method to call on each reference to a traceable thing stored in a
* particular JSObject or other runtime structure. With DEBUG defined the
* caller before calling JS_CallTracer must initialize JSTracer fields
* describing the reference using the macros below.
*/
extern JS_PUBLIC_API(void)
JS_CallTracer(JSTracer *trc, void *thing, uint32 kind);
/*
* Set debugging information about a reference to a traceable thing to prepare
* for the following call to JS_CallTracer.
*
* When printer is null, arg must be const char * or char * C string naming
* the reference and index must be either (size_t)-1 indicating that the name
* alone describes the reference or it must be an index into some array vector
* that stores the reference.
*
* When printer callback is not null, the arg and index arguments are
* available to the callback as debugPrinterArg and debugPrintIndex fields
* of JSTracer.
*
* The storage for name or callback's arguments needs to live only until
* the following call to JS_CallTracer returns.
*/
#ifdef DEBUG
# define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
JS_BEGIN_MACRO \
(trc)->debugPrinter = (printer); \
(trc)->debugPrintArg = (arg); \
(trc)->debugPrintIndex = (index); \
JS_END_MACRO
#else
# define JS_SET_TRACING_DETAILS(trc, printer, arg, index) \
JS_BEGIN_MACRO \
JS_END_MACRO
#endif
/*
* Convenience macro to describe the argument of JS_CallTracer using C string
* and index.
*/
# define JS_SET_TRACING_INDEX(trc, name, index) \
JS_SET_TRACING_DETAILS(trc, NULL, name, index)
/*
* Convenience macro to describe the argument of JS_CallTracer using C string.
*/
# define JS_SET_TRACING_NAME(trc, name) \
JS_SET_TRACING_DETAILS(trc, NULL, name, (size_t)-1)
/*
* Convenience macro to invoke JS_CallTracer using C string as the name for
* the reference to a traceable thing.
*/
# define JS_CALL_TRACER(trc, thing, kind, name) \
JS_BEGIN_MACRO \
JS_SET_TRACING_NAME(trc, name); \
JS_CallTracer((trc), (thing), (kind)); \
JS_END_MACRO
/*
* Convenience macros to invoke JS_CallTracer when jsval represents a
* reference to a traceable thing.
*/
#define JS_CALL_VALUE_TRACER(trc, val, name) \
JS_BEGIN_MACRO \
if (JSVAL_IS_TRACEABLE(val)) { \
JS_CALL_TRACER((trc), JSVAL_TO_GCTHING(val), \
JSVAL_TRACE_KIND(val), name); \
} \
JS_END_MACRO
#define JS_CALL_OBJECT_TRACER(trc, object, name) \
JS_BEGIN_MACRO \
JSObject *obj_ = (object); \
JS_ASSERT(obj_); \
JS_CALL_TRACER((trc), obj_, JSTRACE_OBJECT, name); \
JS_END_MACRO
#define JS_CALL_STRING_TRACER(trc, string, name) \
JS_BEGIN_MACRO \
JSString *str_ = (string); \
JS_ASSERT(str_); \
JS_CALL_TRACER((trc), str_, JSTRACE_STRING, name); \
JS_END_MACRO
#define JS_CALL_DOUBLE_TRACER(trc, number, name) \
JS_BEGIN_MACRO \
jsdouble *num_ = (number); \
JS_ASSERT(num_); \
JS_CALL_TRACER((trc), num_, JSTRACE_DOUBLE, name); \
JS_END_MACRO
/*
* API for JSTraceCallback implementations.
*/
# define JS_TRACER_INIT(trc, cx_, callback_) \
JS_BEGIN_MACRO \
(trc)->context = (cx_); \
(trc)->callback = (callback_); \
(trc)->debugPrinter = NULL; \
(trc)->debugPrintArg = NULL; \
(trc)->debugPrintIndex = (size_t)-1; \
JS_END_MACRO
extern JS_PUBLIC_API(void)
JS_TraceChildren(JSTracer *trc, void *thing, uint32 kind);
extern JS_PUBLIC_API(void)
JS_TraceRuntime(JSTracer *trc);
#ifdef DEBUG
extern JS_PUBLIC_API(void)
JS_PrintTraceThingInfo(char *buf, size_t bufsize, JSTracer *trc,
void *thing, uint32 kind, JSBool includeDetails);
/*
* DEBUG-only method to dump the object graph of heap-allocated things.
*
* fp: file for the dump output.
* start: when non-null, dump only things reachable from start
* thing. Otherwise dump all things reachable from the
* runtime roots.
* startKind: trace kind of start if start is not null. Must be 0 when
* start is null.
* thingToFind: dump only paths in the object graph leading to thingToFind
* when non-null.
* maxDepth: the upper bound on the number of edges to descend from the
* graph roots.
* thingToIgnore: thing to ignore during the graph traversal when non-null.
*/
extern JS_PUBLIC_API(JSBool)
JS_DumpHeap(JSContext *cx, FILE *fp, void* startThing, uint32 startKind,
void *thingToFind, size_t maxDepth, void *thingToIgnore);
#endif
/*
* Garbage collector API.
*/
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_GC(JSContext *cx);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_MaybeGC(JSContext *cx);
extern JS_PUBLIC_API(JSGCCallback)
JS_SetGCCallback(JSContext *cx, JSGCCallback cb);
extern JS_PUBLIC_API(JSGCCallback)
JS_SetGCCallbackRT(JSRuntime *rt, JSGCCallback cb);
extern JS_PUBLIC_API(JSBool)
JS_IsGCMarkingTracer(JSTracer *trc);
extern JS_PUBLIC_API(JSBool)
JS_IsAboutToBeFinalized(JSContext *cx, void *thing);
typedef enum JSGCParamKey {
/* Maximum nominal heap before last ditch GC. */
JSGC_MAX_BYTES = 0,
/* Number of JS_malloc bytes before last ditch GC. */
JSGC_MAX_MALLOC_BYTES = 1,
/* Hoard stackPools for this long, in ms, default is 30 seconds. */
JSGC_STACKPOOL_LIFESPAN = 2,
/*
* The factor that defines when the GC is invoked. The factor is a
* percent of the memory allocated by the GC after the last run of
* the GC. When the current memory allocated by the GC is more than
* this percent then the GC is invoked. The factor cannot be less
* than 100 since the current memory allocated by the GC cannot be less
* than the memory allocated after the last run of the GC.
*/
JSGC_TRIGGER_FACTOR = 3,
/* Amount of bytes allocated by the GC. */
JSGC_BYTES = 4,
/* Number of times when GC was invoked. */
JSGC_NUMBER = 5,
/* Max size of the code cache in bytes. */
JSGC_MAX_CODE_CACHE_BYTES = 6
} JSGCParamKey;
extern JS_PUBLIC_API(void)
JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32 value);
extern JS_PUBLIC_API(uint32)
JS_GetGCParameter(JSRuntime *rt, JSGCParamKey key);
extern JS_PUBLIC_API(void)
JS_SetGCParameterForThread(JSContext *cx, JSGCParamKey key, uint32 value);
extern JS_PUBLIC_API(uint32)
JS_GetGCParameterForThread(JSContext *cx, JSGCParamKey key);
/*
* Flush the code cache for the current thread. The operation might be
* delayed if the cache cannot be flushed currently because native
* code is currently executing.
*/
extern JS_PUBLIC_API(void)
JS_FlushCaches(JSContext *cx);
/*
* Add a finalizer for external strings created by JS_NewExternalString (see
* below) using a type-code returned from this function, and that understands
* how to free or release the memory pointed at by JS_GetStringChars(str).
*
* Return a nonnegative type index if there is room for finalizer in the
* global GC finalizers table, else return -1. If the engine is compiled
* JS_THREADSAFE and used in a multi-threaded environment, this function must
* be invoked on the primordial thread only, at startup -- or else the entire
* program must single-thread itself while loading a module that calls this
* function.
*/
extern JS_PUBLIC_API(intN)
JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
/*
* Remove finalizer from the global GC finalizers table, returning its type
* code if found, -1 if not found.
*
* As with JS_AddExternalStringFinalizer, there is a threading restriction
* if you compile the engine JS_THREADSAFE: this function may be called for a
* given finalizer pointer on only one thread; different threads may call to
* remove distinct finalizers safely.
*
* You must ensure that all strings with finalizer's type have been collected
* before calling this function. Otherwise, string data will be leaked by the
* GC, for want of a finalizer to call.
*/
extern JS_PUBLIC_API(intN)
JS_RemoveExternalStringFinalizer(JSStringFinalizeOp finalizer);
/*
* Create a new JSString whose chars member refers to external memory, i.e.,
* memory requiring special, type-specific finalization. The type code must
* be a nonnegative return value from JS_AddExternalStringFinalizer.
*/
extern JS_PUBLIC_API(JSString *)
JS_NewExternalString(JSContext *cx, jschar *chars, size_t length, intN type);
/*
* Returns the external-string finalizer index for this string, or -1 if it is
* an "internal" (native to JS engine) string.
*/
extern JS_PUBLIC_API(intN)
JS_GetExternalStringGCType(JSRuntime *rt, JSString *str);
/*
* Deprecated. Use JS_SetNativeStackQuoata instead.
*/
extern JS_PUBLIC_API(void)
JS_SetThreadStackLimit(JSContext *cx, jsuword limitAddr);
/*
* Set the size of the native stack that should not be exceed. To disable
* stack size checking pass 0.
*/
extern JS_PUBLIC_API(void)
JS_SetNativeStackQuota(JSContext *cx, size_t stackSize);
/*
* Set the quota on the number of bytes that stack-like data structures can
* use when the runtime compiles and executes scripts. These structures
* consume heap space, so JS_SetThreadStackLimit does not bound their size.
* The default quota is 32MB which is quite generous.
*
* The function must be called before any script compilation or execution API
* calls, i.e. either immediately after JS_NewContext or from JSCONTEXT_NEW
* context callback.
*/
extern JS_PUBLIC_API(void)
JS_SetScriptStackQuota(JSContext *cx, size_t quota);
#define JS_DEFAULT_SCRIPT_STACK_QUOTA ((size_t) 0x2000000)
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Classes, objects, and properties.
*/
/* For detailed comments on the function pointer types, see jspubtd.h. */
1998-03-28 02:44:41 +00:00
struct JSClass {
2000-08-19 19:17:32 +00:00
const char *name;
uint32 flags;
/* Mandatory non-null function pointer members. */
JSPropertyOp addProperty;
JSPropertyOp delProperty;
JSPropertyOp getProperty;
JSPropertyOp setProperty;
JSEnumerateOp enumerate;
JSResolveOp resolve;
JSConvertOp convert;
JSFinalizeOp finalize;
/* Optionally non-null members start here. */
JSGetObjectOps getObjectOps;
JSCheckAccessOp checkAccess;
JSNative call;
JSNative construct;
JSXDRObjectOp xdrObject;
JSHasInstanceOp hasInstance;
JSMarkOp mark;
void (*reserved0)(void);
1998-03-28 02:44:41 +00:00
};
2004-11-17 07:43:01 +00:00
struct JSExtendedClass {
JSClass base;
JSEqualityOp equality;
JSObjectOp outerObject;
JSObjectOp innerObject;
JSIteratorOp iteratorObject;
JSObjectOp wrappedObject; /* NB: infallible, null
returns are treated as
the original object */
void (*reserved0)(void);
void (*reserved1)(void);
void (*reserved2)(void);
2004-11-17 07:43:01 +00:00
};
#define JSCLASS_HAS_PRIVATE (1<<0) /* objects have private slot */
#define JSCLASS_NEW_ENUMERATE (1<<1) /* has JSNewEnumerateOp hook */
#define JSCLASS_NEW_RESOLVE (1<<2) /* has JSNewResolveOp hook */
#define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) /* private is (nsISupports *) */
/* (1<<4) was JSCLASS_SHARE_ALL_PROPERTIES, now obsolete. See bug 527805. */
#define JSCLASS_NEW_RESOLVE_GETS_START (1<<5) /* JSNewResolveOp gets starting
object in prototype chain
passed in via *objp in/out
parameter */
#define JSCLASS_CONSTRUCT_PROTOTYPE (1<<6) /* call constructor on class
prototype */
2004-11-17 07:43:01 +00:00
#define JSCLASS_DOCUMENT_OBSERVER (1<<7) /* DOM document observer */
/*
* To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or
* JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where
* n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1.
*/
#define JSCLASS_RESERVED_SLOTS_SHIFT 8 /* room for 8 flags below */
#define JSCLASS_RESERVED_SLOTS_WIDTH 8 /* and 16 above this field */
#define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH)
#define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \
<< JSCLASS_RESERVED_SLOTS_SHIFT)
#define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \
>> JSCLASS_RESERVED_SLOTS_SHIFT) \
& JSCLASS_RESERVED_SLOTS_MASK)
2004-11-17 07:43:01 +00:00
#define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \
JSCLASS_RESERVED_SLOTS_WIDTH)
/* True if JSClass is really a JSExtendedClass. */
#define JSCLASS_IS_EXTENDED (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0))
#define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1))
#define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2))
/* Indicates that JSClass.mark is a tracer with JSTraceOp type. */
#define JSCLASS_MARK_IS_TRACE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3))
/*
* ECMA-262 requires that most constructors used internally create objects
* with "the original Foo.prototype value" as their [[Prototype]] (__proto__)
* member initial value. The "original ... value" verbiage is there because
* in ECMA-262, global properties naming class objects are read/write and
* deleteable, for the most part.
*
* Implementing this efficiently requires that global objects have classes
* with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was
* prevously allowed, but is now an ES5 violation and thus unsupported.
*/
#define JSCLASS_GLOBAL_FLAGS \
(JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSProto_LIMIT * 3 + 1))
#define JSRESERVED_GLOBAL_COMPARTMENT (JSProto_LIMIT * 3)
/* Fast access to the original value of each standard class's prototype. */
#define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 8)
#define JSCLASS_CACHED_PROTO_WIDTH 8
#define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH)
#define JSCLASS_HAS_CACHED_PROTO(key) ((key) << JSCLASS_CACHED_PROTO_SHIFT)
#define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \
(((clasp)->flags \
>> JSCLASS_CACHED_PROTO_SHIFT) \
& JSCLASS_CACHED_PROTO_MASK))
2004-11-17 07:43:01 +00:00
/* Initializer for unused members of statically initialized JSClass structs. */
#define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,0,0,0
#define JSCLASS_NO_RESERVED_MEMBERS 0,0,0
struct JSIdArray {
jsint length;
jsid vector[1]; /* actually, length jsid words */
};
extern JS_PUBLIC_API(void)
JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
extern JS_PUBLIC_API(JSBool)
JS_IdToValue(JSContext *cx, jsid id, jsval *vp);
/*
* The magic XML namespace id is int-tagged, but not a valid integer jsval.
* Global object classes in embeddings that enable JS_HAS_XML_SUPPORT (E4X)
* should handle this id specially before converting id via JSVAL_TO_INT.
*/
2004-11-17 07:43:01 +00:00
#define JS_DEFAULT_XML_NAMESPACE_ID ((jsid) JSVAL_VOID)
/*
* JSNewResolveOp flag bits.
*/
#define JSRESOLVE_QUALIFIED 0x01 /* resolve a qualified property id */
#define JSRESOLVE_ASSIGNING 0x02 /* resolve on the left of assignment */
#define JSRESOLVE_DETECTING 0x04 /* 'if (o.p)...' or '(o.p) ?...:...' */
#define JSRESOLVE_DECLARING 0x08 /* var, const, or function prolog op */
#define JSRESOLVE_CLASSNAME 0x10 /* class name used when constructing */
#define JSRESOLVE_WITH 0x20 /* resolve inside a with statement */
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_PropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_EnumerateStub(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ResolveStub(JSContext *cx, JSObject *obj, jsval id);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ConvertStub(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_FinalizeStub(JSContext *cx, JSObject *obj);
struct JSConstDoubleSpec {
jsdouble dval;
const char *name;
uint8 flags;
uint8 spare[3];
};
/*
* To define an array element rather than a named property member, cast the
* element's index to (const char *) and initialize name with it, and set the
* JSPROP_INDEX bit in flags.
*/
struct JSPropertySpec {
const char *name;
int8 tinyid;
uint8 flags;
JSPropertyOp getter;
JSPropertyOp setter;
};
struct JSFunctionSpec {
const char *name;
JSNative call;
uint16 nargs;
uint16 flags;
/*
* extra & 0xFFFF: Number of extra argument slots for local GC roots.
* If fast native, must be zero.
* extra >> 16: Reserved for future use (must be 0).
*/
uint32 extra;
1998-03-28 02:44:41 +00:00
};
/*
* Terminating sentinel initializer to put at the end of a JSFunctionSpec array
* that's passed to JS_DefineFunctions or JS_InitClass.
*/
#define JS_FS_END JS_FS(NULL,NULL,0,0,0)
/*
* Initializer macro for a JSFunctionSpec array element. This is the original
* kind of native function specifier initializer. Use JS_FN ("fast native", see
* JSFastNative in jspubtd.h) for all functions that do not need a stack frame
* when activated.
*/
#define JS_FS(name,call,nargs,flags,extra) \
{name, call, nargs, flags, extra}
/*
* "Fast native" initializer macro for a JSFunctionSpec array element. Use this
* in preference to JS_FS if the native in question does not need its own stack
* frame when activated.
*/
#define JS_FN(name,fastcall,nargs,flags) \
JS_FS(name, (JSNative)(fastcall), nargs, \
(flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS, 0)
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
2000-09-08 21:24:14 +00:00
JSClass *clasp, JSNative constructor, uintN nargs,
JSPropertySpec *ps, JSFunctionSpec *fs,
JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
1998-03-28 02:44:41 +00:00
#ifdef JS_THREADSAFE
extern JS_PUBLIC_API(JSClass *)
JS_GetClass(JSContext *cx, JSObject *obj);
#define JS_GET_CLASS(cx,obj) JS_GetClass(cx, obj)
#else
extern JS_PUBLIC_API(JSClass *)
1998-03-28 02:44:41 +00:00
JS_GetClass(JSObject *obj);
#define JS_GET_CLASS(cx,obj) JS_GetClass(obj)
#endif
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_InstanceOf(JSContext *cx, JSObject *obj, JSClass *clasp, jsval *argv);
extern JS_PUBLIC_API(JSBool)
JS_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
extern JS_PUBLIC_API(void *)
1998-03-28 02:44:41 +00:00
JS_GetPrivate(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_SetPrivate(JSContext *cx, JSObject *obj, void *data);
extern JS_PUBLIC_API(void *)
1998-03-28 02:44:41 +00:00
JS_GetInstancePrivate(JSContext *cx, JSObject *obj, JSClass *clasp,
2000-09-08 21:24:14 +00:00
jsval *argv);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetPrototype(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetParent(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_SetParent(JSContext *cx, JSObject *obj, JSObject *parent);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetConstructor(JSContext *cx, JSObject *proto);
/*
* Get a unique identifier for obj, good for the lifetime of obj (even if it
* is moved by a copying GC). Return false on failure (likely out of memory),
* and true with *idp containing the unique id on success.
*/
extern JS_PUBLIC_API(JSBool)
JS_GetObjectId(JSContext *cx, JSObject *obj, jsid *idp);
extern JS_PUBLIC_API(JSObject *)
JS_NewGlobalObject(JSContext *cx, JSClass *clasp);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto, JSObject *parent);
/*
* Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
* proto if proto's actual parameter value is null.
*/
extern JS_PUBLIC_API(JSObject *)
JS_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
JSObject *parent);
extern JS_PUBLIC_API(JSBool)
JS_SealObject(JSContext *cx, JSObject *obj, JSBool deep);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
2000-09-08 21:24:14 +00:00
JSObject *parent);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSObject *)
JS_ConstructObjectWithArguments(JSContext *cx, JSClass *clasp, JSObject *proto,
JSObject *parent, uintN argc, jsval *argv);
extern JS_PUBLIC_API(JSObject *)
JS_New(JSContext *cx, JSObject *ctor, uintN argc, jsval *argv);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, JSClass *clasp,
2000-09-08 21:24:14 +00:00
JSObject *proto, uintN attrs);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefineConstDoubles(JSContext *cx, JSObject *obj, JSConstDoubleSpec *cds);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefineProperties(JSContext *cx, JSObject *obj, JSPropertySpec *ps);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
2000-09-08 21:24:14 +00:00
JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
extern JS_PUBLIC_API(JSBool)
JS_DefinePropertyById(JSContext *cx, JSObject *obj, jsid id, jsval value,
JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
extern JS_PUBLIC_API(JSBool)
JS_DefineOwnProperty(JSContext *cx, JSObject *obj, jsid id, jsval descriptor, JSBool *bp);
/*
* Determine the attributes (JSPROP_* flags) of a property on a given object.
*
* If the object does not have a property by that name, *foundp will be
* JS_FALSE and the value of *attrsp is undefined.
*/
extern JS_PUBLIC_API(JSBool)
JS_GetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
uintN *attrsp, JSBool *foundp);
/*
* The same, but if the property is native, return its getter and setter via
* *getterp and *setterp, respectively (and only if the out parameter pointer
* is not null).
*/
extern JS_PUBLIC_API(JSBool)
JS_GetPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
const char *name,
uintN *attrsp, JSBool *foundp,
JSPropertyOp *getterp,
JSPropertyOp *setterp);
extern JS_PUBLIC_API(JSBool)
JS_GetPropertyAttrsGetterAndSetterById(JSContext *cx, JSObject *obj,
jsid id,
uintN *attrsp, JSBool *foundp,
JSPropertyOp *getterp,
JSPropertyOp *setterp);
/*
* Set the attributes of a property on a given object.
*
* If the object does not have a property by that name, *foundp will be
* JS_FALSE and nothing will be altered.
*/
extern JS_PUBLIC_API(JSBool)
JS_SetPropertyAttributes(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
uintN attrs, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefinePropertyWithTinyId(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
int8 tinyid, jsval value,
JSPropertyOp getter, JSPropertyOp setter,
uintN attrs);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_AliasProperty(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
const char *alias);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
JS_AlreadyHasOwnProperty(JSContext *cx, JSObject *obj, const char *name,
JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_AlreadyHasOwnPropertyById(JSContext *cx, JSObject *obj, jsid id,
JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_HasProperty(JSContext *cx, JSObject *obj, const char *name, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_HasPropertyById(JSContext *cx, JSObject *obj, jsid id, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_LookupProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_LookupPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, const char *name,
uintN flags, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_LookupPropertyWithFlagsById(JSContext *cx, JSObject *obj, jsid id,
uintN flags, JSObject **objp, jsval *vp);
struct JSPropertyDescriptor {
JSObject *obj;
uintN attrs;
JSPropertyOp getter;
JSPropertyOp setter;
uintN shortid;
jsval value;
};
/*
* Like JS_GetPropertyAttrsGetterAndSetterById but will return a property on
* an object on the prototype chain (returned in objp). If data->obj is null,
* then this property was not found on the prototype chain.
*/
extern JS_PUBLIC_API(JSBool)
JS_GetPropertyDescriptorById(JSContext *cx, JSObject *obj, jsid id, uintN flags,
JSPropertyDescriptor *desc);
extern JS_PUBLIC_API(JSBool)
JS_GetOwnPropertyDescriptor(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_GetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_GetPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_GetMethodById(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_GetMethod(JSContext *cx, JSObject *obj, const char *name, JSObject **objp,
jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_SetProperty(JSContext *cx, JSObject *obj, const char *name, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_SetPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DeleteProperty(JSContext *cx, JSObject *obj, const char *name);
extern JS_PUBLIC_API(JSBool)
JS_DeleteProperty2(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
jsval *rval);
extern JS_PUBLIC_API(JSBool)
JS_DeletePropertyById(JSContext *cx, JSObject *obj, jsid id);
extern JS_PUBLIC_API(JSBool)
JS_DeletePropertyById2(JSContext *cx, JSObject *obj, jsid id, jsval *rval);
extern JS_PUBLIC_API(JSBool)
JS_DefineUCProperty(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen, jsval value,
JSPropertyOp getter, JSPropertyOp setter,
uintN attrs);
/*
* Determine the attributes (JSPROP_* flags) of a property on a given object.
*
* If the object does not have a property by that name, *foundp will be
* JS_FALSE and the value of *attrsp is undefined.
*/
extern JS_PUBLIC_API(JSBool)
JS_GetUCPropertyAttributes(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
uintN *attrsp, JSBool *foundp);
/*
* The same, but if the property is native, return its getter and setter via
* *getterp and *setterp, respectively (and only if the out parameter pointer
* is not null).
*/
extern JS_PUBLIC_API(JSBool)
JS_GetUCPropertyAttrsGetterAndSetter(JSContext *cx, JSObject *obj,
const jschar *name, size_t namelen,
uintN *attrsp, JSBool *foundp,
JSPropertyOp *getterp,
JSPropertyOp *setterp);
/*
* Set the attributes of a property on a given object.
*
* If the object does not have a property by that name, *foundp will be
* JS_FALSE and nothing will be altered.
*/
extern JS_PUBLIC_API(JSBool)
JS_SetUCPropertyAttributes(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
uintN attrs, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_DefineUCPropertyWithTinyId(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
int8 tinyid, jsval value,
JSPropertyOp getter, JSPropertyOp setter,
uintN attrs);
extern JS_PUBLIC_API(JSBool)
JS_AlreadyHasOwnUCProperty(JSContext *cx, JSObject *obj, const jschar *name,
size_t namelen, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_HasUCProperty(JSContext *cx, JSObject *obj,
const jschar *name, size_t namelen,
JSBool *vp);
extern JS_PUBLIC_API(JSBool)
JS_LookupUCProperty(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_GetUCProperty(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_SetUCProperty(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_DeleteUCProperty2(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *name, size_t namelen,
jsval *rval);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_NewArrayObject(JSContext *cx, jsint length, jsval *vector);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_IsArrayObject(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
JS_GetArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
JS_SetArrayLength(JSContext *cx, JSObject *obj, jsuint length);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
JS_HasArrayLength(JSContext *cx, JSObject *obj, jsuint *lengthp);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefineElement(JSContext *cx, JSObject *obj, jsint index, jsval value,
2000-09-08 21:24:14 +00:00
JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_AliasElement(JSContext *cx, JSObject *obj, const char *name, jsint alias);
extern JS_PUBLIC_API(JSBool)
JS_AlreadyHasOwnElement(JSContext *cx, JSObject *obj, jsint index,
JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
JS_HasElement(JSContext *cx, JSObject *obj, jsint index, JSBool *foundp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_LookupElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_GetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_SetElement(JSContext *cx, JSObject *obj, jsint index, jsval *vp);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DeleteElement(JSContext *cx, JSObject *obj, jsint index);
extern JS_PUBLIC_API(JSBool)
JS_DeleteElement2(JSContext *cx, JSObject *obj, jsint index, jsval *rval);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_ClearScope(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSIdArray *)
JS_Enumerate(JSContext *cx, JSObject *obj);
/*
* Create an object to iterate over enumerable properties of obj, in arbitrary
* property definition order. NB: This differs from longstanding for..in loop
* order, which uses order of property definition in obj.
*/
extern JS_PUBLIC_API(JSObject *)
JS_NewPropertyIterator(JSContext *cx, JSObject *obj);
/*
* Return true on success with *idp containing the id of the next enumerable
* property to visit using iterobj, or JSVAL_VOID if there is no such property
* left to visit. Return false on error.
*/
extern JS_PUBLIC_API(JSBool)
JS_NextProperty(JSContext *cx, JSObject *iterobj, jsid *idp);
extern JS_PUBLIC_API(JSBool)
JS_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
2000-09-08 21:24:14 +00:00
jsval *vp, uintN *attrsp);
extern JS_PUBLIC_API(JSBool)
JS_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
extern JS_PUBLIC_API(JSBool)
JS_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Security protocol.
*/
struct JSPrincipals {
1998-03-28 02:44:41 +00:00
char *codebase;
/* XXX unspecified and unused by Mozilla code -- can we remove these? */
void * (* getPrincipalArray)(JSContext *cx, JSPrincipals *);
JSBool (* globalPrivilegesEnabled)(JSContext *cx, JSPrincipals *);
1998-03-28 02:44:41 +00:00
/* Don't call "destroy"; use reference counting macros below. */
jsrefcount refcount;
void (* destroy)(JSContext *cx, JSPrincipals *);
JSBool (* subsume)(JSPrincipals *, JSPrincipals *);
};
1998-03-28 02:44:41 +00:00
#ifdef JS_THREADSAFE
#define JSPRINCIPALS_HOLD(cx, principals) JS_HoldPrincipals(cx,principals)
#define JSPRINCIPALS_DROP(cx, principals) JS_DropPrincipals(cx,principals)
extern JS_PUBLIC_API(jsrefcount)
JS_HoldPrincipals(JSContext *cx, JSPrincipals *principals);
extern JS_PUBLIC_API(jsrefcount)
JS_DropPrincipals(JSContext *cx, JSPrincipals *principals);
#else
#define JSPRINCIPALS_HOLD(cx, principals) (++(principals)->refcount)
#define JSPRINCIPALS_DROP(cx, principals) \
((--(principals)->refcount == 0) \
? ((*(principals)->destroy)((cx), (principals)), 0) \
: (principals)->refcount)
#endif
1998-03-28 02:44:41 +00:00
struct JSSecurityCallbacks {
JSCheckAccessOp checkObjectAccess;
JSPrincipalsTranscoder principalsTranscoder;
JSObjectPrincipalsFinder findObjectPrincipals;
JSCSPEvalChecker contentSecurityPolicyAllows;
};
extern JS_PUBLIC_API(JSSecurityCallbacks *)
JS_SetRuntimeSecurityCallbacks(JSRuntime *rt, JSSecurityCallbacks *callbacks);
extern JS_PUBLIC_API(JSSecurityCallbacks *)
JS_GetRuntimeSecurityCallbacks(JSRuntime *rt);
extern JS_PUBLIC_API(JSSecurityCallbacks *)
JS_SetContextSecurityCallbacks(JSContext *cx, JSSecurityCallbacks *callbacks);
extern JS_PUBLIC_API(JSSecurityCallbacks *)
JS_GetSecurityCallbacks(JSContext *cx);
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Functions and scripts.
*/
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_NewFunction(JSContext *cx, JSNative call, uintN nargs, uintN flags,
2000-09-08 21:24:14 +00:00
JSObject *parent, const char *name);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_GetFunctionObject(JSFunction *fun);
/*
* Deprecated, useful only for diagnostics. Use JS_GetFunctionId instead for
* anonymous vs. "anonymous" disambiguation and Unicode fidelity.
*/
extern JS_PUBLIC_API(const char *)
1998-03-28 02:44:41 +00:00
JS_GetFunctionName(JSFunction *fun);
/*
* Return the function's identifier as a JSString, or null if fun is unnamed.
* The returned string lives as long as fun, so you don't need to root a saved
* reference to it if fun is well-connected or rooted, and provided you bound
* the use of the saved reference by fun's lifetime.
*
* Prefer JS_GetFunctionId over JS_GetFunctionName because it returns null for
* truly anonymous functions, and because it doesn't chop to ISO-Latin-1 chars
* from UTF-16-ish jschars.
*/
extern JS_PUBLIC_API(JSString *)
JS_GetFunctionId(JSFunction *fun);
/*
* Return JSFUN_* flags for fun.
*/
extern JS_PUBLIC_API(uintN)
JS_GetFunctionFlags(JSFunction *fun);
/*
* Return the arity (length) of fun.
*/
extern JS_PUBLIC_API(uint16)
JS_GetFunctionArity(JSFunction *fun);
/*
* Infallible predicate to test whether obj is a function object (faster than
* comparing obj's class name to "Function", but equivalent unless someone has
* overwritten the "Function" identifier with a different constructor and then
* created instances using that constructor that might be passed in as obj).
*/
extern JS_PUBLIC_API(JSBool)
JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_DefineFunctions(JSContext *cx, JSObject *obj, JSFunctionSpec *fs);
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_DefineFunction(JSContext *cx, JSObject *obj, const char *name, JSNative call,
2000-09-08 21:24:14 +00:00
uintN nargs, uintN attrs);
1998-03-28 02:44:41 +00:00
2004-05-07 03:21:30 +00:00
extern JS_PUBLIC_API(JSFunction *)
JS_DefineUCFunction(JSContext *cx, JSObject *obj,
const jschar *name, size_t namelen, JSNative call,
uintN nargs, uintN attrs);
extern JS_PUBLIC_API(JSObject *)
JS_CloneFunctionObject(JSContext *cx, JSObject *funobj, JSObject *parent);
/*
* Given a buffer, return JS_FALSE if the buffer might become a valid
* javascript statement with the addition of more lines. Otherwise return
* JS_TRUE. The intent is to support interactive compilation - accumulate
* lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
* the compiler.
*/
extern JS_PUBLIC_API(JSBool)
JS_BufferIsCompilableUnit(JSContext *cx, JSObject *obj,
const char *bytes, size_t length);
/*
* The JSScript objects returned by the following functions refer to string and
* other kinds of literals, including doubles and RegExp objects. These
* literals are vulnerable to garbage collection; to root script objects and
* prevent literals from being collected, create a rootable object using
* JS_NewScriptObject, and root the resulting object using JS_Add[Named]Root.
*/
extern JS_PUBLIC_API(JSScript *)
1998-03-28 02:44:41 +00:00
JS_CompileScript(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const char *bytes, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSScript *)
1998-03-28 02:44:41 +00:00
JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals,
const char *bytes, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSScript *)
1998-03-28 02:44:41 +00:00
JS_CompileUCScript(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *chars, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSScript *)
1998-03-28 02:44:41 +00:00
JS_CompileUCScriptForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals,
const jschar *chars, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSScript *)
1998-03-28 02:44:41 +00:00
JS_CompileFile(JSContext *cx, JSObject *obj, const char *filename);
extern JS_PUBLIC_API(JSScript *)
JS_CompileFileHandle(JSContext *cx, JSObject *obj, const char *filename,
FILE *fh);
extern JS_PUBLIC_API(JSScript *)
JS_CompileFileHandleForPrincipals(JSContext *cx, JSObject *obj,
const char *filename, FILE *fh,
JSPrincipals *principals);
/*
* NB: you must use JS_NewScriptObject and root a pointer to its return value
* in order to keep a JSScript and its atoms safe from garbage collection after
* creating the script via JS_Compile* and before a JS_ExecuteScript* call.
* E.g., and without error checks:
*
* JSScript *script = JS_CompileFile(cx, global, filename);
* JSObject *scrobj = JS_NewScriptObject(cx, script);
2010-06-08 00:05:02 +00:00
* JS_AddNamedObjectRoot(cx, &scrobj, "scrobj");
* do {
* jsval result;
* JS_ExecuteScript(cx, global, script, &result);
* JS_GC();
* } while (!JSVAL_IS_BOOLEAN(result) || JSVAL_TO_BOOLEAN(result));
2010-06-08 00:05:02 +00:00
* JS_RemoveObjectRoot(cx, &scrobj);
*/
extern JS_PUBLIC_API(JSObject *)
JS_NewScriptObject(JSContext *cx, JSScript *script);
/*
* Infallible getter for a script's object. If JS_NewScriptObject has not been
* called on script yet, the return value will be null.
*/
extern JS_PUBLIC_API(JSObject *)
JS_GetScriptObject(JSScript *script);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_DestroyScript(JSContext *cx, JSScript *script);
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_CompileFunction(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
uintN nargs, const char **argnames,
const char *bytes, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals, const char *name,
uintN nargs, const char **argnames,
const char *bytes, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_CompileUCFunction(JSContext *cx, JSObject *obj, const char *name,
2000-09-08 21:24:14 +00:00
uintN nargs, const char **argnames,
const jschar *chars, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSFunction *)
1998-03-28 02:44:41 +00:00
JS_CompileUCFunctionForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals, const char *name,
uintN nargs, const char **argnames,
const jschar *chars, size_t length,
const char *filename, uintN lineno);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_DecompileScript(JSContext *cx, JSScript *script, const char *name,
2000-09-08 21:24:14 +00:00
uintN indent);
1998-03-28 02:44:41 +00:00
- Bumped default compile-time JS version from 1.4 to 1.5. - Add JS1.5 getter/setter support in all its glory: * getter function SN() {return ++x} at top-level or as a closure binds an SN property getter than returns the incremented value of x. Likewise for setter function SN(y) {return y = x}. * getters and setters may be defined in an object literal: o = {p getter:function() {return ++this.x}, p setter:function(y){return this.x = y}, x:42}; * getter= and setter= operators (compound tokens) may be used to bind getter and setter properties dynamically: o = new Object; o.p getter= function() {return ++this.x}; o.p setter= function(y){return this.x = y}; o.x = 42; Waldemar is concerned that this form will collide semantically with JS2, so I am not committing to keeping it in JS1.5. I'd like to check my code in ASAP so shaver can use it, and I'd also like to see this form get used (or not) during Mozilla betas. Caveat emptor, and if you find this "dynamic" or "imperative" form necessary and hard to substitute, please let me know. If this proves important to users, then I think JS1.5 should keep it. - Cleaned up property flags (in a binary-incompatible fashion -- who cares?) by eliminating JSPROP_ASSIGNHACK and JSPROP_TINYIDHACK. - Added JS_DONT_PRETTY_PRINT flag to be ORed with the indent argument to the several JS_Decompile*() API calls. This avoids any newlines or identation in the decompiled string. - Improved and extended (for getter/setter non-reservation) scanner lookahead by using a circular (power-of-2 sized) token buffer. - Fix ECMA Edition 3 deviation where function f(){function g(){}} bound f.g by mistake (it should arrange to make a closure named g in activations of f, but it should not bind a property of function f).
1999-09-21 00:13:48 +00:00
/*
* API extension: OR this into indent to avoid pretty-printing the decompiled
* source resulting from JS_DecompileFunction{,Body}.
*/
#define JS_DONT_PRETTY_PRINT ((uintN)0x8000)
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent);
/*
* NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
* parameter as the initial scope chain header, the 'this' keyword value, and
* the variables object (ECMA parlance for where 'var' and 'function' bind
* names) of the execution context for script.
*
* Using obj as the variables object is problematic if obj's parent (which is
* the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
* this case, variables created by 'var x = 0', e.g., go in obj, but variables
* created by assignment to an unbound id, 'x = 0', go in the last object on
* the scope chain linked by parent.
*
* ECMA calls that last scoping object the "global object", but note that many
* embeddings have several such objects. ECMA requires that "global code" be
* executed with the variables object equal to this global object. But these
* JS API entry points provide freedom to execute code against a "sub-global",
* i.e., a parented or scoped object, in which case the variables object will
* differ from the last object on the scope chain, resulting in confusing and
* non-ECMA explicit vs. implicit variable creation.
*
* Caveat embedders: unless you already depend on this buggy variables object
* binding behavior, you should call JS_SetOptions(cx, JSOPTION_VAROBJFIX) or
* JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_VAROBJFIX) -- the latter if
* someone may have set other options on cx already -- for each context in the
* application, if you pass parented objects as the obj parameter, or may ever
* pass such objects in the future.
*
* Why a runtime option? The alternative is to add six or so new API entry
* points with signatures matching the following six, and that doesn't seem
* worth the code bloat cost. Such new entry points would probably have less
* obvious names, too, so would not tend to be used. The JS_SetOption call,
* OTOH, can be more easily hacked into existing code that does not depend on
* the bug; such code can continue to use the familiar JS_EvaluateScript,
* etc., entry points.
*/
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
/*
* Execute either the function-defining prolog of a script, or the script's
* main body, but not both.
*/
typedef enum JSExecPart { JSEXEC_PROLOG, JSEXEC_MAIN } JSExecPart;
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_EvaluateScript(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const char *bytes, uintN length,
const char *filename, uintN lineno,
jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals,
const char *bytes, uintN length,
const char *filename, uintN lineno,
jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
const jschar *chars, uintN length,
const char *filename, uintN lineno,
jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj,
2000-09-08 21:24:14 +00:00
JSPrincipals *principals,
const jschar *chars, uintN length,
const char *filename, uintN lineno,
jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc,
2000-09-08 21:24:14 +00:00
jsval *argv, jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc,
2000-09-08 21:24:14 +00:00
jsval *argv, jsval *rval);
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
2000-09-08 21:24:14 +00:00
jsval *argv, jsval *rval);
1998-03-28 02:44:41 +00:00
/*
* These functions allow setting an operation callback that will be called
* from the thread the context is associated with some time after any thread
* triggered the callback using JS_TriggerOperationCallback(cx).
*
* In a threadsafe build the engine internally triggers operation callbacks
* under certain circumstances (i.e. GC and title transfer) to force the
* context to yield its current request, which the engine always
* automatically does immediately prior to calling the callback function.
* The embedding should thus not rely on callbacks being triggered through
* the external API only.
*
* Important note: Additional callbacks can occur inside the callback handler
* if it re-enters the JS engine. The embedding must ensure that the callback
* is disconnected before attempting such re-entry.
*/
extern JS_PUBLIC_API(JSOperationCallback)
JS_SetOperationCallback(JSContext *cx, JSOperationCallback callback);
extern JS_PUBLIC_API(JSOperationCallback)
JS_GetOperationCallback(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_TriggerOperationCallback(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_TriggerAllOperationCallbacks(JSRuntime *rt);
extern JS_PUBLIC_API(JSBool)
1998-03-28 02:44:41 +00:00
JS_IsRunning(JSContext *cx);
extern JS_PUBLIC_API(JSBool)
JS_IsConstructing(JSContext *cx);
/*
* Saving and restoring frame chains.
*
* These two functions are used to set aside cx's call stack while that stack
* is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
* code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
* must be balanced and all nested calls to JS_SaveFrameChain must have had
* matching JS_RestoreFrameChain calls.
*
* JS_SaveFrameChain deals with cx not having any code running on it. A null
* return does not signify an error, and JS_RestoreFrameChain handles a null
* frame pointer argument safely.
*/
extern JS_PUBLIC_API(JSStackFrame *)
JS_SaveFrameChain(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_RestoreFrameChain(JSContext *cx, JSStackFrame *fp);
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Strings.
*
* NB: JS_NewString takes ownership of bytes on success, avoiding a copy; but
* on error (signified by null return), it leaves bytes owned by the caller.
* So the caller must free bytes in the error case, if it has no use for them.
* In contrast, all the JS_New*StringCopy* functions do not take ownership of
* the character memory passed to them -- they copy it.
1998-03-28 02:44:41 +00:00
*/
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewString(JSContext *cx, char *bytes, size_t length);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewStringCopyZ(JSContext *cx, const char *s);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_InternString(JSContext *cx, const char *s);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
extern JS_PUBLIC_API(JSString *)
JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
extern JS_PUBLIC_API(JSString *)
1998-03-28 02:44:41 +00:00
JS_InternUCString(JSContext *cx, const jschar *s);
extern JS_PUBLIC_API(char *)
1998-03-28 02:44:41 +00:00
JS_GetStringBytes(JSString *str);
extern JS_PUBLIC_API(jschar *)
1998-03-28 02:44:41 +00:00
JS_GetStringChars(JSString *str);
extern JS_PUBLIC_API(size_t)
1998-03-28 02:44:41 +00:00
JS_GetStringLength(JSString *str);
extern JS_PUBLIC_API(const char *)
JS_GetStringBytesZ(JSContext *cx, JSString *str);
extern JS_PUBLIC_API(const jschar *)
JS_GetStringCharsZ(JSContext *cx, JSString *str);
extern JS_PUBLIC_API(intN)
1998-03-28 02:44:41 +00:00
JS_CompareStrings(JSString *str1, JSString *str2);
/*
* Mutable string support. A string's characters are never mutable in this JS
* implementation, but a growable string has a buffer that can be reallocated,
* and a dependent string is a substring of another (growable, dependent, or
* immutable) string. The direct data members of the (opaque to API clients)
* JSString struct may be changed in a single-threaded way for growable and
* dependent strings.
*
* Therefore mutable strings cannot be used by more than one thread at a time.
* You may call JS_MakeStringImmutable to convert the string from a mutable
* (growable or dependent) string to an immutable (and therefore thread-safe)
* string. The engine takes care of converting growable and dependent strings
* to immutable for you if you store strings in multi-threaded objects using
* JS_SetProperty or kindred API entry points.
*
* If you store a JSString pointer in a native data structure that is (safely)
* accessible to multiple threads, you must call JS_MakeStringImmutable before
* retiring the store.
*/
extern JS_PUBLIC_API(JSString *)
JS_NewGrowableString(JSContext *cx, jschar *chars, size_t length);
/*
* Create a dependent string, i.e., a string that owns no character storage,
* but that refers to a slice of another string's chars. Dependent strings
* are mutable by definition, so the thread safety comments above apply.
*/
extern JS_PUBLIC_API(JSString *)
JS_NewDependentString(JSContext *cx, JSString *str, size_t start,
size_t length);
/*
* Concatenate two strings, resulting in a new growable string. If you create
* the left string and pass it to JS_ConcatStrings on a single thread, try to
* use JS_NewGrowableString to create the left string -- doing so helps Concat
* avoid allocating a new buffer for the result and copying left's chars into
* the new buffer. See above for thread safety comments.
*/
extern JS_PUBLIC_API(JSString *)
JS_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
/*
2003-02-26 01:29:55 +00:00
* Convert a dependent string into an independent one. This function does not
* change the string's mutability, so the thread safety comments above apply.
*/
extern JS_PUBLIC_API(const jschar *)
JS_UndependString(JSContext *cx, JSString *str);
/*
* Convert a mutable string (either growable or dependent) into an immutable,
* thread-safe one.
*/
extern JS_PUBLIC_API(JSBool)
JS_MakeStringImmutable(JSContext *cx, JSString *str);
/*
* Return JS_TRUE if C (char []) strings passed via the API and internally
* are UTF-8.
*/
JS_PUBLIC_API(JSBool)
JS_CStringsAreUTF8(void);
/*
* Update the value to be returned by JS_CStringsAreUTF8(). Once set, it
* can never be changed. This API must be called before the first call to
* JS_NewRuntime.
*/
JS_PUBLIC_API(void)
JS_SetCStringsAreUTF8(void);
/*
* Character encoding support.
*
* For both JS_EncodeCharacters and JS_DecodeBytes, set *dstlenp to the size
* of the destination buffer before the call; on return, *dstlenp contains the
* number of bytes (JS_EncodeCharacters) or jschars (JS_DecodeBytes) actually
* stored. To determine the necessary destination buffer size, make a sizing
* call that passes NULL for dst.
*
* On errors, the functions report the error. In that case, *dstlenp contains
* the number of characters or bytes transferred so far. If cx is NULL, no
* error is reported on failure, and the functions simply return JS_FALSE.
*
* NB: Neither function stores an additional zero byte or jschar after the
* transcoded string.
*
* If JS_CStringsAreUTF8() is true then JS_EncodeCharacters encodes to
* UTF-8, and JS_DecodeBytes decodes from UTF-8, which may create additional
* errors if the character sequence is malformed. If UTF-8 support is
* disabled, the functions deflate and inflate, respectively.
*/
JS_PUBLIC_API(JSBool)
JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
size_t *dstlenp);
JS_PUBLIC_API(JSBool)
JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst,
size_t *dstlenp);
/*
* A variation on JS_EncodeCharacters where a null terminated string is
* returned that you are expected to call JS_free on when done.
*/
JS_PUBLIC_API(char *)
JS_EncodeString(JSContext *cx, JSString *str);
/************************************************************************/
/*
* JSON functions
*/
typedef JSBool (* JSONWriteCallback)(const jschar *buf, uint32 len, void *data);
/*
* JSON.stringify as specified by ES3.1 (draft)
*/
JS_PUBLIC_API(JSBool)
JS_Stringify(JSContext *cx, jsval *vp, JSObject *replacer, jsval space,
JSONWriteCallback callback, void *data);
/*
* Retrieve a toJSON function. If found, set vp to its result.
*/
JS_PUBLIC_API(JSBool)
JS_TryJSON(JSContext *cx, jsval *vp);
/*
* JSON.parse as specified by ES3.1 (draft)
*/
JS_PUBLIC_API(JSONParser *)
JS_BeginJSONParse(JSContext *cx, jsval *vp);
JS_PUBLIC_API(JSBool)
JS_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len);
JS_PUBLIC_API(JSBool)
JS_FinishJSONParse(JSContext *cx, JSONParser *jp, jsval reviver);
1998-03-28 02:44:41 +00:00
/************************************************************************/
/*
* Locale specific string conversion and error message callbacks.
*/
struct JSLocaleCallbacks {
JSLocaleToUpperCase localeToUpperCase;
JSLocaleToLowerCase localeToLowerCase;
JSLocaleCompare localeCompare;
JSLocaleToUnicode localeToUnicode;
JSErrorCallback localeGetErrorMessage;
};
/*
2000-09-08 21:24:14 +00:00
* Establish locale callbacks. The pointer must persist as long as the
* JSContext. Passing NULL restores the default behaviour.
*/
extern JS_PUBLIC_API(void)
JS_SetLocaleCallbacks(JSContext *cx, JSLocaleCallbacks *callbacks);
2000-09-08 21:24:14 +00:00
/*
* Return the address of the current locale callbacks struct, which may
* be NULL.
*/
extern JS_PUBLIC_API(JSLocaleCallbacks *)
JS_GetLocaleCallbacks(JSContext *cx);
/************************************************************************/
1998-03-28 02:44:41 +00:00
/*
* Error reporting.
*/
/*
* Report an exception represented by the sprintf-like conversion of format
* and its arguments. This exception message string is passed to a pre-set
* JSErrorReporter function (set by JS_SetErrorReporter; see jspubtd.h for
* the JSErrorReporter typedef).
1998-03-28 02:44:41 +00:00
*/
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_ReportError(JSContext *cx, const char *format, ...);
/*
* Use an errorNumber to retrieve the format string, args are char *
*/
extern JS_PUBLIC_API(void)
JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
2000-09-08 21:24:14 +00:00
void *userRef, const uintN errorNumber, ...);
/*
* Use an errorNumber to retrieve the format string, args are jschar *
*/
extern JS_PUBLIC_API(void)
JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
2000-09-08 21:24:14 +00:00
void *userRef, const uintN errorNumber, ...);
/*
* As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
* Return true if there was no error trying to issue the warning, and if the
* warning was not converted into an error due to the JSOPTION_WERROR option
* being set, false otherwise.
*/
extern JS_PUBLIC_API(JSBool)
JS_ReportWarning(JSContext *cx, const char *format, ...);
extern JS_PUBLIC_API(JSBool)
JS_ReportErrorFlagsAndNumber(JSContext *cx, uintN flags,
JSErrorCallback errorCallback, void *userRef,
const uintN errorNumber, ...);
extern JS_PUBLIC_API(JSBool)
JS_ReportErrorFlagsAndNumberUC(JSContext *cx, uintN flags,
JSErrorCallback errorCallback, void *userRef,
const uintN errorNumber, ...);
1998-03-28 02:44:41 +00:00
/*
* Complain when out of memory.
*/
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_ReportOutOfMemory(JSContext *cx);
/*
* Complain when an allocation size overflows the maximum supported limit.
*/
extern JS_PUBLIC_API(void)
JS_ReportAllocationOverflow(JSContext *cx);
1998-03-28 02:44:41 +00:00
struct JSErrorReport {
const char *filename; /* source file name, URL, etc., or null */
uintN lineno; /* source line number */
const char *linebuf; /* offending source line without final \n */
const char *tokenptr; /* pointer to error token in linebuf */
const jschar *uclinebuf; /* unicode (original) line buffer */
const jschar *uctokenptr; /* unicode (original) token pointer */
2000-09-08 21:24:14 +00:00
uintN flags; /* error/warning, etc. */
uintN errorNumber; /* the error number, e.g. see js.msg */
const jschar *ucmessage; /* the (default) error message */
const jschar **messageArgs; /* arguments for the error message */
1998-03-28 02:44:41 +00:00
};
/*
* JSErrorReport flag values. These may be freely composed.
*/
#define JSREPORT_ERROR 0x0 /* pseudo-flag for default case */
#define JSREPORT_WARNING 0x1 /* reported via JS_ReportWarning */
#define JSREPORT_EXCEPTION 0x2 /* exception was thrown */
#define JSREPORT_STRICT 0x4 /* error or warning due to strict option */
/*
* This condition is an error in strict mode code, a warning if
* JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
* all. We check the strictness of the context's top frame's script;
* where that isn't appropriate, the caller should do the right checks
* itself instead of using this flag.
*/
#define JSREPORT_STRICT_MODE_ERROR 0x8
/*
* If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
* has been thrown for this runtime error, and the host should ignore it.
* Exception-aware hosts should also check for JS_IsExceptionPending if
* JS_ExecuteScript returns failure, and signal or propagate the exception, as
* appropriate.
*/
#define JSREPORT_IS_WARNING(flags) (((flags) & JSREPORT_WARNING) != 0)
#define JSREPORT_IS_EXCEPTION(flags) (((flags) & JSREPORT_EXCEPTION) != 0)
#define JSREPORT_IS_STRICT(flags) (((flags) & JSREPORT_STRICT) != 0)
#define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) & \
JSREPORT_STRICT_MODE_ERROR) != 0)
extern JS_PUBLIC_API(JSErrorReporter)
1998-03-28 02:44:41 +00:00
JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
/************************************************************************/
/*
* Regular Expressions.
*/
#define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
#define JSREG_GLOB 0x02 /* global exec, creates array of matches */
#define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
#define JSREG_STICKY 0x08 /* only match starting at lastIndex */
#define JSREG_FLAT 0x10 /* parse as a flat regexp */
#define JSREG_NOCOMPILE 0x20 /* do not try to compile to native code */
1998-03-28 02:44:41 +00:00
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);
extern JS_PUBLIC_API(JSObject *)
1998-03-28 02:44:41 +00:00
JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_ClearRegExpStatics(JSContext *cx);
extern JS_PUBLIC_API(void)
1998-03-28 02:44:41 +00:00
JS_ClearRegExpRoots(JSContext *cx);
/* TODO: compile, exec, get/set other statics... */
1998-03-28 02:44:41 +00:00
/************************************************************************/
extern JS_PUBLIC_API(JSBool)
JS_IsExceptionPending(JSContext *cx);
extern JS_PUBLIC_API(JSBool)
JS_GetPendingException(JSContext *cx, jsval *vp);
extern JS_PUBLIC_API(void)
JS_SetPendingException(JSContext *cx, jsval v);
extern JS_PUBLIC_API(void)
JS_ClearPendingException(JSContext *cx);
extern JS_PUBLIC_API(JSBool)
JS_ReportPendingException(JSContext *cx);
/*
* Save the current exception state. This takes a snapshot of cx's current
* exception state without making any change to that state.
*
* The returned state pointer MUST be passed later to JS_RestoreExceptionState
* (to restore that saved state, overriding any more recent state) or else to
* JS_DropExceptionState (to free the state struct in case it is not correct
* or desirable to restore it). Both Restore and Drop free the state struct,
* so callers must stop using the pointer returned from Save after calling the
* Release or Drop API.
*/
extern JS_PUBLIC_API(JSExceptionState *)
JS_SaveExceptionState(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
extern JS_PUBLIC_API(void)
JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
/*
* If the given value is an exception object that originated from an error,
* the exception will contain an error report struct, and this API will return
* the address of that struct. Otherwise, it returns NULL. The lifetime of
* the error report struct that might be returned is the same as the lifetime
* of the exception object.
*/
extern JS_PUBLIC_API(JSErrorReport *)
JS_ErrorFromException(JSContext *cx, jsval v);
/*
* Given a reported error's message and JSErrorReport struct pointer, throw
* the corresponding exception on cx.
*/
extern JS_PUBLIC_API(JSBool)
JS_ThrowReportedError(JSContext *cx, const char *message,
JSErrorReport *reportp);
/*
* Throws a StopIteration exception on cx.
*/
extern JS_PUBLIC_API(JSBool)
JS_ThrowStopIteration(JSContext *cx);
1998-03-28 02:44:41 +00:00
/*
* Associate the current thread with the given context. This is done
* implicitly by JS_NewContext.
*
* Returns the old thread id for this context, which should be treated as
* an opaque value. This value is provided for comparison to 0, which
* indicates that ClearContextThread has been called on this context
* since the last SetContextThread, or non-0, which indicates the opposite.
1998-03-28 02:44:41 +00:00
*/
extern JS_PUBLIC_API(jsword)
JS_GetContextThread(JSContext *cx);
extern JS_PUBLIC_API(jsword)
JS_SetContextThread(JSContext *cx);
extern JS_PUBLIC_API(jsword)
JS_ClearContextThread(JSContext *cx);
2000-09-08 21:24:14 +00:00
/************************************************************************/
#ifdef DEBUG
#define JS_GC_ZEAL 1
#endif
#ifdef JS_GC_ZEAL
extern JS_PUBLIC_API(void)
JS_SetGCZeal(JSContext *cx, uint8 zeal);
#endif
JS_END_EXTERN_C
1998-03-28 02:44:41 +00:00
#endif /* jsapi_h___ */