feat(napi): BigInt codegen support

This commit is contained in:
LongYinan 2021-11-11 00:33:51 +08:00
parent 38d2645632
commit 0424a08c06
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
18 changed files with 313 additions and 53 deletions

View file

@ -1,28 +1,28 @@
use napi::{CallContext, JsBigint, JsNumber, JsObject, Result};
use napi::{CallContext, JsBigInt, JsNumber, JsObject, Result};
use std::convert::TryFrom;
#[js_function]
pub fn test_create_bigint_from_i64(ctx: CallContext) -> Result<JsBigint> {
pub fn test_create_bigint_from_i64(ctx: CallContext) -> Result<JsBigInt> {
ctx.env.create_bigint_from_i64(i64::max_value())
}
#[js_function]
pub fn test_create_bigint_from_u64(ctx: CallContext) -> Result<JsBigint> {
pub fn test_create_bigint_from_u64(ctx: CallContext) -> Result<JsBigInt> {
ctx.env.create_bigint_from_u64(u64::max_value())
}
#[js_function]
pub fn test_create_bigint_from_i128(ctx: CallContext) -> Result<JsBigint> {
pub fn test_create_bigint_from_i128(ctx: CallContext) -> Result<JsBigInt> {
ctx.env.create_bigint_from_i128(i128::max_value())
}
#[js_function]
pub fn test_create_bigint_from_u128(ctx: CallContext) -> Result<JsBigint> {
pub fn test_create_bigint_from_u128(ctx: CallContext) -> Result<JsBigInt> {
ctx.env.create_bigint_from_u128(u128::max_value())
}
#[js_function]
pub fn test_create_bigint_from_words(ctx: CallContext) -> Result<JsBigint> {
pub fn test_create_bigint_from_words(ctx: CallContext) -> Result<JsBigInt> {
ctx
.env
.create_bigint_from_words(true, vec![u64::max_value(), u64::max_value()])
@ -30,14 +30,14 @@ pub fn test_create_bigint_from_words(ctx: CallContext) -> Result<JsBigint> {
#[js_function(1)]
pub fn test_get_bigint_i64(ctx: CallContext) -> Result<JsNumber> {
let js_bigint = ctx.get::<JsBigint>(0)?;
let js_bigint = ctx.get::<JsBigInt>(0)?;
let val = i64::try_from(js_bigint)?;
ctx.env.create_int32(val as i32)
}
#[js_function(1)]
pub fn test_get_bigint_u64(ctx: CallContext) -> Result<JsNumber> {
let js_bigint = ctx.get::<JsBigint>(0)?;
let js_bigint = ctx.get::<JsBigInt>(0)?;
let val = u64::try_from(js_bigint)?;
ctx.env.create_int32(val as i32)
}

View file

@ -13,6 +13,9 @@ Generated by [AVA](https://avajs.dev).
export function sumNums(nums: Array<number>): number␊
export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
export function bigintAdd(a: BigInt, b: BigInt): BigInt␊
export function createBigInt(): BigInt␊
export function createBigIntI64(): BigInt␊
export function getCwd(callback: (arg0: string) => void): void␊
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊
export function eitherStringOrNumber(input: string | number): number␊

View file

@ -35,6 +35,9 @@ import {
withoutAbortController,
withAbortController,
asyncMultiTwo,
bigintAdd,
createBigInt,
createBigIntI64,
} from '../'
test('number', (t) => {
@ -217,3 +220,17 @@ MaybeTest('abort resolved task', async (t) => {
await withAbortController(1, 2, ctrl.signal).then(() => ctrl.abort())
t.pass('should not throw')
})
const BigIntTest = typeof BigInt !== 'undefined' ? test : test.skip
BigIntTest('BigInt add', (t) => {
t.is(bigintAdd(BigInt(1), BigInt(2)), BigInt(3))
})
BigIntTest('create BigInt', (t) => {
t.is(createBigInt(), BigInt('-3689348814741910323300'))
})
BigIntTest('create BigInt i64', (t) => {
t.is(createBigIntI64(), BigInt(100))
})

View file

@ -3,6 +3,9 @@ export function getNums(): Array<number>
export function sumNums(nums: Array<number>): number
export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
export function bigintAdd(a: BigInt, b: BigInt): BigInt
export function createBigInt(): BigInt
export function createBigIntI64(): BigInt
export function getCwd(callback: (arg0: string) => void): void
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
export function eitherStringOrNumber(input: string | number): number

View file

@ -0,0 +1,19 @@
use napi::bindgen_prelude::*;
#[napi]
fn bigint_add(a: BigInt, b: BigInt) -> u128 {
a.get_u128().1 + b.get_u128().1
}
#[napi]
fn create_big_int() -> BigInt {
BigInt {
words: vec![100u64, 200u64],
sign_bit: true,
}
}
#[napi]
fn create_big_int_i64() -> i64n {
i64n(100)
}

View file

@ -5,6 +5,7 @@ extern crate serde_derive;
mod array;
mod r#async;
mod bigint;
mod callback;
mod class;
mod class_factory;

View file

@ -4,7 +4,7 @@
"compilerOptions": {
"outDir": "./dist",
"rootDir": "__test__",
"target": "ES2015"
"target": "ES2018"
},
"exclude": ["dist", "index.d.ts"]
}