Merge pull request #1011 from napi-rs/fix/either-should-receive-class

fix(napi-derive): implement missing trait for JsClass reference
This commit is contained in:
LongYinan 2022-01-06 16:36:59 +08:00 committed by GitHub
commit d2f3a96660
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 0 deletions

View file

@ -25,6 +25,26 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token
} }
} }
impl napi::bindgen_prelude::TypeName for &#name {
fn type_name() -> &'static str {
#name_str
}
fn value_type() -> napi::ValueType {
napi::ValueType::Object
}
}
impl napi::bindgen_prelude::TypeName for &mut #name {
fn type_name() -> &'static str {
#name_str
}
fn value_type() -> napi::ValueType {
napi::ValueType::Object
}
}
#to_napi_val_impl #to_napi_val_impl
impl napi::bindgen_prelude::FromNapiRef for #name { impl napi::bindgen_prelude::FromNapiRef for #name {
@ -60,6 +80,36 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token
Ok(&mut *(wrapped_val as *mut #name)) Ok(&mut *(wrapped_val as *mut #name))
} }
} }
impl napi::bindgen_prelude::FromNapiValue for &#name {
unsafe fn from_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
napi_val: napi::bindgen_prelude::sys::napi_value
) -> napi::bindgen_prelude::Result<Self> {
napi::bindgen_prelude::FromNapiRef::from_napi_ref(env, napi_val)
}
}
impl napi::bindgen_prelude::FromNapiValue for &mut #name {
unsafe fn from_napi_value(
env: napi::bindgen_prelude::sys::napi_env,
napi_val: napi::bindgen_prelude::sys::napi_value
) -> napi::bindgen_prelude::Result<Self> {
napi::bindgen_prelude::FromNapiMutRef::from_napi_mut_ref(env, napi_val)
}
}
impl napi::NapiRaw for &#name {
unsafe fn raw(&self) -> napi::sys::napi_value {
unreachable!()
}
}
impl napi::NapiRaw for &mut #name {
unsafe fn raw(&self) -> napi::sys::napi_value {
unreachable!()
}
}
} }
} }

View file

@ -40,6 +40,8 @@ Generated by [AVA](https://avajs.dev).
v: string | number␊ v: string | number␊
}␊ }␊
export function either4(input: string | number | boolean | Obj): number␊ export function either4(input: string | number | boolean | Obj): number␊
export function receiveClassOrNumber(either: number | JsClassForEither): number␊
export function receiveMutClassOrNumber(either: number | JsClassForEither): number␊
/** default enum values are continuos i32s start from 0 */␊ /** default enum values are continuos i32s start from 0 */␊
export const enum Kind {␊ export const enum Kind {␊
/** Barks */␊ /** Barks */␊
@ -200,6 +202,9 @@ Generated by [AVA](https://avajs.dev).
static withName(name: string): ClassWithFactory␊ static withName(name: string): ClassWithFactory␊
setName(name: string): this␊ setName(name: string): this␊
}␊ }␊
export class JsClassForEither {␊
constructor()␊
}␊
export namespace xxh3 {␊ export namespace xxh3 {␊
export const ALIGNMENT: number␊ export const ALIGNMENT: number␊
export function xxh3_64(input: Buffer): BigInt␊ export function xxh3_64(input: Buffer): BigInt␊

View file

@ -73,6 +73,9 @@ import {
Bird, Bird,
Assets, Assets,
receiveStrictObject, receiveStrictObject,
receiveClassOrNumber,
JsClassForEither,
receiveMutClassOrNumber,
} from '../' } from '../'
test('export const', (t) => { test('export const', (t) => {
@ -320,6 +323,13 @@ test('return either', (t) => {
t.is(returnEither(42), '42') t.is(returnEither(42), '42')
}) })
test('receive class reference in either', (t) => {
const c = new JsClassForEither()
t.is(receiveClassOrNumber(1), 2)
t.is(receiveClassOrNumber(c), 100)
t.is(receiveMutClassOrNumber(c), 100)
})
test('either3', (t) => { test('either3', (t) => {
t.is(either3(2), 2) t.is(either3(2), 2)
t.is(either3('hello'), 'hello'.length) t.is(either3('hello'), 'hello'.length)

View file

@ -30,6 +30,8 @@ export interface Obj {
v: string | number v: string | number
} }
export function either4(input: string | number | boolean | Obj): number export function either4(input: string | number | boolean | Obj): number
export function receiveClassOrNumber(either: number | JsClassForEither): number
export function receiveMutClassOrNumber(either: number | JsClassForEither): number
/** default enum values are continuos i32s start from 0 */ /** default enum values are continuos i32s start from 0 */
export const enum Kind { export const enum Kind {
/** Barks */ /** Barks */
@ -190,6 +192,9 @@ export class ClassWithFactory {
static withName(name: string): ClassWithFactory static withName(name: string): ClassWithFactory
setName(name: string): this setName(name: string): this
} }
export class JsClassForEither {
constructor()
}
export namespace xxh3 { export namespace xxh3 {
export const ALIGNMENT: number export const ALIGNMENT: number
export function xxh3_64(input: Buffer): BigInt export function xxh3_64(input: Buffer): BigInt

View file

@ -55,3 +55,30 @@ fn either4(input: Either4<String, u32, bool, Obj>) -> u32 {
}, },
} }
} }
#[napi]
struct JsClassForEither {}
#[napi]
impl JsClassForEither {
#[napi(constructor)]
pub fn new() -> Self {
JsClassForEither {}
}
}
#[napi]
fn receive_class_or_number(either: Either<u32, &JsClassForEither>) -> u32 {
match either {
Either::A(n) => n + 1,
Either::B(_) => 100,
}
}
#[napi]
fn receive_mut_class_or_number(either: Either<u32, &mut JsClassForEither>) -> u32 {
match either {
Either::A(n) => n + 1,
Either::B(_) => 100,
}
}