diff --git a/kits/javascript/Cargo.lock b/kits/javascript/Cargo.lock index 4ab0dd7..27fac87 100644 --- a/kits/javascript/Cargo.lock +++ b/kits/javascript/Cargo.lock @@ -897,6 +897,7 @@ version = "0.1.0" dependencies = [ "anyhow", "javy", + "once_cell", "regex", "wit-bindgen-rust", ] diff --git a/kits/javascript/Cargo.toml b/kits/javascript/Cargo.toml index 3119d70..eed010a 100644 --- a/kits/javascript/Cargo.toml +++ b/kits/javascript/Cargo.toml @@ -11,3 +11,4 @@ javy = { version = "2.0.0", features = ["json"] } regex = "1.10.1" # Use an old version until we add support for components. wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "cb871cfa1ee460b51eb1d144b175b9aab9c50aba" } +once_cell = "1.18.0" diff --git a/kits/javascript/Makefile b/kits/javascript/Makefile index 5b628f0..434b960 100644 --- a/kits/javascript/Makefile +++ b/kits/javascript/Makefile @@ -1,5 +1,7 @@ -.PHONY: build +.PHONY: build build-src -build: - cargo build --target wasm32-wasi --release && \ - cp ./target/wasm32-wasi/release/wasm-workers-quick-js-engine.wasm ./ +build-src: + cargo build --target wasm32-wasi --release + +build: build-src + wizer --allow-wasi --wasm-bulk-memory true -o ./wasm-workers-quick-js-engine.wasm ./target/wasm32-wasi/release/wasm-workers-quick-js-engine.wasm diff --git a/kits/javascript/src/error.rs b/kits/javascript/src/error.rs index 8d23478..ea97d99 100644 --- a/kits/javascript/src/error.rs +++ b/kits/javascript/src/error.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 /// List of runtime errors +#[derive(Debug)] pub enum RuntimeError { InvalidBinding { invalid_export: String }, } diff --git a/kits/javascript/src/main.rs b/kits/javascript/src/main.rs index 88280e0..ca19400 100644 --- a/kits/javascript/src/main.rs +++ b/kits/javascript/src/main.rs @@ -6,6 +6,7 @@ mod error; use bindings::load_bindings_into_global; use javy::{json, Runtime}; +use once_cell::sync::OnceCell; use regex::Regex; use std::{ env, fs, @@ -15,9 +16,28 @@ use std::{ // Load bindings from WIT file. wit_bindgen_rust::import!({paths: ["../../wit/core/http.wit"]}); +/// Ready to use runtime + polyfill +static mut RUNTIME: OnceCell = OnceCell::new(); + // JS polyfill static POLYFILL: &str = include_str!("../shims/dist/index.js"); +/// Preinitialize the module with Wizer +#[export_name = "wizer.initialize"] +pub extern "C" fn init() { + let runtime = Runtime::default(); + + // Precompile the Polyfill to bytecode + let context = runtime.context(); + let bytecode = context.compile_global("polyfill.js", POLYFILL).unwrap(); + + // Preload it + let _ = context.eval_binary(&bytecode); + + // Store result + unsafe { RUNTIME.set(runtime).unwrap() }; +} + /// Determine the worker JS type enum JSWorkerType { /// Relies on the global scope. No ECMA modules. @@ -47,13 +67,12 @@ fn identify_type(src: &str) -> JSWorkerType { } fn main() { - let runtime = Runtime::default(); + let runtime = unsafe { RUNTIME.get().unwrap() }; let context = runtime.context(); let source = fs::read_to_string("/src/index.js"); let mut contents = String::new(); let mut request = String::new(); - contents.push_str(POLYFILL); stdin().read_to_string(&mut request).unwrap(); diff --git a/kits/javascript/wasm-workers-quick-js-engine.wasm b/kits/javascript/wasm-workers-quick-js-engine.wasm old mode 100755 new mode 100644 index c26963a..be3098d Binary files a/kits/javascript/wasm-workers-quick-js-engine.wasm and b/kits/javascript/wasm-workers-quick-js-engine.wasm differ