Skip to content

Commit

Permalink
Plugin API Surface Refactor (#879)
Browse files Browse the repository at this point in the history
* Refactored and cleaned up interfaces between codegen and cli packages

* Migrate js module to codegen crate.

* Removed feature flagging for now.

* Minor cleanup.

* Removed accidental downgrade of dependency in cli

* Fixed up a few comments/visibilities and moved uninitialized plugins to CLI crate.

* Fixed up vaious comments/docstrings

* Corrected linting violations.
  • Loading branch information
ianmarmour authored Feb 13, 2025
1 parent ce388f5 commit 3fd7fc9
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 433 deletions.
6 changes: 3 additions & 3 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ wasmtime-wasi = { workspace = true }
wasi-common = { workspace = true }
walrus = "0.23.3"
swc_core = { version = "10.7.0", features = [
"common_sourcemap",
"ecma_ast",
"ecma_parser",
"common_sourcemap",
"ecma_ast",
"ecma_parser",
] }
wit-parser = "0.212.0"
convert_case = "0.7.1"
Expand Down
92 changes: 0 additions & 92 deletions crates/cli/src/codegen/builder.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ use anyhow::{anyhow, Result};
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
use wasmtime::{AsContextMut, Engine, Instance, Linker, Memory, Module, Store};

use crate::plugins::Plugin;

pub fn compile_source(plugin: &Plugin, js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env(plugin)?;
pub(crate) fn compile_source(plugin_bytes: &[u8], js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env(plugin_bytes)?;
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(plugin: &Plugin) -> Result<(Store<WasiCtx>, Instance, Memory)> {
fn create_wasm_env(plugin_bytes: &[u8]) -> Result<(Store<WasiCtx>, Instance, Memory)> {
let engine = Engine::default();
let module = Module::new(&engine, plugin.as_bytes())?;
let module = Module::new(&engine, plugin_bytes)?;
let mut linker = Linker::new(&engine);
wasi_common::sync::snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(
&mut linker,
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/codegen/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use anyhow::{anyhow, Result};
use convert_case::{Case, Casing};
use std::{env, path::Path};

use crate::{js::JS, wit};
use crate::{codegen::js::JS, codegen::wit};

pub(crate) type Exports = Vec<Export>;

#[derive(Clone)]
pub(crate) struct Export {
pub wit: String,
pub js: String,
Expand Down
14 changes: 10 additions & 4 deletions crates/cli/src/js.rs → crates/cli/src/codegen/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@ use swc_core::{
},
};

use crate::plugins::Plugin;
use crate::codegen::plugin::Plugin;

/// A structure representing valid JS code.
#[derive(Clone, Debug)]
pub struct JS {
source_code: Rc<String>,
}

impl JS {
fn from_string(source_code: String) -> JS {
/// Generate a valid JS instance from a string containing JS.
pub fn from_string(source_code: String) -> JS {
JS {
source_code: Rc::new(source_code),
}
}

/// Generate a valid JS instance from a file containing JS.
pub fn from_file(path: &Path) -> Result<JS> {
let mut input_file = File::open(path)
.with_context(|| format!("Failed to open input file {}", path.display()))?;
Expand All @@ -46,6 +49,7 @@ impl JS {
Ok(Self::from_string(String::from_utf8(contents)?))
}

/// Get JS source code as bytes.
pub fn as_bytes(&self) -> &[u8] {
self.source_code.as_bytes()
}
Expand All @@ -55,6 +59,7 @@ impl JS {
plugin.compile_source(self.source_code.as_bytes())
}

/// Get Brotli compressed JS source code as bytes.
pub fn compress(&self) -> Result<Vec<u8>> {
let mut compressed_source_code: Vec<u8> = vec![];
enc::BrotliCompress(
Expand All @@ -68,6 +73,7 @@ impl JS {
Ok(compressed_source_code)
}

/// Get the exports from a JS instance.
pub fn exports(&self) -> Result<Vec<String>> {
let module = self.parse_module()?;

Expand Down Expand Up @@ -165,10 +171,10 @@ impl JS {

#[cfg(test)]
mod tests {
use crate::js::JS;

use anyhow::Result;

use crate::codegen::js::JS;

#[test]
fn parse_no_exports() -> Result<()> {
let exports = parse("function foo() {}")?;
Expand Down
Loading

0 comments on commit 3fd7fc9

Please sign in to comment.