Skip to content

Broken napi_call_function example in N-API documentation  #39564

Closed
@Silur

Description

@Silur

Version

16.5.0

Platform

x64 arch linux 5.13.4-arch1-1

Subsystem

No response

What steps will reproduce the bug?

// index.js
const addon = require('./build/Debug/module');
function AddTwo(num) {
    return num + 2;
}
addon.myFunc();
// binding.gyp
{
  "targets": [{
    "target_name": "module",
      "cflags": ["-O0", "-g3"],
      "sources": [ "./addon.c" ]
  }]
}
#include <node_api.h>
#include <stdint.h>
#define NAPI_CALL(env, call)                                      \
  do {                                                            \
    napi_status status = (call);                                  \
    if (status != napi_ok) {                                      \
      const napi_extended_error_info* error_info = NULL;          \
      napi_get_last_error_info((env), &error_info);               \
      bool is_pending;                                            \
      napi_is_exception_pending((env), &is_pending);              \
      if (!is_pending) {                                          \
        const char* message = (error_info->error_message == NULL) \
            ? "empty error message"                               \
            : error_info->error_message;                          \
        napi_throw_error((env), NULL, message);                   \
      }                                                           \
    }                                                             \
  } while(0)
  
static napi_value
myFunc(napi_env env, napi_callback_info info)
{

  napi_value global, add_two;
  NAPI_CALL(env, napi_get_global(env, &global));

  NAPI_CALL(env, napi_get_named_property(env, global, "AddTwo", &add_two));

  napi_value argv[1];
  NAPI_CALL(env, napi_create_int32(env, 1337, &argv[0]));
  size_t argc = 1;

  // AddTwo(arg);
  napi_value return_val;
  // FIXME - invalid argument happens here
  NAPI_CALL(env, napi_call_function(env, global, add_two, argc, argv, &return_val));

  // Convert the result back to a native type
  int32_t result;
  NAPI_CALL(env, napi_get_value_int32(env, return_val, &result));

}
napi_value create_addon(napi_env env) {
  napi_value result;
  NAPI_CALL(env, napi_create_object(env, &result));

  napi_value myFunct_exported;

  NAPI_CALL(env, napi_create_function(env,
                                      "myFunc",
                                      NAPI_AUTO_LENGTH,
                                      myFunc,
                                      NULL,
                                      &myFunc_exported));

  NAPI_CALL(env, napi_set_named_property(env,
                                         result,
                                         "myFunc",
                                         myFunc_exported));

  return result;
}


NAPI_MODULE_INIT() {
  return create_addon(env);
}
       

How often does it reproduce? Is there a required condition?

every time

What is the expected behavior?

example on https://nodejs.org/api/n-api.html#n_api_napi_call_function should work out of the box

What do you see instead?

Error: Invalid argument

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.node-apiIssues and PRs related to the Node-API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions