use super::{ExecutionLimits, ExecutionRequest, Executor, Language};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct WasmExecutor {
executor: Executor,
}
#[wasm_bindgen]
impl WasmExecutor {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
console_error_panic_hook::set_once();
Self {
executor: Executor::new(),
}
}
#[wasm_bindgen]
pub fn with_strict_limits() -> Self {
console_error_panic_hook::set_once();
Self {
executor: Executor::with_limits(ExecutionLimits::strict()),
}
}
#[wasm_bindgen]
pub fn with_relaxed_limits() -> Self {
console_error_panic_hook::set_once();
Self {
executor: Executor::with_limits(ExecutionLimits::relaxed()),
}
}
#[wasm_bindgen]
pub fn execute(&self, language: &str, code: &str) -> Result<JsValue, JsValue> {
let result = self.executor.execute_str(language, code);
serde_wasm_bindgen::to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn execute_request(&self, request_json: &str) -> Result<JsValue, JsValue> {
let request: ExecutionRequest = serde_json::from_str(request_json)
.map_err(|e| JsValue::from_str(&format!("Invalid request: {}", e)))?;
let result = self.executor.execute(request);
serde_wasm_bindgen::to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn supported_languages(&self) -> Vec<String> {
self.executor
.supported_languages()
.iter()
.map(|l| l.as_str().to_string())
.collect()
}
#[wasm_bindgen]
pub fn is_supported(&self, language: &str) -> bool {
Language::parse(language)
.map(|l| self.executor.is_supported(l))
.unwrap_or(false)
}
}
impl Default for WasmExecutor {
fn default() -> Self {
Self::new()
}
}
#[wasm_bindgen]
pub fn execute_code(language: &str, code: &str) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let executor = Executor::new();
let result = executor.execute_str(language, code);
serde_wasm_bindgen::to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
}
#[wasm_bindgen]
pub fn get_supported_languages() -> Vec<String> {
super::supported_languages()
.iter()
.map(|l| l.as_str().to_string())
.collect()
}
mod console_error_panic_hook {
use std::sync::Once;
static SET_HOOK: Once = Once::new();
pub fn set_once() {
SET_HOOK.call_once(|| {
std::panic::set_hook(Box::new(|panic_info| {
let msg = panic_info.to_string();
web_sys::console::error_1(&::wasm_bindgen::JsValue::from_str(&msg));
}));
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wasm_executor_creation() {
let executor = WasmExecutor::new();
assert!(!executor.supported_languages().is_empty());
}
}