fix(napi-derive): implement missing trait for JsClass reference
This commit is contained in:
parent
b47ccbd194
commit
b6d126db53
6 changed files with 97 additions and 0 deletions
|
@ -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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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␊
|
||||||
|
|
Binary file not shown.
|
@ -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)
|
||||||
|
|
5
examples/napi/index.d.ts
vendored
5
examples/napi/index.d.ts
vendored
|
@ -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
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue