Skip to content

Commit

Permalink
fix(napi-derive): object lifetime compile issue (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Nov 11, 2024
1 parent 75ff88c commit 49ed2dd
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 194 deletions.
24 changes: 22 additions & 2 deletions crates/backend/src/codegen/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ impl NapiStruct {

for field in obj.fields.iter() {
let field_js_name = &field.js_name;
let ty = &field.ty;
let mut ty = field.ty.clone();
remove_lifetime_in_type(&mut ty);
let is_optional_field = if let syn::Type::Path(syn::TypePath {
path: syn::Path { segments, .. },
..
Expand Down Expand Up @@ -913,7 +914,8 @@ impl NapiStruct {
let mut field_destructions = vec![];
for field in variant.fields.iter() {
let field_js_name = &field.js_name;
let ty = &field.ty;
let mut ty = field.ty.clone();
remove_lifetime_in_type(&mut ty);
let is_optional_field = if let syn::Type::Path(syn::TypePath {
path: syn::Path { segments, .. },
..
Expand Down Expand Up @@ -1202,3 +1204,21 @@ pub fn rm_raw_prefix(s: &str) -> &str {
s
}
}

fn remove_lifetime_in_type(ty: &mut syn::Type) {
if let syn::Type::Path(syn::TypePath { path, .. }) = ty {
path.segments.iter_mut().for_each(|segment| {
if let syn::PathArguments::AngleBracketed(ref mut args) = segment.arguments {
args.args.iter_mut().for_each(|arg| match arg {
syn::GenericArgument::Type(ref mut ty) => {
remove_lifetime_in_type(ty);
}
syn::GenericArgument::Lifetime(lifetime) => {
lifetime.ident = Ident::new("_", lifetime.ident.span());
}
_ => {}
});
}
});
}
}
6 changes: 6 additions & 0 deletions examples/napi/__tests__/__snapshots__/typegen.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ Generated by [AVA](https://avajs.dev).
Baz = 'Baz'␊
}␊
export interface Data {␊
data: string | Buffer␊
}␊
export interface DatesWithTimeZone {␊
start: Date␊
end?: Date␊
Expand Down Expand Up @@ -667,6 +671,8 @@ Generated by [AVA](https://avajs.dev).
export declare function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void␊
export declare function receiveBufferSliceWithLifetime(data: Data): number␊
export declare function receiveClassOrNumber(either: number | JsClassForEither): number␊
export declare function receiveDifferentClass(either: JsClassForEither | AnotherClassForEither): number␊
Expand Down
Binary file modified examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap
Binary file not shown.
Binary file modified examples/napi/__tests__/__snapshots__/values.spec.ts.snap
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/napi/__tests__/values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ import {
withinAsyncRuntimeIfAvailable,
errorMessageContainsNullByte,
returnCString,
receiveBufferSliceWithLifetime,
} from '../index.cjs'

import { test } from './test.framework.js'
Expand Down Expand Up @@ -613,6 +614,9 @@ test('object', (t) => {
'Failed to convert JavaScript value `Number 1 ` into rust type `String` on AllOptionalObject.name',
},
)

t.is(receiveBufferSliceWithLifetime({ data: 'foo' }), 3)
t.is(receiveBufferSliceWithLifetime({ data: Buffer.from('barz') }), 4)
})

test('get str from object', (t) => {
Expand Down
195 changes: 99 additions & 96 deletions examples/napi/example.wasi-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,102 +287,104 @@ function __napi_rs_initialize_modules(__napiInstance) {
__napiInstance.exports['__napi_register__ObjectOnlyToJs_struct_280']?.()
__napiInstance.exports['__napi_register__return_object_only_to_js_281']?.()
__napiInstance.exports['__napi_register__TupleObject_struct_282']?.()
__napiInstance.exports['__napi_register__async_plus_100_283']?.()
__napiInstance.exports['__napi_register__call_then_on_promise_284']?.()
__napiInstance.exports['__napi_register__call_catch_on_promise_285']?.()
__napiInstance.exports['__napi_register__call_finally_on_promise_286']?.()
__napiInstance.exports['__napi_register__JsRepo_struct_287']?.()
__napiInstance.exports['__napi_register__JsRepo_impl_290']?.()
__napiInstance.exports['__napi_register__JsRemote_struct_291']?.()
__napiInstance.exports['__napi_register__JsRemote_impl_294']?.()
__napiInstance.exports['__napi_register__CSSRuleList_struct_295']?.()
__napiInstance.exports['__napi_register__CSSRuleList_impl_299']?.()
__napiInstance.exports['__napi_register__CSSStyleSheet_struct_300']?.()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_301']?.()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_303']?.()
__napiInstance.exports['__napi_register__CSSStyleSheet_impl_307']?.()
__napiInstance.exports['__napi_register__PackageJson_struct_308']?.()
__napiInstance.exports['__napi_register__read_package_json_309']?.()
__napiInstance.exports['__napi_register__get_package_json_name_310']?.()
__napiInstance.exports['__napi_register__test_serde_roundtrip_311']?.()
__napiInstance.exports['__napi_register__test_serde_big_number_precision_312']?.()
__napiInstance.exports['__napi_register__test_serde_buffer_bytes_313']?.()
__napiInstance.exports['__napi_register__pass_set_to_rust_314']?.()
__napiInstance.exports['__napi_register__pass_set_to_js_315']?.()
__napiInstance.exports['__napi_register__btree_set_to_rust_316']?.()
__napiInstance.exports['__napi_register__btree_set_to_js_317']?.()
__napiInstance.exports['__napi_register__return_from_shared_crate_318']?.()
__napiInstance.exports['__napi_register__contains_319']?.()
__napiInstance.exports['__napi_register__concat_str_320']?.()
__napiInstance.exports['__napi_register__concat_utf16_321']?.()
__napiInstance.exports['__napi_register__concat_latin1_322']?.()
__napiInstance.exports['__napi_register__roundtrip_str_323']?.()
__napiInstance.exports['__napi_register__return_c_string_324']?.()
__napiInstance.exports['__napi_register__set_symbol_in_obj_325']?.()
__napiInstance.exports['__napi_register__create_symbol_326']?.()
__napiInstance.exports['__napi_register__create_symbol_for_327']?.()
__napiInstance.exports['__napi_register__DelaySum_impl_328']?.()
__napiInstance.exports['__napi_register__without_abort_controller_329']?.()
__napiInstance.exports['__napi_register__with_abort_controller_330']?.()
__napiInstance.exports['__napi_register__AsyncTaskVoidReturn_impl_331']?.()
__napiInstance.exports['__napi_register__async_task_void_return_332']?.()
__napiInstance.exports['__napi_register__AsyncTaskOptionalReturn_impl_333']?.()
__napiInstance.exports['__napi_register__async_task_optional_return_334']?.()
__napiInstance.exports['__napi_register__AsyncTaskReadFile_impl_335']?.()
__napiInstance.exports['__napi_register__async_task_read_file_336']?.()
__napiInstance.exports['__napi_register__call_threadsafe_function_337']?.()
__napiInstance.exports['__napi_register__call_long_threadsafe_function_338']?.()
__napiInstance.exports['__napi_register__threadsafe_function_throw_error_339']?.()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_340']?.()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_341']?.()
__napiInstance.exports['__napi_register__threadsafe_function_closure_capture_342']?.()
__napiInstance.exports['__napi_register__tsfn_call_with_callback_343']?.()
__napiInstance.exports['__napi_register__tsfn_async_call_344']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_345']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_346']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_347']?.()
__napiInstance.exports['__napi_register__tsfn_return_promise_348']?.()
__napiInstance.exports['__napi_register__tsfn_return_promise_timeout_349']?.()
__napiInstance.exports['__napi_register__tsfn_throw_from_js_350']?.()
__napiInstance.exports['__napi_register__spawn_thread_in_thread_351']?.()
__napiInstance.exports['__napi_register__Pet_struct_352']?.()
__napiInstance.exports['__napi_register__tsfn_in_either_353']?.()
__napiInstance.exports['__napi_register__get_buffer_354']?.()
__napiInstance.exports['__napi_register__get_buffer_slice_355']?.()
__napiInstance.exports['__napi_register__append_buffer_356']?.()
__napiInstance.exports['__napi_register__get_empty_buffer_357']?.()
__napiInstance.exports['__napi_register__create_external_buffer_slice_358']?.()
__napiInstance.exports['__napi_register__create_buffer_slice_from_copied_data_359']?.()
__napiInstance.exports['__napi_register__get_empty_typed_array_360']?.()
__napiInstance.exports['__napi_register__convert_u32_array_361']?.()
__napiInstance.exports['__napi_register__create_external_typed_array_362']?.()
__napiInstance.exports['__napi_register__mutate_typed_array_363']?.()
__napiInstance.exports['__napi_register__deref_uint8_array_364']?.()
__napiInstance.exports['__napi_register__buffer_pass_through_365']?.()
__napiInstance.exports['__napi_register__buffer_with_async_block_366']?.()
__napiInstance.exports['__napi_register__array_buffer_pass_through_367']?.()
__napiInstance.exports['__napi_register__accept_slice_368']?.()
__napiInstance.exports['__napi_register__accept_arraybuffer_369']?.()
__napiInstance.exports['__napi_register__create_arraybuffer_370']?.()
__napiInstance.exports['__napi_register__u8_array_to_array_371']?.()
__napiInstance.exports['__napi_register__i8_array_to_array_372']?.()
__napiInstance.exports['__napi_register__u16_array_to_array_373']?.()
__napiInstance.exports['__napi_register__i16_array_to_array_374']?.()
__napiInstance.exports['__napi_register__u32_array_to_array_375']?.()
__napiInstance.exports['__napi_register__i32_array_to_array_376']?.()
__napiInstance.exports['__napi_register__f32_array_to_array_377']?.()
__napiInstance.exports['__napi_register__f64_array_to_array_378']?.()
__napiInstance.exports['__napi_register__u64_array_to_array_379']?.()
__napiInstance.exports['__napi_register__i64_array_to_array_380']?.()
__napiInstance.exports['__napi_register__accept_uint8_clamped_slice_381']?.()
__napiInstance.exports['__napi_register__accept_uint8_clamped_slice_and_buffer_slice_382']?.()
__napiInstance.exports['__napi_register__AsyncBuffer_impl_383']?.()
__napiInstance.exports['__napi_register__async_reduce_buffer_384']?.()
__napiInstance.exports['__napi_register__async_buffer_to_array_385']?.()
__napiInstance.exports['__napi_register__u_init8_array_from_string_386']?.()
__napiInstance.exports['__napi_register__AsyncReader_impl_387']?.()
__napiInstance.exports['__napi_register__Reader_struct_388']?.()
__napiInstance.exports['__napi_register__Reader_impl_390']?.()
__napiInstance.exports['__napi_register__Data_struct_283']?.()
__napiInstance.exports['__napi_register__receive_buffer_slice_with_lifetime_284']?.()
__napiInstance.exports['__napi_register__async_plus_100_285']?.()
__napiInstance.exports['__napi_register__call_then_on_promise_286']?.()
__napiInstance.exports['__napi_register__call_catch_on_promise_287']?.()
__napiInstance.exports['__napi_register__call_finally_on_promise_288']?.()
__napiInstance.exports['__napi_register__JsRepo_struct_289']?.()
__napiInstance.exports['__napi_register__JsRepo_impl_292']?.()
__napiInstance.exports['__napi_register__JsRemote_struct_293']?.()
__napiInstance.exports['__napi_register__JsRemote_impl_296']?.()
__napiInstance.exports['__napi_register__CSSRuleList_struct_297']?.()
__napiInstance.exports['__napi_register__CSSRuleList_impl_301']?.()
__napiInstance.exports['__napi_register__CSSStyleSheet_struct_302']?.()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_303']?.()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_305']?.()
__napiInstance.exports['__napi_register__CSSStyleSheet_impl_309']?.()
__napiInstance.exports['__napi_register__PackageJson_struct_310']?.()
__napiInstance.exports['__napi_register__read_package_json_311']?.()
__napiInstance.exports['__napi_register__get_package_json_name_312']?.()
__napiInstance.exports['__napi_register__test_serde_roundtrip_313']?.()
__napiInstance.exports['__napi_register__test_serde_big_number_precision_314']?.()
__napiInstance.exports['__napi_register__test_serde_buffer_bytes_315']?.()
__napiInstance.exports['__napi_register__pass_set_to_rust_316']?.()
__napiInstance.exports['__napi_register__pass_set_to_js_317']?.()
__napiInstance.exports['__napi_register__btree_set_to_rust_318']?.()
__napiInstance.exports['__napi_register__btree_set_to_js_319']?.()
__napiInstance.exports['__napi_register__return_from_shared_crate_320']?.()
__napiInstance.exports['__napi_register__contains_321']?.()
__napiInstance.exports['__napi_register__concat_str_322']?.()
__napiInstance.exports['__napi_register__concat_utf16_323']?.()
__napiInstance.exports['__napi_register__concat_latin1_324']?.()
__napiInstance.exports['__napi_register__roundtrip_str_325']?.()
__napiInstance.exports['__napi_register__return_c_string_326']?.()
__napiInstance.exports['__napi_register__set_symbol_in_obj_327']?.()
__napiInstance.exports['__napi_register__create_symbol_328']?.()
__napiInstance.exports['__napi_register__create_symbol_for_329']?.()
__napiInstance.exports['__napi_register__DelaySum_impl_330']?.()
__napiInstance.exports['__napi_register__without_abort_controller_331']?.()
__napiInstance.exports['__napi_register__with_abort_controller_332']?.()
__napiInstance.exports['__napi_register__AsyncTaskVoidReturn_impl_333']?.()
__napiInstance.exports['__napi_register__async_task_void_return_334']?.()
__napiInstance.exports['__napi_register__AsyncTaskOptionalReturn_impl_335']?.()
__napiInstance.exports['__napi_register__async_task_optional_return_336']?.()
__napiInstance.exports['__napi_register__AsyncTaskReadFile_impl_337']?.()
__napiInstance.exports['__napi_register__async_task_read_file_338']?.()
__napiInstance.exports['__napi_register__call_threadsafe_function_339']?.()
__napiInstance.exports['__napi_register__call_long_threadsafe_function_340']?.()
__napiInstance.exports['__napi_register__threadsafe_function_throw_error_341']?.()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_342']?.()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_343']?.()
__napiInstance.exports['__napi_register__threadsafe_function_closure_capture_344']?.()
__napiInstance.exports['__napi_register__tsfn_call_with_callback_345']?.()
__napiInstance.exports['__napi_register__tsfn_async_call_346']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_347']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_348']?.()
__napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_349']?.()
__napiInstance.exports['__napi_register__tsfn_return_promise_350']?.()
__napiInstance.exports['__napi_register__tsfn_return_promise_timeout_351']?.()
__napiInstance.exports['__napi_register__tsfn_throw_from_js_352']?.()
__napiInstance.exports['__napi_register__spawn_thread_in_thread_353']?.()
__napiInstance.exports['__napi_register__Pet_struct_354']?.()
__napiInstance.exports['__napi_register__tsfn_in_either_355']?.()
__napiInstance.exports['__napi_register__get_buffer_356']?.()
__napiInstance.exports['__napi_register__get_buffer_slice_357']?.()
__napiInstance.exports['__napi_register__append_buffer_358']?.()
__napiInstance.exports['__napi_register__get_empty_buffer_359']?.()
__napiInstance.exports['__napi_register__create_external_buffer_slice_360']?.()
__napiInstance.exports['__napi_register__create_buffer_slice_from_copied_data_361']?.()
__napiInstance.exports['__napi_register__get_empty_typed_array_362']?.()
__napiInstance.exports['__napi_register__convert_u32_array_363']?.()
__napiInstance.exports['__napi_register__create_external_typed_array_364']?.()
__napiInstance.exports['__napi_register__mutate_typed_array_365']?.()
__napiInstance.exports['__napi_register__deref_uint8_array_366']?.()
__napiInstance.exports['__napi_register__buffer_pass_through_367']?.()
__napiInstance.exports['__napi_register__buffer_with_async_block_368']?.()
__napiInstance.exports['__napi_register__array_buffer_pass_through_369']?.()
__napiInstance.exports['__napi_register__accept_slice_370']?.()
__napiInstance.exports['__napi_register__accept_arraybuffer_371']?.()
__napiInstance.exports['__napi_register__create_arraybuffer_372']?.()
__napiInstance.exports['__napi_register__u8_array_to_array_373']?.()
__napiInstance.exports['__napi_register__i8_array_to_array_374']?.()
__napiInstance.exports['__napi_register__u16_array_to_array_375']?.()
__napiInstance.exports['__napi_register__i16_array_to_array_376']?.()
__napiInstance.exports['__napi_register__u32_array_to_array_377']?.()
__napiInstance.exports['__napi_register__i32_array_to_array_378']?.()
__napiInstance.exports['__napi_register__f32_array_to_array_379']?.()
__napiInstance.exports['__napi_register__f64_array_to_array_380']?.()
__napiInstance.exports['__napi_register__u64_array_to_array_381']?.()
__napiInstance.exports['__napi_register__i64_array_to_array_382']?.()
__napiInstance.exports['__napi_register__accept_uint8_clamped_slice_383']?.()
__napiInstance.exports['__napi_register__accept_uint8_clamped_slice_and_buffer_slice_384']?.()
__napiInstance.exports['__napi_register__AsyncBuffer_impl_385']?.()
__napiInstance.exports['__napi_register__async_reduce_buffer_386']?.()
__napiInstance.exports['__napi_register__async_buffer_to_array_387']?.()
__napiInstance.exports['__napi_register__u_init8_array_from_string_388']?.()
__napiInstance.exports['__napi_register__AsyncReader_impl_389']?.()
__napiInstance.exports['__napi_register__Reader_struct_390']?.()
__napiInstance.exports['__napi_register__Reader_impl_392']?.()
}
export const Animal = __napiModule.exports.Animal
export const AnimalWithDefaultConstructor = __napiModule.exports.AnimalWithDefaultConstructor
Expand Down Expand Up @@ -572,6 +574,7 @@ export const readFile = __napiModule.exports.readFile
export const readFileAsync = __napiModule.exports.readFileAsync
export const readPackageJson = __napiModule.exports.readPackageJson
export const receiveAllOptionalObject = __napiModule.exports.receiveAllOptionalObject
export const receiveBufferSliceWithLifetime = __napiModule.exports.receiveBufferSliceWithLifetime
export const receiveClassOrNumber = __napiModule.exports.receiveClassOrNumber
export const receiveDifferentClass = __napiModule.exports.receiveDifferentClass
export const receiveMutClassOrNumber = __napiModule.exports.receiveMutClassOrNumber
Expand Down
Loading

0 comments on commit 49ed2dd

Please sign in to comment.