-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to compile Wasm modules in host functions called from the Wasmi…
… executor (take 2) (#1122) * add test to compile a Wasm module in a called host func * refactor CodeMap This makes it possible to compile Wasm modules from host functions called from Wasm. * add #[inline] and #[cold] attributes where useful * fix clippy warning * further refactoring of CodeMap::get method * add safety comment * refactor and clean-up CodeMap * simplify InternalFuncEntity::get_uncompiled * add safety comment * change inline annotation * fix safety comment * add note comment * improve docs and comments * fix doc link * remove unused methods * fix assert matches * move CodeMap up in the source file * remove unused From impls * move UncompiledFuncEntity::new up * move TypeIndex type up * refactor UncompiledFuncEntity::new params * improve (and kinda fix) doc comment * add comment to explain how we chose MAX_INLINE_SIZE * rename InternalFuncEntity -> FuncEntity * rename CodeMap public methods * apply rustfmt * update and fix docs (and doc links)
- Loading branch information
Showing
8 changed files
with
473 additions
and
518 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! This tests that a host function called from Wasm can compile Wasm modules and does not deadlock. | ||
use wasmi::{AsContextMut, Caller, Engine, Linker, Module, Store}; | ||
|
||
/// Converts the given `.wat` into `.wasm`. | ||
fn wat2wasm(wat: &str) -> Result<Vec<u8>, wat::Error> { | ||
wat::parse_str(wat) | ||
} | ||
|
||
fn compile_module(engine: &Engine) -> wasmi::Module { | ||
let wasm = wat2wasm(include_str!("../wat/host_call_compilation.wat")).unwrap(); | ||
Module::new(engine, &wasm[..]).unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_compile_in_host_call() { | ||
let engine = Engine::default(); | ||
let mut store = <Store<()>>::new(&engine, ()); | ||
let module = compile_module(store.engine()); | ||
let mut linker = <Linker<()>>::new(&engine); | ||
linker | ||
.func_wrap( | ||
"env", | ||
"compile", | ||
|mut caller: Caller<()>| -> Result<(), wasmi::Error> { | ||
let store = caller.as_context_mut(); | ||
let engine = store.engine(); | ||
let _module = compile_module(engine); | ||
Ok(()) | ||
}, | ||
) | ||
.unwrap(); | ||
let instance = linker | ||
.instantiate(&mut store, &module) | ||
.unwrap() | ||
.ensure_no_start(&mut store) | ||
.unwrap(); | ||
instance | ||
.get_typed_func::<(), ()>(&mut store, "run") | ||
.unwrap() | ||
.call(&mut store, ()) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(module | ||
(import "env" "compile" (func $compile)) | ||
|
||
(func (export "run") | ||
(call $compile) | ||
) | ||
) |