Skip to content

Commit

Permalink
Revert to old builder interface except for config
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles committed Aug 29, 2024
1 parent 11c3c2f commit 490b86b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
24 changes: 19 additions & 5 deletions crates/cli/src/codegen/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::codegen::{CodeGen, DynamicGenerator, StaticGenerator};
use crate::codegen::{CodeGen, CodeGenType, DynamicGenerator, StaticGenerator};
use anyhow::{bail, Result};
use javy_config::Config;
use std::path::PathBuf;
Expand Down Expand Up @@ -79,8 +79,23 @@ impl CodeGenBuilder {
self
}

/// Build a static [`CodeGenerator`].
pub fn build_static(self, js_runtime_config: Config) -> Result<Box<dyn CodeGen>> {
/// Build a [`CodeGenerator`].
pub fn build<T>(self, js_runtime_config: Config) -> Result<Box<dyn CodeGen>>
where
T: CodeGen,
{
match T::classify() {
CodeGenType::Static => self.build_static(js_runtime_config),
CodeGenType::Dynamic => {
if js_runtime_config != Config::all() {
bail!("Cannot set JS runtime options when building a dynamic module")
}
self.build_dynamic()
}
}
}

fn build_static(self, js_runtime_config: Config) -> Result<Box<dyn CodeGen>> {
let mut static_gen = Box::new(StaticGenerator::new(js_runtime_config));

static_gen.source_compression = self.source_compression;
Expand All @@ -89,8 +104,7 @@ impl CodeGenBuilder {
Ok(static_gen)
}

/// Build a dynamic [`CodeGenerator`].
pub fn build_dynamic(self) -> Result<Box<dyn CodeGen>> {
fn build_dynamic(self) -> Result<Box<dyn CodeGen>> {
let mut dynamic_gen = Box::new(DynamicGenerator::new());

if let Some(v) = self.provider_version {
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/codegen/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::transform::{self, SourceCodeSection};
use crate::{
codegen::{exports, CodeGen, Exports, WitOptions},
codegen::{exports, CodeGen, CodeGenType, Exports, WitOptions},
js::JS,
};
use anyhow::Result;
Expand Down Expand Up @@ -285,6 +285,10 @@ impl CodeGen for DynamicGenerator {
print_wat(&wasm)?;
Ok(wasm)
}

fn classify() -> CodeGenType {
CodeGenType::Dynamic
}
}

#[cfg(feature = "dump_wat")]
Expand Down
11 changes: 11 additions & 0 deletions crates/cli/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ pub(crate) use exports::*;

use crate::JS;

pub(crate) enum CodeGenType {
/// Static code generation.
Static,
/// Dynamic code generation.
Dynamic,
}

/// Code generator trait to abstract the multiple JS to Wasm code generation
/// paths.
pub(crate) trait CodeGen {
/// Generate Wasm from a given JS source.
fn generate(&mut self, source: &JS) -> anyhow::Result<Vec<u8>>;
/// Classify the [`CodeGen`] type.
fn classify() -> CodeGenType
where
Self: Sized;
}
6 changes: 5 additions & 1 deletion crates/cli/src/codegen/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
codegen::{
exports,
transform::{self, SourceCodeSection},
CodeGen, Exports, WitOptions,
CodeGen, CodeGenType, Exports, WitOptions,
},
js::JS,
};
Expand Down Expand Up @@ -147,6 +147,10 @@ impl CodeGen for StaticGenerator {
transform::add_producers_section(&mut module.producers);
Ok(module.emit_wasm())
}

fn classify() -> CodeGenType {
CodeGenType::Static
}
}

fn export_exported_js_functions(
Expand Down
17 changes: 7 additions & 10 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ mod wit;

use crate::codegen::WitOptions;
use crate::commands::{Cli, Command, EmitProviderCommandOpts};
use anyhow::{bail, Result};
use anyhow::Result;
use clap::Parser;
use codegen::CodeGenBuilder;
use codegen::{CodeGenBuilder, DynamicGenerator, StaticGenerator};
use commands::{CodegenOptionGroup, JsRuntimeOptionGroup};
use javy_config::Config;
use js::JS;
Expand Down Expand Up @@ -44,11 +44,11 @@ fn main() -> Result<()> {
.source_compression(!opts.no_source_compression)
.provider_version("2");

let config = Config::all();
let mut gen = if opts.dynamic {
builder.build_dynamic()?
builder.build::<DynamicGenerator>(config)?
} else {
let config = Config::all();
builder.build_static(config)?
builder.build::<StaticGenerator>(config)?
};

let wasm = gen.generate(&js)?;
Expand All @@ -67,12 +67,9 @@ fn main() -> Result<()> {

let js_runtime_options: JsRuntimeOptionGroup = opts.js_runtime.clone().into();
let mut gen = if codegen.dynamic {
if js_runtime_options != JsRuntimeOptionGroup::default() {
bail!("Cannot set JS runtime options when building a dynamic module");
}
builder.build_dynamic()?
builder.build::<DynamicGenerator>(js_runtime_options.into())?
} else {
builder.build_static(js_runtime_options.into())?
builder.build::<StaticGenerator>(js_runtime_options.into())?
};

let wasm = gen.generate(&js)?;
Expand Down

0 comments on commit 490b86b

Please sign in to comment.