You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I'm pretty confused about how to use Arrays/TypedArrays using this library. Basically, I'm looking for the equivalent of the .push method. I'd also love a way to reserve space in chunks, as I know how big the resulting array is going to be, though that might be too complicated considering V8.
The documentation includes a .Set(int, Value) with the example obj.Set(42, "The Answer to Life, the Universe, and Everything");. This example is given for Object, not for Array (I can't find anything with Array at all).
I tried to do something similar in the code (I'm trying to make an array of arrays):
obj.Set(0, Napi::Array::New(env));
but got the error:
error: call of overloaded ‘Set(int, Napi::Array)’ is ambiguous
obj.Set(0, Napi::Array::New(env));
I'm very confused as to why that's ambiguous, but I also suspect it won't update the length properly or at least will be quite inefficient. When I wrap 0 in uint32_t(0), it compiles. Either way, the documentation here is pretty lacking and I think an example using arrays efficiently would be very welcome.
Hi @rivertam,
sorry for the late in answer. As you suggested in the next days I will try to add some examples about Napi::Array and improve the corresponding documentation.
Reacted by Ben Berman, Andrew Nosenko, davidnorthetal, Cody Schrank, Michelle Tilley, Michael K and Merwan Achibet
I did try the .Set method but this turned out to be WAY slower (10+ seconds in my case) than just encoding the array as a buffer and then looping through the bytes in an orderly fashion in JS (30-50ms).
Hi @NickNaso@rivertam,
I am also facing the problem with arrays. I already have a C++ implemented library which accepts an integer array.
but "node-addon-api" have Napi::Array. I didn't find the conversion method for this.
if I create new int array then assign all element from Napi::Array will be expensive. so what do you suggest?
Yeah, I think this is exactly what I was looking for at the time. I can't test it or anything because we essentially dropped that project (we had to move to Python for unrelated reasons -- I'm still bitter), but this definitely seems like it covers the concern I had initially.
I suppose I'll close this, but I'm not sure what the exact protocol here is so anyone can feel free to reopen.
Activity
NickNaso commentedon Sep 27, 2018
Hi @rivertam,
sorry for the late in answer. As you suggested in the next days I will try to add some examples about
Napi::Arrayand improve the corresponding documentation.rivertam commentedon Sep 28, 2018
Great, thanks @NickNaso!
I did try the
.Setmethod but this turned out to be WAY slower (10+ seconds in my case) than just encoding the array as a buffer and then looping through the bytes in an orderly fashion in JS (30-50ms).abhi11210646 commentedon Mar 30, 2019
Hi @NickNaso @rivertam,
I am also facing the problem with arrays. I already have a C++ implemented library which accepts an integer array.
but "node-addon-api" have Napi::Array. I didn't find the conversion method for this.
if I create new int array then assign all element from Napi::Array will be expensive. so what do you suggest?
gabrielschulhof commentedon Mar 31, 2019
@abhi11210646 converting from a JavaScript array to a native array is an expensive operation. OTOH you can use a JavaScript Int32Array as in the following example: https://github.com/gabrielschulhof/node-addon-examples/blob/array_buffer_to_native/array_buffer_to_native/node-addon-api/index.js. On the native side, you can accept the resulting
ArrayBufferand pass the data directly to the native side without needing to convert from a JavaScript type to a native type. BTW, I've submitted the above example to node-addon-examples.mhdawson commentedon Apr 2, 2019
Landed the example in node-addon-examples. Let is know if that resolves your questions.
abhi11210646 commentedon Apr 3, 2019
Hi @gabrielschulhof, it worked this way. thank you.
rivertam commentedon Apr 3, 2019
For those (like myself) who want a link to the example @mhdawson is referencing: nodejs/node-addon-examples@7124e5f#diff-36c91849c5180ea4227b8c709138a26b
NickNaso commentedon Apr 3, 2019
@rivertam I want update you that the example has been landed in the example repo and eveyone can find it here: https://github.com/nodejs/node-addon-examples/tree/master/array_buffer_to_native/node-addon-api and @gabrielschulhof added some explanations in the array documentation #465
What do you think? Do we need to do more?
rivertam commentedon Apr 3, 2019
Yeah, I think this is exactly what I was looking for at the time. I can't test it or anything because we essentially dropped that project (we had to move to Python for unrelated reasons -- I'm still bitter), but this definitely seems like it covers the concern I had initially.
I suppose I'll close this, but I'm not sure what the exact protocol here is so anyone can feel free to reopen.