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

Need example of napi_get_value_string_utf8 #83

Open
luii opened this issue Dec 10, 2017 · 9 comments
Open

Need example of napi_get_value_string_utf8 #83

luii opened this issue Dec 10, 2017 · 9 comments

Comments

@luii
Copy link

luii commented Dec 10, 2017

How to create utf8 string from a argument

@mhdawson
Copy link
Member

Is it that you would like an example to get started ? Or would you like to volunteer to add this ?

@Globik
Copy link

Globik commented Dec 25, 2017

I'm also intrested in this method api. I shall do my best. Trying to give req.header["user-agent"] to the c native code : )
By the way, nullptr on C code kinda undeclared. One need NULL or ((void*)0) to write...

@JamesRamm
Copy link

An example to get started would be good.

@mhdawson mhdawson transferred this issue from nodejs/abi-stable-node-addon-examples Jan 30, 2019
@Misery42
Copy link

Misery42 commented Aug 5, 2019

I dont understand it too... i struggle with segmentation fault!

@Misery42
Copy link

Misery42 commented Aug 5, 2019

JS

var sRes = addon.helloWorld("hello", "world");
console.log(`helloWorld returned: ${sRes}`);

C Code:

napi_value hello_world (napi_env env, napi_callback_info info) {

  size_t argc = 2;

  napi_value args[2];

  napi_get_cb_info(env, info, &argc, args, NULL, NULL);
  
  size_t str_size;
  size_t str_size_read;
  napi_get_value_string_utf8(env, args[1], NULL, 0, &str_size);
  char * buf;
  buf = (char*)calloc(str_size + 1, sizeof(char));
  str_size = str_size + 1;
  napi_get_value_string_utf8(env, args[1], buf, str_size, &str_size_read);
  
  printf("%s, %s \n", "Hello", buf); // output: Hello, world
  
  napi_value result;
  
  napi_create_string_utf8(env, buf, str_size_read, &result);
  free(buf);
  return result;
}

Output:
helloWorld returned: worl
However one char "d" is missing any idea?

@Misery42
Copy link

Misery42 commented Aug 5, 2019

Note!
str_size = str_size + 1;

["w", "o", "r", "l", "d", "NULL"]
In this case str_size is 5!
But when you want to fill the buffer you need a size of 6!
Otherwise your string got truncated to
["w", "o", "r", "l", "NULL"]

@mhdawson
Copy link
Member

Believe you can also use NAPI_AUTO_LENGTH if it is NULL terminated and you don't want to specify the size

@Misery42 any chance you want to submit a PR for an example covering this?

Misery42 added a commit to Misery42/node-addon-examples that referenced this issue Aug 17, 2019
* fixes nodejs#83
* NAPI example for working with UTF-8 encoded strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@JamesRamm @luii @Globik @mhdawson @Misery42 and others