Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 90 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ too_many_arguments = 'allow'
anyhow = { version = "1.0.95", default-features = false }
heck = { version = "0.5", default-features = false }
js-component-bindgen = { version = "1.11.0" }
orca-wasm = { version = "0.9.2", default-features = false }
wirm = { version = "2.1.0", features = ["parallel"] }
rand = { version = "0.8", default-features = false }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
wasm-encoder = { version = "0.227.1", features = [ "component-model", "std" ] }
wasmparser = { version = "0.227.1", features = ["features",
wasmparser = { version = "0.239.0", features = ["features",
"component-model",
"hash-collections",
"serde",
Expand Down
3 changes: 2 additions & 1 deletion crates/spidermonkey-embedding-splicer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ anyhow = { workspace = true }
clap = { version = "4.5.31", features = ["suggestions", "color", "derive"] }
heck = { workspace = true }
js-component-bindgen = { workspace = true, features = [ "transpile-bindgen" ] }
orca-wasm = { workspace = true }
wirm = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
wasm-encoder = { workspace = true }
wasmparser = { workspace = true }
wit-bindgen = { workspace = true }
Expand Down
108 changes: 107 additions & 1 deletion crates/spidermonkey-embedding-splicer/src/bin/splicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::str::FromStr;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

use spidermonkey_embedding_splicer::wit::exports::local::spidermonkey_embedding_splicer::splicer::Feature;
use spidermonkey_embedding_splicer::wit::exports::local::spidermonkey_embedding_splicer::splicer::{
CoreFn, CoreTy, Feature,
};
use spidermonkey_embedding_splicer::{splice, stub_wasi};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -139,8 +141,112 @@ fn main() -> Result<()> {
out_dir.join("initializer.js").display()
)
})?;

// Write exports and imports as JSON (manual serialization)
let exports_json = serialize_exports(&result.exports);
fs::write(out_dir.join("exports.json"), exports_json).with_context(|| {
format!(
"Failed to write exports file: {}",
out_dir.join("exports.json").display()
)
})?;

let imports_json = serialize_imports(&result.imports);
fs::write(out_dir.join("imports.json"), imports_json).with_context(|| {
format!(
"Failed to write imports file: {}",
out_dir.join("imports.json").display()
)
})?;

println!(
"Successfully generated bindings and saved to {}",
out_dir.display()
);
}
}

Ok(())
}

/// Manually serialize exports to JSON
fn serialize_exports(exports: &[(String, CoreFn)]) -> String {
let mut result = String::from("[\n");
for (i, (name, core_fn)) in exports.iter().enumerate() {
if i > 0 {
result.push_str(",\n");
}
result.push_str(" [\"");
result.push_str(&name.replace('\\', "\\\\").replace('"', "\\\""));
result.push_str("\", ");
result.push_str(&serialize_core_fn(core_fn));
result.push(']');
}
result.push_str("\n]");
result
}

/// Manually serialize imports to JSON
fn serialize_imports(imports: &[(String, String, u32)]) -> String {
let mut result = String::from("[\n");
for (i, (specifier, name, arg_count)) in imports.iter().enumerate() {
if i > 0 {
result.push_str(",\n");
}
result.push_str(" [\"");
result.push_str(&specifier.replace('\\', "\\\\").replace('"', "\\\""));
result.push_str("\", \"");
result.push_str(&name.replace('\\', "\\\\").replace('"', "\\\""));
result.push_str("\", ");
result.push_str(&arg_count.to_string());
result.push(']');
}
result.push_str("\n]");
result
}

/// Manually serialize CoreFn to JSON
fn serialize_core_fn(core_fn: &CoreFn) -> String {
let mut result = String::from("{");

// params
result.push_str("\"params\": [");
for (i, param) in core_fn.params.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}
result.push_str(&serialize_core_ty(param));
}
result.push_str("], ");

// ret
result.push_str("\"ret\": ");
if let Some(ref ret) = core_fn.ret {
result.push_str(&serialize_core_ty(ret));
} else {
result.push_str("null");
}
result.push_str(", ");

// retptr
result.push_str(&format!("\"retptr\": {}, ", core_fn.retptr));

// retsize
result.push_str(&format!("\"retsize\": {}, ", core_fn.retsize));

// paramptr
result.push_str(&format!("\"paramptr\": {}", core_fn.paramptr));

result.push('}');
result
}

/// Manually serialize CoreTy to JSON
fn serialize_core_ty(core_ty: &CoreTy) -> String {
match core_ty {
CoreTy::I32 => "\"i32\"".to_string(),
CoreTy::I64 => "\"i64\"".to_string(),
CoreTy::F32 => "\"f32\"".to_string(),
CoreTy::F64 => "\"f64\"".to_string(),
}
}
Loading