From c76ea9d02a5d7651c3eca56fbb13122ac0aae106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Cabrera?= Date: Wed, 18 Sep 2024 17:39:35 -0400 Subject: [PATCH 1/2] Fully thread the `source-compression` value This commit fixes the handling of the `source-compression` option when `C dynamic` is set. Even though the codegen option gropup defines the options as `true` by default, it was not threaded correctly. Additionally this commit set `source-compression` as `true` in the default implementation of the builders to ensure that the CLI options intent is accurately reflected. --- crates/cli/src/codegen/builder.rs | 1 + crates/cli/src/codegen/dynamic.rs | 4 ++-- crates/cli/src/codegen/static.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/cli/src/codegen/builder.rs b/crates/cli/src/codegen/builder.rs index d6fa9661..2d2b1007 100644 --- a/crates/cli/src/codegen/builder.rs +++ b/crates/cli/src/codegen/builder.rs @@ -106,6 +106,7 @@ impl CodeGenBuilder { fn build_dynamic(self) -> Result> { let mut dynamic_gen = Box::new(DynamicGenerator::new()); + dynamic_gen.source_compression = self.source_compression; if let Some(v) = self.provider_version { dynamic_gen.import_namespace = String::from("javy_quickjs_provider_v"); diff --git a/crates/cli/src/codegen/dynamic.rs b/crates/cli/src/codegen/dynamic.rs index c07b4fb6..c50880b5 100644 --- a/crates/cli/src/codegen/dynamic.rs +++ b/crates/cli/src/codegen/dynamic.rs @@ -49,7 +49,7 @@ pub(crate) struct DynamicGenerator { /// JavaScript function exports. function_exports: Exports, /// Whether to embed the compressed JS source in the generated module. - source_compression: bool, + pub source_compression: bool, /// WIT options for code generation. pub wit_opts: WitOptions, } @@ -58,7 +58,7 @@ impl DynamicGenerator { /// Creates a new [`DynamicGenerator`]. pub fn new() -> Self { Self { - source_compression: Default::default(), + source_compression: true, import_namespace: "".into(), function_exports: Default::default(), wit_opts: Default::default(), diff --git a/crates/cli/src/codegen/static.rs b/crates/cli/src/codegen/static.rs index c43e51f8..6286a23f 100644 --- a/crates/cli/src/codegen/static.rs +++ b/crates/cli/src/codegen/static.rs @@ -36,7 +36,7 @@ impl StaticGenerator { let engine = include_bytes!(concat!(env!("OUT_DIR"), "/engine.wasm")); Self { engine, - source_compression: Default::default(), + source_compression: true, function_exports: Default::default(), wit_opts: Default::default(), js_runtime_config, From 17ae1659728dff2a174302de5b2bf260e799e3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Cabrera?= Date: Wed, 18 Sep 2024 17:55:40 -0400 Subject: [PATCH 2/2] Add sanity tests --- crates/cli/src/codegen/dynamic.rs | 17 +++++++++++++++++ crates/cli/src/codegen/static.rs | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/crates/cli/src/codegen/dynamic.rs b/crates/cli/src/codegen/dynamic.rs index c50880b5..6dd56fde 100644 --- a/crates/cli/src/codegen/dynamic.rs +++ b/crates/cli/src/codegen/dynamic.rs @@ -304,3 +304,20 @@ fn print_wat(wasm_binary: &[u8]) -> Result<()> { fn print_wat(_wasm_binary: &[u8]) -> Result<()> { Ok(()) } + +#[cfg(test)] +mod test { + use super::DynamicGenerator; + use super::WitOptions; + use anyhow::Result; + + #[test] + fn default_values() -> Result<()> { + let gen = DynamicGenerator::new(); + assert!(gen.source_compression); + assert_eq!(gen.import_namespace, ""); + assert_eq!(gen.wit_opts, WitOptions::default()); + + Ok(()) + } +} diff --git a/crates/cli/src/codegen/static.rs b/crates/cli/src/codegen/static.rs index 6286a23f..b0bcdb35 100644 --- a/crates/cli/src/codegen/static.rs +++ b/crates/cli/src/codegen/static.rs @@ -201,3 +201,20 @@ fn optimize_wasm(wasm: &[u8]) -> Result> { Ok(fs::read(&tempfile_path)?) } + +#[cfg(test)] +mod test { + use super::StaticGenerator; + use super::WitOptions; + use anyhow::Result; + use javy_config::Config; + + #[test] + fn default_values() -> Result<()> { + let gen = StaticGenerator::new(Config::default()); + assert!(gen.source_compression); + assert_eq!(gen.wit_opts, WitOptions::default()); + + Ok(()) + } +}