GithubHelp home page GithubHelp logo

Comments (8)

mhdawson avatar mhdawson commented on May 15, 2024

I don't quite understand it's overwrite exports, are you saying that having called napi_set_named_property with "getTemp2" that removes the "getTemp" value set in the first call to napi_set_named_property?

Looking at the implementation of napi_set_named_property I don't see how that could happen.

from abi-stable-node-addon-examples.

khvalov avatar khvalov commented on May 15, 2024

@mhdawson , thanks for reply.
>napi_set_named_property with "getTemp2" that removes the "getTemp" value set in the first call to napi_set_named_property?
Correct

So my initial question is - could you please expose your sample with assigning 2 named properties

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 15, 2024

if it is removing the getTemp2 when getTemp is set then there is a bug that will need to be investigated.

from abi-stable-node-addon-examples.

gniezen avatar gniezen commented on May 15, 2024

Is it maybe because you're using the variable fn for both functions? Does it work if you replace the first fn with fn1 and the second fn with fn2?

from abi-stable-node-addon-examples.

andrew-cc-chen avatar andrew-cc-chen commented on May 15, 2024

@khvalov I was unable to recreate the error you encountered using the following code. The export contained both functions, instead of replacing the first function with the second like you mentioned.

napi_value Method(napi_env env, napi_callback_info args) {
  napi_value greeting;
  napi_status status;

  status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &greeting);
  if (status != napi_ok) return nullptr;
  return greeting;
}

napi_value Method2(napi_env env, napi_callback_info args) {
  napi_value greeting;
  napi_status status;

  status = napi_create_string_utf8(env, "hello2", NAPI_AUTO_LENGTH, &greeting);
  if (status != napi_ok) return nullptr;
  return greeting;
}

napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;

    status = napi_create_function(env, NULL, 0, Method, NULL, &fn);
  if (status != napi_ok) {
    napi_throw_error(env, NULL, "Unable to wrap native function");
  }

  status = napi_set_named_property(env, exports, "hello", fn);
  if (status != napi_ok) {
    napi_throw_error(env, NULL, "Unable to populate exports");
  }

  status = napi_create_function(env, NULL, 0, Method2, NULL, &fn);
  if (status != napi_ok) {
    napi_throw_error(env, NULL, "Unable to wrap native function");
  }

  status = napi_set_named_property(env, exports, "hello2", fn);
  if (status != napi_ok) {
    napi_throw_error(env, NULL, "Unable to populate exports");
  }

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

https://github.com/nodejs/node/tree/master/test/addons-napi/3_callbacks provides another example of exporting two functions in a similar implementation, and it works correctly.

from abi-stable-node-addon-examples.

xthdraft avatar xthdraft commented on May 15, 2024

Andrew Chen's example works fine for me after adding the required header #include <node_api.h> and changing nullptr to NULL.

After building with node-gyp configure build, I used an extra build instruction for my version of electron:
node-gyp rebuild --target=3.0.6 --arch=x64 --dist-url=https://atom.io/download/electron

This code could well be decorated with comment lines and put in the docs, as it is a very useful example.

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 15, 2024

@khvalov we don't seem to be able to recreate the problem. Unless you think otherwise I think this can be close

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 15, 2024

Going to close @khvalov if you still see the issue please open an issue in https://github.com/nodejs/node-addon-examples where future work on the examples is being done.

from abi-stable-node-addon-examples.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.