11use crate :: javascript:: escape_js_ident;
2- use crate :: rust_bindgen:: escape_rust_ident ;
2+ use crate :: rust_bindgen:: RustWitFunction ;
33use crate :: types:: {
44 ProcessedParameter , WrappedType , get_function_name, get_wrapped_type,
5- ident_in_exported_interface, ident_in_exported_interface_or_global, identity_wrapper,
6- param_refs_as_tuple, process_parameter, to_original_func_arg_list, to_wrapped_param_refs,
7- type_borrows_resource,
5+ ident_in_exported_interface, ident_in_exported_interface_or_global, param_refs_as_tuple,
6+ process_parameter, to_original_func_arg_list, to_wrapped_param_refs, type_borrows_resource,
87} ;
98use crate :: { EmbeddingMode , GeneratorContext , JsModuleSpec } ;
109use anyhow:: { Context , anyhow} ;
11- use heck:: { ToLowerCamelCase , ToSnakeCase , ToUpperCamelCase } ;
10+ use heck:: { ToLowerCamelCase , ToUpperCamelCase } ;
1211use proc_macro2:: { Ident , Span , TokenStream } ;
1312use quote:: quote;
1413use std:: collections:: BTreeMap ;
@@ -294,18 +293,32 @@ fn generate_exported_function_impl(
294293 name : & str ,
295294 function : & Function ,
296295) -> anyhow:: Result < TokenStream > {
297- let func_name = Ident :: new ( & escape_rust_ident ( & name. to_snake_case ( ) ) , Span :: call_site ( ) ) ;
296+ let rust_fn = RustWitFunction :: new ( context, name, function) ;
297+ let func_name = rust_fn. function_name_ident ( ) ;
298+
298299 let param_ident_type: Vec < _ > = function
299300 . params
300301 . iter ( )
301- . map ( |( param_name, param_type) | process_parameter ( context, param_name, param_type) )
302+ . zip ( rust_fn. export_parameters )
303+ . zip ( rust_fn. import_parameters )
304+ . map (
305+ |( ( ( param_name, param_type) , export_parameter) , import_parameter) | {
306+ process_parameter (
307+ context,
308+ param_name,
309+ param_type,
310+ & export_parameter,
311+ & import_parameter,
312+ )
313+ } ,
314+ )
302315 . collect :: < anyhow:: Result < Vec < _ > > > ( ) ?;
303316
304317 let func_arg_list = to_original_func_arg_list ( & param_ident_type) ;
305318 let func_ret = match & function. result {
306- Some ( typ) => get_wrapped_type ( context, typ)
319+ Some ( typ) => get_wrapped_type ( context, & rust_fn . return_type , & rust_fn . return_type , typ)
307320 . context ( format ! ( "Failed to encode result type for {name}" ) ) ?,
308- None => WrappedType :: unit ( false ) ,
321+ None => WrappedType :: unit ( ) ,
309322 } ;
310323
311324 let param_refs = to_wrapped_param_refs ( & param_ident_type) ;
@@ -346,7 +359,7 @@ fn generate_exported_function_impl(
346359 let original_result = & func_ret. original_type_ref ;
347360 let wrapped_result = & func_ret. wrapped_type_ref ;
348361 let unwrap = & func_ret. unwrap ;
349- let unwrap_result = ( unwrap) ( quote ! { result } ) ;
362+ let unwrap_result = unwrap. run ( quote ! { result } ) ;
350363 let func_impl = quote ! {
351364 fn #func_name( #( #func_arg_list) , * ) -> #original_result {
352365 crate :: internal:: async_exported_function( async move {
@@ -371,15 +384,16 @@ fn generate_exported_resource_function_impl(
371384 function : & Function ,
372385) -> anyhow:: Result < TokenStream > {
373386 let func_name = get_function_name ( name, & function) ?;
374- let func_name_ident = Ident :: new (
375- & escape_rust_ident ( & func_name. to_snake_case ( ) ) ,
376- Span :: call_site ( ) ,
377- ) ;
387+
388+ let rust_fn = RustWitFunction :: new ( context, & func_name, function) ;
389+ let func_name_ident = rust_fn. function_name_ident ( ) ;
378390
379391 let param_ident_type: Vec < _ > = function
380392 . params
381393 . iter ( )
382- . map ( |( param_name, param_type) | {
394+ . zip ( rust_fn. export_parameters )
395+ . zip ( rust_fn. import_parameters )
396+ . map ( |( ( ( param_name, param_type) , export_param) , import_param) | {
383397 if matches ! (
384398 function. kind,
385399 FunctionKind :: Method ( _) | FunctionKind :: AsyncMethod ( _)
@@ -388,21 +402,29 @@ fn generate_exported_resource_function_impl(
388402 Ok ( ProcessedParameter {
389403 ident : Ident :: new ( param_name, Span :: call_site ( ) ) ,
390404 wrapped_type : None ,
405+ export_parameter : export_param,
406+ import_parameter : import_param,
391407 } )
392408 } else {
393- process_parameter ( context, param_name, param_type)
409+ process_parameter (
410+ context,
411+ param_name,
412+ param_type,
413+ & export_param,
414+ & import_param,
415+ )
394416 }
395417 } )
396418 . collect :: < anyhow:: Result < Vec < _ > > > ( ) ?;
397419
398420 let func_arg_list = to_original_func_arg_list ( & param_ident_type) ;
399421 let func_ret = if matches ! ( function. kind, FunctionKind :: Constructor ( _) ) {
400- WrappedType :: no_wrapping ( quote ! { Self } , identity_wrapper ( ) )
422+ WrappedType :: no_wrapping ( quote ! { Self } )
401423 } else {
402424 match & function. result {
403- Some ( typ) => get_wrapped_type ( context, typ)
425+ Some ( typ) => get_wrapped_type ( context, & rust_fn . return_type , & rust_fn . return_type , typ)
404426 . context ( format ! ( "Failed to encode result type for {name}" ) ) ?,
405- None => WrappedType :: unit ( false ) ,
427+ None => WrappedType :: unit ( ) ,
406428 }
407429 } ;
408430
@@ -489,7 +511,7 @@ fn generate_exported_resource_function_impl(
489511 let original_result = & func_ret. original_type_ref ;
490512 let wrapped_result = & func_ret. wrapped_type_ref ;
491513 let unwrap = & func_ret. unwrap ;
492- let unwrap_result = ( unwrap) ( quote ! { result } ) ;
514+ let unwrap_result = unwrap. run ( quote ! { result } ) ;
493515 quote ! {
494516 fn #func_name_ident( #( #func_arg_list) , * ) -> #original_result {
495517 crate :: internal:: async_exported_function( async move {
@@ -510,7 +532,7 @@ fn generate_exported_resource_function_impl(
510532 let original_result = & func_ret. original_type_ref ;
511533 let wrapped_result = & func_ret. wrapped_type_ref ;
512534 let unwrap = & func_ret. unwrap ;
513- let unwrap_result = ( unwrap) ( quote ! { result } ) ;
535+ let unwrap_result = unwrap. run ( quote ! { result } ) ;
514536 quote ! {
515537 fn #func_name_ident( #( #func_arg_list) , * ) -> #original_result {
516538 crate :: internal:: async_exported_function( async move {
0 commit comments