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

Array documentation #340

Closed
rivertam opened this issue Sep 12, 2018 · 9 comments
Closed

Array documentation #340

rivertam opened this issue Sep 12, 2018 · 9 comments
Assignees
Labels

Comments

@rivertam
Copy link
Contributor

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.

@NickNaso NickNaso self-assigned this Sep 26, 2018
@NickNaso
Copy link
Member

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.

@NickNaso NickNaso added the doc label Sep 27, 2018
@rivertam
Copy link
Contributor Author

Great, thanks @NickNaso!

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).

@abhi11210646
Copy link
Contributor

abhi11210646 commented 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
Copy link
Contributor

gabrielschulhof commented 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 ArrayBuffer and 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
Copy link
Member

mhdawson commented Apr 2, 2019

Landed the example in node-addon-examples. Let is know if that resolves your questions.

@abhi11210646
Copy link
Contributor

Hi @gabrielschulhof, it worked this way. thank you.

@rivertam
Copy link
Contributor Author

rivertam commented 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
Copy link
Member

NickNaso commented 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
Copy link
Contributor Author

rivertam commented 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.

@rivertam rivertam closed this as completed Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants