pub mod api;
pub mod value;
use crate::ast::arena::AstArena;
use crate::host::{Host, HostError, HostValue};
use crate::utils::fx::HashMap;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use value::Val;
thread_local! {
pub static EXCHANGE: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(1 << 16));
static ARENAS: RefCell<Vec<Option<(Rc<AstArena>, usize)>>> = const { RefCell::new(Vec::new()) };
static HANDLES: RefCell<HashMap<usize, u32>> = RefCell::new(HashMap::default());
static FREE_HANDLES: RefCell<Vec<u32>> = const { RefCell::new(Vec::new()) };
static EXTERNAL_MEMORY: Cell<i64> = const { Cell::new(0) };
static HOST_CALL: RefCell<Option<fn(&[u8]) -> Vec<u8>>> = const { RefCell::new(None) };
}
pub fn write_exchange(bytes: &[u8]) {
EXCHANGE.with(|e| {
let mut buffer = e.borrow_mut();
if buffer.len() < bytes.len() {
buffer.resize(bytes.len(), 0);
}
buffer[..bytes.len()].copy_from_slice(bytes);
});
}
thread_local! {
static OUTPUTS: RefCell<Vec<Option<Vec<u8>>>> = const { RefCell::new(Vec::new()) };
}
pub fn give_output(mut bytes: Vec<u8>) -> Val {
let len = bytes.len() as u32;
bytes.resize(bytes.capacity(), 0);
OUTPUTS.with(|o| {
let mut outputs = o.borrow_mut();
let entry = Some(bytes);
let id = match outputs.iter().position(|x| x.is_none()) {
Some(i) => {
outputs[i] = entry;
i + 1
}
None => {
outputs.push(entry);
outputs.len()
}
};
Val::Output(id as u32, len)
})
}
pub fn take_output(id: u32) -> Option<Vec<u8>> {
OUTPUTS.with(|o| o.borrow_mut().get_mut((id as usize).wrapping_sub(1)).and_then(|x| x.take()))
}
pub fn set_host_call(f: fn(&[u8]) -> Vec<u8>) {
HOST_CALL.with(|h| *h.borrow_mut() = Some(f));
}
pub fn arena_handle(arena: &Rc<AstArena>) -> u32 {
let address = Rc::as_ptr(arena) as usize;
if let Some(handle) = HANDLES.with(|h| h.borrow().get(&address).copied()) {
return handle;
}
let size = arena.memory_size();
EXTERNAL_MEMORY.with(|m| m.set(m.get() + size as i64));
let handle = ARENAS.with(|a| {
let mut arenas = a.borrow_mut();
match FREE_HANDLES.with(|f| f.borrow_mut().pop()) {
Some(handle) => {
arenas[handle as usize - 1] = Some((arena.clone(), size));
handle
}
None => {
arenas.push(Some((arena.clone(), size)));
arenas.len() as u32
}
}
});
HANDLES.with(|h| h.borrow_mut().insert(address, handle));
handle
}
pub fn arena_of(handle: u32) -> Result<Rc<AstArena>, HostError> {
ARENAS.with(|a| a.borrow().get((handle as usize).wrapping_sub(1)).and_then(|x| x.as_ref().map(|(arena, _)| arena.clone())))
.ok_or_else(|| HostError(format!("invalid arena handle {}", handle)))
}
pub fn free_arena(handle: u32) {
let released = ARENAS.with(|a| a.borrow_mut().get_mut((handle as usize).wrapping_sub(1)).and_then(|slot| slot.take()));
if let Some((arena, size)) = released {
HANDLES.with(|h| h.borrow_mut().remove(&(Rc::as_ptr(&arena) as usize)));
FREE_HANDLES.with(|f| f.borrow_mut().push(handle));
EXTERNAL_MEMORY.with(|m| m.set(m.get() - size as i64));
}
}
pub fn take_external_memory() -> i64 {
EXTERNAL_MEMORY.with(|m| m.replace(0))
}
pub fn call_js(callback: u32, arena: Option<&Rc<AstArena>>, args: Vec<Val>) -> Result<Val, HostError> {
let mut request = Vec::with_capacity(64);
let mut list = vec![Val::Num(callback as f64), arena.map_or(Val::Undefined, |a| Val::Arena(arena_handle(a)))];
list.extend(args);
Val::Array(list).encode(&mut request);
let f = HOST_CALL.with(|h| *h.borrow()).ok_or_else(|| HostError("no javascript host".into()))?;
let response = f(&request);
match Val::decode(&response, &mut 0) {
Val::Error(message) => Err(HostError(message.to_string())),
value => Ok(value),
}
}
pub struct JsHost;
impl Host for JsHost {
fn call(&self, callback: u32, arena: Option<&Rc<AstArena>>, args: Vec<HostValue>) -> Result<HostValue, HostError> {
let handle = arena.map_or(0, arena_handle);
let args = args.iter().map(|a| Val::from_host(a, handle)).collect();
call_js(callback, arena, args).map(|v| v.to_host())
}
fn arena(&self, handle: u32) -> Option<Rc<AstArena>> {
arena_of(handle).ok()
}
}
pub fn call(length: usize) -> usize {
let request = EXCHANGE.with(|e| {
let buffer = e.borrow();
Val::decode(&buffer[..length.min(buffer.len())], &mut 0)
});
let (method, args) = match request {
Val::Array(mut items) if !items.is_empty() => {
let method = items.remove(0);
(method.as_str().unwrap_or("").to_string(), items)
}
_ => (String::new(), Vec::new()),
};
let result = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| api::dispatch(&method, args))) {
Ok(Ok(value)) => value,
Ok(Err(error)) => Val::Error(Rc::from(error.0.as_str())),
Err(panic) => {
let message = panic.downcast_ref::<String>().cloned().or_else(|| panic.downcast_ref::<&str>().map(|s| s.to_string())).unwrap_or_default();
Val::Error(Rc::from(format!("internal error in {}: {}", method, message).as_str()))
}
};
let mut out = Vec::with_capacity(256);
result.encode(&mut out);
write_exchange(&out);
out.len()
}