Skip to content

Commit

Permalink
Unify static and dynamic codegen (#793)
Browse files Browse the repository at this point in the history
* Move dynamic code generator into mod.rs

* Unify static and dynamic codegen

* Remove engine from GitHub actions
  • Loading branch information
jeffcharles authored Oct 24, 2024
1 parent d52e7d4 commit 2ac7f2c
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 513 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/build-assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ jobs:
- name: Make core
run: make core

- name: Upload core binary to artifacts
uses: actions/upload-artifact@v4
with:
name: engine
path: target/wasm32-wasip1/release/javy_core.wasm

- name: Upload quickjs_provider to artifacts
uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -130,11 +124,6 @@ jobs:
run: sudo apt-get update && sudo apt-get --assume-yes install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
if: matrix.target == 'aarch64-unknown-linux-gnu'

- uses: actions/download-artifact@v4
with:
name: engine
path: target/wasm32-wasip1/release/

- uses: actions/download-artifact@v4
with:
name: provider
Expand Down
11 changes: 0 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ jobs:
- name: Lint Runner
run: cargo clippy --package=javy-runner -- -D warnings

- name: Upload core binary to artifacts
uses: actions/upload-artifact@v4
with:
name: engine
path: target/wasm32-wasip1/release/javy_core.wasm

- name: Upload quickjs_provider to artifacts
uses: actions/upload-artifact@v4
with:
Expand All @@ -77,11 +71,6 @@ jobs:
with:
os: linux

- uses: actions/download-artifact@v4
with:
name: engine
path: target/wasm32-wasip1/release/

- uses: actions/download-artifact@v4
with:
name: provider
Expand Down
19 changes: 5 additions & 14 deletions crates/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ fn main() -> Result<()> {
}
}

// When using clippy, we need to write stubbed engine.wasm and provider.wasm files to ensure
// compilation succeeds. This skips building the actual engine.wasm and provider.wasm that would
// When using clippy, we need to write stubbed provider.wasm file to ensure
// compilation succeeds. This skips building the actual provider.wasm that would
// be injected into the CLI binary.
fn stub_javy_core_for_clippy() -> Result<()> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let engine_path = out_dir.join("engine.wasm");
let provider_path = out_dir.join("provider.wasm");

if !engine_path.exists() {
std::fs::write(engine_path, [])?;
println!("cargo:warning=using stubbed engine.wasm for static analysis purposes...");
}

if !provider_path.exists() {
std::fs::write(provider_path, [])?;
println!("cargo:warning=using stubbed provider.wasm for static analysis purposes...");
Expand All @@ -40,7 +34,7 @@ fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>> {
Ok(buf)
}

// Copy the engine binary build from the `core` crate
// Copy the provider binary build from the `core` crate
fn copy_javy_core() -> Result<()> {
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let module_path = PathBuf::from(&cargo_manifest_dir)
Expand All @@ -49,31 +43,28 @@ fn copy_javy_core() -> Result<()> {
.parent()
.unwrap()
.join("target/wasm32-wasip1/release");
let engine_path = module_path.join("javy_core.wasm");
let quickjs_provider_path = module_path.join("javy_quickjs_provider.wasm");
let quickjs_provider_wizened_path = module_path.join("javy_quickjs_provider_wizened.wasm");

let mut wizer = wizer::Wizer::new();
let wizened = wizer
.init_func("initialize_runtime")
.keep_init_func(true) // Necessary for static codegen.
.allow_wasi(true)?
.wasm_bulk_memory(true)
.run(read_file(&quickjs_provider_path)?.as_slice())?;
fs::File::create(&quickjs_provider_wizened_path)?.write_all(&wizened)?;

println!("cargo:rerun-if-changed={}", engine_path.to_str().unwrap());
println!(
"cargo:rerun-if-changed={}",
quickjs_provider_path.to_str().unwrap()
);
println!("cargo:rerun-if-changed=build.rs");

if engine_path.exists() {
if quickjs_provider_wizened_path.exists() {
let out_dir = env::var("OUT_DIR")?;
let copied_engine_path = Path::new(&out_dir).join("engine.wasm");
let copied_provider_path = Path::new(&out_dir).join("provider.wasm");

fs::copy(&engine_path, copied_engine_path)?;
fs::copy(&quickjs_provider_wizened_path, copied_provider_path)?;
}
Ok(())
Expand Down
35 changes: 9 additions & 26 deletions crates/cli/src/codegen/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
codegen::{CodeGen, CodeGenType, DynamicGenerator, StaticGenerator},
codegen::{CodeGenType, Generator},
providers::Provider,
};
use anyhow::{bail, Result};
Expand Down Expand Up @@ -83,32 +83,15 @@ impl CodeGenBuilder {
}

/// Build a [`CodeGenerator`].
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() {
bail!("Cannot set JS runtime options when building a dynamic module")
}
self.build_dynamic()
pub fn build(self, ty: CodeGenType, js_runtime_config: Config) -> Result<Generator> {
if let CodeGenType::Dynamic = ty {
if js_runtime_config != Config::default() {
bail!("Cannot set JS runtime options when building a dynamic module")
}
}
}

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;
static_gen.wit_opts = self.wit_opts;

Ok(static_gen)
}

fn build_dynamic(self) -> Result<Box<dyn CodeGen>> {
let mut dynamic_gen = Box::new(DynamicGenerator::new());
dynamic_gen.source_compression = self.source_compression;
dynamic_gen.provider = self.provider;
dynamic_gen.wit_opts = self.wit_opts;
Ok(dynamic_gen)
let mut generator = Generator::new(ty, js_runtime_config, self.provider);
generator.source_compression = self.source_compression;
generator.wit_opts = self.wit_opts;
Ok(generator)
}
}
Loading

0 comments on commit 2ac7f2c

Please sign in to comment.