From 40b53caf9a6e6f5c184292436d9bf5d723060608 Mon Sep 17 00:00:00 2001 From: Jeffrey Charles Date: Fri, 30 Aug 2024 11:15:21 -0400 Subject: [PATCH] Derive runtime config defaults from javy_config::Config (#741) --- crates/cli/src/codegen/builder.rs | 2 +- crates/cli/src/commands.rs | 14 ++++++++++---- crates/cli/src/main.rs | 2 +- crates/config/src/lib.rs | 12 ++++++++++++ crates/core/src/lib.rs | 4 ++-- crates/core/src/runtime.rs | 9 ++++----- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/cli/src/codegen/builder.rs b/crates/cli/src/codegen/builder.rs index a70f500d..b000e125 100644 --- a/crates/cli/src/codegen/builder.rs +++ b/crates/cli/src/codegen/builder.rs @@ -87,7 +87,7 @@ impl CodeGenBuilder { match T::classify() { CodeGenType::Static => self.build_static(js_runtime_config), CodeGenType::Dynamic => { - if js_runtime_config != Config::all() { + if js_runtime_config != Config::default() { bail!("Cannot set JS runtime options when building a dynamic module") } self.build_dynamic() diff --git a/crates/cli/src/commands.rs b/crates/cli/src/commands.rs index 3c78ce50..eea37479 100644 --- a/crates/cli/src/commands.rs +++ b/crates/cli/src/commands.rs @@ -210,9 +210,7 @@ pub struct JsRuntimeOptionGroup { impl Default for JsRuntimeOptionGroup { fn default() -> Self { - Self { - redirect_stdout_to_stderr: true, - } + Config::default().into() } } @@ -262,7 +260,7 @@ impl From> for JsRuntimeOptionGroup { impl From for Config { fn from(value: JsRuntimeOptionGroup) -> Self { - let mut config = Self::all(); + let mut config = Self::default(); config.set( Config::REDIRECT_STDOUT_TO_STDERR, value.redirect_stdout_to_stderr, @@ -270,3 +268,11 @@ impl From for Config { config } } + +impl From for JsRuntimeOptionGroup { + fn from(value: Config) -> Self { + Self { + redirect_stdout_to_stderr: value.contains(Config::REDIRECT_STDOUT_TO_STDERR), + } + } +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index fc3ae783..814a4e74 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -44,7 +44,7 @@ fn main() -> Result<()> { .source_compression(!opts.no_source_compression) .provider_version("2"); - let config = Config::all(); + let config = Config::default(); let mut gen = if opts.dynamic { builder.build::(config)? } else { diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 8836b9e2..581ce482 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -34,6 +34,18 @@ bitflags! { } } +impl Default for Config { + fn default() -> Self { + let mut config = Config::empty(); + config.set(Config::OVERRIDE_JSON_PARSE_AND_STRINGIFY, false); + config.set(Config::JAVY_JSON, false); + config.set(Config::JAVY_STREAM_IO, true); + config.set(Config::REDIRECT_STDOUT_TO_STDERR, true); + config.set(Config::TEXT_ENCODING, true); + config + } +} + #[cfg(test)] mod tests { use super::Config; diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 2aa11856..0685731d 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -17,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(Config::all()).unwrap(); + let runtime = runtime::new(Config::default()).unwrap(); unsafe { RUNTIME .set(runtime) @@ -44,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(Config::all()).unwrap(); + let runtime = runtime::new(Config::default()).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/runtime.rs b/crates/core/src/runtime.rs index b973bd7b..e50beacc 100644 --- a/crates/core/src/runtime.rs +++ b/crates/core/src/runtime.rs @@ -8,11 +8,10 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result { .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)) - // Due to an issue with our custom serializer and property accesses - // we're disabling this temporarily. It will be enabled once we have a - // fix forward. - .override_json_parse_and_stringify(false) - .javy_json(false); + .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)) }