feat: add a histrical wit-bindgen
This commit is contained in:
@@ -0,0 +1 @@
|
||||
thunk: func()
|
||||
@@ -0,0 +1,27 @@
|
||||
from exports.bindings import Exports
|
||||
from imports.bindings import add_imports_to_linker, Imports
|
||||
import sys
|
||||
import wasmtime
|
||||
|
||||
class MyImports:
|
||||
def thunk(self) -> None:
|
||||
self.hit = True
|
||||
|
||||
def run(wasm_file: str) -> None:
|
||||
store = wasmtime.Store()
|
||||
module = wasmtime.Module.from_file(store.engine, wasm_file)
|
||||
linker = wasmtime.Linker(store.engine)
|
||||
linker.define_wasi()
|
||||
wasi = wasmtime.WasiConfig()
|
||||
wasi.inherit_stdout()
|
||||
wasi.inherit_stderr()
|
||||
store.set_wasi(wasi)
|
||||
|
||||
imports = MyImports()
|
||||
add_imports_to_linker(linker, store, imports)
|
||||
wasm = Exports(store, linker, module)
|
||||
wasm.thunk(store)
|
||||
assert(imports.hit)
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(sys.argv[1])
|
||||
@@ -0,0 +1,33 @@
|
||||
use anyhow::Result;
|
||||
|
||||
wit_bindgen_wasmtime::export!("../../tests/runtime/smoke/imports.wit");
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyImports {
|
||||
hit: bool,
|
||||
}
|
||||
|
||||
impl imports::Imports for MyImports {
|
||||
fn thunk(&mut self) {
|
||||
self.hit = true;
|
||||
println!("in the host");
|
||||
}
|
||||
}
|
||||
|
||||
wit_bindgen_wasmtime::import!("../../tests/runtime/smoke/exports.wit");
|
||||
|
||||
fn run(wasm: &str) -> Result<()> {
|
||||
let (exports, mut store) = crate::instantiate(
|
||||
wasm,
|
||||
|linker| imports::add_to_linker(linker, |cx| -> &mut MyImports { &mut cx.imports }),
|
||||
|store, module, linker| {
|
||||
exports::Exports::instantiate(store, module, linker, |cx| &mut cx.exports)
|
||||
},
|
||||
)?;
|
||||
|
||||
exports.thunk(&mut store)?;
|
||||
|
||||
assert!(store.data().imports.hit);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { addImportsToImports, Imports } from "./imports.js";
|
||||
import { Exports } from "./exports.js";
|
||||
import { getWasm, addWasiToImports } from "./helpers.js";
|
||||
|
||||
function assert(x: boolean, msg: string) {
|
||||
if (!x)
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const importObj = {};
|
||||
let hit = false;
|
||||
addImportsToImports(importObj, {
|
||||
thunk() {
|
||||
hit = true;
|
||||
}
|
||||
});
|
||||
const wasi = addWasiToImports(importObj);
|
||||
|
||||
const wasm = new Exports();
|
||||
await wasm.instantiate(getWasm(), importObj);
|
||||
wasi.start(wasm.instance);
|
||||
|
||||
wasm.thunk();
|
||||
assert(hit, "import not called");
|
||||
}
|
||||
|
||||
await run()
|
||||
@@ -0,0 +1 @@
|
||||
thunk: func()
|
||||
@@ -0,0 +1,6 @@
|
||||
#include <imports.h>
|
||||
#include <exports.h>
|
||||
|
||||
void exports_thunk() {
|
||||
imports_thunk();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
wit_bindgen_rust::import!("../../tests/runtime/smoke/imports.wit");
|
||||
wit_bindgen_rust::export!("../../tests/runtime/smoke/exports.wit");
|
||||
|
||||
struct Exports;
|
||||
|
||||
impl exports::Exports for Exports {
|
||||
fn thunk() {
|
||||
imports::thunk();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user