nodejs / abi-stable-node-addon-examples Goto Github PK
View Code? Open in Web Editor NEWNode Add-on Examples with PoC ABI stable API for native modules
Node Add-on Examples with PoC ABI stable API for native modules
Would it be possible to bring the node-addon-api Object::Wrap example, up-to-date with respective napi example, adding destructor and New methods?
When I tried the example of 1_hello_world\node-api
, I first run npm install
, and then it gave me the following errors. I also looked the https://www.npmjs.com/package/node-api
, but it seems not this package.
D:\code\node\abi-stable-node-addon-examples\1_hello_world\node-api>npm install
npm WARN npm npm does not support Node.js v10.0.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected]
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! notarget
npm ERR! notarget It was specified as a dependency of 'hello_world'
npm ERR! notarget
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Liu.D.H\AppData\Roaming\npm-cache\_logs\2018-04-25T05_33_59_261Z-debug.log
Hi,
I've been toying with these examples for sometime (as my introduction to node.js from c++ side) and noticed something peculiar that I'm not sure is the expected behavior.
In Example 6 (N-API), I cannot confirm that the created c++ objects are ever destructed. Here is what I've done:
At the top of myobject.cc
, I added
#include <fstream>
std::ofstream os("test.txt",std::fstream::out); // defined globally
Then, in MyObject's constructor and destructor as well as in New()
and Destructor()
member functions I inserted at the top of each function:
os << "MyObject::XXX" << std::endl; // XXX is 'MyObject', '~MyObject', 'New' or 'Destructor'
I expect executing node ./addon.js
creates test.txt
with 2 sets of the 4 unique MyObject::XXX
lines. (In my novice node.js eyes, it should create 2 instances of the c++ class.) However, the generated test.txt
looks like
MyObject::New
MyObject::MyObject
MyObject::New
MyObject::MyObject
MyObject::New
MyObject::MyObject
MyObject::New
MyObject::MyObject
This output suggests 4 c++ instantiations and none of them being destructed. Is this a bug or lack of my understanding of how things work in this framework?
Thanks
Kesh
Curent examples cover cases only with 1 properties get created inside Init.
Is any chance to give examples how to create multiple properties?
next approach is incorrect - it's overwrite exports
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value fn;
read_dht_data();
status = napi_create_function(env, NULL, 0, myGetTemp, NULL, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to wrap native function");
}
status = napi_set_named_property(env, exports, "getTemp", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to populate exports");
}
status = napi_create_function(env, NULL, 0, myGetTemp2, NULL, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to wrap native function");
}
status = napi_set_named_property(env, exports, "getTemp2", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to populate exports");
}
return exports;
}
There in .js file must be simply require without ('addon'). Segmentation fault.
When running my N-API code I get this warning
(node:21984) Warning: N-API is an experimental feature and could change at any time.
This is a nuisance. Can we have an API call to disable the warning message if we dont want it ?
By the way, I like N-API
Hello guys!
Why does every example use asserts
instead of if checks & maybe some exceptions?
Wouldn't it be better to just throw errors so the js code has the opportunity to recover from possible errors and only use asserts for testing etc?
P.S.: Thanks for all the examples. You guys really helped me understand all the APIs!
Hi, I have problem figuring out, how to pass buffer from process Buffer argument in node-api-addon into Napi:Buffer<uint8_T>.
This is old non-napi snippet:
Local<Object> data = args[0]->ToObject();
if(!Buffer::HasInstance(data))
return except("Argument should be a buffer object.");
char * input = Buffer::Data(data);
I tryed:
Napi::Buffer<uint8_t> input_data = info[0].As<Napi::Buffer<uint8_t>>();
which fails to compile with error, that As does not contain overload for converting to Napi::Buffer<uint8_t>. There is also no ToBuffer function.
So I ended up with casting to Napi::Object :
Napi::Object input_data = info[0].As<Napi::Object>();
But this can not be cast to Napi::Buffer<uint8_t> directly. So I´m looking for example, how to handle this situation - to get from Napi::Value
parameter/argument specific Napi::Buffer<T>
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.