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
Name | Type | Description |
---|---|---|
cx | JSContext * | The context in which to perform any necessary conversions.
JS_THREADSAFE build, the caller must be in a request on this JSContext . |
args | const JS::CallArgs & | Reference to the arguments to convert. Added in SpiderMonkey 31 |
argc | unsigned | The number of arguments to convert. Obsolete since JSAPI 30 |
argv | jsval * | Pointer to the vector of arguments to convert. Obsolete since JSAPI 30 This must be an
|
format | const 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 Variables for optional parameters must already be initialized, because if an optional parameter is not in 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:
Character | C Type | Description |
---|---|---|
b | bool | Boolean |
c | uint16_t | ECMA uint16_t, Unicode character |
i | int32_t | ECMA int32_t |
j | int32_t | ECMA int32_t (used to be different, behaves like i now) Obsolete since JSAPI 28 |
u | uint32_t | ECMA uint32_t |
d | double | IEEE double |
I | double | Integral IEEE double |
s | char * (C string) | bug 607292 |
S | JSString * | Unicode string, accessed by a JSString pointer |
W | char16_t * | Unicode character vector, 0-terminated (W for wide) |
o | JSObject * | Object reference |
f | JSFunction * | The argument is converted to a function as though by a call to This conversion is dangerous and almost entirely useless, because the resulting |
v | jsval | Argument value (no conversion) |
* | N/A | None. If an asterisk (*) is present in format, it tells the conversion routine to skip converting the current argument. |
/ | N/A | None. 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 jsval
s, use JS_PushArguments
.
See Also
- MXR ID Search for
JS_ConvertArguments
JS_ConvertArgumentsVA
- bug 607292 - obsolete
s
format - bug 933946 - obsolete
j
format - bug 969798 - added
args
- bug 1125784
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论