Skip to content

N-API: unable to perform on completion callback through napi_env #1856

@Aerrus-zz

Description

@Aerrus-zz
  • Node.js Version: 11.11.0
  • OS: Mac
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant): node-addon-api 1.6.2

Seen a few threads about the why napi_get_current_env() was removed. Seen the rationale behind it and how the api will simply be designed to revolve around it.

However, it doesn't seem like it's possible to pass in napi_env to a C++ lambda that fires after a long completing task. Use case is that I need to call the long task and make a callback to a JS function that updates the UI accordingly.

napi_value somefunc(napi_env env, napi_callback_info cbinfo) {
    someLongFunc("yay", [env](int num) {
            napi_value test;
            napi_status s = napi_create_string_utf8(env, "moretest", NAPI_AUTO_LENGTH, &test);
            //check status, make callback, etc.
        });
    return nullptr;
}

Running this gives EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on the create string line, which as far as I can tell, can be caused by bad access to napi_env.

I have considered the napi async work way, but I cannot use that since my long running function has to be on the UI thread and async work creates a new thread to run the execute and complete functions from.

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