Skip to content

Commit

Permalink
Add wasm ctor and dtor calls to non-main exports in core (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Sep 22, 2023
1 parent 77489ce commit 3b02858
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static mut BYTECODE: OnceCell<Vec<u8>> = OnceCell::new();

#[export_name = "wizer.initialize"]
pub extern "C" fn init() {
let _wasm_ctx = WasmCtx::new();

let runtime = runtime::new_runtime().unwrap();

let mut contents = String::new();
Expand Down Expand Up @@ -45,7 +47,42 @@ fn main() {
/// length.
#[export_name = "javy.invoke"]
pub unsafe extern "C" fn invoke(fn_name_ptr: *mut u8, fn_name_size: usize) {
let _wasm_ctx = WasmCtx::new();

let js_fn_name = str::from_utf8_unchecked(slice::from_raw_parts(fn_name_ptr, fn_name_size));
let runtime = unsafe { RUNTIME.take().unwrap() };
execution::invoke_function(&runtime, FUNCTION_MODULE_NAME, js_fn_name);
}

// RAII abstraction for calling Wasm ctors and dtors for exported non-main functions.
struct WasmCtx;

impl WasmCtx {
#[must_use = "Failing to assign the return value will result in the wasm dtors being run immediately"]
fn new() -> Self {
unsafe { __wasm_call_ctors() };
Self
}
}

impl Drop for WasmCtx {
fn drop(&mut self) {
unsafe { __wasm_call_dtors() };
}
}

extern "C" {
// `__wasm_call_ctors` is generated by `wasm-ld` and invokes all of the global constructors.
// In a Rust bin crate, the `_start` function will invoke this implicitly but no other exported
// Wasm functions will invoke this.
// If this is not invoked, access to environment variables and directory preopens will not be
// available.
// This should only be invoked at the start of exported Wasm functions that are not the `main`
// function.
// References:
// - [Rust 1.67.0 stopped initializing the WASI environment for exported functions](https://github.com/rust-lang/rust/issues/107635)
// - [Wizer header in Fastly's JS compute runtime](https://github.com/fastly/js-compute-runtime/blob/main/runtime/js-compute-runtime/third_party/wizer.h#L92)
fn __wasm_call_ctors();

fn __wasm_call_dtors();
}

0 comments on commit 3b02858

Please sign in to comment.