Generate the Go function code for the given function. The signature is obtained by: - getting the function parameters from the `wit_parser::Function`, converting names to to Go identifiers and types to Go types. - similar for the result To implement the body, we: - creating a `Func` struct which implements `Bindgen` and passing it to the `wit_bindgen_core::abi::call` function. This will call `Fu
(&self, func: &Function, tokens: &mut Tokens<Go>)
| 32 | /// times, one for each instruction in the function, and `Func::emit` will generate |
| 33 | /// Go code for each instruction |
| 34 | fn generate_function(&self, func: &Function, tokens: &mut Tokens<Go>) { |
| 35 | let params = func |
| 36 | .params |
| 37 | .iter() |
| 38 | .map( |
| 39 | |Param { name, ty, .. }| match crate::resolve_param_type(ty, self.config.resolve) { |
| 40 | GoType::ValueOrOk(t) => (GoIdentifier::local(name), *t), |
| 41 | t => (GoIdentifier::local(name), t), |
| 42 | }, |
| 43 | ) |
| 44 | .collect::<Vec<_>>(); |
| 45 | |
| 46 | let result = if let Some(wit_type) = &func.result { |
| 47 | GoResult::Anon(crate::resolve_type(wit_type, self.config.resolve)) |
| 48 | } else { |
| 49 | GoResult::Empty |
| 50 | }; |
| 51 | |
| 52 | let mut f = crate::Func::export(result, self.config.sizes); |
| 53 | wit_bindgen_core::abi::call( |
| 54 | self.config.resolve, |
| 55 | wit_bindgen_core::abi::AbiVariant::GuestExport, |
| 56 | wit_bindgen_core::abi::LiftLower::LowerArgsLiftResults, |
| 57 | func, |
| 58 | &mut f, |
| 59 | // async is not currently supported |
| 60 | false, |
| 61 | ); |
| 62 | |
| 63 | let arg_assignments = f |
| 64 | .args() |
| 65 | .iter() |
| 66 | .zip(¶ms) |
| 67 | .map(|(arg, (param, _))| (arg, param)) |
| 68 | .collect::<Vec<_>>(); |
| 69 | let fn_name = &GoIdentifier::public(&func.name); |
| 70 | quote_in! { *tokens => |
| 71 | $['\n'] |
| 72 | func (i *$(self.config.instance)) $fn_name( |
| 73 | $['\r'] |
| 74 | ctx $CONTEXT_CONTEXT, |
| 75 | $(for (name, typ) in ¶ms join ($['\r']) => $name $typ,) |
| 76 | ) $(f.result()) { |
| 77 | $(for (arg, param) in arg_assignments join ($['\r']) => $arg := $param) |
| 78 | $(f.body()) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl FormatInto<Go> for ExportGenerator<'_> { |