Giter Club home page Giter Club logo

Comments (8)

addaleax avatar addaleax commented on July 27, 2024 6

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.

zandaqo avatar zandaqo commented on July 27, 2024 1

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

zandaqo avatar zandaqo commented on July 27, 2024 1

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

addaleax avatar addaleax commented on July 27, 2024

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.

zandaqo avatar zandaqo commented on July 27, 2024

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

addaleax avatar addaleax commented on July 27, 2024

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

addaleax avatar addaleax commented on July 27, 2024

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

zandaqo avatar zandaqo commented on July 27, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.