📄 jsapi.h
字号:
/*
* 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 indepenent 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_ClearRegExpStatics(JSContext *cx);
extern JS_PUBLIC_API(void)
JS_ClearRegExpRoots(JSContext *cx);
/* TODO: compile, exec, get/set other statics... */
/************************************************************************/
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);
/*
* Save the current exception state. This takes a snapshot of the current
* exception state without making any change to that state.
*
* The returned object MUST be later passed to either JS_RestoreExceptionState
* (to restore that saved state) or JS_DropExceptionState (to cleanup the state
* object in case it is not desireable to restore to that state). Both
* JS_RestoreExceptionState and JS_DropExceptionState will destroy the
* JSExceptionState object -- so that object can not be referenced again
* after making either of those calls.
*/
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 jsval is an engine exception with an attached error report
* then return a pointer to that report. Else, return NULL.
* The lifetime of the error report that might be returned is linked to the
* lifetime of the exception.
*/
extern JS_PUBLIC_API(JSErrorReport *)
JS_ErrorFromException(JSContext *cx, jsval v);
#ifdef JS_THREADSAFE
/*
* 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.
*/
extern JS_PUBLIC_API(intN)
JS_GetContextThread(JSContext *cx);
extern JS_PUBLIC_API(intN)
JS_SetContextThread(JSContext *cx);
extern JS_PUBLIC_API(intN)
JS_ClearContextThread(JSContext *cx);
#endif /* JS_THREADSAFE */
/************************************************************************/
JS_END_EXTERN_C
#endif /* jsapi_h___ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -