Skip to content

Commit

Permalink
feat(kernel): hide 'static requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
emturner authored and johnyob committed Jun 4, 2024
1 parent b0ca154 commit d9f8d3f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
16 changes: 9 additions & 7 deletions crates/jstz_core/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Runtime>(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)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/jstz_core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ impl boa_engine::job::JobQueue for JobQueue {

thread_local! {
/// Thread-local host context
static JS_HOST_RUNTIME: RefCell<Option<JsHostRuntime>> = RefCell::new(None);
static JS_HOST_RUNTIME: RefCell<Option<JsHostRuntime<'static>>> = RefCell::new(None);

/// Thread-local transaction
static JS_TRANSACTION: RefCell<Option<JsTransaction>> = RefCell::new(None);
}

/// Enters a new host context, running the closure `f` with the new context
pub fn enter_js_host_context<F, R>(
hrt: &mut (impl HostRuntime + 'static),
hrt: &mut impl HostRuntime,
tx: &mut Transaction,
f: F,
) -> R
Expand Down Expand Up @@ -160,7 +160,7 @@ where
/// Returns a reference to the host runtime in the current js host context
pub fn with_js_hrt<F, R>(f: F) -> R
where
F: FnOnce(&mut JsHostRuntime) -> R,
F: FnOnce(&mut JsHostRuntime<'static>) -> R,
{
JS_HOST_RUNTIME.with(|hrt| {
f(hrt
Expand All @@ -184,7 +184,7 @@ where

pub fn with_js_hrt_and_tx<F, R>(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)))
}
Expand Down Expand Up @@ -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<JsPromise> {
self.realm.eval_module(module, &mut self.context)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/jstz_kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn read_ticketer(rt: &impl Runtime) -> Option<ContractKt1Hash> {
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();

Expand All @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions crates/jstz_proto/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<receipt::Content> {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/jstz_proto/src/executor/smart_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d9f8d3f

Please sign in to comment.