Skip to content

Commit

Permalink
use export to maybe retain fetchEvent
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <[email protected]>
  • Loading branch information
karthik2804 committed Jun 30, 2024
1 parent 3faee41 commit ff30f7d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
22 changes: 19 additions & 3 deletions crates/spidermonkey-embedding-splicer/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ pub struct Componentization {
pub resource_imports: Vec<(String, String, u32)>,
}

pub fn componentize_bindgen(resolve: &Resolve, id: WorldId, name: &str) -> Componentization {
pub fn componentize_bindgen(
resolve: &Resolve,
id: WorldId,
name: &str,
guest_imports: &Vec<String>,
guest_exports: &Vec<String>,
) -> Componentization {
let mut bindgen = JsBindgen {
src: Source::default(),
esm_bindgen: EsmBindgen::default(),
Expand All @@ -147,7 +153,7 @@ pub fn componentize_bindgen(resolve: &Resolve, id: WorldId, name: &str) -> Compo
.local_names
.exclude_globals(Intrinsic::get_global_names());

bindgen.imports_bindgen();
bindgen.imports_bindgen(&guest_imports);

bindgen.exports_bindgen();
bindgen.esm_bindgen.populate_export_aliases();
Expand Down Expand Up @@ -342,6 +348,7 @@ pub fn componentize_bindgen(resolve: &Resolve, id: WorldId, name: &str) -> Compo
"$source_mod",
&mut bindgen.local_names,
name,
&guest_exports,
);

let js_intrinsics = render_intrinsics(&mut bindgen.all_intrinsics, false, true);
Expand Down Expand Up @@ -451,9 +458,12 @@ impl JsBindgen<'_> {
}
}

fn imports_bindgen(&mut self) {
fn imports_bindgen(&mut self, guest_imports: &Vec<String>) {
for (key, impt) in &self.resolve.worlds[self.world].imports {
let import_name = self.resolve.name_world_key(key);
if !guest_imports.contains(&import_name) {
continue;
}
match &impt {
WorldItem::Function(f) => {
self.import_bindgen(import_name, f, false, None);
Expand Down Expand Up @@ -1001,6 +1011,7 @@ impl EsmBindgen {
imports_object: &str,
_local_names: &mut LocalNames,
source_name: &str,
guest_exports: &Vec<String>,
) {
// TODO: bring back these validations of imports
// including using the flattened bindings
Expand Down Expand Up @@ -1043,6 +1054,11 @@ impl EsmBindgen {
");
}
for (export_name, binding) in &self.exports {
if export_name == "wasi:http/[email protected]"
&& !guest_exports.contains(&"incomingHandler".to_string())
{
continue;
}
match binding {
Binding::Interface(bindings) => {
uwrite!(output, "const ");
Expand Down
20 changes: 10 additions & 10 deletions crates/spidermonkey-embedding-splicer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl Guest for SpidermonkeyEmbeddingSplicerComponent {
wit_path: Option<String>,
world_name: Option<String>,
guest_imports: Vec<String>,
guest_exports: Vec<String>,
debug: bool,
) -> Result<SpliceResult, String> {
let source_name = source_name.unwrap_or("source.js".to_string());
Expand All @@ -135,7 +136,13 @@ impl Guest for SpidermonkeyEmbeddingSplicerComponent {
.map_err(|e| e.to_string())?;

let mut wasm_bytes = wit_component::dummy_module(&resolve, world);
let componentized = bindgen::componentize_bindgen(&resolve, world, &source_name);
let componentized = bindgen::componentize_bindgen(
&resolve,
world,
&source_name,
&guest_imports,
&guest_exports,
);

// merge the engine world with the target world, retaining the engine producers
let producers = if let Ok((
Expand Down Expand Up @@ -330,18 +337,11 @@ impl Guest for SpidermonkeyEmbeddingSplicerComponent {
Some(i32::try_from(*return_count).unwrap()),
));
}
let mut trimmed_imports: Vec<(String, String, CoreFn, Option<i32>)> = Vec::new();
for (import, b, c, d) in imports {
// List of imports that are actually imported by the guest content
if guest_imports.contains(&import) {
trimmed_imports.push((import, b, c, d))
}
}

// println!("{:?}", &componentized.imports);
// println!("{:?}", &exports);
let mut wasm = splice::splice(engine, trimmed_imports, exports, debug)
.map_err(|e| format!("{:?}", e))?;
let mut wasm =
splice::splice(engine, imports, exports, debug).map_err(|e| format!("{:?}", e))?;

// add the world section to the spliced wasm
wasm.push(section.id());
Expand Down
17 changes: 10 additions & 7 deletions crates/spidermonkey-embedding-splicer/src/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ pub fn splice(
module.exports.delete(expt.id());
module.funcs.delete(run);
}
if let Ok(serve) = module
.exports
.get_func("wasi:http/[email protected]#handle")
{
let expt = module.exports.get_exported_func(serve).unwrap();
module.exports.delete(expt.id());
module.funcs.delete(serve);

if exports.iter().any(|(name, _)| name == "incoming-handler") {
if let Ok(serve) = module
.exports
.get_func("wasi:http/[email protected]#handle")
{
let expt = module.exports.get_exported_func(serve).unwrap();
module.exports.delete(expt.id());
module.funcs.delete(serve);
}
}

// we reencode the WASI world component data, so strip it out from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ world spidermonkey-embedding-splicer {

export stub-wasi: func(engine: list<u8>, features: list<features>, wit-world: option<string>, wit-path: option<string>, world-name: option<string>) -> result<list<u8>, string>;

export splice-bindings: func(source-name: option<string>, spidermonkey-engine: list<u8>, wit-world: option<string>, wit-path: option<string>, world-name: option<string>, guest-imports: list<string>, debug: bool) -> result<splice-result, string>;
export splice-bindings: func(source-name: option<string>, spidermonkey-engine: list<u8>, wit-world: option<string>, wit-path: option<string>, world-name: option<string>, guest-imports: list<string>, guest-exports: list<string>, debug: bool) -> result<splice-result, string>;
}
9 changes: 8 additions & 1 deletion src/componentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export async function componentize(jsSource, witWorld, opts) {

await lexerInit;
let jsImports = [];
let jsExports = [];
try {
[jsImports] = parse(jsSource);
[jsImports, jsExports] = parse(jsSource);
} catch {
// ignore parser errors - will show up as engine parse errors shortly
}
Expand All @@ -60,13 +61,19 @@ export async function componentize(jsSource, witWorld, opts) {
guestImports.push(k.n)
})

let guestExports = []
jsExports.map(k => {
guestExports.push(k.n)
})

let { wasm, jsBindings, importWrappers, exports, imports } = spliceBindings(
sourceName,
await readFile(engine),
witWorld,
maybeWindowsPath(witPath),
worldName,
guestImports,
guestExports,
false
);

Expand Down

0 comments on commit ff30f7d

Please sign in to comment.