From 3450dd62136f01d946e2e00495f0bd3c13a9737a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Cabrera?= Date: Tue, 11 Jun 2024 15:02:36 -0400 Subject: [PATCH 1/3] Introduce the `javy-config` crate This commit prepares the terrain to bridge the gap in configurability between the CLI and Javy, the crate. The intention behind introducing a new crate is to: * Reduce code duplication and sync the options between the CLI and the core crate. * Make it easy to pass the options into WebAssemlby by using bitflags. This PR doesn't introduce any new functionality. A follow up PR will include new commands in the CLI which will make use of the share configuration. --- Cargo.lock | 8 ++++++++ Cargo.toml | 3 +++ crates/core/Cargo.toml | 1 + crates/core/src/lib.rs | 5 +++-- crates/core/src/main.rs | 3 ++- crates/core/src/runtime.rs | 15 +++++++++------ crates/javy-config/Cargo.toml | 11 +++++++++++ crates/javy-config/README.md | 3 +++ crates/javy-config/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ crates/javy/Cargo.toml | 2 +- 10 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 crates/javy-config/Cargo.toml create mode 100644 crates/javy-config/README.md create mode 100644 crates/javy-config/src/lib.rs 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"] } From 961d3d882e06f3bfd5d40828cea7afe0ce5a5281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Cabrera?= Date: Wed, 12 Jun 2024 09:31:28 -0400 Subject: [PATCH 2/3] Add tests --- .github/workflows/ci.yml | 5 ++++- Makefile | 5 ++++- crates/javy-config/src/lib.rs | 20 +++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7fef738d..179c28d1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,10 @@ jobs: - name: Test env: CARGO_TARGET_WASM32_WASI_RUNNER: wasmtime --dir=. - run: cargo hack wasi test --workspace --exclude=javy-cli --each-feature -- --nocapture + run: cargo hack wasi test --workspace --exclude=javy-cli --exclude=javy-config --each-feature -- --nocapture + + - name: Test Config + run: cargo test --package=javy-config - name: Lint run: cargo clippy --workspace --exclude=javy-cli --target=wasm32-wasi --all-targets -- -D warnings diff --git a/Makefile b/Makefile index b642ae0e..e72f0073 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,10 @@ test-wpt: npm install --prefix wpt npm test --prefix wpt -tests: test-javy test-core test-cli test-wpt +test-config: + CARGO_PROFILE_RELEASE_LTO=off cargo test --package=javy-config -- --nocapture + +tests: test-javy test-core test-cli test-wpt test-config fmt: fmt-quickjs-wasm-sys fmt-quickjs-wasm-rs fmt-javy fmt-apis fmt-core fmt-cli diff --git a/crates/javy-config/src/lib.rs b/crates/javy-config/src/lib.rs index 89431e26..2d7e6d50 100644 --- a/crates/javy-config/src/lib.rs +++ b/crates/javy-config/src/lib.rs @@ -24,11 +24,25 @@ use bitflags::bitflags; bitflags! { + #[derive(Eq, PartialEq)] 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; + const JAVY_STREAM_IO = 1 << 2; + const REDIRECT_STDOUT_TO_STDERR = 1 << 3; + const TEXT_ENCODING = 1 << 4; + } +} + +#[cfg(test)] +mod tests { + use super::Config; + #[test] + fn check_bits() { + assert!(Config::OVERRIDE_JSON_PARSE_AND_STRINGIFY == Config::from_bits(1).unwrap()); + assert!(Config::JAVY_JSON == Config::from_bits(1 << 1).unwrap()); + assert!(Config::JAVY_STREAM_IO == Config::from_bits(1 << 2).unwrap()); + assert!(Config::REDIRECT_STDOUT_TO_STDERR == Config::from_bits(1 << 3).unwrap()); + assert!(Config::TEXT_ENCODING == Config::from_bits(1 << 4).unwrap()); } } From cbe56d6eaa284278486066c94bd17a238062cf0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Cabrera?= Date: Wed, 12 Jun 2024 09:33:00 -0400 Subject: [PATCH 3/3] Fix typo --- crates/javy-config/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/javy-config/src/lib.rs b/crates/javy-config/src/lib.rs index 2d7e6d50..8836b9e2 100644 --- a/crates/javy-config/src/lib.rs +++ b/crates/javy-config/src/lib.rs @@ -18,7 +18,7 @@ //! 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 +//! In general this crate should be treated as an internal detail and //! a contract between the CLI and the Javy crate. use bitflags::bitflags;