diff --git a/bin/src/main.rs b/bin/src/main.rs index fe2d86c..912646b 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -11,6 +11,7 @@ use tempfile::TempDir; use std::env; use std::io::Write; +use std::path::Path; use std::process::{Command, Stdio}; const CORE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/core.wasm")); @@ -46,9 +47,13 @@ fn main() -> Result<(), Error> { user_code.push_str("\n"); user_code += INVOKE; - let tmp_dir = TempDir::new()?; - let core_path = tmp_dir.path().join("core.wasm"); - let shim_path = tmp_dir.path().join("shim.wasm"); + // let tmp_dir = TempDir::new()?; + // let core_path = tmp_dir.path().join("core.wasm"); + // let shim_path = tmp_dir.path().join("shim.wasm"); + + let core_path = Path::new("/Users/ben/py-out/core.wasm"); + let export_shim_path = Path::new("/Users/ben/py-out/main.wasm"); + let import_shim_path = Path::new("/Users/ben/py-out/import_shim.wasm"); let self_cmd = env::args().next().expect("Expected a command argument"); { @@ -75,7 +80,7 @@ fn main() -> Result<(), Error> { anyhow::bail!("No exports found, use __all__ to specify exported functions") } - shim::generate(&exports, &[], &shim_path)?; + shim::generate(&exports, &[], &export_shim_path, &import_shim_path)?; let output = Command::new("wasm-merge") .arg("--version") @@ -90,8 +95,10 @@ fn main() -> Result<(), Error> { let status = Command::new("wasm-merge") .arg(&core_path) .arg("core") - .arg(&shim_path) - .arg("shim") + .arg(&export_shim_path) + .arg("main") + .arg(&import_shim_path) + .arg("import_shim") .arg("-o") .arg(&opts.output) .arg("--enable-reference-types") diff --git a/bin/src/shim.rs b/bin/src/shim.rs index 30a4c56..6bc737a 100644 --- a/bin/src/shim.rs +++ b/bin/src/shim.rs @@ -4,8 +4,10 @@ use wagen::{Instr, ValType}; pub(crate) fn generate( exports: &[String], imports: &[Import], - shim_path: &std::path::Path, + export_shim_path: &std::path::Path, + import_shim_path: &std::path::Path, ) -> Result<(), Error> { + // export shim let mut module = wagen::Module::new(); let invoke = module.import( "core", @@ -27,6 +29,11 @@ pub(crate) fn generate( elements.push(fn_index.index); } + module.validate_save(&export_shim_path)?; + + // import shim + let mut module = wagen::Module::new(); + let n_imports = imports.len(); let import_table = module.tables().push(wagen::TableType { element_type: wagen::RefType::FUNCREF, @@ -82,6 +89,7 @@ pub(crate) fn generate( wagen::Elements::Functions(&import_elements), ); - module.validate_save(&shim_path)?; + module.validate_save(&import_shim_path)?; + Ok(()) } diff --git a/lib/src/py_module.rs b/lib/src/py_module.rs index 792b50b..6850646 100644 --- a/lib/src/py_module.rs +++ b/lib/src/py_module.rs @@ -356,7 +356,7 @@ pub fn make_extism_ffi_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> P Ok(()) } -#[link(wasm_import_module = "shim")] +#[link(wasm_import_module = "import_shim")] extern "C" { // this import will get satisified by the import shim fn __invokeHostFunc_0_0(func_idx: u32);