| 446 | } |
| 447 | |
| 448 | fn generate_host_function_builder( |
| 449 | &self, |
| 450 | method: &InterfaceMethod, |
| 451 | // The name of the parameter representing the interface instance |
| 452 | // in the generated function. |
| 453 | param_name: &GoIdentifier, |
| 454 | ) -> Tokens<Go> { |
| 455 | let func_name = &method.name; |
| 456 | |
| 457 | let wasm_sig = self |
| 458 | .resolve |
| 459 | .wasm_signature(AbiVariant::GuestImport, &method.wit_function); |
| 460 | let result = if wasm_sig.results.is_empty() { |
| 461 | GoResult::Empty |
| 462 | } else if wasm_sig.results.len() == 1 { |
| 463 | GoResult::Anon(resolve_wasm_type(&wasm_sig.results[0])) |
| 464 | } else { |
| 465 | todo!("implement handling of wasm signatures with multiple results"); |
| 466 | }; |
| 467 | let mut f = Func::import(param_name, result, self.sizes); |
| 468 | |
| 469 | // Magic |
| 470 | wit_bindgen_core::abi::call( |
| 471 | self.resolve, |
| 472 | AbiVariant::GuestImport, |
| 473 | LiftLower::LiftArgsLowerResults, |
| 474 | &method.wit_function, |
| 475 | &mut f, |
| 476 | // async is not currently supported |
| 477 | false, |
| 478 | ); |
| 479 | |
| 480 | // Collect all host function parameters into a single list so |
| 481 | // that the join produces correct commas even when there are no |
| 482 | // WIT-level parameters (only ctx and mod). |
| 483 | let mut all_params: Vec<Tokens<Go>> = vec![ |
| 484 | quote! { ctx $CONTEXT_CONTEXT }, |
| 485 | quote! { mod $WAZERO_API_MODULE }, |
| 486 | ]; |
| 487 | for arg in f.args() { |
| 488 | all_params.push(quote! { $arg uint32 }); |
| 489 | } |
| 490 | |
| 491 | quote! { |
| 492 | NewFunctionBuilder(). |
| 493 | WithFunc(func( |
| 494 | $(for param in all_params join (,$['\r']) => $param), |
| 495 | ) $(f.result()){ |
| 496 | $(f.body()) |
| 497 | }). |
| 498 | Export($(quoted(func_name))). |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | #[cfg(test)] |