JS_ConvertArguments 编辑

Obsolete since JSAPI 38
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Converts a series of JS values, passed in an argument array, to their corresponding JS types.

Syntax

bool
JS_ConvertArguments(JSContext *cx, const JS::CallArgs &args,
                    const char *format, ...); // Added in SpiderMonkey 31

bool
JS_ConvertArguments(JSContext *cx, unsigned argc, jsval *argv,
                    const char *format, ...); // Obsolete since JSAPI 30
NameTypeDescription
cxJSContext *The context in which to perform any necessary conversions.

cx also affects the interpretation of format, if JS_AddArgumentFormatter has been called.

Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
argsconst JS::CallArgs &

Reference to the arguments to convert. Added in SpiderMonkey 31

argcunsignedThe number of arguments to convert. Obsolete since JSAPI 30
argvjsval *

Pointer to the vector of arguments to convert. Obsolete since JSAPI 30

This must be an argv pointer created by the JS engine and passed to a JSNative or JSFastNative callback, not an array created by application code. In certain error cases, JS_ConvertArguments calls JS_ARGV_CALLEE(argv), which accesses memory outside the range [argv .. argv+argc).

JS_ConvertArguments may modify argv in place by replacing an argument with the converted value. (The purpose is to ensure GC safety.)

formatconst char *Null-terminated string describing the types of the out parameters and how to convert the values in argv.
...void *

Out parameters. Pointers to variables into which to store the converted values. There must be one pointer for each parameter described in format.

Variables for optional parameters must already be initialized, because if an optional parameter is not in argv, JS_ConvertArguments does not modify the corresponding variable.

The variables do not need to be rooted. (If conversion creates a new GC thing, the corresponding jsval is written back to argv, which is rooted.)

Description

JS_ConvertArguments provides a convenient way to translate a series of JS values into their corresponding JS types with a single function call. It saves you from having to write separate tests and elaborate if...else statements in your function code to retrieve and translate multiple JS values for use with your own functions.

cx is the context for the call. argc indicates the number of JS values you are passing in for conversion. argv is a pointer to the array of JS values to convert.

format is a null-terminated C string. Each element of the array indicates the C type into which to convert the next available JS value. format can contain one or more instances of the following characters, as appropriate:

CharacterC TypeDescription
bboolBoolean
cuint16_tECMA uint16_t, Unicode character
iint32_tECMA int32_t
jint32_tECMA int32_t (used to be different, behaves like i now) Obsolete since JSAPI 28
uuint32_tECMA uint32_t
ddoubleIEEE double
IdoubleIntegral IEEE double
schar * (C string) bug 607292
SJSString *Unicode string, accessed by a JSString pointer
Wchar16_t *Unicode character vector, 0-terminated (W for wide)
oJSObject *Object reference
fJSFunction *

The argument is converted to a function as though by a call to JS_ValueToFunction.

This conversion is dangerous and almost entirely useless, because the resulting JSFunction is not a real function object and therefore cannot be safely passed to any other JSAPI function. That includes JS_CallFunction() and JS_GetFunctionObject(). A JSFunction represents only the compiled code and not the environment of the function. Unless the function happens to be a native function, this means it isn't attached to any global or enclosing scope, and therefore must not be treated like a real function. Instead, use JSVAL_IS_OBJECT and JS_ObjectIsFunction() to check whether a value is already a function, or use JS_ConvertValue() to convert a value to JSTYPE_FUNCTION safely.

vjsvalArgument value (no conversion)
*N/ANone. If an asterisk (*) is present in format, it tells the conversion routine to skip converting the current argument.
/N/ANone. Arguments after the slash are optional. If an optional argument is missing from argv, JS_ConvertArguments neither assigns anything to any variable nor reports an error.

This function also takes in consideration any additional custom types defined on cx using JS_AddArgumentFormatter.

For example, if format is "bIob", then JS_ConvertArguments converts the first JS value in argv into a bool, the second value into a double, the third value into a JSObject *, and the last value into a bool.

To skip a given argument, pass an asterisk in the corresponding position in format.

If argc is less than the number of arguments required by format, JS_ConvertArgument reports an error and returns false. If argc is greater than the number of arguments described in format, the extraneous arguments are ignored.

On success, JS_ConvertArgument returns true. If any argument conversion fails or there are not enough arguments, it returns false.

To perform the opposite conversion, converting values of various C types to an array of jsvals, use JS_PushArguments.

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:147 次

字数:10779

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文