mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 18:32:15 +00:00
Merge #601
601: simplify example and make public get_wasi_state unsafe r=MarkMcCaskey a=MarkMcCaskey Co-authored-by: Mark McCaskey <mark@wasmer.io>
This commit is contained in:
commit
b5e20e508d
@ -31,8 +31,8 @@ install:
|
|||||||
|
|
||||||
# Install Rust
|
# Install Rust
|
||||||
# uncomment these lines if the cache is cleared, or if we must re-install rust for some reason
|
# uncomment these lines if the cache is cleared, or if we must re-install rust for some reason
|
||||||
# - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
|
- appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
|
||||||
# - rustup-init.exe -yv --default-host %target%
|
- rustup-init.exe -yv --default-host %target%
|
||||||
- set PATH=%PATH%;%USERPROFILE%\.cargo\bin
|
- set PATH=%PATH%;%USERPROFILE%\.cargo\bin
|
||||||
- rustup default stable-%target%
|
- rustup default stable-%target%
|
||||||
- rustup update
|
- rustup update
|
||||||
|
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).
|
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/
|
[wasi]: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
fn it_works() -> i32;
|
fn it_works() -> i32;
|
||||||
fn initialize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -10,6 +9,4 @@ pub fn plugin_entrypoint(n: i32) -> i32 {
|
|||||||
result + n
|
result + n
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {}
|
||||||
unsafe { initialize() };
|
|
||||||
}
|
|
||||||
|
@ -103,7 +103,7 @@ impl WasiFile for LoggingWrapper {
|
|||||||
|
|
||||||
/// Called by the program when it wants to set itself up
|
/// Called by the program when it wants to set itself up
|
||||||
fn initialize(ctx: &mut Ctx) {
|
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 {
|
let wasi_file_inner = LoggingWrapper {
|
||||||
wasm_module_name: "example module name".to_string(),
|
wasm_module_name: "example module name".to_string(),
|
||||||
};
|
};
|
||||||
@ -127,17 +127,16 @@ fn main() {
|
|||||||
let custom_imports = imports! {
|
let custom_imports = imports! {
|
||||||
"env" => {
|
"env" => {
|
||||||
"it_works" => func!(it_works),
|
"it_works" => func!(it_works),
|
||||||
"initialize" => func!(initialize),
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// The WASI imports object contains all required import functions for a WASI module to run.
|
// 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.
|
// Extend this imports with our custom imports containing "it_works" function so that our custom wasm code may run.
|
||||||
base_imports.extend(custom_imports);
|
base_imports.extend(custom_imports);
|
||||||
let instance =
|
let mut instance =
|
||||||
instantiate(&wasm_bytes[..], &base_imports).expect("failed to instantiate wasm module");
|
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");
|
|
||||||
// get a reference to the function "plugin_entrypoint" which takes an i32 and returns an i32
|
// get a reference to the function "plugin_entrypoint" which takes an i32 and returns an i32
|
||||||
let entry_point = instance.func::<(i32), i32>("plugin_entrypoint").unwrap();
|
let entry_point = instance.func::<(i32), i32>("plugin_entrypoint").unwrap();
|
||||||
// call the "entry_point" function in WebAssembly with the number "2" as the i32 argument
|
// call the "entry_point" function in WebAssembly with the number "2" as the i32 argument
|
||||||
|
@ -25,8 +25,9 @@ pub const VIRTUAL_ROOT_FD: __wasi_fd_t = 4;
|
|||||||
pub const ALL_RIGHTS: __wasi_rights_t = 0x1FFFFFFF;
|
pub const ALL_RIGHTS: __wasi_rights_t = 0x1FFFFFFF;
|
||||||
|
|
||||||
/// Get WasiState from a Ctx
|
/// Get WasiState from a Ctx
|
||||||
pub fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState {
|
/// This function is unsafe because it must be called on a WASI Ctx
|
||||||
unsafe { &mut *(ctx.data as *mut WasiState) }
|
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
|
/// A completely aribtrary "big enough" number used as the upper limit for
|
||||||
|
Loading…
x
Reference in New Issue
Block a user