MCPcopy Create free account
hub / github.com/explodingcamera/tinywasm / proxy_module

Function proxy_module

crates/tinywasm/tests/host_func_signature_check.rs:129–167  ·  view source on GitHub ↗
(func_ty: &FuncType)

Source from the content-addressed store, hash-verified

127}
128
129fn 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}

Callers 1

module_casesFunction · 0.85

Calls 6

parse_bytesFunction · 0.85
resultsMethod · 0.80
paramsMethod · 0.80
mapMethod · 0.80
is_emptyMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected