JS_ValueToFunction 编辑
Convert a JS::Value
to a JSFunction
.
Syntax
JSFunction *
JS_ValueToFunction(JSContext *cx, JS::HandleValue v);
JSFunction *
JS_ValueToConstructor(JSContext *cx, JS::HandleValue v);
Name | Type | Description |
---|---|---|
cx | JSContext * | The context in which to perform the conversion. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext . |
v | JS::HandleValue | The value to convert. |
Description
JS_ValueToFunction
converts a specified JavaScript value, v
, to a function.
JS_ValueToConstructor
is the exactly same function as 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.
- If
v
is aFunction
object, this returns the associatedJSFunction
. - If
v
isnull
,undefined
, a boolean, a number, or a string, aTypeError
is reported andJS_ValueToFunction
returnsNULL
. - Otherwise,
v
is an object. The JavaScript engine attempts to convert it to a function. (Implementation note: The actual conversion is performed by the object itself. The object'sJSObjectOps.defaultValue
method is called withhint=JSTYPE_FUNCTION
.)JS_ValueToFunction
returns a pointer to the converted function. If conversion fails with an error or exception,JS_ValueToFunction
returnsNULL
.
See Also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论