Skip to content

Commit

Permalink
Derive runtime config defaults from javy_config::Config (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Aug 30, 2024
1 parent da6adb4 commit 40b53ca
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/codegen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ pub struct JsRuntimeOptionGroup {

impl Default for JsRuntimeOptionGroup {
fn default() -> Self {
Self {
redirect_stdout_to_stderr: true,
}
Config::default().into()
}
}

Expand Down Expand Up @@ -262,11 +260,19 @@ impl From<Vec<JsRuntimeOption>> for JsRuntimeOptionGroup {

impl From<JsRuntimeOptionGroup> 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,
);
config
}
}

impl From<Config> for JsRuntimeOptionGroup {
fn from(value: Config) -> Self {
Self {
redirect_stdout_to_stderr: value.contains(Config::REDIRECT_STDOUT_TO_STDERR),
}
}
}
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<DynamicGenerator>(config)?
} else {
Expand Down
12 changes: 12 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static mut RUNTIME: OnceCell<Runtime> = 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)
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions crates/core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
.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))
}

0 comments on commit 40b53ca

Please sign in to comment.