Skip to content

Commit

Permalink
Set JS runtime config from CLI for static modules (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Aug 27, 2024
1 parent 5e61628 commit eb7817e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions 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 crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ convert_case = "0.6.0"
wasm-opt = "0.116.1"
tempfile = { workspace = true }
clap = { version = "4.5.15", features = ["derive"] }
javy-config = { workspace = true }

[dev-dependencies]
serde_json = "1.0"
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/codegen/static.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, fs, rc::Rc, sync::OnceLock};

use anyhow::Result;
use javy_config::Config;
use walrus::{DataKind, ExportItem, FunctionBuilder, FunctionId, MemoryId, ValType};
use wasi_common::{pipe::ReadPipe, sync::WasiCtxBuilder, WasiCtx};
use wasm_opt::{OptimizationOptions, ShrinkLevel};
Expand Down Expand Up @@ -69,6 +70,10 @@ impl CodeGen for StaticGenerator {
WASI.get_mut()
.unwrap()
.set_stdin(Box::new(ReadPipe::from(js.as_bytes())));

WASI.get_mut()
.unwrap()
.push_env("JS_RUNTIME_CONFIG", &Config::all().bits().to_string())?;
};

let wasm = Wizer::new()
Expand Down
12 changes: 10 additions & 2 deletions crates/core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use anyhow::anyhow;
use javy::Runtime;
use once_cell::sync::OnceCell;
use std::io::{self, Read};
use std::slice;
use std::str;
use std::string::String;
use std::{env, slice};

use javy_config::Config;
mod execution;
Expand All @@ -19,7 +19,15 @@ static mut BYTECODE: OnceCell<Vec<u8>> = OnceCell::new();
pub extern "C" fn init() {
let _wasm_ctx = WasmCtx::new();

let runtime = runtime::new(Config::all()).unwrap();
let js_runtime_config = Config::from_bits(
env::var("JS_RUNTIME_CONFIG")
.expect("JS_RUNTIME_CONFIG should be set")
.parse()
.expect("JS_RUNTIME_CONFIG should be a u32"),
)
.expect("JS_RUNTIME_CONFIG should only contain valid flags");

let runtime = runtime::new(js_runtime_config).unwrap();

let mut contents = String::new();
io::stdin().read_to_string(&mut contents).unwrap();
Expand Down

0 comments on commit eb7817e

Please sign in to comment.