diff --git a/crates/jstz_core/src/host.rs b/crates/jstz_core/src/host.rs index 6c10a42c8..8ccd552fa 100644 --- a/crates/jstz_core/src/host.rs +++ b/crates/jstz_core/src/host.rs @@ -430,25 +430,27 @@ mod erased_runtime { } } -pub struct JsHostRuntime { - inner: &'static mut dyn erased_runtime::Runtime, +pub struct JsHostRuntime<'a> { + inner: &'a mut dyn erased_runtime::Runtime, } -impl JsHostRuntime { - pub unsafe fn new(rt: &mut (impl Runtime + 'static)) -> Self { +impl<'a> JsHostRuntime<'a> { + pub unsafe fn new(rt: &'a mut R) -> JsHostRuntime<'static> { let rt_ptr: *mut dyn erased_runtime::Runtime = rt; // SAFETY // From the pov of the `Host` struct, it is permitted to cast // the `rt` reference to `'static` since the lifetime of `Host` // is always shorter than the lifetime of `rt` - let rt: &'static mut dyn erased_runtime::Runtime = &mut *rt_ptr; + let rt: &'a mut dyn erased_runtime::Runtime = &mut *rt_ptr; - Self { inner: rt } + let jhr: Self = Self { inner: rt }; + + unsafe { std::mem::transmute(jhr) } } } -impl HostRuntime for JsHostRuntime { +impl<'a: 'static> HostRuntime for JsHostRuntime<'a> { fn write_output(&mut self, from: &[u8]) -> Result<(), HostError> { self.inner.write_output(from) } diff --git a/crates/jstz_core/src/runtime.rs b/crates/jstz_core/src/runtime.rs index d09dc2468..b73b18084 100644 --- a/crates/jstz_core/src/runtime.rs +++ b/crates/jstz_core/src/runtime.rs @@ -123,7 +123,7 @@ impl boa_engine::job::JobQueue for JobQueue { thread_local! { /// Thread-local host context - static JS_HOST_RUNTIME: RefCell> = RefCell::new(None); + static JS_HOST_RUNTIME: RefCell>> = RefCell::new(None); /// Thread-local transaction static JS_TRANSACTION: RefCell> = RefCell::new(None); @@ -131,7 +131,7 @@ thread_local! { /// Enters a new host context, running the closure `f` with the new context pub fn enter_js_host_context( - hrt: &mut (impl HostRuntime + 'static), + hrt: &mut impl HostRuntime, tx: &mut Transaction, f: F, ) -> R @@ -160,7 +160,7 @@ where /// Returns a reference to the host runtime in the current js host context pub fn with_js_hrt(f: F) -> R where - F: FnOnce(&mut JsHostRuntime) -> R, + F: FnOnce(&mut JsHostRuntime<'static>) -> R, { JS_HOST_RUNTIME.with(|hrt| { f(hrt @@ -184,7 +184,7 @@ where pub fn with_js_hrt_and_tx(f: F) -> R where - F: FnOnce(&mut JsHostRuntime, &mut Transaction) -> R, + F: FnOnce(&mut JsHostRuntime<'static>, &mut Transaction) -> R, { with_js_hrt(|hrt| with_js_tx(|tx| f(hrt, tx))) } @@ -241,7 +241,7 @@ impl<'host> Runtime<'host> { /// /// Returns the module instance and the module promise. Implementors must manually /// call `Runtime::run_event_loop` or poll/resolve the promise to drive the - /// module's evaluation. + /// module's evaluation. pub fn eval_module(&mut self, module: &Module) -> JsResult { self.realm.eval_module(module, &mut self.context) } diff --git a/crates/jstz_kernel/src/lib.rs b/crates/jstz_kernel/src/lib.rs index 5db5394e7..5d51719e6 100644 --- a/crates/jstz_kernel/src/lib.rs +++ b/crates/jstz_kernel/src/lib.rs @@ -17,7 +17,7 @@ fn read_ticketer(rt: &impl Runtime) -> Option { Storage::get(rt, &TICKETER).ok()? } -fn handle_message(hrt: &mut (impl Runtime + 'static), message: Message) -> Result<()> { +fn handle_message(hrt: &mut impl Runtime, message: Message) -> Result<()> { let mut tx = Transaction::default(); tx.begin(); @@ -38,7 +38,7 @@ fn handle_message(hrt: &mut (impl Runtime + 'static), message: Message) -> Resul } // kernel entry -pub fn entry(rt: &mut (impl Runtime + 'static)) { +pub fn entry(rt: &mut impl Runtime) { let ticketer = read_ticketer(rt); if let Some(message) = read_message(rt, ticketer.as_ref()) { diff --git a/crates/jstz_proto/src/executor/mod.rs b/crates/jstz_proto/src/executor/mod.rs index 266a78fc9..afdb97568 100644 --- a/crates/jstz_proto/src/executor/mod.rs +++ b/crates/jstz_proto/src/executor/mod.rs @@ -10,7 +10,7 @@ pub mod deposit; pub mod smart_function; fn execute_operation_inner( - hrt: &mut (impl HostRuntime + 'static), + hrt: &mut impl HostRuntime, tx: &mut Transaction, signed_operation: SignedOperation, ) -> Result { @@ -54,7 +54,7 @@ pub fn execute_external_operation( } pub fn execute_operation( - hrt: &mut (impl HostRuntime + 'static), + hrt: &mut impl HostRuntime, tx: &mut Transaction, signed_operation: SignedOperation, ) -> Receipt { diff --git a/crates/jstz_proto/src/executor/smart_function.rs b/crates/jstz_proto/src/executor/smart_function.rs index 714145ee8..3fddda4f7 100644 --- a/crates/jstz_proto/src/executor/smart_function.rs +++ b/crates/jstz_proto/src/executor/smart_function.rs @@ -370,7 +370,7 @@ pub mod run { } pub fn execute( - hrt: &mut (impl HostRuntime + 'static), + hrt: &mut impl HostRuntime, tx: &mut Transaction, source: &Address, run: operation::RunFunction,