Skip to content

Commit

Permalink
fix: update ci
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Oct 19, 2020
1 parent 0fa7bb7 commit de41209
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/quick-sort
Submodule quick-sort deleted from 7a2ef2
23 changes: 23 additions & 0 deletions packages/quick-sort1/Cargo.toml
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"
14 changes: 14 additions & 0 deletions packages/quick-sort1/__test__/index.spec.ts
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)
})
28 changes: 28 additions & 0 deletions packages/quick-sort1/benchmark/bench.ts
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)
})
5 changes: 5 additions & 0 deletions packages/quick-sort1/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate napi_build;

fn main() {
napi_build::setup();
}
3 changes: 3 additions & 0 deletions packages/quick-sort1/index.d.ts
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>
11 changes: 11 additions & 0 deletions packages/quick-sort1/index.js
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')
3 changes: 3 additions & 0 deletions packages/quick-sort1/npm/darwin/README.md
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`
32 changes: 32 additions & 0 deletions packages/quick-sort1/npm/darwin/package.json
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"
}
3 changes: 3 additions & 0 deletions packages/quick-sort1/npm/linux-musl/README.md
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`
32 changes: 32 additions & 0 deletions packages/quick-sort1/npm/linux-musl/package.json
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"
}
3 changes: 3 additions & 0 deletions packages/quick-sort1/npm/linux/README.md
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`
32 changes: 32 additions & 0 deletions packages/quick-sort1/npm/linux/package.json
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"
}
3 changes: 3 additions & 0 deletions packages/quick-sort1/npm/win32/README.md
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`
32 changes: 32 additions & 0 deletions packages/quick-sort1/npm/win32/package.json
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"
}
53 changes: 53 additions & 0 deletions packages/quick-sort1/package.json
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"
}
}
57 changes: 57 additions & 0 deletions packages/quick-sort1/src/lib.rs
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)
}

0 comments on commit de41209

Please sign in to comment.