Outer Cloud
Outer Cloud
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
If anyone has any incite into why this happens, it would be helpful
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
I found a fix but I don't understand why it fixes it. If I manually drop first runtime before creating a second, There is no longer a crash.
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
No description
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
No description
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
Maybe it has to do with creating multiple Js runtimes...
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
After updating the call to instead get the function from the global scope instead of storing it, it still crashes.
pub fn advance(&mut self) {
let binding = self.js_runtime.main_context();

let context = binding.open(&mut self.js_runtime.v8_isolate());

let mut scope = self.js_runtime.handle_scope();

let global = context.global(&mut scope);

let key = v8::String::new(&mut scope, "advance").unwrap();

let advance = global.get(&mut scope, key.into()).unwrap();

let advance = v8::Local::<v8::Function>::try_from(advance).unwrap();

let this = v8::undefined(&mut scope);

advance.call(&mut scope, this.into(), &[]);
}
pub fn advance(&mut self) {
let binding = self.js_runtime.main_context();

let context = binding.open(&mut self.js_runtime.v8_isolate());

let mut scope = self.js_runtime.handle_scope();

let global = context.global(&mut scope);

let key = v8::String::new(&mut scope, "advance").unwrap();

let advance = global.get(&mut scope, key.into()).unwrap();

let advance = v8::Local::<v8::Function>::try_from(advance).unwrap();

let this = v8::undefined(&mut scope);

advance.call(&mut scope, this.into(), &[]);
}
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
(had to split it up due to message length limit)
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();

runtime.block_on(self.js_runtime.call_and_await(&advance)).unwrap();
}
}

[...]

#[op2]
fn op_register_advance(state: &mut OpState, scope: &mut v8::HandleScope, #[global] value: v8::Global<v8::Function>) -> Result<(), AnyError> {
let state_mutex = state.borrow_mut::<Arc<Mutex<ClipRuntimeState>>>();
let mut state = state_mutex.lock().unwrap();

state.advance_function = Some(value);

Ok(())
}

[...]
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();

runtime.block_on(self.js_runtime.call_and_await(&advance)).unwrap();
}
}

[...]

#[op2]
fn op_register_advance(state: &mut OpState, scope: &mut v8::HandleScope, #[global] value: v8::Global<v8::Function>) -> Result<(), AnyError> {
let state_mutex = state.borrow_mut::<Arc<Mutex<ClipRuntimeState>>>();
let mut state = state_mutex.lock().unwrap();

state.advance_function = Some(value);

Ok(())
}

[...]
11 replies
DDeno
Created by Outer Cloud on 3/2/2024 in #help
Multiple JsRuntimes crashe if first is not dropped before a second is created and ran
Here's a simplified source with [...] to show omitted code:
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);
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);
11 replies