(func_ty: &FuncType)
| 127 | } |
| 128 | |
| 129 | fn proxy_module(func_ty: &FuncType) -> Module { |
| 130 | let results = func_ty.results(); |
| 131 | let params = func_ty.params(); |
| 132 | let join_surround = |list: &[WasmType], keyword| { |
| 133 | if list.is_empty() { |
| 134 | return "".to_string(); |
| 135 | } |
| 136 | let step = list.iter().map(|ty| format!("{} ", to_name(ty))).collect::<String>(); |
| 137 | format!("({keyword} {step})") |
| 138 | }; |
| 139 | |
| 140 | let results_text = join_surround(results, "result"); |
| 141 | let params_text = join_surround(params, "param"); |
| 142 | |
| 143 | let params_gets: String = params.iter().enumerate().fold(String::new(), |mut acc, (num, _)| { |
| 144 | let _ = writeln!(acc, "(local.get {num})"); |
| 145 | acc |
| 146 | }); |
| 147 | |
| 148 | let result_drops = "(drop)\n".repeat(results.len()); |
| 149 | let wasm_text = format!( |
| 150 | r#"(module |
| 151 | (import "host" "hfn" (func $host_fn {params_text} {results_text})) |
| 152 | (func (export "call_hfn") {params_text} {results_text} |
| 153 | {params_gets} |
| 154 | (call $host_fn) |
| 155 | ) |
| 156 | (func (export "call_hfn_discard") {params_text} |
| 157 | {params_gets} |
| 158 | (call $host_fn) |
| 159 | ;; Keep stack balanced for arbitrary result arity. |
| 160 | {result_drops} |
| 161 | ) |
| 162 | ) |
| 163 | "# |
| 164 | ); |
| 165 | let wasm = wat::parse_str(wasm_text).expect("failed to parse wat"); |
| 166 | tinywasm::parse_bytes(&wasm).expect("failed to make module") |
| 167 | } |
no test coverage detected