diff --git a/crates/napi/src/bindgen_runtime/js_values.rs b/crates/napi/src/bindgen_runtime/js_values.rs index 301a7208..27bf6c6f 100644 --- a/crates/napi/src/bindgen_runtime/js_values.rs +++ b/crates/napi/src/bindgen_runtime/js_values.rs @@ -126,7 +126,7 @@ pub trait FromNapiMutRef { ) -> Result<&'static mut Self>; } -pub trait ValidateNapiValue: FromNapiValue + TypeName { +pub trait ValidateNapiValue: TypeName { /// # Safety /// /// this function called to validate whether napi value passed to rust is valid type diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index f529533c..ed0416e5 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -522,6 +522,11 @@ Generated by [AVA](https://avajs.dev). callback: (err: Error | null, arg: number) => any␊ }␊ ␊ + export interface ObjectOnlyToJs {␊ + name: number␊ + dependencies: any␊ + }␊ + ␊ export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊ ␊ export function optionOnly(callback: (arg0?: string | undefined | null) => void): void␊ @@ -584,6 +589,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function returnNull(): null␊ ␊ + export function returnObjectOnlyToJs(): ObjectOnlyToJs␊ + ␊ export function returnUndefined(): void␊ ␊ export function returnUndefinedIfInvalid(input: boolean): boolean␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 1d5b5c85..4855e659 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/__tests__/values.spec.ts b/examples/napi/__tests__/values.spec.ts index a0be79d8..76e68131 100644 --- a/examples/napi/__tests__/values.spec.ts +++ b/examples/napi/__tests__/values.spec.ts @@ -169,6 +169,7 @@ import { getModuleFileName, throwSyntaxError, type AliasedStruct, + returnObjectOnlyToJs, } from '../index.cjs' import { test } from './test.framework.js' @@ -518,6 +519,13 @@ test('object', (t) => { bar: '3', }), ) + t.deepEqual(returnObjectOnlyToJs(), { + name: 42, + dependencies: { + '@napi-rs/cli': '^3.0.0', + rollup: '^4.0.0', + }, + }) }) test('get str from object', (t) => { diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index 403224b3..cc92f5d1 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -501,6 +501,7 @@ module.exports.returnEitherClass = nativeBinding.returnEitherClass module.exports.returnFromSharedCrate = nativeBinding.returnFromSharedCrate module.exports.returnJsFunction = nativeBinding.returnJsFunction module.exports.returnNull = nativeBinding.returnNull +module.exports.returnObjectOnlyToJs = nativeBinding.returnObjectOnlyToJs module.exports.returnUndefined = nativeBinding.returnUndefined module.exports.returnUndefinedIfInvalid = nativeBinding.returnUndefinedIfInvalid module.exports.returnUndefinedIfInvalidPromise = nativeBinding.returnUndefinedIfInvalidPromise diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index 40f22290..24feb183 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -512,6 +512,11 @@ export interface ObjectOnlyFromJs { callback: (err: Error | null, arg: number) => any } +export interface ObjectOnlyToJs { + name: number + dependencies: any +} + export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void export function optionOnly(callback: (arg0?: string | undefined | null) => void): void @@ -574,6 +579,8 @@ export function returnJsFunction(): (...args: any[]) => any export function returnNull(): null +export function returnObjectOnlyToJs(): ObjectOnlyToJs + export function returnUndefined(): void export function returnUndefinedIfInvalid(input: boolean): boolean diff --git a/examples/napi/src/object.rs b/examples/napi/src/object.rs index 480b0f60..4f85284f 100644 --- a/examples/napi/src/object.rs +++ b/examples/napi/src/object.rs @@ -131,3 +131,17 @@ fn object_get_named_property_should_perform_typecheck(obj: Object) -> Result<()> obj.get_named_property::("bar")?; Ok(()) } + +#[napi(object, object_from_js = false)] +struct ObjectOnlyToJs { + pub name: u32, + pub dependencies: serde_json::Value, +} + +#[napi] +fn return_object_only_to_js() -> ObjectOnlyToJs { + ObjectOnlyToJs { + name: 42, + dependencies: serde_json::json!({ "@napi-rs/cli": "^3.0.0", "rollup": "^4.0.0" }), + } +}