oden/oden-js/src/runtime.rs
John Doty 9f808cea31 [oden] The big lifetime removal
It turns out that rust can't really reason about the relationship
between the runtime lifetime and the context lifetime in a way that is
actually usable. This removes the lifetime stuff in favor of reference
counting the runtime itself, via a block that we embed in the
pointer. This, I think, it the least worst option here.
2023-06-19 08:28:26 -07:00

93 lines
2.4 KiB
Rust

use oden_js_sys as sys;
use std::cell::RefCell;
struct PrivateState {
refs: u64,
}
#[derive(Debug)]
pub struct Runtime {
pub(crate) rt: *mut sys::JSRuntime,
}
impl Runtime {
pub fn new() -> Runtime {
let state = Box::new(RefCell::new(PrivateState { refs: 1 }));
let rt = unsafe {
let rt = sys::JS_NewRuntime();
sys::JS_SetRuntimeOpaque(rt, Box::into_raw(state) as *mut _);
rt
};
Runtime { rt }
}
pub(crate) fn from_raw(rt: *mut sys::JSRuntime) -> Self {
let mut state = unsafe {
let ptr = sys::JS_GetRuntimeOpaque(rt) as *const RefCell<PrivateState>;
ptr.as_ref()
.expect("We already know this runtime is one of ours!")
.borrow_mut()
};
state.refs += 1;
Runtime { rt }
}
pub fn set_memory_limit(&mut self, limit: usize) {
unsafe {
sys::JS_SetMemoryLimit(self.rt, limit);
}
}
pub fn set_gc_threshold(&mut self, threshold: usize) {
unsafe {
sys::JS_SetGCThreshold(self.rt, threshold);
}
}
/// Pass in 0 to disable the maximum size check.
pub fn set_max_stack_size(&mut self, max_stack: usize) {
unsafe {
sys::JS_SetMaxStackSize(self.rt, max_stack);
}
}
pub fn run_gc(&mut self) {
unsafe {
sys::JS_RunGC(self.rt);
}
}
}
impl Clone for Runtime {
fn clone(&self) -> Self {
Runtime::from_raw(self.rt)
}
}
impl Drop for Runtime {
fn drop(&mut self) {
let should_free = {
let mut state = unsafe {
let ptr = sys::JS_GetRuntimeOpaque(self.rt) as *const RefCell<PrivateState>;
ptr.as_ref()
.expect("We already know this runtime is one of ours!")
.borrow_mut()
};
state.refs -= 1;
state.refs == 0
};
if should_free {
unsafe {
let opaque = sys::JS_GetRuntimeOpaque(self.rt);
sys::JS_RunGC(self.rt);
sys::JS_FreeRuntime(self.rt);
if !opaque.is_null() {
// Just let the system drop it here.
let _ = Box::from_raw(opaque as *mut RefCell<PrivateState>);
}
}
}
}
}