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
7983242
    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
7983242
        let params: Vec<ValType> = spec
23
7983242
            .params
24
7983242
            .iter()
25
9769247
            .map(|t| self.host_import_return_type(*t))
26
7983242
            .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
7983242
        let results: Vec<ValType> = spec
35
7983242
            .result
36
7983242
            .iter()
37
7983242
            .map(|t| self.host_import_return_type(*t))
38
7983242
            .collect();
39
7983241
        let func_idx =
40
7983242
            self.register_import(&spec.import_module, &spec.import_name, &params, &results)?;
41
7983241
        self.host_fns.insert(
42
7983241
            spec.nomi_name.clone(),
43
7983241
            HostFnEntry {
44
7983241
                func_idx,
45
7983241
                params: spec.params.clone(),
46
7983241
                result: spec.result,
47
7983241
            },
48
        );
49
7983241
        Ok(())
50
7983242
    }
51

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