From 26eccca7fcfd9c5e3e634cfe3164e0c8be1e71b8 Mon Sep 17 00:00:00 2001 From: EngoDev Date: Tue, 7 Oct 2025 20:20:28 +0300 Subject: [PATCH 1/3] Updated orca_wasm into wirm latest version --- Cargo.lock | 114 ++++++++++++++---- Cargo.toml | 5 +- .../spidermonkey-embedding-splicer/Cargo.toml | 3 +- .../src/bin/splicer.rs | 108 ++++++++++++++++- .../src/splice.rs | 70 +++++++---- .../src/stub_wasi.rs | 18 +-- 6 files changed, 255 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9078cb2a..e4e827e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,31 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "displaydoc" version = "0.2.5" @@ -199,6 +224,12 @@ dependencies = [ "syn", ] +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "embedded-io" version = "0.4.0" @@ -227,12 +258,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - [[package]] name = "fastrand" version = "2.3.0" @@ -371,9 +396,7 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" dependencies = [ - "fallible-iterator", "indexmap", - "stable_deref_trait", ] [[package]] @@ -642,20 +665,6 @@ version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cde51589ab56b20a6f686b2c68f7a0bd6add753d697abf720d63f8db3ab7b1ad" -[[package]] -name = "orca-wasm" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86ce9b3c6e21f067e2c1c85a4478ec26dda0a51fec5bc932dfa77f016a880861" -dependencies = [ - "gimli", - "log", - "serde_json", - "tempfile", - "wasm-encoder 0.227.1", - "wasmparser 0.227.1", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -729,6 +738,26 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rayon" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "rustix" version = "1.0.2" @@ -824,10 +853,11 @@ dependencies = [ "clap", "heck", "js-component-bindgen", - "orca-wasm", "rand", + "serde_json", "wasm-encoder 0.227.1", - "wasmparser 0.227.1", + "wasmparser 0.239.0", + "wirm", "wit-bindgen", "wit-bindgen-core", "wit-component", @@ -996,6 +1026,16 @@ dependencies = [ "wasmparser 0.227.1", ] +[[package]] +name = "wasm-encoder" +version = "0.239.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be00faa2b4950c76fe618c409d2c3ea5a3c9422013e079482d78544bb2d184c" +dependencies = [ + "leb128fmt", + "wasmparser 0.239.0", +] + [[package]] name = "wasm-metadata" version = "0.227.1" @@ -1033,6 +1073,18 @@ name = "wasmparser" version = "0.227.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f51cad774fb3c9461ab9bccc9c62dfb7388397b5deda31bf40e8108ccd678b2" +dependencies = [ + "bitflags", + "hashbrown", + "indexmap", + "semver", +] + +[[package]] +name = "wasmparser" +version = "0.239.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9d90bb93e764f6beabf1d02028c70a2156a6583e63ac4218dd07ef733368b0" dependencies = [ "bitflags", "hashbrown", @@ -1187,6 +1239,20 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wirm" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14732cb9a0eaf9ec52ecd36b9394ade5c16eea5405d160d8829f0199d97d507d" +dependencies = [ + "log", + "rayon", + "serde_json", + "tempfile", + "wasm-encoder 0.239.0", + "wasmparser 0.239.0", +] + [[package]] name = "wit-bindgen" version = "0.41.0" diff --git a/Cargo.toml b/Cargo.toml index 39e8d176..80a49c0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/crates/spidermonkey-embedding-splicer/Cargo.toml b/crates/spidermonkey-embedding-splicer/Cargo.toml index be8d7e6d..438408df 100644 --- a/crates/spidermonkey-embedding-splicer/Cargo.toml +++ b/crates/spidermonkey-embedding-splicer/Cargo.toml @@ -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 } diff --git a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs index 7d62a8d8..09124898 100644 --- a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs +++ b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs @@ -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)] @@ -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(), + } +} diff --git a/crates/spidermonkey-embedding-splicer/src/splice.rs b/crates/spidermonkey-embedding-splicer/src/splice.rs index 38606a0d..039d573a 100644 --- a/crates/spidermonkey-embedding-splicer/src/splice.rs +++ b/crates/spidermonkey-embedding-splicer/src/splice.rs @@ -1,17 +1,17 @@ use std::path::PathBuf; use anyhow::Result; -use orca_wasm::ir::function::{FunctionBuilder, FunctionModifier}; -use orca_wasm::ir::id::{ExportsID, FunctionID, LocalID}; -use orca_wasm::ir::module::Module; -use orca_wasm::ir::types::{BlockType, ElementItems, InstrumentationMode}; -use orca_wasm::module_builder::AddLocal; -use orca_wasm::opcode::{Inject, InjectAt}; -use orca_wasm::{DataType, Opcode}; use wasm_encoder::{Encode, Section}; use wasmparser::ExternalKind; use wasmparser::MemArg; use wasmparser::Operator; +use wirm::ir::function::{FunctionBuilder, FunctionModifier}; +use wirm::ir::id::{ExportsID, FunctionID, LocalID}; +use wirm::ir::module::Module; +use wirm::ir::types::{BlockType, ElementItems, InstrumentationMode}; +use wirm::module_builder::AddLocal; +use wirm::opcode::Inject; +use wirm::{DataType, Opcode}; use wit_component::metadata::{decode, Bindgen}; use wit_component::StringEncoding; use wit_parser::Resolve; @@ -725,7 +725,7 @@ fn synthesize_import_functions( // create imported function table let els = module.elements.iter_mut().next().unwrap(); - if let ElementItems::Functions(ref mut funcs) = &mut els.1 { + if let ElementItems::Functions(ref mut funcs) = &mut els.items { for fid in import_fnids { funcs.push(fid); } @@ -757,32 +757,50 @@ fn synthesize_import_functions( .get_fn_modifier(coreabi_get_import_fid) .unwrap(); - // walk until we get to the const representing the table index - let mut table_instr_idx = 0; - for (idx, instr) in builder.body.instructions.iter_mut().enumerate() { - if let Operator::I32Const { value: ref mut v } = instr.op { - // we specifically need the const "around" 3393 - // which is the coreabi_sample_i32 table offset - if *v < 1000 || *v > 5000 { - continue; + // Find the I32Const base index and compute the delta to new base + let mut table_instr_idx = 0usize; + let mut delta: i32 = 0; + { + let ops_ro = builder.body.instructions.get_ops(); + for (idx, op) in ops_ro.iter().enumerate() { + if let Operator::I32Const { value } = op { + // we specifically need the const "around" 3393 + // which is the coreabi_sample_i32 table offset + if *value < 1000 || *value > 5000 { + continue; + } + delta = import_fn_table_start_idx - *value; + table_instr_idx = idx; + break; } - *v = import_fn_table_start_idx; - table_instr_idx = idx; - break; } } - builder.inject_at( + // Inject adjustments after the located instruction: add delta and add arg index + builder + .body + .instructions + .set_current_mode(table_instr_idx, InstrumentationMode::After); + if delta != 0 { + builder + .body + .instructions + .add_instr(table_instr_idx, Operator::I32Const { value: delta }); + builder + .body + .instructions + .add_instr(table_instr_idx, Operator::I32Add); + } + builder.body.instructions.add_instr( table_instr_idx, - InstrumentationMode::Before, Operator::LocalGet { local_index: *arg_idx, }, ); - builder.inject_at( - table_instr_idx + 1, - InstrumentationMode::Before, - Operator::I32Add, - ); + builder + .body + .instructions + .add_instr(table_instr_idx, Operator::I32Add); + builder.body.instructions.finish_instr(table_instr_idx); } // remove unnecessary exports diff --git a/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs b/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs index 9fe125bd..7484d405 100644 --- a/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs +++ b/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs @@ -3,13 +3,13 @@ use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use anyhow::{bail, Result}; -use orca_wasm::ir::function::FunctionBuilder; -use orca_wasm::ir::id::{FunctionID, LocalID}; -use orca_wasm::ir::module::module_functions::FuncKind; -use orca_wasm::ir::types::{BlockType, InitExpr, Value}; -use orca_wasm::module_builder::AddLocal; -use orca_wasm::{DataType, Instructions, Module, Opcode}; use wasmparser::{MemArg, TypeRef}; +use wirm::ir::function::FunctionBuilder; +use wirm::ir::id::{FunctionID, LocalID}; +use wirm::ir::module::module_functions::FuncKind; +use wirm::ir::types::{BlockType, InitExpr, Value}; +use wirm::module_builder::AddLocal; +use wirm::{DataType, InitInstr, Module, Opcode}; use wit_parser::Resolve; use crate::parse_wit; @@ -44,14 +44,14 @@ where }; let ty = module.types.get(ty_id).unwrap(); - let (params, results) = (ty.params().to_vec(), ty.results().to_vec()); - let mut builder = FunctionBuilder::new(params.as_slice(), results.as_slice()); + let mut builder = FunctionBuilder::new(ty.params().as_slice(), ty.results().as_slice()); let _args = stub(&mut builder)?; builder.replace_import_in_module(module, iid); return Ok(Some(fid)); } + Ok(None) } @@ -206,7 +206,7 @@ fn stub_random(module: &mut Module) -> Result<()> { // create a mutable random seed global let seed_val: i64 = 0; let seed_global = module.add_global( - InitExpr::new(vec![Instructions::Value(Value::I64(seed_val))]), + InitExpr::new(vec![InitInstr::Value(Value::I64(seed_val))]), DataType::I64, true, false, From 3ba0bc0e5ae9acc078f1c57f9f1de070bcfc4fe5 Mon Sep 17 00:00:00 2001 From: EngoDev Date: Tue, 7 Oct 2025 21:28:50 +0300 Subject: [PATCH 2/3] Fixed CR comments --- Cargo.lock | 52 --------- Cargo.toml | 2 +- .../src/bin/splicer.rs | 104 ------------------ .../src/splice.rs | 32 +++--- 4 files changed, 16 insertions(+), 174 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4e827e7..fcc90796 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,31 +188,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossbeam-deque" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - [[package]] name = "displaydoc" version = "0.2.5" @@ -224,12 +199,6 @@ dependencies = [ "syn", ] -[[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - [[package]] name = "embedded-io" version = "0.4.0" @@ -738,26 +707,6 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -[[package]] -name = "rayon" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" -dependencies = [ - "crossbeam-deque", - "crossbeam-utils", -] - [[package]] name = "rustix" version = "1.0.2" @@ -1246,7 +1195,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14732cb9a0eaf9ec52ecd36b9394ade5c16eea5405d160d8829f0199d97d507d" dependencies = [ "log", - "rayon", "serde_json", "tempfile", "wasm-encoder 0.239.0", diff --git a/Cargo.toml b/Cargo.toml index 80a49c0c..3767688e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ 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" } -wirm = { version = "2.1.0", features = ["parallel"] } +wirm = { version = "2.1.0", default-features = false } 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" ] } diff --git a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs index 09124898..33d94417 100644 --- a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs +++ b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs @@ -141,112 +141,8 @@ 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(), - } -} diff --git a/crates/spidermonkey-embedding-splicer/src/splice.rs b/crates/spidermonkey-embedding-splicer/src/splice.rs index 039d573a..a1a137f7 100644 --- a/crates/spidermonkey-embedding-splicer/src/splice.rs +++ b/crates/spidermonkey-embedding-splicer/src/splice.rs @@ -10,7 +10,7 @@ use wirm::ir::id::{ExportsID, FunctionID, LocalID}; use wirm::ir::module::Module; use wirm::ir::types::{BlockType, ElementItems, InstrumentationMode}; use wirm::module_builder::AddLocal; -use wirm::opcode::Inject; +use wirm::opcode::{Inject, InjectAt}; use wirm::{DataType, Opcode}; use wit_component::metadata::{decode, Bindgen}; use wit_component::StringEncoding; @@ -775,11 +775,20 @@ fn synthesize_import_functions( } } } - // Inject adjustments after the located instruction: add delta and add arg index - builder - .body - .instructions - .set_current_mode(table_instr_idx, InstrumentationMode::After); + + builder.inject_at( + table_instr_idx, + InstrumentationMode::Before, + Operator::LocalGet { + local_index: *arg_idx, + }, + ); + builder.inject_at( + table_instr_idx + 1, + InstrumentationMode::Before, + Operator::I32Add, + ); + if delta != 0 { builder .body @@ -790,17 +799,6 @@ fn synthesize_import_functions( .instructions .add_instr(table_instr_idx, Operator::I32Add); } - builder.body.instructions.add_instr( - table_instr_idx, - Operator::LocalGet { - local_index: *arg_idx, - }, - ); - builder - .body - .instructions - .add_instr(table_instr_idx, Operator::I32Add); - builder.body.instructions.finish_instr(table_instr_idx); } // remove unnecessary exports From 765634c47cbab6de990b5f17f9bb4e870df38305 Mon Sep 17 00:00:00 2001 From: EngoDev Date: Fri, 10 Oct 2025 20:17:29 +0300 Subject: [PATCH 3/3] Fixed CR comment --- crates/spidermonkey-embedding-splicer/src/bin/splicer.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs index 33d94417..7d62a8d8 100644 --- a/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs +++ b/crates/spidermonkey-embedding-splicer/src/bin/splicer.rs @@ -5,9 +5,7 @@ use std::str::FromStr; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; -use spidermonkey_embedding_splicer::wit::exports::local::spidermonkey_embedding_splicer::splicer::{ - CoreFn, CoreTy, Feature, -}; +use spidermonkey_embedding_splicer::wit::exports::local::spidermonkey_embedding_splicer::splicer::Feature; use spidermonkey_embedding_splicer::{splice, stub_wasi}; #[derive(Parser, Debug)]