use std::any::Any;
use std::sync::RwLock;
use std::collections::HashMap;
use crate::error::{Error, Result};
use crate::runtime::{
ModuleId, RuntimeConfig, RuntimeMetrics, WasmInstanceState,
WasmInstance, WasmModule, WasmRuntime, WasmFunctionCaller
};
use crate::security::{Capabilities, ResourceLimits};
pub struct WasmerModule {
id: ModuleId,
metadata: HashMap<String, String>,
}
impl WasmModule for WasmerModule {
fn id(&self) -> ModuleId {
self.id
}
fn name(&self) -> Option<&str> {
None
}
fn exports(&self) -> Vec<String> {
Vec::new()
}
fn size(&self) -> usize {
0
}
fn clone_module(&self) -> Box<dyn WasmModule> {
Box::new(WasmerModule {
id: self.id,
metadata: self.metadata.clone(),
})
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub struct WasmerInstance {
module_id: ModuleId,
}
impl WasmInstance for WasmerInstance {
fn state(&self) -> WasmInstanceState {
WasmInstanceState::Created
}
fn memory_usage(&self) -> usize {
0
}
fn fuel_usage(&self) -> Option<u64> {
None
}
fn reset_fuel(&self) -> Result<()> {
Ok(())
}
fn add_fuel(&self, _fuel: u64) -> Result<()> {
Ok(())
}
unsafe fn memory_ptr(&self) -> Result<*mut u8> {
Err(Error::InstanceCreation("Wasmer runtime is not fully implemented - use Wasmtime instead".to_string()))
}
fn memory_size(&self) -> usize {
0
}
fn function_caller(&self) -> Box<dyn WasmFunctionCaller> {
Box::new(WasmerInstance { module_id: self.module_id })
}
fn call_simple_function(&self, _function_name: &str, _params: &[i32]) -> Result<i32> {
Err(Error::InstanceCreation("Wasmer runtime is not fully implemented - use Wasmtime instead".to_string()))
}
}
impl WasmFunctionCaller for WasmerInstance {
fn call_function_json(
&self,
_function_name: &str,
_params_json: &str,
) -> Result<String> {
Err(Error::InstanceCreation("Wasmer runtime is not fully implemented - use Wasmtime instead".to_string()))
}
fn call_function_msgpack(
&self,
_function_name: &str,
_params_msgpack: &[u8],
) -> Result<Vec<u8>> {
Err(Error::InstanceCreation("Wasmer runtime is not fully implemented - use Wasmtime instead".to_string()))
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub struct WasmerRuntime {
metrics: RwLock<RuntimeMetrics>,
}
impl WasmerRuntime {
pub fn new() -> Result<Self> {
let metrics = RuntimeMetrics {
compiled_modules: 0,
active_instances: 0,
total_memory_usage: 0,
peak_memory_usage: 0,
fuel_consumption_rate: None,
cache_hit_rate: None,
last_compilation_time_ms: None,
};
Ok(Self {
metrics: RwLock::new(metrics),
})
}
}
impl WasmRuntime for WasmerRuntime {
fn initialize(&mut self, _config: RuntimeConfig) -> Result<()> {
Ok(())
}
fn load_module(&self, _wasm_bytes: &[u8]) -> Result<Box<dyn WasmModule>> {
let module = WasmerModule {
id: ModuleId::new(),
metadata: HashMap::new(),
};
Ok(Box::new(module))
}
fn get_module(&self, id: ModuleId) -> Result<std::sync::Arc<dyn WasmModule>> {
Err(Error::ModuleNotFound(id.to_string()))
}
fn get_module_ids(&self) -> Vec<ModuleId> {
Vec::new()
}
fn create_instance(
&self,
module: &dyn WasmModule,
_resource_limits: ResourceLimits,
_capabilities: Capabilities,
) -> Result<Box<dyn WasmInstance>> {
Ok(Box::new(WasmerInstance {
module_id: module.id(),
}))
}
fn get_metrics(&self) -> RuntimeMetrics {
self.metrics.read().unwrap().clone()
}
fn shutdown(&self) -> Result<()> {
Ok(())
}
}