Comments (6)
If you know the array type in advance, then you can convert directly to it, for example .As<TypedArrayOf<uint8_t>>()
. Otherwise, yes you do need to convert to TypedArray
and call TypedArray::TypedArrayType()
first, before converting to the appropriate TypedArrayOf<T>
type.
In most cases conversions between Napi::Value
types using .As<T>()
are cheap because they are effectively just casts - they don't allocate memory and most don't call any N-API functions. However, TypedArrayOf<T>
is an exception, because it does call napi_get_typedarray_info()
.
So, this scenario is slightly inefficient in that it will result in two calls to napi_get_typedarray_info()
: first when getting the array type and second when constructing the TypedArrayOf<T>
instance. We could consider adding a more efficient API for converting from TypedArray
to TypedArrayOf<T>
when the array info is already known.
from node-addon-api.
But such a factory function could not return a TypedArrayOf<T>
without the caller already knowing what T
is.
(If these objects were allocated on the heap, the factory could return a base class pointer (TypedArray*
), and then the caller could down-cast that pointer at a later time. But for efficiency all these objects are allocated on the stack, so that wouldn't work.)
from node-addon-api.
@jasongin I should have clarified that I'm talking about the second scenario you mentioned: when we don't know the type of an array in advance. In that case, converting to TypedArray
and then to TypedArrayOf
not only takes an extra call to napi_get_typedarray_info()
that could be avoided, but also forces us to add extra interfaces when we have a function that accepts any type of a typed array from JS and treats them differently in C++.
For example, my existing C++ code expects to work with standard containers and to use it I have to convert typed arrays into vectors of their respective types (e.g. std::vector<float>
, std::vector<double>
, etc.). To do that I need to convert a Value
to a TypedArray
, then call the type getting function, then use switch
on its result and map the resulting enum value to a type, and only then use the type to convert the array into TypedArrayOf
, get its Data
, and instantiate vector from it. Having something that can just convert from Value
straight to the right typed array would make it all a lot easier.
from node-addon-api.
forces us to add extra interfaces when we have a function that accepts any type of a typed array from JS and treats them differently in C++.
Can you give an example of what you mean by "extra interfaces"?
Having something that can just convert from Value straight to the right typed array would make it all a lot easier.
I'm still not sure I understand what you're suggesting. How can the "right" type be returned unless you do a switch
on the type enum first? Consider the following (hypothetical) API that can do the conversion in one step:
TypedArrayOf<T> Value::AsTypedArrayOf<T>();
You'd have to know what T
is before calling it.
from node-addon-api.
I see. What I'm suggesting is probably more in the line of hiding that switching from the api consumer (perhaps implemented as a factory function) since TypedArrayOf
might be more useful than just TypedArray
.
from node-addon-api.
But for efficiency all these objects are allocated on the stack, so that wouldn't work.
Oh I get it. Yes, that point juggling is what I had in mind, but I missed the stack/heap part in my reasoning, too much JavaScript I guess. Thanks for clarifying this, and sorry for bothering with it.
from node-addon-api.
Related Issues (20)
- Napi::Function, C++ lambdas & finalisers HOT 5
- ES6 Proxy Like Property Accessor HOT 3
- Consume async JS function by TypedThreadSafeFunction ? HOT 4
- Obtain JavaScript function name from Napi::Function instance ? HOT 2
- idea: detect JS calls at compile time HOT 4
- Instantiation of ObjectWrap in c++ code causes crash HOT 3
- Just a simple question. HOT 2
- Enabling C++ exceptions on Windows is hell HOT 2
- New SemVer Major release? HOT 4
- Getting sigsegv when passing functions as parameters and building on M1 HOT 4
- Are native addons affected by Node's max memory size constraint? HOT 2
- Running single AsyncWorker at a time HOT 3
- <doc/setup.md> seems outdated HOT 1
- Query on reading/writing js typed arrays from C module. HOT 2
- Request a c++ call js demo. HOT 3
- How to properly handle `Object/Buffer/Uint8Array` in `AsyncWorker`? HOT 9
- How about add C++20 coroutine support to `Napi::Value`? HOT 9
- Node throws exception in main thread (?) when trying to execute event via ThreadSafeFunction HOT 2
- Throwing an object as an exception? HOT 3
- Napi::Value is not properly initialized the first time a program is run HOT 22
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 node-addon-api.