-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.xmake/ | ||
/prebuilds/ | ||
*.tar.gz | ||
/xmake-*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Example | ||
|
||
Modified from | ||
<https://github.com/nodejs/node-addon-examples/blob/main/src/1-getting-started/1_hello_world/> | ||
|
||
**NOTE**: Don't support nan because | ||
<https://github.com/xmake-io/xmake-repo/issues/10#issuecomment-2587462047> | ||
and node_version.h is missing. | ||
|
||
## Example 1: *Hello world* | ||
|
||
To get started let's make a small addon which is the C++ equivalent of | ||
the following JavaScript code: | ||
|
||
```js | ||
module.exports.hello = function() { return 'world'; }; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <assert.h> | ||
#include <node_api.h> | ||
|
||
static napi_value Method(napi_env env, napi_callback_info info) { | ||
napi_status status; | ||
napi_value world; | ||
status = napi_create_string_utf8(env, "world", 5, &world); | ||
assert(status == napi_ok); | ||
return world; | ||
} | ||
|
||
#define DECLARE_NAPI_METHOD(name, func) \ | ||
{ name, 0, func, 0, 0, 0, napi_default, 0 } | ||
|
||
static napi_value Init(napi_env env, napi_value exports) { | ||
napi_status status; | ||
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); | ||
status = napi_define_properties(env, exports, 1, &desc); | ||
assert(status == napi_ok); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var addon = require('bindings')('hello'); | ||
|
||
console.log(addon.hello()); // 'world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "hello_world", | ||
"version": "0.0.0", | ||
"description": "Node.js Addons Example #1", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": { | ||
"xmake-build-system": "^2.9.7", | ||
"bindings": "~1.5.0" | ||
}, | ||
"scripts": { | ||
"prepack": "xmake -y && xmake install", | ||
"test": "node hello.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- luacheck: ignore 113 143 | ||
---@diagnostic disable: undefined-global | ||
add_rules("mode.debug", "mode.release") | ||
|
||
add_requires("node-api-headers") | ||
|
||
target("hello") | ||
do | ||
add_rules("nodejs.module") | ||
add_packages("node-api-headers") | ||
add_files("*.c") | ||
end |
16 changes: 16 additions & 0 deletions
16
examples/nodejs/1_hello_world/node-addon-api-addon-class/hello.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <napi.h> | ||
|
||
class HelloAddon : public Napi::Addon<HelloAddon> { | ||
public: | ||
HelloAddon(Napi::Env env, Napi::Object exports) { | ||
DefineAddon(exports, | ||
{InstanceMethod("hello", &HelloAddon::Hello, napi_enumerable)}); | ||
} | ||
|
||
private: | ||
Napi::Value Hello(const Napi::CallbackInfo& info) { | ||
return Napi::String::New(info.Env(), "world"); | ||
} | ||
}; | ||
|
||
NODE_API_ADDON(HelloAddon) |
3 changes: 3 additions & 0 deletions
3
examples/nodejs/1_hello_world/node-addon-api-addon-class/hello.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var addon = require('bindings')('hello'); | ||
|
||
console.log(addon.hello()); // 'world' |
15 changes: 15 additions & 0 deletions
15
examples/nodejs/1_hello_world/node-addon-api-addon-class/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "hello_world", | ||
"version": "0.0.0", | ||
"description": "Node.js Addons Example #1", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": { | ||
"xmake-build-system": "^2.9.7", | ||
"bindings": "~1.5.0" | ||
}, | ||
"scripts": { | ||
"prepack": "xmake -y && xmake install", | ||
"test": "node hello.js" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/nodejs/1_hello_world/node-addon-api-addon-class/xmake.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- luacheck: ignore 113 143 | ||
---@diagnostic disable: undefined-global | ||
add_rules("mode.debug", "mode.release") | ||
|
||
add_requires("node-addon-api") | ||
|
||
target("hello") | ||
do | ||
add_rules("nodejs.module") | ||
add_packages("node-addon-api") | ||
add_files("*.cc") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <napi.h> | ||
|
||
Napi::String Method(const Napi::CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
return Napi::String::New(env, "world"); | ||
} | ||
|
||
Napi::Object Init(Napi::Env env, Napi::Object exports) { | ||
exports.Set(Napi::String::New(env, "hello"), | ||
Napi::Function::New(env, Method)); | ||
return exports; | ||
} | ||
|
||
NODE_API_MODULE(hello, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var addon = require('bindings')('hello'); | ||
|
||
console.log(addon.hello()); // 'world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "hello_world", | ||
"version": "0.0.0", | ||
"description": "Node.js Addons Example #1", | ||
"main": "hello.js", | ||
"private": true, | ||
"dependencies": { | ||
"xmake-build-system": "^2.9.7", | ||
"bindings": "~1.5.0" | ||
}, | ||
"scripts": { | ||
"prepack": "xmake -y && xmake install", | ||
"test": "node hello.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- luacheck: ignore 113 143 | ||
---@diagnostic disable: undefined-global | ||
add_rules("mode.debug", "mode.release") | ||
|
||
add_requires("node-addon-api") | ||
|
||
target("hello") | ||
do | ||
add_rules("nodejs.module") | ||
add_packages("node-addon-api") | ||
add_files("*.cc") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
cd "$(dirname "$(dirname "$(readlink -f "$0")")")" | ||
|
||
cd examples/nodejs/1_hello_world | ||
for dir in ./*/; do | ||
cd "$dir" | ||
npm install | ||
npm run prepack | ||
npm test | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters