diff --git a/Cargo.lock b/Cargo.lock index ed117b2dd..b02c01753 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -491,8 +491,8 @@ dependencies = [ "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-clif-backend 0.1.1", "wasmer-emscripten 0.1.0", - "wasmer-runtime 0.1.1", - "wasmer-runtime-core 0.1.1", + "wasmer-runtime 0.1.3", + "wasmer-runtime-core 0.1.2", ] [[package]] @@ -507,7 +507,7 @@ dependencies = [ "hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "target-lexicon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmer-runtime-core 0.1.1", + "wasmer-runtime-core 0.1.2", "wasmparser 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -522,20 +522,20 @@ dependencies = [ "time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-clif-backend 0.1.1", - "wasmer-runtime-core 0.1.1", + "wasmer-runtime-core 0.1.2", ] [[package]] name = "wasmer-runtime" -version = "0.1.1" +version = "0.1.3" dependencies = [ "wasmer-clif-backend 0.1.1", - "wasmer-runtime-core 0.1.1", + "wasmer-runtime-core 0.1.2", ] [[package]] name = "wasmer-runtime-core" -version = "0.1.1" +version = "0.1.2" dependencies = [ "errno 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "field-offset 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index 76b1b8d96..b1551c149 100644 --- a/lib/runtime-core/Cargo.toml +++ b/lib/runtime-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasmer-runtime-core" -version = "0.1.1" +version = "0.1.2" description = "Wasmer runtime core library" license = "MIT" authors = ["The Wasmer Engineering Team "] diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index 459ae8e68..204f33391 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -60,7 +60,6 @@ impl LocalBacking { for (_, mem) in &module.memories { // If we use emscripten, we set a fixed initial and maximum - debug!("Instance - init memory ({}, {:?})", mem.min, mem.max); // let memory = if options.abi == InstanceABI::Emscripten { // // We use MAX_PAGES, so at the end the result is: // // (initial * LinearMemory::PAGE_SIZE) == LinearMemory::DEFAULT_HEAP_SIZE diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index dc0972d43..17523f428 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -140,6 +140,7 @@ pub enum Error { CompileError(CompileError), LinkError(Vec), RuntimeError(RuntimeError), + ResolveError(ResolveError), CallError(CallError), } @@ -167,6 +168,12 @@ impl From> for Box { } } +impl From> for Box { + fn from(resolve_err: Box) -> Self { + Box::new(Error::ResolveError(*resolve_err)) + } +} + impl From> for Box { fn from(call_err: Box) -> Self { Box::new(Error::CallError(*call_err)) diff --git a/lib/runtime-core/src/memory.rs b/lib/runtime-core/src/memory.rs index a37e26037..2b7fb11c3 100644 --- a/lib/runtime-core/src/memory.rs +++ b/lib/runtime-core/src/memory.rs @@ -47,7 +47,6 @@ impl LinearMemory { pub fn new(mem: &Memory) -> Self { assert!(mem.min <= Self::MAX_PAGES); assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES); - debug!("Instantiate LinearMemory(mem: {:?})", mem); let (mmap_size, initial_pages, offset_guard_size, requires_signal_catch) = if /*mem.is_static_heap()*/ diff --git a/lib/runtime/Cargo.toml b/lib/runtime/Cargo.toml index b7cafc895..4642f9d84 100644 --- a/lib/runtime/Cargo.toml +++ b/lib/runtime/Cargo.toml @@ -1,14 +1,15 @@ [package] name = "wasmer-runtime" -version = "0.1.1" +version = "0.1.3" description = "Wasmer runtime library" license = "MIT" authors = ["The Wasmer Engineering Team "] repository = "https://github.com/wasmerio/wasmer" edition = "2018" +readme = "README.md" [dependencies] -wasmer-runtime-core = { path = "../runtime-core", version = "0.1.1" } +wasmer-runtime-core = { path = "../runtime-core", version = "0.1.2" } wasmer-clif-backend = { path = "../clif-backend", version = "0.1.1", optional = true } [features] diff --git a/lib/runtime/README.md b/lib/runtime/README.md new file mode 100644 index 000000000..bd9bb0f0c --- /dev/null +++ b/lib/runtime/README.md @@ -0,0 +1,75 @@ +# Wasmer-Runtime + +Wasmer-runtime is a library that makes embedding WebAssembly +in your application easy, efficient, and safe. + +# How to use Wasmer-Runtime + +The easiest way is to use the [`instantiate`] function to create an [`Instance`]. +Then you can use [`call`] or [`func`] and then [`call`][func.call] to call an exported function safely. + +[`instantiate`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/fn.instantiate.html +[`Instance`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html +[`call`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html#method.call +[`func`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html#method.func +[func.call]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Function.html#method.call + +## Here's an example: + +Given this WebAssembly: + +```wat +(module + (type $t0 (func (param i32) (result i32))) + (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) + get_local $p0 + i32.const 1 + i32.add)) +``` + +compiled into wasm bytecode, we can call the exported "add_one" function: + +```rust +static WASM: &'static [u8] = &[ + // The module above compiled to bytecode goes here. + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, + 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07, + 0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, + 0x07, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x0b, 0x00, 0x1a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x01, 0x0a, 0x01, 0x00, 0x07, 0x61, 0x64, 0x64, 0x5f, + 0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02, 0x70, 0x30, +]; + +use wasmer_runtime::{ + instantiate, + Value, + imports, + error, +}; + +fn main() -> error::Result<()> { + // We're not importing anything, so make an empty import object. + let import_object = imports! {}; + + let mut instance = instantiate(WASM, import_object)?; + + let values = instance + .func("add_one")? + .call(&[Value::I32(42)])?; + + assert_eq!(values[0], Value::I32(43)); + + Ok(()) +} +``` + +# Additional Notes: + +The `wasmer-runtime` is build to support compiler multiple backends. +Currently, we support the [Cranelift] compiler with the [`wasmer-clif-backend`] crate. + +You can specify the compiler you wish to use with the [`compile_with`] function. + +[Cranelift]: https://github.com/CraneStation/cranelift +[`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend +[`compile_with`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/fn.compile_with.html \ No newline at end of file diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index fd40a8b4a..5f488fa74 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -1,3 +1,78 @@ +//! Wasmer-runtime is a library that makes embedding WebAssembly +//! in your application easy, efficient, and safe. +//! +//! # How to use Wasmer-Runtime +//! +//! The easiest way is to use the [`instantiate`] function to create an [`Instance`]. +//! Then you can use [`call`] or [`func`] and then [`call`][func.call] to call an exported function safely. +//! +//! [`instantiate`]: fn.instantiate.html +//! [`Instance`]: struct.Instance.html +//! [`call`]: struct.Instance.html#method.call +//! [`func`]: struct.Instance.html#method.func +//! [func.call]: struct.Function.html#method.call +//! +//! ## Here's an example: +//! +//! Given this WebAssembly: +//! +//! ```wat +//! (module +//! (type $t0 (func (param i32) (result i32))) +//! (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) +//! get_local $p0 +//! i32.const 1 +//! i32.add)) +//! ``` +//! +//! compiled into wasm bytecode, we can call the exported "add_one" function: +//! +//! ``` +//! static WASM: &'static [u8] = &[ +//! // The module above compiled to bytecode goes here. +//! // ... +//! # 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, +//! # 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07, +//! # 0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, +//! # 0x07, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x0b, 0x00, 0x1a, 0x04, 0x6e, +//! # 0x61, 0x6d, 0x65, 0x01, 0x0a, 0x01, 0x00, 0x07, 0x61, 0x64, 0x64, 0x5f, +//! # 0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02, 0x70, 0x30, +//! ]; +//! +//! use wasmer_runtime::{ +//! instantiate, +//! Value, +//! imports, +//! error, +//! }; +//! +//! fn main() -> error::Result<()> { +//! // We're not importing anything, so make an empty import object. +//! let import_object = imports! {}; +//! +//! let mut instance = instantiate(WASM, import_object)?; +//! +//! let values = instance +//! .func("add_one")? +//! .call(&[Value::I32(42)])?; +//! +//! assert_eq!(values[0], Value::I32(43)); +//! +//! Ok(()) +//! } +//! ``` +//! +//! # Additional Notes: +//! +//! The `wasmer-runtime` is build to support compiler multiple backends. +//! Currently, we support the [Cranelift] compiler with the [`wasmer-clif-backend`] crate. +//! +//! You can specify the compiler you wish to use with the [`compile_with`] function. +//! +//! [Cranelift]: https://github.com/CraneStation/cranelift +//! [`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend +//! [`compile_with`]: fn.compile_with.html + pub use wasmer_runtime_core::import::ImportObject; pub use wasmer_runtime_core::instance::{Function, Instance}; pub use wasmer_runtime_core::module::Module;