Skip to content

Commit

Permalink
feat: improve JavaScript performance by caching the runtime + polyfill (
Browse files Browse the repository at this point in the history
#257)

* feat: improve JS runtime performance by caching polyfill with wizer

* feat: update JS engine with the latest compilation
  • Loading branch information
Angelmmiguel authored Nov 27, 2023
1 parent 641d773 commit 8e78113
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions kits/javascript/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kits/javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
10 changes: 6 additions & 4 deletions kits/javascript/Makefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions kits/javascript/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

/// List of runtime errors
#[derive(Debug)]
pub enum RuntimeError {
InvalidBinding { invalid_export: String },
}
23 changes: 21 additions & 2 deletions kits/javascript/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Runtime> = 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.
Expand Down Expand Up @@ -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();

Expand Down
Binary file modified kits/javascript/wasm-workers-quick-js-engine.wasm
100755 → 100644
Binary file not shown.

0 comments on commit 8e78113

Please sign in to comment.