feat(napi): support export rust mod as ts namespace

This commit is contained in:
LongYinan 2021-11-23 19:00:31 +08:00
parent e4ca46f32b
commit 1fe39ff66d
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
29 changed files with 639 additions and 336 deletions

View file

@ -42,6 +42,9 @@ Generated by [AVA](https://avajs.dev).
export function createExternalString(content: string): ExternalObject<string>
export function getExternal(external: ExternalObject<number>): number␊
export function mutateExternal(external: ExternalObject<number>, newVal: number): void␊
export function xxh64Alias(input: Buffer): BigInt␊
export function mapOption(val?: number | undefined | null): number | undefined | null␊
export function add(a: number, b: number): number␊
export function fibonacci(n: number): number␊
@ -97,4 +100,21 @@ Generated by [AVA](https://avajs.dev).
name: string␊
static withName(name: string): ClassWithFactory␊
}␊
export namespace xxh3 {␊
export const ALIGNMENT: number␊
export function xxh3_64(input: Buffer): BigInt␊
export function xxh128(input: Buffer): BigInt␊
export class Xxh3 {␊
constructor()␊
update(input: Buffer): void␊
digest(): BigInt␊
}␊
}␊
export namespace xxh2 {␊
export function xxh2Plus(a: number, b: number): number␊
export function xxh3Xxh64Alias(input: Buffer): BigInt␊
}␊
`

View file

@ -52,6 +52,9 @@ import {
getExternal,
mutateExternal,
createExternalString,
xxh2,
xxh3,
xxh64Alias,
} from '../'
test('export const', (t) => {
@ -294,6 +297,18 @@ BigIntTest('create BigInt i64', (t) => {
t.is(createBigIntI64(), BigInt(100))
})
BigIntTest('js mod test', (t) => {
t.is(xxh64Alias(Buffer.from('hello world')), BigInt('1116'))
t.is(xxh3.xxh3_64(Buffer.from('hello world')), BigInt('1116'))
t.is(xxh3.xxh128(Buffer.from('hello world')), BigInt('1116'))
t.is(xxh2.xxh2Plus(1, 2), 3)
t.is(xxh2.xxh3Xxh64Alias(Buffer.from('hello world')), BigInt('1116'))
t.is(xxh3.ALIGNMENT, 16)
const xx3 = new xxh3.Xxh3()
xx3.update(Buffer.from('hello world'))
t.is(xx3.digest(), BigInt('1116'))
})
const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip
Napi4Test('call thread safe function', (t) => {

View file

@ -32,6 +32,9 @@ export function createExternal(size: number): ExternalObject<number>
export function createExternalString(content: string): ExternalObject<string>
export function getExternal(external: ExternalObject<number>): number
export function mutateExternal(external: ExternalObject<number>, newVal: number): void
export function xxh64Alias(input: Buffer): BigInt
export function mapOption(val?: number | undefined | null): number | undefined | null
export function add(a: number, b: number): number
export function fibonacci(n: number): number
@ -87,3 +90,20 @@ export class ClassWithFactory {
name: string
static withName(name: string): ClassWithFactory
}
export namespace xxh3 {
export const ALIGNMENT: number
export function xxh3_64(input: Buffer): BigInt
export function xxh128(input: Buffer): BigInt
export class Xxh3 {
constructor()
update(input: Buffer): void
digest(): BigInt
}
}
export namespace xxh2 {
export function xxh2Plus(a: number, b: number): number
export function xxh3Xxh64Alias(input: Buffer): BigInt
}

View file

@ -0,0 +1,79 @@
#[napi]
mod xxh3 {
use napi::bindgen_prelude::{BigInt, Buffer};
#[napi]
pub const ALIGNMENT: u32 = 16;
#[napi(js_name = "xxh3_64")]
pub fn xxh64(input: Buffer) -> u64 {
let mut h: u64 = 0;
for i in input.as_ref() {
h = h.wrapping_add(*i as u64);
}
h
}
#[napi]
pub fn xxh128(input: Buffer) -> u128 {
let mut h: u128 = 0;
for i in input.as_ref() {
h = h.wrapping_add(*i as u128);
}
h
}
#[napi]
pub struct Xxh3 {
inner: BigInt,
}
#[napi]
impl Xxh3 {
#[napi(constructor)]
pub fn new() -> Xxh3 {
Xxh3 {
inner: BigInt {
sign_bit: false,
words: vec![0],
},
}
}
#[napi]
pub fn update(&mut self, input: Buffer) {
for i in input.as_ref() {
self.inner = BigInt {
sign_bit: false,
words: vec![self.inner.get_u64().1.wrapping_add(*i as u64)],
};
}
}
#[napi]
pub fn digest(&self) -> BigInt {
self.inner.clone()
}
}
}
#[napi]
mod xxh2 {
use napi::bindgen_prelude::*;
#[napi]
pub fn xxh2_plus(a: u32, b: u32) -> u32 {
a + b
}
#[napi]
pub fn xxh3_xxh64_alias(input: Buffer) -> u64 {
super::xxh3::xxh64(input)
}
}
use napi::bindgen_prelude::Buffer;
#[napi]
pub fn xxh64_alias(input: Buffer) -> u64 {
xxh3::xxh64(input)
}

View file

@ -16,6 +16,7 @@ mod either;
mod r#enum;
mod error;
mod external;
mod js_mod;
mod nullable;
mod number;
mod object;

View file

@ -5,7 +5,7 @@ use napi::Task;
struct DelaySum(u32, u32);
#[napi(task)]
#[napi]
impl Task for DelaySum {
type Output = u32;
type JsValue = u32;