Skip to content

Commit 3fd7fc9

Browse files
authored
Plugin API Surface Refactor (#879)
* 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.
1 parent ce388f5 commit 3fd7fc9

File tree

14 files changed

+488
-433
lines changed

14 files changed

+488
-433
lines changed

crates/cli/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ wasmtime-wasi = { workspace = true }
2323
wasi-common = { workspace = true }
2424
walrus = "0.23.3"
2525
swc_core = { version = "10.7.0", features = [
26-
"common_sourcemap",
27-
"ecma_ast",
28-
"ecma_parser",
26+
"common_sourcemap",
27+
"ecma_ast",
28+
"ecma_parser",
2929
] }
3030
wit-parser = "0.212.0"
3131
convert_case = "0.7.1"

crates/cli/src/codegen/builder.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.

crates/cli/src/bytecode.rs renamed to crates/cli/src/codegen/bytecode.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ use anyhow::{anyhow, Result};
22
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
33
use wasmtime::{AsContextMut, Engine, Instance, Linker, Memory, Module, Store};
44

5-
use crate::plugins::Plugin;
6-
7-
pub fn compile_source(plugin: &Plugin, js_source_code: &[u8]) -> Result<Vec<u8>> {
8-
let (mut store, instance, memory) = create_wasm_env(plugin)?;
5+
pub(crate) fn compile_source(plugin_bytes: &[u8], js_source_code: &[u8]) -> Result<Vec<u8>> {
6+
let (mut store, instance, memory) = create_wasm_env(plugin_bytes)?;
97
let (js_src_ptr, js_src_len) =
108
copy_source_code_into_instance(js_source_code, store.as_context_mut(), &instance, &memory)?;
119
let ret_ptr = call_compile(js_src_ptr, js_src_len, store.as_context_mut(), &instance)?;
1210
let bytecode = copy_bytecode_from_instance(ret_ptr, store.as_context_mut(), &memory)?;
1311
Ok(bytecode)
1412
}
1513

16-
fn create_wasm_env(plugin: &Plugin) -> Result<(Store<WasiCtx>, Instance, Memory)> {
14+
fn create_wasm_env(plugin_bytes: &[u8]) -> Result<(Store<WasiCtx>, Instance, Memory)> {
1715
let engine = Engine::default();
18-
let module = Module::new(&engine, plugin.as_bytes())?;
16+
let module = Module::new(&engine, plugin_bytes)?;
1917
let mut linker = Linker::new(&engine);
2018
wasi_common::sync::snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(
2119
&mut linker,

crates/cli/src/codegen/exports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use anyhow::{anyhow, Result};
22
use convert_case::{Case, Casing};
33
use std::{env, path::Path};
44

5-
use crate::{js::JS, wit};
5+
use crate::{codegen::js::JS, codegen::wit};
66

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

9+
#[derive(Clone)]
910
pub(crate) struct Export {
1011
pub wit: String,
1112
pub js: String,

crates/cli/src/js.rs renamed to crates/cli/src/codegen/js.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,23 @@ use swc_core::{
2424
},
2525
};
2626

27-
use crate::plugins::Plugin;
27+
use crate::codegen::plugin::Plugin;
2828

29+
/// A structure representing valid JS code.
2930
#[derive(Clone, Debug)]
3031
pub struct JS {
3132
source_code: Rc<String>,
3233
}
3334

3435
impl JS {
35-
fn from_string(source_code: String) -> JS {
36+
/// Generate a valid JS instance from a string containing JS.
37+
pub fn from_string(source_code: String) -> JS {
3638
JS {
3739
source_code: Rc::new(source_code),
3840
}
3941
}
4042

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

52+
/// Get JS source code as bytes.
4953
pub fn as_bytes(&self) -> &[u8] {
5054
self.source_code.as_bytes()
5155
}
@@ -55,6 +59,7 @@ impl JS {
5559
plugin.compile_source(self.source_code.as_bytes())
5660
}
5761

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

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

@@ -165,10 +171,10 @@ impl JS {
165171

166172
#[cfg(test)]
167173
mod tests {
168-
use crate::js::JS;
169-
170174
use anyhow::Result;
171175

176+
use crate::codegen::js::JS;
177+
172178
#[test]
173179
fn parse_no_exports() -> Result<()> {
174180
let exports = parse("function foo() {}")?;

0 commit comments

Comments
 (0)