Giter Club home page Giter Club logo

Comments (7)

Misery42 avatar Misery42 commented on June 12, 2024 8

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?

from node-addon-examples.

JamesRamm avatar JamesRamm commented on June 12, 2024 3

An example to get started would be good.

from node-addon-examples.

mhdawson avatar mhdawson commented on June 12, 2024 1

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

from node-addon-examples.

Globik avatar Globik commented on June 12, 2024

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

from node-addon-examples.

Misery42 avatar Misery42 commented on June 12, 2024

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

from node-addon-examples.

Misery42 avatar Misery42 commented on June 12, 2024

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"]

from node-addon-examples.

mhdawson avatar mhdawson commented on June 12, 2024

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?

from node-addon-examples.

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.