Skip to content

Experiment: Dynamic Linking of core #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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");
{
Expand All @@ -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")
Expand All @@ -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")
Expand Down
12 changes: 10 additions & 2 deletions bin/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion lib/src/py_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down