Skip to content

Commit f680f1f

Browse files
authored
Introduce the javy-config crate (#664)
* 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. * Add tests * Fix typo
1 parent ab0b31e commit f680f1f

File tree

12 files changed

+97
-12
lines changed

12 files changed

+97
-12
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ jobs:
3636
- name: Test
3737
env:
3838
CARGO_TARGET_WASM32_WASI_RUNNER: wasmtime --dir=.
39-
run: cargo hack wasi test --workspace --exclude=javy-cli --each-feature -- --nocapture
39+
run: cargo hack wasi test --workspace --exclude=javy-cli --exclude=javy-config --each-feature -- --nocapture
40+
41+
- name: Test Config
42+
run: cargo test --package=javy-config
4043

4144
- name: Lint
4245
run: cargo clippy --workspace --exclude=javy-cli --target=wasm32-wasi --all-targets -- -D warnings

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"crates/core",
88
"crates/cli",
99
"crates/javy-test-macros",
10+
"crates/javy-config",
1011
]
1112
resolver = "2"
1213

@@ -23,7 +24,9 @@ wasmtime-wasi = "19"
2324
wasi-common = "19"
2425
anyhow = "1.0"
2526
once_cell = "1.19"
27+
bitflags = "2.5.0"
2628
javy = { path = "crates/javy", version = "3.0.0-alpha.1" }
29+
javy-config = { path = "crates/javy-config" }
2730

2831
[profile.release]
2932
lto = true

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ test-wpt:
4141
npm install --prefix wpt
4242
npm test --prefix wpt
4343

44-
tests: test-javy test-core test-cli test-wpt
44+
test-config:
45+
CARGO_PROFILE_RELEASE_LTO=off cargo test --package=javy-config -- --nocapture
46+
47+
tests: test-javy test-core test-cli test-wpt test-config
4548

4649
fmt: fmt-quickjs-wasm-sys fmt-quickjs-wasm-rs fmt-javy fmt-apis fmt-core fmt-cli
4750

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ crate-type = ["cdylib"]
1717
anyhow = { workspace = true }
1818
javy = { workspace = true, features = ["export_alloc_fns", "json"] }
1919
once_cell = { workspace = true }
20+
javy-config = { workspace = true }
2021

2122
[features]
2223
experimental_event_loop = []

crates/core/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::anyhow;
22
use javy::Runtime;
3+
use javy_config::Config;
34
use once_cell::sync::OnceCell;
45
use std::slice;
56
use std::str;
@@ -16,7 +17,7 @@ static mut RUNTIME: OnceCell<Runtime> = OnceCell::new();
1617
/// Used by Wizer to preinitialize the module.
1718
#[export_name = "wizer.initialize"]
1819
pub extern "C" fn init() {
19-
let runtime = runtime::new_runtime().unwrap();
20+
let runtime = runtime::new(Config::all()).unwrap();
2021
unsafe {
2122
RUNTIME
2223
.set(runtime)
@@ -43,7 +44,7 @@ pub extern "C" fn init() {
4344
#[export_name = "compile_src"]
4445
pub unsafe extern "C" fn compile_src(js_src_ptr: *const u8, js_src_len: usize) -> *const u32 {
4546
// Use fresh runtime to avoid depending on Wizened runtime
46-
let runtime = runtime::new_runtime().unwrap();
47+
let runtime = runtime::new(Config::all()).unwrap();
4748
let js_src = str::from_utf8(slice::from_raw_parts(js_src_ptr, js_src_len)).unwrap();
4849

4950
let bytecode = runtime

crates/core/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::slice;
66
use std::str;
77
use std::string::String;
88

9+
use javy_config::Config;
910
mod execution;
1011
mod runtime;
1112

@@ -18,7 +19,7 @@ static mut BYTECODE: OnceCell<Vec<u8>> = OnceCell::new();
1819
pub extern "C" fn init() {
1920
let _wasm_ctx = WasmCtx::new();
2021

21-
let runtime = runtime::new_runtime().unwrap();
22+
let runtime = runtime::new(Config::all()).unwrap();
2223

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

crates/core/src/runtime.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use anyhow::Result;
22
use javy::{Config, Runtime};
3+
use javy_config::Config as SharedConfig;
34

4-
pub(crate) fn new_runtime() -> Result<Runtime> {
5+
pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
56
let mut config = Config::default();
67
let config = config
7-
.text_encoding(true)
8-
.redirect_stdout_to_stderr(true)
9-
.javy_stream_io(true)
10-
.override_json_parse_and_stringify(true)
11-
.javy_json(true);
8+
.text_encoding(shared_config.contains(SharedConfig::TEXT_ENCODING))
9+
.redirect_stdout_to_stderr(shared_config.contains(SharedConfig::REDIRECT_STDOUT_TO_STDERR))
10+
.javy_stream_io(shared_config.contains(SharedConfig::JAVY_STREAM_IO))
11+
.override_json_parse_and_stringify(
12+
shared_config.contains(SharedConfig::OVERRIDE_JSON_PARSE_AND_STRINGIFY),
13+
)
14+
.javy_json(shared_config.contains(SharedConfig::JAVY_JSON));
1215

1316
Runtime::new(std::mem::take(config))
1417
}

crates/javy-config/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "javy-config"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
bitflags = { workspace = true }

crates/javy-config/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Shared Configuration for Javy
2+
3+
See `src/lib.rs` for more details.

0 commit comments

Comments
 (0)