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

Added HelloWorld example as requested #107

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions 1_hello_world/napi/example2/libHelloWorld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
25 changes: 25 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This example covers handling of UTF-8 encoded strings as parameters for an external C library. It goes beyond the demonstration of the initial example, seen in the `example1` folder. Combining both examples together should result in a clearer and more complete view of how to use the NAPI for this type of use cases.

1. Dependencies
```
gcc
python2
nodejs
node-gyp
```
Installable on Debian-based systems like this:
```
sudo apt install build-essential python2 nodejs node-gyp
```

2. Building and running the project:
```
cd node-addon-examples/1_hello_world/napi/example2/libHelloWorld/
./build.sh
```
This will execute `build.sh` which will compile `libHelloWorld.so` within `lib/`. Afterwards, the shared object will be copied to `/usr/lib` and its header will be copied to `/usr/include`. Done that, the NAPI module will be built and `module.js` will be executed.

3. Once the previous steps were finished successfully, the output should be as follows:
```
Hello concat World equals HelloWorld
```
9 changes: 9 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"targets": [
{
"target_name": "module",
"sources": [ "./src/module.c" ],
"libraries": [ "/usr/lib/libHelloWorld.so" ]
}
]
}
4 changes: 4 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sudo ./lib/build.sh && \
node-gyp configure build && \
sudo node module.js
exit 0
1 change: 1 addition & 0 deletions 1_hello_world/napi/example2/libHelloWorld/lib/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./compileLibHelloWorld.sh && sudo cp libHelloWorld.so /usr/lib && sudo cp libHelloWorld.h /usr/include
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc -o libHelloWorld.so -shared libHelloWorld.c
10 changes: 10 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/lib/libHelloWorld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "libHelloWorld.h"
#include <string.h>

char * HelloWorldConcat (char * cHello, char * cWorld){
char * cHelloWorld = NULL;

cHelloWorld = strcat(cHello, cWorld);

return cHelloWorld;
}
6 changes: 6 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/lib/libHelloWorld.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __HELLOWORLDSDK_TYPES_H__
#define __HELLOWORLDSDK_TYPES_H__

char * HelloWorldConcat (char * cHello, char * cWorld);

#endif
5 changes: 5 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const addon = require('./build/Release/module');
const hello = "Hello";
const world = "World";

console.log(`${hello} concat ${world} equals`, addon.helloWorldConcat(hello, world));
12 changes: 12 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "helloworldlib",
"version": "1.0.0",
"description": "NAPI Test",
"main": "module.js",
"scripts": {
"start": "node-gyp configure build && node module.js",
"test": "console.log(\"Hello World!\");"
},
"author": "Steven Klein",
"license": "MIT"
}
62 changes: 62 additions & 0 deletions 1_hello_world/napi/example2/libHelloWorld/src/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <node_api.h>
#include <stdlib.h>
#include <stdio.h>
#include "libHelloWorld.h"

napi_value hello_world_concat (napi_env env, napi_callback_info info) {

size_t argc = 2;
size_t iTotalSize = 0;

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[0], NULL, 0, &str_size);
char * cHello;
cHello = (char*)calloc(str_size + 1, sizeof(char));
str_size = str_size + 1;
napi_get_value_string_utf8(env, args[0], cHello, str_size, &str_size_read);

iTotalSize = iTotalSize + str_size_read;

napi_get_value_string_utf8(env, args[1], NULL, 0, &str_size);
char * cWorld;
cWorld = (char*)calloc(str_size + 1, sizeof(char));
str_size = str_size + 1;
napi_get_value_string_utf8(env, args[1], cWorld, str_size, &str_size_read);

iTotalSize = iTotalSize + str_size_read;

char * cResult = HelloWorldConcat(cHello, cWorld);
//printf("%s, %s \n", "Hello", cResult); // Debug output: Hello, world

napi_value result;

napi_create_string_utf8(env, cResult, iTotalSize, &result);
free(cHello);
free(cWorld);
return result;
}

napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value fn;

status = napi_create_function(env, NULL, 0, hello_world_concat, NULL, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to wrap native function");
}

status = napi_set_named_property(env, exports, "helloWorldConcat", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to populate exports");
}

return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)