generated from napi-rs/package-template
-
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
1 parent
0fa7bb7
commit de41209
Showing
17 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
Submodule quick-sort
deleted from
7a2ef2
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 @@ | ||
[package] | ||
authors = ["yuuang <569105585@qq.com>"] | ||
edition = "2018" | ||
name = "napi-napi-quick-sort" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
napi = {version = "0.5"} | ||
napi-derive = {version = "0.5"} | ||
|
||
[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies] | ||
jemallocator = {version = "0.3", features = ["disable_initial_exec_tls"]} | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
mimalloc = {version = "0.1"} | ||
|
||
[build-dependencies] | ||
napi-build = "0.2" |
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 @@ | ||
import test from 'ava' | ||
|
||
import { sleep, sync } from '../index' | ||
|
||
test('sync function from native code', (t) => { | ||
const fixture = 42 | ||
t.is(sync(fixture), fixture + 100) | ||
}) | ||
|
||
test('sleep function from native code', async (t) => { | ||
const timeToSleep = 200 | ||
const value = await sleep(timeToSleep) | ||
t.is(value, timeToSleep * 2) | ||
}) |
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,28 @@ | ||
import b from 'benny' | ||
|
||
import { sync } from '../index' | ||
|
||
function add(a: number) { | ||
return a + 100 | ||
} | ||
|
||
async function run() { | ||
await b.suite( | ||
'Add 100', | ||
|
||
b.add('Native a + 100', () => { | ||
sync(10) | ||
}), | ||
|
||
b.add('JavaScript a + 100', () => { | ||
add(10) | ||
}), | ||
|
||
b.cycle(), | ||
b.complete(), | ||
) | ||
} | ||
|
||
run().catch((e) => { | ||
console.error(e) | ||
}) |
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,5 @@ | ||
extern crate napi_build; | ||
|
||
fn main() { | ||
napi_build::setup(); | ||
} |
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 @@ | ||
export const sync: (input: number) => number | ||
// sleep [duration] ms, return Promise which resolved 2 * duration | ||
export const sleep: (duration: number) => Promise<number> |
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 @@ | ||
const { loadBinding } = require('@node-rs/helper') | ||
|
||
/** | ||
* __dirname means load native addon from current dir | ||
* 'napi-quick-sort' means native addon name is `napi-quick-sort` | ||
* the first arguments was decided by `napi.name` field in `package.json` | ||
* the second arguments was decided by `name` field in `package.json` | ||
* loadBinding helper will load `napi-quick-sort.[PLATFORM].node` from `__dirname` first | ||
* If failed to load addon, it will fallback to load from `napi-quick-sort-[PLATFORM]` | ||
*/ | ||
module.exports = loadBinding(__dirname, 'napi-quick-sort', 'napi-quick-sort') |
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 @@ | ||
# `napi-quick-sort-darwin` | ||
|
||
This is the **darwin** 64-bit binary for `napi-quick-sort` |
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,32 @@ | ||
{ | ||
"name": "napi-quick-sort-darwin", | ||
"version": "0.1.0", | ||
"os": [ | ||
"darwin" | ||
], | ||
"main": "napi-quick-sort.darwin.node", | ||
"files": [ | ||
"napi-quick-sort.darwin.node" | ||
], | ||
"description": "Template project for writing node package with napi-rs", | ||
"keywords": [ | ||
"napi-rs", | ||
"NAPI", | ||
"N-API", | ||
"Rust", | ||
"node-addon", | ||
"node-addon-api" | ||
], | ||
"license": "MIT", | ||
"cpu": [ | ||
"x64" | ||
], | ||
"engines": { | ||
"node": ">= 8.9" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"repository": "git@github.com:napi-rs/napi-quick-sort.git" | ||
} |
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 @@ | ||
# `napi-quick-sort-linux-musl` | ||
|
||
This is the **linux-musl** 64-bit binary for `napi-quick-sort` |
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,32 @@ | ||
{ | ||
"name": "napi-quick-sort-linux-musl", | ||
"version": "0.1.0", | ||
"os": [ | ||
"linux" | ||
], | ||
"main": "napi-quick-sort.linux-musl.node", | ||
"files": [ | ||
"napi-quick-sort.linux-musl.node" | ||
], | ||
"description": "Template project for writing node package with napi-rs", | ||
"keywords": [ | ||
"napi-rs", | ||
"NAPI", | ||
"N-API", | ||
"Rust", | ||
"node-addon", | ||
"node-addon-api" | ||
], | ||
"license": "MIT", | ||
"cpu": [ | ||
"x64" | ||
], | ||
"engines": { | ||
"node": ">= 8.9" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"repository": "git@github.com:napi-rs/napi-quick-sort.git" | ||
} |
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 @@ | ||
# `napi-quick-sort-linux` | ||
|
||
This is the **linux** 64-bit binary for `napi-quick-sort` |
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,32 @@ | ||
{ | ||
"name": "napi-quick-sort-linux", | ||
"version": "0.1.0", | ||
"os": [ | ||
"linux" | ||
], | ||
"main": "napi-quick-sort.linux.node", | ||
"files": [ | ||
"napi-quick-sort.linux.node" | ||
], | ||
"description": "Template project for writing node package with napi-rs", | ||
"keywords": [ | ||
"napi-rs", | ||
"NAPI", | ||
"N-API", | ||
"Rust", | ||
"node-addon", | ||
"node-addon-api" | ||
], | ||
"license": "MIT", | ||
"cpu": [ | ||
"x64" | ||
], | ||
"engines": { | ||
"node": ">= 8.9" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"repository": "git@github.com:napi-rs/napi-quick-sort.git" | ||
} |
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 @@ | ||
# `napi-quick-sort-win32` | ||
|
||
This is the **win32** 32-bit binary for `napi-quick-sort` |
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,32 @@ | ||
{ | ||
"name": "napi-quick-sort-win32", | ||
"version": "0.1.0", | ||
"os": [ | ||
"win32" | ||
], | ||
"main": "napi-quick-sort.win32.node", | ||
"files": [ | ||
"napi-quick-sort.win32.node" | ||
], | ||
"description": "Template project for writing node package with napi-rs", | ||
"keywords": [ | ||
"napi-rs", | ||
"NAPI", | ||
"N-API", | ||
"Rust", | ||
"node-addon", | ||
"node-addon-api" | ||
], | ||
"license": "MIT", | ||
"cpu": [ | ||
"x64" | ||
], | ||
"engines": { | ||
"node": ">= 8.9" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"repository": "git@github.com:napi-rs/napi-quick-sort.git" | ||
} |
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,53 @@ | ||
{ | ||
"name": "napi-quick-sort", | ||
"version": "0.1.0", | ||
"description": "Template project for writing node package with napi-rs", | ||
"main": "index.js", | ||
"repository": "git@github.com:napi-rs/napi-quick-sort.git", | ||
"license": "MIT", | ||
"keywords": [ | ||
"napi-rs", | ||
"NAPI", | ||
"N-API", | ||
"Rust", | ||
"node-addon", | ||
"node-addon-api" | ||
], | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"os": [ | ||
"darwin", | ||
"linux", | ||
"win32" | ||
], | ||
"cpu": [ | ||
"x64" | ||
], | ||
"napi": { | ||
"name": "napi-quick-sort", | ||
"musl": [ | ||
"linux" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">= 8.9" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"artifacts": "napi artifacts", | ||
"bench": "node -r @swc-node/register benchmark/bench.ts" | ||
}, | ||
"dependencies": { | ||
"@node-rs/helper": "^0.4.0" | ||
}, | ||
"optionalDependencies": { | ||
"napi-quick-sort-darwin": "^0.1.0", | ||
"napi-quick-sort-linux": "^0.1.0", | ||
"napi-quick-sort-win32": "^0.1.0" | ||
} | ||
} |
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,57 @@ | ||
#[macro_use] | ||
extern crate napi; | ||
#[macro_use] | ||
extern crate napi_derive; | ||
|
||
use std::convert::TryInto; | ||
|
||
use napi::{CallContext, Env, JsNumber, JsObject, Module, Result, Task}; | ||
|
||
#[cfg(all(unix, not(target_env = "musl")))] | ||
#[global_allocator] | ||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; | ||
|
||
#[cfg(windows)] | ||
#[global_allocator] | ||
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; | ||
|
||
register_module!(example, init); | ||
|
||
struct AsyncTask(u32); | ||
|
||
impl Task for AsyncTask { | ||
type Output = u32; | ||
type JsValue = JsNumber; | ||
|
||
fn compute(&mut self) -> Result<Self::Output> { | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
sleep(Duration::from_millis(self.0 as u64)); | ||
Ok(self.0 * 2) | ||
} | ||
|
||
fn resolve(&self, env: &mut Env, output: Self::Output) -> Result<Self::JsValue> { | ||
env.create_uint32(output) | ||
} | ||
} | ||
|
||
fn init(module: &mut Module) -> Result<()> { | ||
module.create_named_method("sync", sync_fn)?; | ||
|
||
module.create_named_method("sleep", sleep)?; | ||
Ok(()) | ||
} | ||
|
||
#[js_function(1)] | ||
fn sync_fn(ctx: CallContext) -> Result<JsNumber> { | ||
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?; | ||
|
||
ctx.env.create_uint32(argument + 100) | ||
} | ||
|
||
#[js_function(1)] | ||
fn sleep(ctx: CallContext) -> Result<JsObject> { | ||
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?; | ||
let task = AsyncTask(argument); | ||
ctx.env.spawn(task) | ||
} |