Skip to content

Commit

Permalink
Rename provider to plugin (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcharles authored Oct 31, 2024
1 parent 761e010 commit dbb3c32
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 177 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/build-assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,43 @@ jobs:
- name: Make core
run: make core

- name: Upload quickjs_provider to artifacts
- name: Upload plugin to artifacts
uses: actions/upload-artifact@v4
with:
name: provider
path: target/wasm32-wasip1/release/javy_quickjs_provider.wasm
name: plugin
path: target/wasm32-wasip1/release/plugin.wasm

- name: Wizen and archive wizened quickjs_provider
- name: Wizen and archive wizened plugin
run: |
wizer target/wasm32-wasip1/release/javy_quickjs_provider.wasm --allow-wasi --init-func initialize_runtime --wasm-bulk-memory true -o target/wasm32-wasip1/release/javy_quickjs_provider_wizened.wasm
gzip -k -f target/wasm32-wasip1/release/javy_quickjs_provider_wizened.wasm && mv target/wasm32-wasip1/release/javy_quickjs_provider_wizened.wasm.gz javy-quickjs_provider.wasm.gz
wizer target/wasm32-wasip1/release/plugin.wasm --allow-wasi --init-func initialize_runtime --wasm-bulk-memory true -o target/wasm32-wasip1/release/plugin_wizened.wasm
gzip -k -f target/wasm32-wasip1/release/plugin_wizened.wasm && mv target/wasm32-wasip1/release/plugin_wizened.wasm.gz plugin.wasm.gz
- name: Upload archived quickjs_provider to artifacts
- name: Upload archived plugin to artifacts
uses: actions/upload-artifact@v4
with:
name: javy-quickjs_provider.wasm.gz
path: javy-quickjs_provider.wasm.gz
name: plugin.wasm.gz
path: plugin.wasm.gz

- name: Upload archived quickjs_provider to release
- name: Upload archived plugin to release
if: github.event_name == 'release'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ github.event.release.tag_name }} javy-quickjs_provider.wasm.gz
run: gh release upload ${{ github.event.release.tag_name }} plugin.wasm.gz

- name: Generate archived quickjs_provider hash
run: shasum -a 256 javy-quickjs_provider.wasm.gz | awk '{ print $1 }' > javy-quickjs_provider.wasm.gz.sha256
- name: Generate archived plugin hash
run: shasum -a 256 plugin.wasm.gz | awk '{ print $1 }' > plugin.wasm.gz.sha256

- name: Upload asset hash to artifacts
uses: actions/upload-artifact@v4
with:
name: javy-quickjs_provider.wasm.gz.sha256
path: javy-quickjs_provider.wasm.gz.sha256
name: plugin.wasm.gz.sha256
path: plugin.wasm.gz.sha256

- name: Upload asset hash to release
if: github.event_name == 'release'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ github.event.release.tag_name }} javy-quickjs_provider.wasm.gz.sha256
run: gh release upload ${{ github.event.release.tag_name }} plugin.wasm.gz.sha256

compile_cli:
name: compile_cli-${{ matrix.name }}
Expand Down Expand Up @@ -126,7 +126,7 @@ jobs:

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

