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.

See Also