Comments (8)
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.
@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.
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.
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.
@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.
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.
@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.
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)
- Experimental feature warning HOT 13
- Bug in napi folder HOT 3
- 6_object_wrap/napi - Destructor not called? HOT 13
- Why use assert? HOT 2
- No matching version found for [email protected] while npm install on 1_hello_world\node-api HOT 3
- node-addon-api Object::Wrap example HOT 1
- pass Buffer as argument HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from abi-stable-node-addon-examples.