- name: Build CLI ${{ matrix.os }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ jobs:
- name: Lint Runner
run: cargo clippy --package=javy-runner -- -D warnings

- name: Upload quickjs_provider to artifacts
- name: Upload plugin to artifacts
uses: actions/upload-artifact@v4
with:
name: provider
path: target/wasm32-wasip1/release/javy_quickjs_provider.wasm
name: plugin
path: target/wasm32-wasip1/release/plugin.wasm

cli:
name: test_cli
Expand All @@ -70,7 +70,7 @@ jobs:

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

- name: Test CLI
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ impl FunctionCase {
let mut store = Store::new(&self.engine, wasi);

if let Linking::Dynamic = self.linking {
let qjs_provider = Module::new(
let plugin = Module::new(
&self.engine,
fs::read(Path::new(
"../../target/wasm32-wasip1/release/javy_quickjs_provider_wizened.wasm",
"../../target/wasm32-wasip1/release/plugin_wizened.wasm",
))?,
)?;
let instance = linker.instantiate(store.as_context_mut(), &qjs_provider)?;
linker.instance(store.as_context_mut(), "javy_quickjs_provider_v2", instance)?;
let instance = linker.instantiate(store.as_context_mut(), &plugin)?;
linker.instance(store.as_context_mut(), "javy_quickjs_provider_v3", instance)?;
}

Ok((linker, store))
Expand Down
41 changes: 19 additions & 22 deletions crates/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use anyhow::Result;

fn main() -> Result<()> {
if let Ok("cargo-clippy") = env::var("CARGO_CFG_FEATURE").as_ref().map(String::as_str) {
stub_javy_core_for_clippy()
stub_plugin_for_clippy()
} else {
copy_javy_core()
copy_plugin()
}
}

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

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

// Copy the provider binary build from the `core` crate
fn copy_javy_core() -> Result<()> {
// Copy the plugin binary build from the `core` crate
fn copy_plugin() -> Result<()> {
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let module_path = PathBuf::from(&cargo_manifest_dir)
.parent()
.unwrap()
.parent()
.unwrap()
.join("target/wasm32-wasip1/release");
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 plugin_path = module_path.join("plugin.wasm");
let plugin_wizened_path = module_path.join("plugin_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)?;
.run(read_file(&plugin_path)?.as_slice())?;
fs::File::create(&plugin_wizened_path)?.write_all(&wizened)?;

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

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

fs::copy(&quickjs_provider_wizened_path, copied_provider_path)?;
fs::copy(&plugin_wizened_path, copied_plugin_path)?;
}
Ok(())
}
10 changes: 5 additions & 5 deletions crates/cli/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ use anyhow::{anyhow, Result};
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
use wasmtime::{AsContextMut, Engine, Instance, Linker, Memory, Module, Store};

use crate::providers::Provider;
use crate::plugins::Plugin;

pub fn compile_source(provider: &Provider, js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env(provider)?;
pub fn compile_source(plugin: &Plugin, js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env(plugin)?;
let (js_src_ptr, js_src_len) =
copy_source_code_into_instance(js_source_code, store.as_context_mut(), &instance, &memory)?;
let ret_ptr = call_compile(js_src_ptr, js_src_len, store.as_context_mut(), &instance)?;
let bytecode = copy_bytecode_from_instance(ret_ptr, store.as_context_mut(), &memory)?;
Ok(bytecode)
}

fn create_wasm_env(provider: &Provider) -> Result<(Store<WasiCtx>, Instance, Memory)> {
fn create_wasm_env(plugin: &Plugin) -> Result<(Store<WasiCtx>, Instance, Memory)> {
let engine = Engine::default();
let module = Module::new(&engine, provider.as_bytes())?;
let module = Module::new(&engine, plugin.as_bytes())?;
let mut linker = Linker::new(&engine);
wasi_common::sync::snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(
&mut linker,
Expand Down
14 changes: 7 additions & 7 deletions crates/cli/src/codegen/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
codegen::{CodeGenType, Generator},
js_config::JsConfig,
providers::Provider,
plugins::Plugin,
};
use anyhow::{bail, Result};
use std::path::PathBuf;
Expand Down Expand Up @@ -50,8 +50,8 @@ impl WitOptions {
/// A code generation builder.
#[derive(Default)]
pub(crate) struct CodeGenBuilder {
/// The provider to use.
provider: Provider,
/// The plugin to use.
plugin: Plugin,
/// WIT options for code generation.
wit_opts: WitOptions,
/// Whether to compress the original JS source.
Expand All @@ -64,9 +64,9 @@ impl CodeGenBuilder {
Self::default()
}

/// Set the provider.
pub fn provider(&mut self, provider: Provider) -> &mut Self {
self.provider = provider;
/// Set the plugin.
pub fn plugin(&mut self, plugin: Plugin) -> &mut Self {
self.plugin = plugin;
self
}

Expand All @@ -89,7 +89,7 @@ impl CodeGenBuilder {
bail!("Cannot set JS runtime options when building a dynamic module")
}
}
let mut generator = Generator::new(ty, js_runtime_config, self.provider);
let mut generator = Generator::new(ty, js_runtime_config, self.plugin);
generator.source_compression = self.source_compression;
generator.wit_opts = self.wit_opts;
Ok(generator)
Expand Down
39 changes: 18 additions & 21 deletions crates/cli/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
//! contains instructions to execute that bytecode through dynamic linking
//! at runtime.
//!
//! Dynamic code generation requires a provider module to be used and linked
//! Dynamic code generation requires a plugin module to be used and linked
//! against at runtime in order to execute the JavaScript bytecode. This
//! operation involves carefully ensuring that a given provider version matches
//! the provider version of the imports requested by the generated Wasm module
//! as well as ensuring that any features available in the provider match the
//! operation involves carefully ensuring that a given plugin version matches
//! the plugin version of the imports requested by the generated Wasm module
//! as well as ensuring that any features available in the plugin match the
//! features requsted by the JavaScript bytecode.
mod builder;
Expand All @@ -50,7 +50,7 @@ use wasi_common::{pipe::ReadPipe, sync::WasiCtxBuilder, WasiCtx};
use wasm_opt::{OptimizationOptions, ShrinkLevel};
use wizer::{Linker, Wizer};

use crate::{js_config::JsConfig, providers::Provider, JS};
use crate::{js_config::JsConfig, plugins::Plugin, JS};
use anyhow::Result;

static mut WASI: OnceLock<WasiCtx> = OnceLock::new();
Expand Down Expand Up @@ -111,8 +111,8 @@ pub(crate) struct Generator {
pub ty: CodeGenType,
/// JS runtime config.
pub js_runtime_config: JsConfig,
/// Provider to use.
pub provider: Provider,
/// Plugin to use.
pub plugin: Plugin,
/// JavaScript function exports.
function_exports: Exports,
/// Whether to embed the compressed JS source in the generated module.
Expand All @@ -123,12 +123,12 @@ pub(crate) struct Generator {

impl Generator {
/// Creates a new [`Generator`].
pub fn new(ty: CodeGenType, js_runtime_config: JsConfig, provider: Provider) -> Self {
pub fn new(ty: CodeGenType, js_runtime_config: JsConfig, plugin: Plugin) -> Self {
Self {
ty,
js_runtime_config,
source_compression: true,
provider,
plugin,
function_exports: Default::default(),
wit_opts: Default::default(),
}
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Generator {
Ok(linker)
})))?
.wasm_bulk_memory(true)
.run(self.provider.as_bytes())?;
.run(self.plugin.as_bytes())?;
config.parse(&wasm)?
}
CodeGenType::Dynamic => Module::with_config(config),
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Generator {
))
}
CodeGenType::Dynamic => {
let import_namespace = self.provider.import_namespace()?;
let import_namespace = self.plugin.import_namespace()?;
let canonical_abi_realloc_type = module.types.add(
&[ValType::I32, ValType::I32, ValType::I32, ValType::I32],
&[ValType::I32],
Expand All @@ -211,11 +211,8 @@ impl Generator {
&[ValType::I32, ValType::I32, ValType::I32, ValType::I32],
&[],
);
let (invoke_fn_id, _) = module.add_import_func(
&self.provider.import_namespace()?,
"invoke",
invoke_type,
);
let (invoke_fn_id, _) =
module.add_import_func(&self.plugin.import_namespace()?, "invoke", invoke_type);

let (memory_id, _) = module.add_import_memory(
&import_namespace,
Expand Down Expand Up @@ -244,14 +241,14 @@ impl Generator {
js: &JS,
imports: &Identifiers,
) -> Result<BytecodeMetadata> {
let bytecode = js.compile(&self.provider)?;
let bytecode = js.compile(&self.plugin)?;
let bytecode_len: i32 = bytecode.len().try_into()?;
let bytecode_data = module.data.add(DataKind::Passive, bytecode);

let mut main = FunctionBuilder::new(&mut module.types, &[], &[]);
let bytecode_ptr_local = module.locals.add(ValType::I32);
main.func_body()
// Allocate memory in javy_quickjs_provider for bytecode array.
// Allocate memory in plugin instance for bytecode array.
.i32_const(0) // orig ptr
.i32_const(0) // orig size
.i32_const(1) // alignment
Expand Down Expand Up @@ -461,7 +458,7 @@ fn print_wat(_wasm_binary: &[u8]) -> Result<()> {
#[cfg(test)]
mod test {
use crate::js_config::JsConfig;
use crate::providers::Provider;
use crate::plugins::Plugin;

use super::Generator;
use super::WitOptions;
Expand All @@ -472,11 +469,11 @@ mod test {
let gen = Generator::new(
crate::codegen::CodeGenType::Dynamic,
JsConfig::default(),
Provider::Default,
Plugin::Default,
);
assert!(!gen.js_runtime_config.has_configs());
assert!(gen.source_compression);
assert!(matches!(gen.provider, Provider::Default));
assert!(matches!(gen.plugin, Plugin::Default));
assert_eq!(gen.wit_opts, WitOptions::default());

Ok(())
Expand Down
Loading

0 comments on commit dbb3c32

Please sign in to comment.