diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index dc2efb75..07718f4a 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -239,17 +239,37 @@ impl NapiStruct { for field in self.fields.iter() { let field_js_name = &field.js_name; let ty = &field.ty; - + let is_optional_field = if let syn::Type::Path(syn::TypePath { + path: syn::Path { segments, .. }, + .. + }) = &ty + { + if let Some(last_path) = segments.last() { + last_path.ident.to_string() == "Option" + } else { + false + } + } else { + false + }; match &field.name { syn::Member::Named(ident) => { field_destructions.push(quote! { #ident }); obj_field_setters.push(quote! { obj.set(#field_js_name, #ident)?; }); - obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); }); + if is_optional_field { + obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?; }); + } else { + obj_field_getters.push(quote! { let #ident: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); }); + } } syn::Member::Unnamed(i) => { field_destructions.push(quote! { arg#i }); obj_field_setters.push(quote! { obj.set(#field_js_name, arg#1)?; }); - obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); }); + if is_optional_field { + obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?; }); + } else { + obj_field_getters.push(quote! { let arg#i: #ty = obj.get(#field_js_name)?.expect(&format!("Field {} should exist", #field_js_name)); }); + } } } } diff --git a/crates/napi/src/bindgen_runtime/js_values/object.rs b/crates/napi/src/bindgen_runtime/js_values/object.rs index ef0b4d94..04870e59 100644 --- a/crates/napi/src/bindgen_runtime/js_values/object.rs +++ b/crates/napi/src/bindgen_runtime/js_values/object.rs @@ -35,7 +35,7 @@ impl Object { let ty = type_of!(self.0.env, ret)?; - Ok(if ty == ValueType::Undefined { + Ok(if ty == ValueType::Undefined || ty == ValueType::Null { None } else { Some(V::from_napi_value(self.0.env, ret)?) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a098885d..546498dd 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -73,6 +73,11 @@ Generated by [AVA](https://avajs.dev). export function getGlobal(): typeof global␊ export function getUndefined(): void␊ export function getNull(): JsNull␊ + export interface AllOptionalObject {␊ + name?: string | undefined | null␊ + age?: number | undefined | null␊ + }␊ + export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void␊ export function asyncPlus100(p: Promise): Promise␊ /** This is an interface for package.json */␊ export interface PackageJson {␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index f674e93d..8852cae6 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 570fbd3c..e270c751 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -59,6 +59,7 @@ import { convertU32Array, createExternalTypedArray, mutateTypedArray, + receiveAllOptionalObject, } from '../' test('export const', (t) => { @@ -202,6 +203,11 @@ test('function ts type override', (t) => { t.deepEqual(tsRename({ foo: 1, bar: 2, baz: 2 }), ['foo', 'bar', 'baz']) }) +test('option object', (t) => { + t.notThrows(() => receiveAllOptionalObject()) + t.notThrows(() => receiveAllOptionalObject({})) +}) + test('serde-json', (t) => { const packageJson = readPackageJson() t.is(packageJson.name, 'napi-rs') diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index f514c30d..216febe4 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -63,6 +63,11 @@ export function createObj(): object export function getGlobal(): typeof global export function getUndefined(): void export function getNull(): JsNull +export interface AllOptionalObject { + name?: string | undefined | null + age?: number | undefined | null +} +export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void export function asyncPlus100(p: Promise): Promise /** This is an interface for package.json */ export interface PackageJson { diff --git a/examples/napi/src/object.rs b/examples/napi/src/object.rs index 71c868a1..389ecefd 100644 --- a/examples/napi/src/object.rs +++ b/examples/napi/src/object.rs @@ -27,3 +27,18 @@ fn get_undefined(env: Env) -> Result { fn get_null(env: Env) -> Result { env.get_null() } + +#[napi(object)] +struct AllOptionalObject { + pub name: Option, + pub age: Option, +} + +#[napi] +fn receive_all_optional_object(obj: Option) -> Result<()> { + if !obj.is_none() { + assert!(obj.as_ref().unwrap().name.is_none()); + assert!(obj.as_ref().unwrap().age.is_none()); + } + Ok(()) +}