use super::{RuntimeEngine, RuntimeResult, RuntimeValue, RuntimeStats, RuntimeConfig};
use std::collections::HashMap;
pub struct WasmRuntime {
#[allow(dead_code)]
config: RuntimeConfig,
stats: RuntimeStats,
modules: HashMap<String, Vec<u8>>,
}
impl WasmRuntime {
pub fn new(config: RuntimeConfig) -> RuntimeResult<Self> {
Ok(Self {
config,
stats: RuntimeStats {
memory_used: 0,
functions_executed: 0,
total_execution_time_ms: 0,
gc_collections: 0,
},
modules: HashMap::new(),
})
}
}
impl RuntimeEngine for WasmRuntime {
fn execute(&mut self, function_name: &str, _args: &[RuntimeValue]) -> RuntimeResult<RuntimeValue> {
use super::RuntimeError;
Err(RuntimeError::WasmError(format!(
"WASM runtime is provided by embedding application. \
Function '{}' should be executed via DataSource.execute_wasm_function(). \
When using HyperQL in Hyperspatial, compute functions are available through RouterDataSource.",
function_name
)))
}
fn load_function(&mut self, name: &str, code: &str) -> RuntimeResult<()> {
self.modules.insert(name.to_string(), code.as_bytes().to_vec());
Ok(())
}
fn has_function(&self, name: &str) -> bool {
self.modules.contains_key(name)
}
fn get_stats(&self) -> RuntimeStats {
self.stats.clone()
}
}
#[derive(Debug)]
#[allow(dead_code)]
struct WasmModule {
bytecode: Vec<u8>,
signature: FunctionSignature,
metadata: ModuleMetadata,
}
#[derive(Debug, Clone)]
pub struct FunctionSignature {
pub name: String,
pub parameters: Vec<WasmType>,
pub returns: Vec<WasmType>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WasmType {
I32,
I64,
F32,
F64,
V128,
FuncRef,
ExternRef,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct ModuleMetadata {
compiled_at: std::time::SystemTime,
source_hash: u64,
exports: Vec<String>,
memory_estimate: usize,
}
#[derive(Debug, Clone)]
pub struct WasmConfig {
pub enable_simd: bool,
pub enable_multi_memory: bool,
pub max_memory_pages: u32,
pub max_stack_depth: u32,
pub enable_aot: bool,
}
impl Default for WasmConfig {
fn default() -> Self {
Self {
enable_simd: true,
enable_multi_memory: false,
max_memory_pages: 1024,
max_stack_depth: 1024,
enable_aot: true,
}
}
}
#[derive(Debug, Clone)]
pub struct HostFunction {
pub name: String,
pub signature: FunctionSignature,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WasmValue {
I32(i32),
I64(i64),
F32(f32),
F64(f64),
V128([u8; 16]),
FuncRef(Option<u32>),
ExternRef(Option<u32>),
}