MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
Your Search Results

    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.

    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.

    See Also

    Document Tags and Contributors

    Contributors to this page: MMondor, Dria, fscholz, Jorend, arai
    Last updated by: arai,
    Hide Sidebar