📄 jsapi.h
字号:
* 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)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)JS_ExecuteScriptPart(JSContext *cx, JSObject *obj, JSScript *script, JSExecPart part, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_EvaluateScript(JSContext *cx, JSObject *obj, const char *bytes, uintN length, const char *filename, uintN lineno, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj, JSPrincipals *principals, const char *bytes, uintN length, const char *filename, uintN lineno, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_EvaluateUCScript(JSContext *cx, JSObject *obj, const jschar *chars, uintN length, const char *filename, uintN lineno, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj, JSPrincipals *principals, const jschar *chars, uintN length, const char *filename, uintN lineno, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc, jsval *argv, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, uintN argc, jsval *argv, jsval *rval);extern JS_PUBLIC_API(JSBool)JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc, jsval *argv, jsval *rval);extern JS_PUBLIC_API(JSBranchCallback)JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);extern JS_PUBLIC_API(JSBool)JS_IsRunning(JSContext *cx);extern JS_PUBLIC_API(JSBool)JS_IsConstructing(JSContext *cx);/* * Returns true if a script is executing and its current bytecode is a set * (assignment) operation, even if there are native (no script) stack frames * between the script and the caller to JS_IsAssigning. */extern JS_FRIEND_API(JSBool)JS_IsAssigning(JSContext *cx);/* * Set the second return value, which should be a string or int jsval that * identifies a property in the returned object, to form an ECMA reference * type value (obj, id). Only native methods can return reference types, * and if the returned value is used on the left-hand side of an assignment * op, the identified property will be set. If the return value is in an * r-value, the interpreter just gets obj[id]'s value. */extern JS_PUBLIC_API(void)JS_SetCallReturnValue2(JSContext *cx, jsval v);/************************************************************************//* * 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. */extern JS_PUBLIC_API(JSString *)JS_NewString(JSContext *cx, char *bytes, size_t length);extern JS_PUBLIC_API(JSString *)JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);extern JS_PUBLIC_API(JSString *)JS_NewStringCopyZ(JSContext *cx, const char *s);extern JS_PUBLIC_API(JSString *)JS_InternString(JSContext *cx, const char *s);extern JS_PUBLIC_API(JSString *)JS_NewUCString(JSContext *cx, jschar *chars, size_t length);extern JS_PUBLIC_API(JSString *)JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);extern JS_PUBLIC_API(JSString *)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 *)JS_InternUCString(JSContext *cx, const jschar *s);extern JS_PUBLIC_API(char *)JS_GetStringBytes(JSString *str);extern JS_PUBLIC_API(jschar *)JS_GetStringChars(JSString *str);extern JS_PUBLIC_API(size_t)JS_GetStringLength(JSString *str);extern JS_PUBLIC_API(intN)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);/* * 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);/************************************************************************//* * Locale specific string conversion callback. */struct JSLocaleCallbacks { JSLocaleToUpperCase localeToUpperCase; JSLocaleToLowerCase localeToLowerCase; JSLocaleCompare localeCompare;};/* * 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);/* * Return the address of the current locale callbacks struct, which may * be NULL. */extern JS_PUBLIC_API(JSLocaleCallbacks *)JS_GetLocaleCallbacks(JSContext *cx);/************************************************************************//* * 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). */extern JS_PUBLIC_API(void)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, 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, 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, ...);/* * Complain when out of memory. */extern JS_PUBLIC_API(void)JS_ReportOutOfMemory(JSContext *cx);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 */ 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 */};/* * 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 *//* * 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)extern JS_PUBLIC_API(JSErrorReporter)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 */extern JS_PUBLIC_API(JSObject *)JS_NewRegExpObject(JSContext *cx, char *bytes, size_t length, uintN flags);extern JS_PUBLIC_API(JSObject *)JS_NewUCRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);extern JS_PUBLIC_API(void)JS_SetRegExpInput(JSContext *cx, JSString *input, JSBool multiline);extern JS_PUBLIC_API(void)JS_Cle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -