Skip to content

Commit 4435100

Browse files
committed
Adds additional tutorial examples
1 parent e796e0f commit 4435100

26 files changed

Lines changed: 494 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# N-API A First Project
2+
3+
This is an example project that accompanies the N-API workshop tutorials
4+
5+
A tutorial describing this project can be found at the [N-API Resource](https://napi.inspiredware.com/getting-started/first.html).
6+
7+
To build and run this program on your system, clone it to your computer and run these two commands inside your clone:
8+
9+
```
10+
npm install
11+
npm test
12+
```
13+
14+
> You need to have Node 10.5.0 or later installed.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'hello-world-native',
5+
'sources': [ 'src/hello_world.cc' ],
6+
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
7+
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
8+
'cflags!': [ '-fno-exceptions' ],
9+
'cflags_cc!': [ '-fno-exceptions' ],
10+
'xcode_settings': {
11+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
12+
'CLANG_CXX_LIBRARY': 'libc++',
13+
'MACOSX_DEPLOYMENT_TARGET': '10.7'
14+
},
15+
'msvs_settings': {
16+
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
17+
}
18+
}
19+
]
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const addon = require('../build/Release/hello-world-native');
2+
3+
module.exports = addon.HelloWorld
4+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"main": "lib/binding.js",
3+
"private": true,
4+
"dependencies": {
5+
"node-addon-api": "^1.1.0"
6+
},
7+
"scripts": {
8+
"test": "node --napi-modules ./test/test_binding.js"
9+
},
10+
"gypfile": true,
11+
"name": "hello-world",
12+
"version": "1.0.0",
13+
"description": "A first project.",
14+
"author": "Your name goes here",
15+
"license": "ISC"
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <napi.h>
2+
3+
using namespace Napi;
4+
5+
Napi::String Method(const Napi::CallbackInfo& info) {
6+
Napi::Env env = info.Env();
7+
return Napi::String::New(env, "world");
8+
}
9+
10+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
11+
exports.Set(Napi::String::New(env, "HelloWorld"),
12+
Napi::Function::New(env, Method));
13+
return exports;
14+
}
15+
16+
NODE_API_MODULE(addon, Init)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const HelloWorld = require("../lib/binding.js");
2+
const assert = require("assert");
3+
4+
assert(HelloWorld, "The expected function is undefined");
5+
6+
function testBasic()
7+
{
8+
const result = HelloWorld("hello");
9+
assert.strictEqual(result, "world", "Unexpected value returned");
10+
}
11+
12+
assert.doesNotThrow(testBasic, undefined, "testBasic threw an expection");
13+
14+
console.log("Tests passed- everything looks OK!");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [
6+
"src/binding.cc",
7+
"src/native-addon.cc"
8+
],
9+
'cflags!': [ '-fno-exceptions' ],
10+
'cflags_cc!': [ '-fno-exceptions' ],
11+
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
12+
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
13+
'conditions': [
14+
['OS=="win"', {
15+
"msvs_settings": {
16+
"VCCLCompilerTool": {
17+
"ExceptionHandling": 1
18+
}
19+
}
20+
}],
21+
['OS=="mac"', {
22+
"xcode_settings": {
23+
"CLANG_CXX_LIBRARY": "libc++",
24+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
25+
'MACOSX_DEPLOYMENT_TARGET': '10.7'
26+
}
27+
}]
28+
]
29+
}
30+
]
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const { NativeAddon } = require('bindings')('addon')
4+
5+
function JSFnRef() {
6+
console.log('Hi! I\'m a JS function reference!')
7+
}
8+
9+
function JSFn() {
10+
console.log('Hi! I\'m a JS function!')
11+
}
12+
13+
const ITERATIONS = 5
14+
15+
const addon = new NativeAddon(JSFnRef, JSFn)
16+
17+
for (let i = 0; i < ITERATIONS; i++) {
18+
addon.tryCallByStoredReference()
19+
}
20+
21+
try {
22+
addon.tryCallByStoredFunction()
23+
} catch (err) {
24+
console.error('This code crashes because JSFn is valid only inside the constructor function.')
25+
console.error('The lifespan of the JSFn function is limited to the execution of the constructor function.')
26+
console.error('After that, the function stored in JSFn is ready to be garbage collected and it cannot be used anymore.')
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "function-reference-demo",
3+
"version": "0.0.0",
4+
"description": "Tutorial example showing the use of function references.",
5+
"main": "index.js",
6+
"gypfile": true,
7+
"scripts": {
8+
"start": "node index.js"
9+
},
10+
"dependencies": {
11+
"node-addon-api": "*",
12+
"bindings": "*"
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <napi.h>
2+
#include "native-addon.h"
3+
4+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
5+
NativeAddon::Init(env, exports);
6+
return exports;
7+
}
8+
9+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

0 commit comments

Comments
 (0)