[oden-js] Fix various bugs in module loader

This commit is contained in:
John Doty 2023-06-24 11:03:07 -07:00
parent db8a5f8eed
commit 9e685bc569
2 changed files with 15 additions and 7 deletions

View file

@ -3,10 +3,11 @@ use crate::ContextRef;
use oden_js_sys as sys;
use std::cell::RefCell;
use std::ffi::CStr;
use std::sync::Arc;
struct PrivateState {
refs: u64,
loader: Box<dyn ModuleLoader>,
loader: Arc<Box<dyn ModuleLoader>>,
}
impl PrivateState {
@ -20,9 +21,16 @@ impl PrivateState {
Err(_) => return std::ptr::null_mut(),
};
let state = opaque as *mut PrivateState;
let loader = unsafe {
let ptr = opaque as *const RefCell<PrivateState>;
ptr.as_ref()
.expect("We already know this runtime is one of ours!")
.borrow()
.loader
.clone()
};
let context = ContextRef::from_raw(ctx);
load_module(&context, path, &mut (*state).loader)
load_module(&context, path, &loader)
}
}
@ -39,7 +47,7 @@ impl Runtime {
pub fn with_loader<TLoader: ModuleLoader + 'static>(loader: TLoader) -> Runtime {
let state = Box::new(RefCell::new(PrivateState {
refs: 1,
loader: Box::new(loader),
loader: Arc::new(Box::new(loader)),
}));
let rt = unsafe {
let rt = sys::JS_NewRuntime();