Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken napi_call_function example in N-API documentation #39564

Closed
Silur opened this issue Jul 28, 2021 · 4 comments
Closed

Broken napi_call_function example in N-API documentation #39564

Silur opened this issue Jul 28, 2021 · 4 comments
Labels
doc Issues and PRs related to the documentations. node-api Issues and PRs related to the Node-API.

Comments

@Silur
Copy link

Silur commented Jul 28, 2021

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

@legendecas legendecas added node-api Issues and PRs related to the Node-API. doc Issues and PRs related to the documentations. labels Jul 29, 2021
@mhdawson
Copy link
Member

I'll try to take a look at this next week.

@deepakrkris
Copy link

deepakrkris commented Dec 21, 2021

@Silur this line NAPI_CALL(env, napi_call_function(env, global, add_two, argc, argv, &return_val)); is throwing invalid argument exception because the referenced function AddTwo is not in the global scope. if you add the following line global.AddTwo = AddTwo as below, you can avoid this error

// index.js
const addon = require('./build/Release/module');
function AddTwo(num) {
    return num + 2;
}
global.AddTwo = AddTwo
addon.myFunc();

mhdawson added a commit to mhdawson/io.js that referenced this issue Dec 21, 2021
Fixes: nodejs#39564

Signed-off-by: Michael Dawson <mdawson@devrus.com>
@mhdawson
Copy link
Member

Sorry I missed getting back to this. @deepakrkris thanks for taking a look. Can you confirm that what I have in #41264 is what you suggested?

@deepakrkris
Copy link

@mhdawson yes the changes in the PR is what I suggested

targos pushed a commit that referenced this issue Jan 14, 2022
Fixes: #39564

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: #41264
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
danielleadams pushed a commit that referenced this issue Jan 31, 2022
Fixes: #39564

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: #41264
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
Linkgoron pushed a commit to Linkgoron/node that referenced this issue Jan 31, 2022
Fixes: nodejs#39564

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: nodejs#41264
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
danielleadams pushed a commit that referenced this issue Feb 1, 2022
Fixes: #39564

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: #41264
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants