mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-05 02:20:19 +00:00
simplify example and make public get_wasi_state unsafe
This commit is contained in:
parent
7d1ed7a056
commit
7760d5a4a2
Binary file not shown.
@ -40,6 +40,4 @@ In this example, we instantiate a system with an extended (WASI)[wasi] ABI, allo
|
||||
|
||||
Because the Rust WASI doesn't support the crate type of `cdylib`, we have to include a main function which we don't use. This is being discussed [here](https://github.com/WebAssembly/WASI/issues/24).
|
||||
|
||||
We call the main function to initialize WASI's libpreopen internal datastructures and have the module call back into the host to set swap out the modules implementation of stdout. The host then provides a wrapper around stdout, allowing the guest's writes to stdout to be formatted in a host-appropriate manner.
|
||||
|
||||
[wasi]: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/
|
||||
|
@ -1,6 +1,5 @@
|
||||
extern "C" {
|
||||
fn it_works() -> i32;
|
||||
fn initialize();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@ -10,6 +9,4 @@ pub fn plugin_entrypoint(n: i32) -> i32 {
|
||||
result + n
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
unsafe { initialize() };
|
||||
}
|
||||
pub fn main() {}
|
||||
|
@ -103,7 +103,7 @@ impl WasiFile for LoggingWrapper {
|
||||
|
||||
/// Called by the program when it wants to set itself up
|
||||
fn initialize(ctx: &mut Ctx) {
|
||||
let state = state::get_wasi_state(ctx);
|
||||
let state = unsafe { state::get_wasi_state(ctx) };
|
||||
let wasi_file_inner = LoggingWrapper {
|
||||
wasm_module_name: "example module name".to_string(),
|
||||
};
|
||||
@ -127,14 +127,15 @@ fn main() {
|
||||
let custom_imports = imports! {
|
||||
"env" => {
|
||||
"it_works" => func!(it_works),
|
||||
"initialize" => func!(initialize),
|
||||
},
|
||||
};
|
||||
// The WASI imports object contains all required import functions for a WASI module to run.
|
||||
// Extend this imports with our custom imports containing "it_works" function so that our custom wasm code may run.
|
||||
base_imports.extend(custom_imports);
|
||||
let instance =
|
||||
let mut instance =
|
||||
instantiate(&wasm_bytes[..], &base_imports).expect("failed to instantiate wasm module");
|
||||
// set up logging by replacing stdout
|
||||
initialize(instance.context_mut());
|
||||
|
||||
let main = instance.func::<(), ()>("_start").unwrap();
|
||||
main.call().expect("Could not initialize");
|
||||
|
@ -25,8 +25,9 @@ pub const VIRTUAL_ROOT_FD: __wasi_fd_t = 4;
|
||||
pub const ALL_RIGHTS: __wasi_rights_t = 0x1FFFFFFF;
|
||||
|
||||
/// Get WasiState from a Ctx
|
||||
pub fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState {
|
||||
unsafe { &mut *(ctx.data as *mut WasiState) }
|
||||
/// This function is unsafe because it must be called on a WASI Ctx
|
||||
pub unsafe fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState {
|
||||
&mut *(ctx.data as *mut WasiState)
|
||||
}
|
||||
|
||||
/// A completely aribtrary "big enough" number used as the upper limit for
|
||||
|
Loading…
Reference in New Issue
Block a user