Skip to content

Commit

Permalink
Refactor to minimize dependence on codegen trait (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Oct 23, 2024
1 parent ccb7ca4 commit de316f3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 24 deletions.
7 changes: 2 additions & 5 deletions crates/cli/src/codegen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ impl CodeGenBuilder {
}

/// Build a [`CodeGenerator`].
pub fn build<T>(self, js_runtime_config: Config) -> Result<Box<dyn CodeGen>>
where
T: CodeGen,
{
match T::classify() {
pub fn build(self, ty: CodeGenType, js_runtime_config: Config) -> Result<Box<dyn CodeGen>> {
match ty {
CodeGenType::Static => self.build_static(js_runtime_config),
CodeGenType::Dynamic => {
if js_runtime_config != Config::default() {
Expand Down
6 changes: 1 addition & 5 deletions 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, CodeGenType, Exports, WitOptions},
codegen::{exports, CodeGen, Exports, WitOptions},
js::JS,
providers::Provider,
};
Expand Down Expand Up @@ -285,10 +285,6 @@ impl CodeGen for DynamicGenerator {
print_wat(&wasm)?;
Ok(wasm)
}

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

#[cfg(feature = "dump_wat")]
Expand Down
4 changes: 0 additions & 4 deletions crates/cli/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,4 @@ pub(crate) enum CodeGenType {
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: 1 addition & 5 deletions 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, CodeGenType, Exports, WitOptions,
CodeGen, Exports, WitOptions,
},
js::JS,
};
Expand Down Expand Up @@ -148,10 +148,6 @@ 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
10 changes: 5 additions & 5 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::codegen::WitOptions;
use crate::commands::{Cli, Command, EmitProviderCommandOpts};
use anyhow::Result;
use clap::Parser;
use codegen::{CodeGenBuilder, DynamicGenerator, StaticGenerator};
use codegen::{CodeGenBuilder, CodeGenType};
use commands::{CodegenOptionGroup, JsOptionGroup};
use javy_config::Config;
use js::JS;
Expand Down Expand Up @@ -49,9 +49,9 @@ fn main() -> Result<()> {

let config = Config::default();
let mut gen = if opts.dynamic {
builder.build::<DynamicGenerator>(config)?
builder.build(CodeGenType::Dynamic, config)?
} else {
builder.build::<StaticGenerator>(config)?
builder.build(CodeGenType::Static, config)?
};

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

let js_opts: JsOptionGroup = opts.js.clone().into();
let mut gen = if codegen.dynamic {
builder.build::<DynamicGenerator>(js_opts.into())?
builder.build(CodeGenType::Dynamic, js_opts.into())?
} else {
builder.build::<StaticGenerator>(js_opts.into())?
builder.build(CodeGenType::Static, js_opts.into())?
};

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

0 comments on commit de316f3

Please sign in to comment.