From 46f08ee6dd02ebf0b776cbf42e6f1124923faad7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 17 Jan 2023 00:05:19 +0800 Subject: [PATCH] fix(napi): missing From implementation for Bigint (#1440) --- .../src/bindgen_runtime/js_values/bigint.rs | 30 ++++++++++++++++++ examples/napi/__test__/typegen.spec.ts.md | 2 ++ examples/napi/__test__/typegen.spec.ts.snap | Bin 3645 -> 3657 bytes examples/napi/__test__/values.spec.ts | 7 ++++ examples/napi/index.d.ts | 2 ++ examples/napi/src/bigint.rs | 10 ++++++ 6 files changed, 51 insertions(+) diff --git a/crates/napi/src/bindgen_runtime/js_values/bigint.rs b/crates/napi/src/bindgen_runtime/js_values/bigint.rs index 67256488..5261e137 100644 --- a/crates/napi/src/bindgen_runtime/js_values/bigint.rs +++ b/crates/napi/src/bindgen_runtime/js_values/bigint.rs @@ -205,6 +205,15 @@ impl ToNapiValue for isize { } } +impl From for BigInt { + fn from(val: i64) -> Self { + BigInt { + sign_bit: val < 0, + words: vec![val as u64], + } + } +} + impl From for BigInt { fn from(val: u64) -> Self { BigInt { @@ -213,3 +222,24 @@ impl From for BigInt { } } } + +impl From for BigInt { + fn from(val: i128) -> Self { + let sign_bit = val < 0; + let words = (if sign_bit { -val } else { val }).to_ne_bytes(); + BigInt { + sign_bit, + words: unsafe { std::slice::from_raw_parts(words.as_ptr() as *mut _, 2).to_vec() }, + } + } +} + +impl From for BigInt { + fn from(val: u128) -> Self { + let words = val.to_ne_bytes(); + BigInt { + sign_bit: false, + words: unsafe { std::slice::from_raw_parts(words.as_ptr() as *mut _, 2).to_vec() }, + } + } +} diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index d104c6fb..76385fc9 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -34,6 +34,8 @@ Generated by [AVA](https://avajs.dev). export function createBigInt(): bigint␊ export function createBigIntI64(): bigint␊ export function bigintGetU64AsString(bi: bigint): string␊ + export function bigintFromI64(): bigint␊ + export function bigintFromI128(): bigint␊ export function getCwd(callback: (arg0: string) => void): void␊ export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊ export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 22ff5dba4642f40fade9205f867782c97f6a02c0..375b4dee2ebcc942c9b675e21db4fd4336c118c1 100644 GIT binary patch delta 3652 zcmV-K4!iNa9LXGiK~_N^Q*L2!b7*gLAa*kf0|0!i^C{o(z}p5C2Io~J2Qp~SO-PiU zJYYyaiW5knA(8UYaX$->w;v z@q!!=ULW3nKRwd`>?#$RB4JFWB>Pv2WeJT>#vg&MC!=TN`VJu(qmfAB1^M#JzV2vr zThbiBw2|B|x|L_74VcUXA`a zJUJWnNs`aUEVB@kJP8#S3CK^q6CFxlo&;=!ycker)n1oGh!{Wa!c-)l( zP@#cbBw>(JHG^%?qJf#OGco6qJ=x7ClP*pvPCCkC#YdMSpxM+e=FIg;Y0Rgfc!MYk zXuoKGY>=`2?Aedjybvf>Y?n_D6BVoncKB##38d);6?pS#XCTis@4=Wmlo)wnxz*Hn$!x0ffC9UPeJk<9JNN^FBe5J}mcxJbgwk1djj#{ds^Ev+0QbOxwL$S z&CVFmmb{P$MF*;G@ZiA%Kn#YBCX09PH4gY{pTB~@Q;owKgVD3VLL4S(t^l9LFanZ) zDwqx8u*b5j;?8U`$O~)^cnOz-I*JZ@QWUgCbeIea{6RS0v@{?dSb4lvm#I%maDZ7h zp&=v2^Br&&M?-Xphv1cH0t4vx#ypGqWEcJhch+sB%>1`p&GdAQ2u1C~+p$j};}_j{ zj5F~g>lMT`ai(!DPZAa=@VH`p4 zC|92w60FpZMwR*;Ak+R4vSe*DbV0a$uo;B9fX;o<}RwR<;EMrpw z-glg7@#UR_0Yeb2idJ0c5ue{;Mzx!_*9VKEG@jwu#<;6u6+87C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N@-gSJ+B5d zPO#0`?s&Ct>~=~~R;Owav(p)4I*x;-s*cWoJfquv)aDr?9Xw4k9nhv2>w!Rq^f#!t zZYQ(6xdI`O3YeqguA(K-(cfUgf;4#PoY{F?Tbz06EEh(8Ws0TntE-vO_}aD2;MKub zM~8byBS+&qE;t{3|9IPXyJ>eelNT=c3#bJ8Bu}ri>1vA?< z#MWkJ8EH4y({2UFBR++=KX6I7@yKq=m7UAb7G7tpxWnw0={GAcj&JV#A98v(Cv-Fb E01&h9C;$Ke diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index b6f38072..1747067c 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -114,6 +114,8 @@ import { plusOne, Width, captureErrorInCallback, + bigintFromI128, + bigintFromI64, } from '../' test('export const', (t) => { @@ -679,6 +681,11 @@ BigIntTest('js mod test', (t) => { t.is(xx3.digest(), BigInt('1116')) }) +BigIntTest('from i128 i64', (t) => { + t.is(bigintFromI64(), BigInt('100')) + t.is(bigintFromI128(), BigInt('-100')) +}) + const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip Napi4Test('call thread safe function', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 70132707..aab94c85 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -24,6 +24,8 @@ export function bigintAdd(a: bigint, b: bigint): bigint export function createBigInt(): bigint export function createBigIntI64(): bigint export function bigintGetU64AsString(bi: bigint): string +export function bigintFromI64(): bigint +export function bigintFromI128(): bigint export function getCwd(callback: (arg0: string) => void): void export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void diff --git a/examples/napi/src/bigint.rs b/examples/napi/src/bigint.rs index 6962c76e..b6e30b67 100644 --- a/examples/napi/src/bigint.rs +++ b/examples/napi/src/bigint.rs @@ -22,3 +22,13 @@ fn create_big_int_i64() -> i64n { pub fn bigint_get_u64_as_string(bi: BigInt) -> String { bi.get_u64().1.to_string() } + +#[napi] +pub fn bigint_from_i64() -> BigInt { + BigInt::from(100i64) +} + +#[napi] +pub fn bigint_from_i128() -> BigInt { + BigInt::from(-100i128) +}