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

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