We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

JS ValueToFunction

Convert a jsval to a JSFunction.


JSFunction * JS_ValueToFunction(JSContext *cx, jsval 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 jsval The value to convert.


JS_ValueToFunction converts a specified JavaScript value, v, to a function.

Warning: 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 a Function object, this returns the associated JSFunction.
  • If v is null, undefined, a boolean, a number, or a string, a TypeError is reported and JS_ValueToFunction returns NULL.
  • 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's JSObjectOps.defaultValue method is called with hint=JSTYPE_FUNCTION.) JS_ValueToFunction returns a pointer to the converted function. If conversion fails with an error or exception, JS_ValueToFunction returns NULL.

MXR ID Search for JS_ValueToFunction

Document Tags and Contributors

Last updated by: Jorend,