struct RuntimeState {
[...]
advance_function: Option<Global<v8::Function>>,
}
struct ScriptRuntime {
js_runtime: deno_core::JsRuntime,
state: Arc<Mutex<RuntimeState>>,
}
impl ScriptRuntime {
pub fn new() -> ScriptRuntime {
let state = Arc::new(Mutex::new(RuntimeState {
[...]
advance_function: None,
}));
let state_arc = state.clone();
let runtime_extension = Extension::builder("runtime_extension")
.ops(vec![op_register_advance::DECL, [...]])
.state(|extension_state| {
extension_state.put::<Arc<Mutex<RuntimeState>>>(state_arc);
})
.build();
let js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(TsModuleLoader)),
extensions: vec![runtime_extension],
..Default::default()
});
ScriptRuntime { js_runtime, state }
}
pub fn initialize(&mut self, script: &String) {
self.js_runtime
.execute_script("vector-engine/runtime.ts", deno_core::FastString::from(transpile_ts(String::from(include_str!("./runtime.ts")))))
.unwrap();
self.js_runtime.execute_script("project/clip.ts", deno_core::FastString::from(transpile_ts(script.clone()))).unwrap();
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
runtime.block_on(self.js_runtime.run_event_loop(false)).unwrap();
}
// The crash happens when calling this function
pub fn advance(&mut self) {
let state = self.state.lock().unwrap();
let advance = state.advance_function.clone();
if advance.is_none() {
return;
}
let advance = advance.unwrap();
drop(state);