Skip to content

[Question] how to set the name of functions created by JS_NewCFunctionData #1190

@neko-para

Description

@neko-para
JS_EXTERN JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
                                   const char *name, // <-- this one
                                   int length, JSCFunctionEnum cproto, int magic);
...
JS_EXTERN JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
                                      int length, int magic, int data_len,
                                      JSValueConst *data);

It seems that other variants all accept a name argument, while this one doesn't.

static void js_function_set_properties(JSContext *ctx, JSValue func_obj,
                                       JSAtom name, int len)
{
    /* ES6 feature non compatible with ES5.1: length is configurable */
    JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_length, js_int32(len),
                           JS_PROP_CONFIGURABLE);
    JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_name, // <-- this one
                           JS_AtomToString(ctx, name), JS_PROP_CONFIGURABLE);
}

I believe it could be done by inspecting the value of JS_ATOM_name and defining it directly, but it isn't portable though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions