ruka_runtime/
wasm_api.rs

1/// Wasm value types used by runtime ABI metadata.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum WasmValType {
4    /// 32-bit integer value.
5    I32,
6    /// 64-bit integer value.
7    I64,
8}
9
10/// Lowering strategy used by code generators for a runtime function.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum LoweringMode {
13    /// Emit a direct host call with no adapter trampoline.
14    Direct,
15}
16
17/// Descriptor for one runtime function available to generated WASM code.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct RuntimeFnDescriptor {
20    /// Canonical symbol name used in MIR extern calls.
21    pub symbol: &'static str,
22    /// Export name expected in the final WASM module.
23    pub export_name: &'static str,
24    /// Ordered parameter value types.
25    pub params: &'static [WasmValType],
26    /// Optional result value type.
27    pub result: Option<WasmValType>,
28    /// Backend lowering behavior for this function.
29    pub lowering: LoweringMode,
30}
31
32/// Public symbol for `std::io::log`.
33pub const IO_LOG_SYMBOL: &str = crate::io::LOG_SYMBOL;
34/// Public symbol for `std::io::int_to_string`.
35pub const IO_INT_TO_STRING_SYMBOL: &str = crate::io::INT_TO_STRING_SYMBOL;
36/// Public symbol for `std::io::bool_to_string`.
37pub const IO_BOOL_TO_STRING_SYMBOL: &str = crate::io::BOOL_TO_STRING_SYMBOL;
38/// Public symbol for `std::string::concat`.
39pub const STRING_CONCAT_SYMBOL: &str = crate::string::CONCAT_SYMBOL;
40/// Public symbol for `std::string::starts_with`.
41pub const STRING_STARTS_WITH_SYMBOL: &str = crate::string::STARTS_WITH_SYMBOL;
42/// Public symbol for `std::string::drop`.
43pub const STRING_DROP_SYMBOL: &str = crate::string::DROP_SYMBOL;
44/// Public symbol for `std::string::take`.
45pub const STRING_TAKE_SYMBOL: &str = crate::string::TAKE_SYMBOL;
46/// Public symbol for `std::panic`.
47pub const PANIC_SYMBOL: &str = "std::panic";
48
49/// Internal symbol used by codegen for shared heap allocation.
50pub const RT_ALLOC_BYTES_SYMBOL: &str = "__ruka_rt::alloc_bytes";
51/// Internal symbol used by codegen for tracked shared heap allocation.
52pub const RT_ALLOC_BYTES_TRACKED_SYMBOL: &str = "__ruka_rt::alloc_bytes_tracked";
53/// Internal symbol used by codegen for tracked allocation with source metadata.
54pub const RT_ALLOC_BYTES_TRACKED_SITE_SYMBOL: &str = "__ruka_rt::alloc_bytes_tracked_site";
55/// Internal symbol used by codegen for shared heap deallocation.
56pub const RT_FREE_BYTES_SYMBOL: &str = "__ruka_rt::free_bytes";
57/// Internal symbol used by codegen for runtime leak assertions.
58pub const RT_ASSERT_NO_LEAKS_SYMBOL: &str = "__ruka_rt::assert_no_leaks";
59/// Internal symbol used by codegen to reserve one shadow stack frame.
60pub const RT_SHADOW_STACK_RESERVE_SYMBOL: &str = "__ruka_rt::shadow_stack_reserve";
61/// Internal symbol used by codegen to release one shadow stack frame.
62pub const RT_SHADOW_STACK_RELEASE_SYMBOL: &str = "__ruka_rt::shadow_stack_release";
63
64/// Allocation site id used for direct pointer allocation.
65pub const ALLOC_SITE_POINTER_NEW: i32 = 1001;
66/// Allocation site id used for pointer clone allocation.
67pub const ALLOC_SITE_POINTER_CLONE: i32 = 1002;
68/// Allocation site id used for array allocation.
69pub const ALLOC_SITE_ARRAY_NEW: i32 = 1101;
70/// Allocation site id used for struct/tuple aggregate allocation.
71pub const ALLOC_SITE_AGGREGATE_NEW: i32 = 1201;
72/// Allocation site id used for enum allocation.
73pub const ALLOC_SITE_ENUM_NEW: i32 = 1202;
74/// Allocation site id used for boxed aggregate payload allocation.
75pub const ALLOC_SITE_BOX_AGGREGATE_NEW: i32 = 1301;
76const RUNTIME_WASM_FUNCTIONS: &[RuntimeFnDescriptor] = crate::runtime_wasm_functions! {
77    IO_LOG_SYMBOL => "io_log"(I32) -> ();
78    IO_INT_TO_STRING_SYMBOL => "int_to_string"(I64) -> I32;
79    IO_BOOL_TO_STRING_SYMBOL => "bool_to_string"(I32) -> I32;
80    STRING_CONCAT_SYMBOL => "string_concat"(I32, I32) -> I32;
81    STRING_STARTS_WITH_SYMBOL => "string_starts_with"(I32, I32) -> I32;
82    STRING_DROP_SYMBOL => "string_drop"(I32, I64) -> I32;
83    STRING_TAKE_SYMBOL => "string_take"(I32, I64) -> I32;
84    PANIC_SYMBOL => "panic"(I64) -> ();
85    RT_ALLOC_BYTES_SYMBOL => "alloc_bytes"(I32) -> I32;
86    RT_ALLOC_BYTES_TRACKED_SYMBOL => "alloc_bytes_tracked"(I32, I32) -> I32;
87    RT_ALLOC_BYTES_TRACKED_SITE_SYMBOL => "alloc_bytes_tracked_site"(I32, I32, I32, I32, I32) -> I32;
88    RT_FREE_BYTES_SYMBOL => "free_bytes"(I32, I32) -> ();
89    RT_ASSERT_NO_LEAKS_SYMBOL => "assert_no_leaks"() -> ();
90    RT_SHADOW_STACK_RESERVE_SYMBOL => "shadow_stack_reserve"(I32) -> I32;
91    RT_SHADOW_STACK_RELEASE_SYMBOL => "shadow_stack_release"(I32) -> ();
92};
93
94/// Return all runtime wasm function descriptors.
95pub fn runtime_wasm_functions() -> Vec<RuntimeFnDescriptor> {
96    RUNTIME_WASM_FUNCTIONS.to_vec()
97}
98
99/// Parse a symbol and return its runtime descriptor.
100pub fn find_runtime_wasm_function(symbol: &str) -> Option<RuntimeFnDescriptor> {
101    RUNTIME_WASM_FUNCTIONS
102        .iter()
103        .copied()
104        .find(|descriptor| descriptor.symbol == symbol)
105}