1
//! Host-fn import registration + lookup.
2
//!
3
//! Each entry in the rpc `HostFnSpec` registry gets wired up here:
4
//! the wasm import is declared with the spec's param/result types
5
//! (collapsed to abstract heap-types for GC-ref kinds), and a
6
//! lookup-table entry records the function index plus the original
7
//! `WasmType` signature for the compiler's call-site emit.
8

            
9
use super::{CompileContext, HostFnEntry};
10
use crate::error::Result;
11
use crate::host_fn::HostFnSpec;
12
use wasm_encoder::ValType;
13

            
14
impl CompileContext {
15
17857535
    pub(super) fn register_host_fn(&mut self, spec: &HostFnSpec) -> Result<()> {
16
        // Args ride the wasm function signature directly. Each declared
17
        // param type maps to its abstract `WasmTy::valtype()` (`(ref null
18
        // struct)` for Commodity / Ratio / Pair / EntityRef, `(ref null
19
        // array)` for StringRef, primitive types unchanged); host fn body
20
        // reads them as closure args. The earlier capture-queue path
21
        // retired in P4 A6.b.
22
17857535
        let params: Vec<ValType> = spec
23
17857535
            .params
24
17857535
            .iter()
25
21571453
            .map(|t| self.host_import_return_type(*t))
26
17857535
            .collect();
27
        // Result signature is driven by `spec.result` alone. Host fns
28
        // returning typed GC refs (Ratio / Commodity / StringRef / Pair)
29
        // report their wasm signature via `WasmTy::valtype()` as the
30
        // abstract heap type (`(ref null struct)` / `(ref null array)`)
31
        // — the import declaration matches, and the guest follows the
32
        // call with a `ref.cast` to recover the concrete type. See
33
        // `host_import_return_type` for the mapping.
34
17857535
        let results: Vec<ValType> = spec
35
17857535
            .result
36
17857535
            .iter()
37
17857535
            .map(|t| self.host_import_return_type(*t))
38
17857535
            .collect();
39
17857534
        let func_idx =
40
17857535
            self.register_import(&spec.import_module, &spec.import_name, &params, &results)?;
41
17857534
        self.host_fns.insert(
42
17857534
            spec.nomi_name.clone(),
43
17857534
            HostFnEntry {
44
17857534
                func_idx,
45
17857534
                params: spec.params.clone(),
46
17857534
                result: spec.result,
47
17857534
            },
48
        );
49
17857534
        Ok(())
50
17857535
    }
51

            
52
908358
    pub(crate) fn lookup_host_fn(&self, name: &str) -> Option<&HostFnEntry> {
53
908358
        self.host_fns.get(name)
54
908358
    }
55
}