diff --git a/Cargo.lock b/Cargo.lock index 8b273f8e..949e9ccc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1558,12 +1558,20 @@ dependencies = [ "wizer", ] +[[package]] +name = "javy-config" +version = "1.4.0" +dependencies = [ + "bitflags 2.5.0", +] + [[package]] name = "javy-core" version = "0.2.0" dependencies = [ "anyhow", "javy", + "javy-config", "once_cell", ] diff --git a/Cargo.toml b/Cargo.toml index 708f174c..094414a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "crates/core", "crates/cli", "crates/javy-test-macros", + "crates/javy-config", ] resolver = "2" @@ -23,7 +24,9 @@ wasmtime-wasi = "19" wasi-common = "19" anyhow = "1.0" once_cell = "1.19" +bitflags = "2.5.0" javy = { path = "crates/javy", version = "3.0.0-alpha.1" } +javy-config = { path = "crates/javy-config" } [profile.release] lto = true diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 75869601..0c122095 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -17,6 +17,7 @@ crate-type = ["cdylib"] anyhow = { workspace = true } javy = { workspace = true, features = ["export_alloc_fns", "json"] } once_cell = { workspace = true } +javy-config = { workspace = true } [features] experimental_event_loop = [] diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index c4a53db9..2aa11856 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -1,5 +1,6 @@ use anyhow::anyhow; use javy::Runtime; +use javy_config::Config; use once_cell::sync::OnceCell; use std::slice; use std::str; @@ -16,7 +17,7 @@ static mut RUNTIME: OnceCell = OnceCell::new(); /// Used by Wizer to preinitialize the module. #[export_name = "wizer.initialize"] pub extern "C" fn init() { - let runtime = runtime::new_runtime().unwrap(); + let runtime = runtime::new(Config::all()).unwrap(); unsafe { RUNTIME .set(runtime) @@ -43,7 +44,7 @@ pub extern "C" fn init() { #[export_name = "compile_src"] pub unsafe extern "C" fn compile_src(js_src_ptr: *const u8, js_src_len: usize) -> *const u32 { // Use fresh runtime to avoid depending on Wizened runtime - let runtime = runtime::new_runtime().unwrap(); + let runtime = runtime::new(Config::all()).unwrap(); let js_src = str::from_utf8(slice::from_raw_parts(js_src_ptr, js_src_len)).unwrap(); let bytecode = runtime diff --git a/crates/core/src/main.rs b/crates/core/src/main.rs index f3698119..abd53eee 100644 --- a/crates/core/src/main.rs +++ b/crates/core/src/main.rs @@ -6,6 +6,7 @@ use std::slice; use std::str; use std::string::String; +use javy_config::Config; mod execution; mod runtime; @@ -18,7 +19,7 @@ static mut BYTECODE: OnceCell> = OnceCell::new(); pub extern "C" fn init() { let _wasm_ctx = WasmCtx::new(); - let runtime = runtime::new_runtime().unwrap(); + let runtime = runtime::new(Config::all()).unwrap(); let mut contents = String::new(); io::stdin().read_to_string(&mut contents).unwrap(); diff --git a/crates/core/src/runtime.rs b/crates/core/src/runtime.rs index 15112bfe..e50beacc 100644 --- a/crates/core/src/runtime.rs +++ b/crates/core/src/runtime.rs @@ -1,14 +1,17 @@ use anyhow::Result; use javy::{Config, Runtime}; +use javy_config::Config as SharedConfig; -pub(crate) fn new_runtime() -> Result { +pub(crate) fn new(shared_config: SharedConfig) -> Result { let mut config = Config::default(); let config = config - .text_encoding(true) - .redirect_stdout_to_stderr(true) - .javy_stream_io(true) - .override_json_parse_and_stringify(true) - .javy_json(true); + .text_encoding(shared_config.contains(SharedConfig::TEXT_ENCODING)) + .redirect_stdout_to_stderr(shared_config.contains(SharedConfig::REDIRECT_STDOUT_TO_STDERR)) + .javy_stream_io(shared_config.contains(SharedConfig::JAVY_STREAM_IO)) + .override_json_parse_and_stringify( + shared_config.contains(SharedConfig::OVERRIDE_JSON_PARSE_AND_STRINGIFY), + ) + .javy_json(shared_config.contains(SharedConfig::JAVY_JSON)); Runtime::new(std::mem::take(config)) } diff --git a/crates/javy-config/Cargo.toml b/crates/javy-config/Cargo.toml new file mode 100644 index 00000000..1e400f50 --- /dev/null +++ b/crates/javy-config/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "javy-config" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bitflags = { workspace = true } diff --git a/crates/javy-config/README.md b/crates/javy-config/README.md new file mode 100644 index 00000000..7dfcd62b --- /dev/null +++ b/crates/javy-config/README.md @@ -0,0 +1,3 @@ +# Shared Configuration for Javy + +See `src/lib.rs` for more details. diff --git a/crates/javy-config/src/lib.rs b/crates/javy-config/src/lib.rs new file mode 100644 index 00000000..89431e26 --- /dev/null +++ b/crates/javy-config/src/lib.rs @@ -0,0 +1,34 @@ +//! Shared Configuration for Javy. +//! +//! This crate serves as a central place to facilitate configuration sharing +//! between the Javy CLI and the Javy crate. It addresses the challenge of +//! passing configuration settings in environments where the Javy CLI commands +//! predominantly execute WebAssembly. +//! +//! The purpose of this crate is to consolidate configuration parameters, +//! ensuring consistent and accessible settings across both the CLI and the +//! crate. This approach simplifies the management of configuration settings and +//! enhances the integration between different components of the Javy ecosystem. +//! +//! Currently, this crate includes only a subset of the available configuration +//! options. The objective is to eventually encompass all configurable +//! parameters found in [javy::Config]. +//! +//! The selection of the current configuration options was influenced by the +//! need to override non-standard defaults typically set during CLI invocations. +//! These defaults often do not align with the preferences of the CLI users. +//! +//! In gneneral this crate should be treated as an internal detail and +//! a contract between the CLI and the Javy crate. + +use bitflags::bitflags; + +bitflags! { + pub struct Config: u32 { + const OVERRIDE_JSON_PARSE_AND_STRINGIFY = 1; + const JAVY_JSON = 1 << 1; + const JAVY_STREAM_IO = 1 << 1; + const REDIRECT_STDOUT_TO_STDERR = 1 << 2; + const TEXT_ENCODING = 1 << 3; + } +} diff --git a/crates/javy/Cargo.toml b/crates/javy/Cargo.toml index b8c8c8ee..d5c4ff32 100644 --- a/crates/javy/Cargo.toml +++ b/crates/javy/Cargo.toml @@ -19,7 +19,7 @@ rmp-serde = { version = "^1.3", optional = true } # TODO: cargo doesn't seem to pickup the fact that quickcheck is only used for # tests. quickcheck = "1" -bitflags = "2.5.0" +bitflags = { workspace = true } fastrand = "2.1.0" simd-json = { version = "0.13.10", optional = true, default-features = false, features = ["big-int-as-float", "serde_impl"] }