Skip to content

Array documentation #340

Closed
Closed

Description

@rivertam

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.

Activity

  1. self-assigned this
    on Sep 26, 2018
  2. NickNaso commented on Sep 27, 2018

    @NickNaso
    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.

  3. rivertam commented on Sep 28, 2018

    @rivertam
    ContributorAuthor

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

  4. abhi11210646 commented on Mar 30, 2019

    @abhi11210646
    Contributor

    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?

  5. gabrielschulhof commented on Mar 31, 2019

    @gabrielschulhof
    Contributor

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

  6. mhdawson commented on Apr 2, 2019

    @mhdawson
    Member

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

  7. abhi11210646 commented on Apr 3, 2019

    @abhi11210646
    Contributor

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

  8. rivertam commented on Apr 3, 2019

    @rivertam
    ContributorAuthor

    For those (like myself) who want a link to the example @mhdawson is referencing: nodejs/node-addon-examples@7124e5f#diff-36c91849c5180ea4227b8c709138a26b

  9. NickNaso commented on Apr 3, 2019

    @NickNaso
    Member

    @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?

  10. rivertam commented on Apr 3, 2019

    @rivertam
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions