Comments (8)
if I understand it correctly, getting a string with it should be as simple as
new Napi::String::String (env, args[0])
If you’re creating your functions using this wrapper API it gets even easier than that. :) Something like this should work:
void Foobar(const CallbackInfo& info) {
assert(info[0].IsString());
std::string arg = info[0].As<String>().Utf8Value();
}
void Init(Env env, Object exports, Object module) {
exports.Set("foobar", Function::New(env, Foobar));
}
NODE_API_MODULE(addon, Init)
from node-addon-api.
@addaleax Thank you! That's a lot of code to get a string, to be honest, but NAPI is still more readable than v8 bindings, I guess.
from node-addon-api.
@addaleax I didn't notice that this repository was for a different API :) Google led me here when I was looking for N-API. The wrapper API seems quite nice, if I understand it correctly, getting a string with it should be as simple as new Napi::String::String (env, args[0])
and it can be then converted with std::u16string()
. That's nice indeed. Thank you!
from node-addon-api.
You can call napi_get_value_string_utf16
with the buf
argument set to NULL
– in that case, copied0
would still be filled with the size of the string.
(In your example, sizeof(args[0])
doesn’t seem correct – it will give you the size of args[0]
as a pointer, not of the JavaScript value that it refers to.)
from node-addon-api.
@addaleax So in that case I do two calls to napi_get_value_string_utf16
, the first one with buf set to NULL to get the size of the string, and the second one get the string itself, correct?
// where args[0] is a JS string passed into the C++ function
size_t strSize;
status = napi_get_value_string_utf16(env, args[0], NULL, NULL, &strSize);
assert(status == napi_ok);
char16_t value0[strSize];
size_t copied0;
status = napi_get_value_string_utf16(env, args[0], value0, bufSize, &copied0);
assert(status == napi_ok);
from node-addon-api.
@zandaqo In C++ that should work, yup. :) You don’t need the extra copied0
, though, since you already know the string size.
from node-addon-api.
@zandaqo Keep an eye on the C++ wrapper API in this module here (https://www.npmjs.com/package/node-addon-api) – it’s a lot more readable than the raw C APIs exposed by NAPI, and much more so than the V8 API itself.
from node-addon-api.
@addaleax Now that's impressive! It took me some 50 locs of N-API to expose a single function that takes two strings and returns an integer; this API reduces that to mere handful locs. it's almost too easy.
from node-addon-api.
Related Issues (20)
- 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
- Strange behavior HOT 2
- How do I initialize a variable of type napi_value to NULL to avoid compiler warnings HOT 4
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.