📄 jsapi.h
字号:
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * 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. * * 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 ***** */#ifndef jsapi_h___#define jsapi_h___/* * JavaScript API. */#include <stddef.h>#include <stdio.h>#include "jspubtd.h"JS_BEGIN_EXTERN_C/* * Type tags stored in the low bits of a jsval. */#define JSVAL_OBJECT 0x0 /* untagged reference to object */#define JSVAL_INT 0x1 /* tagged 31-bit integer value */#define JSVAL_DOUBLE 0x2 /* tagged reference to double */#define JSVAL_STRING 0x4 /* tagged reference to string */#define JSVAL_BOOLEAN 0x6 /* tagged boolean value *//* Type tag bitfield length and derived macros. */#define JSVAL_TAGBITS 3#define JSVAL_TAGMASK JS_BITMASK(JSVAL_TAGBITS)#define JSVAL_TAG(v) ((v) & JSVAL_TAGMASK)#define JSVAL_SETTAG(v,t) ((v) | (t))#define JSVAL_CLRTAG(v) ((v) & ~(jsval)JSVAL_TAGMASK)#define JSVAL_ALIGN JS_BIT(JSVAL_TAGBITS)/* Predicates for type testing. */#define JSVAL_IS_OBJECT(v) (JSVAL_TAG(v) == JSVAL_OBJECT)#define JSVAL_IS_NUMBER(v) (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))#define JSVAL_IS_INT(v) (((v) & JSVAL_INT) && (v) != JSVAL_VOID)#define JSVAL_IS_DOUBLE(v) (JSVAL_TAG(v) == JSVAL_DOUBLE)#define JSVAL_IS_STRING(v) (JSVAL_TAG(v) == JSVAL_STRING)#define JSVAL_IS_BOOLEAN(v) (JSVAL_TAG(v) == JSVAL_BOOLEAN)#define JSVAL_IS_NULL(v) ((v) == JSVAL_NULL)#define JSVAL_IS_VOID(v) ((v) == JSVAL_VOID)#define JSVAL_IS_PRIMITIVE(v) (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))/* Objects, strings, and doubles are GC'ed. */#define JSVAL_IS_GCTHING(v) (!((v) & JSVAL_INT) && !JSVAL_IS_BOOLEAN(v))#define JSVAL_TO_GCTHING(v) ((void *)JSVAL_CLRTAG(v))#define JSVAL_TO_OBJECT(v) ((JSObject *)JSVAL_TO_GCTHING(v))#define JSVAL_TO_DOUBLE(v) ((jsdouble *)JSVAL_TO_GCTHING(v))#define JSVAL_TO_STRING(v) ((JSString *)JSVAL_TO_GCTHING(v))#define OBJECT_TO_JSVAL(obj) ((jsval)(obj))#define DOUBLE_TO_JSVAL(dp) JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)#define STRING_TO_JSVAL(str) JSVAL_SETTAG((jsval)(str), JSVAL_STRING)/* Lock and unlock the GC thing held by a jsval. */#define JSVAL_LOCK(cx,v) (JSVAL_IS_GCTHING(v) \ ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v)) \ : JS_TRUE)#define JSVAL_UNLOCK(cx,v) (JSVAL_IS_GCTHING(v) \ ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v)) \ : JS_TRUE)/* Domain limits for the jsval int type. */#define JSVAL_INT_BITS 31#define JSVAL_INT_POW2(n) ((jsval)1 << (n))#define JSVAL_INT_MIN ((jsval)1 - JSVAL_INT_POW2(30))#define JSVAL_INT_MAX (JSVAL_INT_POW2(30) - 1)#define INT_FITS_IN_JSVAL(i) ((jsuint)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)#define JSVAL_TO_INT(v) ((jsint)(v) >> 1)#define INT_TO_JSVAL(i) (((jsval)(i) << 1) | JSVAL_INT)/* Convert between boolean and jsval. */#define JSVAL_TO_BOOLEAN(v) ((JSBool)((v) >> JSVAL_TAGBITS))#define BOOLEAN_TO_JSVAL(b) JSVAL_SETTAG((jsval)(b) << JSVAL_TAGBITS, \ JSVAL_BOOLEAN)/* 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)(p) | JSVAL_INT)/* Property attributes, set in JSPropertySpec and passed to API functions. */#define JSPROP_ENUMERATE 0x01 /* property is visible to for/in loop */#define JSPROP_READONLY 0x02 /* not settable: assignment is no-op */#define JSPROP_PERMANENT 0x04 /* property cannot be deleted */#define JSPROP_EXPORTED 0x08 /* property is exported from object */#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 */#define JSPROP_INDEX 0x80 /* name is actually (jsint) index *//* Function flags, set in JSFunctionSpec and passed to JS_NewFunction etc. */#define JSFUN_LAMBDA 0x08 /* expressed, not declared, function */#define JSFUN_GETTER JSPROP_GETTER#define JSFUN_SETTER JSPROP_SETTER#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))#ifdef MOZILLA_1_8_BRANCH/* * Squeeze three more bits into existing 8-bit flags by taking advantage of * the invalid combination (JSFUN_GETTER | JSFUN_SETTER). */#define JSFUN_GETTER_TEST(f) (JSFUN_GSFLAGS(f) == JSFUN_GETTER)#define JSFUN_SETTER_TEST(f) (JSFUN_GSFLAGS(f) == JSFUN_SETTER)#define JSFUN_FLAGS_TEST(f,t) (JSFUN_GSFLAGS(~(f)) ? (f) & (t) : 0)#define JSFUN_BOUND_METHOD_TEST(f) JSFUN_FLAGS_TEST(f, JSFUN_BOUND_METHOD)#define JSFUN_HEAVYWEIGHT_TEST(f) JSFUN_FLAGS_TEST(f, JSFUN_HEAVYWEIGHT)#define JSFUN_GSFLAG2ATTR(f) (JSFUN_GETTER_TEST(f) ? JSPROP_GETTER : \ JSFUN_SETTER_TEST(f) ? JSPROP_SETTER : 0)#define JSFUN_THISP_FLAGS(f) (JSFUN_GSFLAGS(~(f)) ? 0 : \ (f) & JSFUN_THISP_PRIMITIVE)#define JSFUN_THISP_TEST(f,t) ((f) == (t) || (f) == JSFUN_THISP_PRIMITIVE)#define JSFUN_THISP_STRING 0x30 /* |this| may be a primitive string */#define JSFUN_THISP_NUMBER 0x70 /* |this| may be a primitive number */#define JSFUN_THISP_BOOLEAN 0xb0 /* |this| may be a primitive boolean */#define JSFUN_THISP_PRIMITIVE 0xf0 /* |this| may be any primitive value */#define JSFUN_FLAGS_MASK 0xf8 /* overlay JSFUN_* attributes */#else#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_FLAGS_MASK 0x07f8 /* overlay JSFUN_* attributes -- note that bit #15 is used internally to flag interpreted functions */#endif/* * 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/* * Well-known JS values. The extern'd variables are initialized when the * first JSContext is created by JS_NewContext (see below). */#define JSVAL_VOID INT_TO_JSVAL(0 - JSVAL_INT_POW2(30))#define JSVAL_NULL OBJECT_TO_JSVAL(0)#define JSVAL_ZERO INT_TO_JSVAL(0)#define JSVAL_ONE INT_TO_JSVAL(1)#define JSVAL_FALSE BOOLEAN_TO_JSVAL(JS_FALSE)#define JSVAL_TRUE BOOLEAN_TO_JSVAL(JS_TRUE)/* * 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();/* Don't want to export data, so provide accessors for non-inline jsvals. */extern JS_PUBLIC_API(jsval)JS_GetNaNValue(JSContext *cx);extern JS_PUBLIC_API(jsval)JS_GetNegativeInfinityValue(JSContext *cx);extern JS_PUBLIC_API(jsval)JS_GetPositiveInfinityValue(JSContext *cx);extern JS_PUBLIC_API(jsval)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, ...);#ifdef va_startextern JS_PUBLIC_API(JSBool)JS_ConvertArgumentsVA(JSContext *cx, uintN argc, jsval *argv, const char *format, va_list ap);#endif/* * Inverse of JS_ConvertArguments: scan format and convert trailing arguments * into jsvals, GC-rooted if necessary by the JS stack. Return null on error, * and a pointer to the new argument vector on success. Also return a stack * mark on success via *markp, in which case the caller must eventually clean * up by calling JS_PopArguments. * * Note that the number of actual arguments supplied is specified exclusively * by format, so there is no argc parameter. */extern JS_PUBLIC_API(jsval *)JS_PushArguments(JSContext *cx, void **markp, const char *format, ...);#ifdef va_startextern JS_PUBLIC_API(jsval *)JS_PushArgumentsVA(JSContext *cx, void **markp, const char *format, va_list ap);#endifextern JS_PUBLIC_API(void)JS_PopArguments(JSContext *cx, void *mark);#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, JSArgumentFormatter formatter);extern JS_PUBLIC_API(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -