feat(napi): support into_instance in class struct
This commit is contained in:
parent
717f96acfc
commit
c1e07b3c12
9 changed files with 127 additions and 1 deletions
examples/napi
|
@ -41,6 +41,11 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊
|
||||
export function returnJsFunction(): (...args: any[]) => any␊
|
||||
export function callbackReturnPromise<T>(functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void): T | Promise<T>␊
|
||||
export interface ObjectFieldClassInstance {␊
|
||||
bird: Bird␊
|
||||
}␊
|
||||
export function createObjectWithClassField(): ObjectFieldClassInstance␊
|
||||
export function receiveObjectWithClassField(object: ObjectFieldClassInstance): Bird␊
|
||||
export function dateToNumber(input: Date): number␊
|
||||
export function chronoDateToMillis(input: Date): number␊
|
||||
export function chronoDateAdd1Minute(input: Date): Date␊
|
||||
|
|
Binary file not shown.
|
@ -97,6 +97,8 @@ import {
|
|||
eitherFromOption,
|
||||
overrideIndividualArgOnFunction,
|
||||
overrideIndividualArgOnFunctionWithCbArg,
|
||||
createObjectWithClassField,
|
||||
receiveObjectWithClassField,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -206,6 +208,12 @@ test('class Factory return Result', (t) => {
|
|||
t.is(c.method(), 'not empty')
|
||||
})
|
||||
|
||||
test('class in object field', (t) => {
|
||||
const obj = createObjectWithClassField()
|
||||
t.is(obj.bird.name, 'Carolyn')
|
||||
t.is(receiveObjectWithClassField(obj), obj.bird)
|
||||
})
|
||||
|
||||
test('should be able to create object reference and shared reference', (t) => {
|
||||
const repo = new JsRepo('.')
|
||||
t.is(repo.remote().name(), 'origin')
|
||||
|
|
5
examples/napi/index.d.ts
vendored
5
examples/napi/index.d.ts
vendored
|
@ -31,6 +31,11 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void)
|
|||
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void
|
||||
export function returnJsFunction(): (...args: any[]) => any
|
||||
export function callbackReturnPromise<T>(functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void): T | Promise<T>
|
||||
export interface ObjectFieldClassInstance {
|
||||
bird: Bird
|
||||
}
|
||||
export function createObjectWithClassField(): ObjectFieldClassInstance
|
||||
export function receiveObjectWithClassField(object: ObjectFieldClassInstance): Bird
|
||||
export function dateToNumber(input: Date): number
|
||||
export function chronoDateToMillis(input: Date): number
|
||||
export function chronoDateAdd1Minute(input: Date): Date
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use napi::{bindgen_prelude::Buffer, Result};
|
||||
use napi::{
|
||||
bindgen_prelude::{Buffer, ClassInstance},
|
||||
Env, Result,
|
||||
};
|
||||
|
||||
use crate::r#enum::Kind;
|
||||
|
||||
|
@ -305,3 +308,25 @@ impl Optional {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct ObjectFieldClassInstance {
|
||||
pub bird: ClassInstance<Bird>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn create_object_with_class_field(env: Env) -> Result<ObjectFieldClassInstance> {
|
||||
Ok(ObjectFieldClassInstance {
|
||||
bird: Bird {
|
||||
name: "Carolyn".to_owned(),
|
||||
}
|
||||
.into_instance(env)?,
|
||||
})
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn receive_object_with_class_field(
|
||||
object: ObjectFieldClassInstance,
|
||||
) -> Result<ClassInstance<Bird>> {
|
||||
Ok(object.bird)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue