Remove all checked in *.wasm files to the repo (#563)
* Tidy up the `hello` example for `wasmtime` * Remove the `*.wat` and `*.wasm` files and instead just inline the `*.wat` into the example. * Touch up comments so they're not just a repeat of the `println!` below. * Move `*.wat` for `memory` example inline No need to handle auxiliary files with the ability to parse it inline! * Move `multi.wasm` inline into `multi.rs` example * Move `*.wasm` for gcd example inline * Move `*.wat` inline with `import_calling_export` test * Remove checked in `lightbeam/test.wasm` Instead move the `*.wat` into the source and parse it into wasm there. * Run rustfmt
This commit is contained in:
@@ -54,6 +54,7 @@ wasmtime-wast = { path = "../wast" }
|
|||||||
wasmtime-wasi = { path = "../wasi" }
|
wasmtime-wasi = { path = "../wasi" }
|
||||||
rayon = "1.1"
|
rayon = "1.1"
|
||||||
file-per-thread-logger = "0.1.1"
|
file-per-thread-logger = "0.1.1"
|
||||||
|
wat = "1.0"
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|||||||
@@ -2,11 +2,40 @@
|
|||||||
//! invoking its exported function.
|
//! invoking its exported function.
|
||||||
|
|
||||||
use anyhow::{format_err, Result};
|
use anyhow::{format_err, Result};
|
||||||
use std::fs::read;
|
|
||||||
use wasmtime_api::*;
|
use wasmtime_api::*;
|
||||||
|
|
||||||
|
const WAT: &str = r#"
|
||||||
|
(module
|
||||||
|
(func $gcd (param i32 i32) (result i32)
|
||||||
|
(local i32)
|
||||||
|
block ;; label = @1
|
||||||
|
block ;; label = @2
|
||||||
|
local.get 0
|
||||||
|
br_if 0 (;@2;)
|
||||||
|
local.get 1
|
||||||
|
local.set 2
|
||||||
|
br 1 (;@1;)
|
||||||
|
end
|
||||||
|
loop ;; label = @2
|
||||||
|
local.get 1
|
||||||
|
local.get 0
|
||||||
|
local.tee 2
|
||||||
|
i32.rem_u
|
||||||
|
local.set 0
|
||||||
|
local.get 2
|
||||||
|
local.set 1
|
||||||
|
local.get 0
|
||||||
|
br_if 0 (;@2;)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local.get 2
|
||||||
|
)
|
||||||
|
(export "gcd" (func $gcd))
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let wasm = read("examples/gcd.wasm")?;
|
let wasm = wat::parse_str(WAT)?;
|
||||||
|
|
||||||
// Instantiate engine and store.
|
// Instantiate engine and store.
|
||||||
let engine = HostRef::new(Engine::default());
|
let engine = HostRef::new(Engine::default());
|
||||||
|
|||||||
Binary file not shown.
@@ -1,11 +1,8 @@
|
|||||||
//! Translation of hello example
|
//! Translation of hello example
|
||||||
|
|
||||||
extern crate alloc;
|
|
||||||
|
|
||||||
use alloc::rc::Rc;
|
|
||||||
use anyhow::{ensure, format_err, Context as _, Result};
|
use anyhow::{ensure, format_err, Context as _, Result};
|
||||||
use core::cell::Ref;
|
use core::cell::Ref;
|
||||||
use std::fs::read;
|
use std::rc::Rc;
|
||||||
use wasmtime_api::*;
|
use wasmtime_api::*;
|
||||||
|
|
||||||
struct HelloCallback;
|
struct HelloCallback;
|
||||||
@@ -19,25 +16,37 @@ impl Callable for HelloCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Initialize.
|
// Configure the initial compilation environment, creating more global
|
||||||
|
// structures such as an `Engine` and a `Store`.
|
||||||
println!("Initializing...");
|
println!("Initializing...");
|
||||||
let engine = HostRef::new(Engine::default());
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(&engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
// Load binary.
|
// Next upload the `*.wasm` binary file, which in this case we're going to
|
||||||
|
// be parsing an inline text format into a binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
let binary = read("examples/hello.wasm")?;
|
let binary = wat::parse_str(
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func $hello (import "" "hello"))
|
||||||
|
(func (export "run") (call $hello))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
|
||||||
// Compile.
|
// Compiler the `*.wasm` binary into an in-memory instance of a `Module`.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
|
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
|
||||||
|
|
||||||
// Create external print functions.
|
// Here we handle the imports of the module, which in this case is our
|
||||||
|
// `HelloCallback` type and its associated implementation of `Callback.
|
||||||
println!("Creating callback...");
|
println!("Creating callback...");
|
||||||
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
||||||
let hello_func = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
|
let hello_func = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
|
||||||
|
|
||||||
// Instantiate.
|
// Once we've got that all set up we can then move to the instantiation
|
||||||
|
// phase, pairing together a compiled module as well as a set of imports.
|
||||||
|
// Note that this is where the wasm `start` function, if any, would run.
|
||||||
println!("Instantiating module...");
|
println!("Instantiating module...");
|
||||||
let imports = vec![hello_func.into()];
|
let imports = vec![hello_func.into()];
|
||||||
let instance = HostRef::new(
|
let instance = HostRef::new(
|
||||||
@@ -45,24 +54,19 @@ fn main() -> Result<()> {
|
|||||||
.context("> Error instantiating module!")?,
|
.context("> Error instantiating module!")?,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Extract export.
|
// Next we poke around a bit to extract the `run` function from the module.
|
||||||
println!("Extracting export...");
|
println!("Extracting export...");
|
||||||
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
|
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
|
||||||
ensure!(!exports.is_empty(), "> Error accessing exports!");
|
ensure!(!exports.is_empty(), "> Error accessing exports!");
|
||||||
let run_func = exports[0].func().context("> Error accessing exports!")?;
|
let run_func = exports[0].func().context("> Error accessing exports!")?;
|
||||||
|
|
||||||
// Call.
|
// And last but not least we can call it!
|
||||||
println!("Calling export...");
|
println!("Calling export...");
|
||||||
run_func
|
run_func
|
||||||
.borrow()
|
.borrow()
|
||||||
.call(&[])
|
.call(&[])
|
||||||
.map_err(|e| format_err!("> Error calling function: {:?}", e))?;
|
.map_err(|e| format_err!("> Error calling function: {:?}", e))?;
|
||||||
|
|
||||||
// Shut down.
|
|
||||||
println!("Shutting down...");
|
|
||||||
drop(store);
|
|
||||||
|
|
||||||
// All done.
|
|
||||||
println!("Done.");
|
println!("Done.");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -1,4 +0,0 @@
|
|||||||
(module
|
|
||||||
(func $hello (import "" "hello"))
|
|
||||||
(func (export "run") (call $hello))
|
|
||||||
)
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
//! Translation of the memory example
|
//! Translation of the memory example
|
||||||
|
|
||||||
use anyhow::{bail, ensure, Context as _, Error};
|
use anyhow::{bail, ensure, Context as _, Error};
|
||||||
use core::cell::Ref;
|
use std::cell::Ref;
|
||||||
use std::fs::read;
|
|
||||||
use wasmtime_api::*;
|
use wasmtime_api::*;
|
||||||
|
|
||||||
fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> {
|
fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> {
|
||||||
@@ -69,7 +68,23 @@ fn main() -> Result<(), Error> {
|
|||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
let binary = read("examples/memory.wasm")?;
|
let binary = wat::parse_str(
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(memory (export "memory") 2 3)
|
||||||
|
|
||||||
|
(func (export "size") (result i32) (memory.size))
|
||||||
|
(func (export "load") (param i32) (result i32)
|
||||||
|
(i32.load8_s (local.get 0))
|
||||||
|
)
|
||||||
|
(func (export "store") (param i32 i32)
|
||||||
|
(i32.store8 (local.get 0) (local.get 1))
|
||||||
|
)
|
||||||
|
|
||||||
|
(data (i32.const 0x1000) "\01\02\03\04")
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
|
|||||||
Binary file not shown.
@@ -1,11 +0,0 @@
|
|||||||
(module
|
|
||||||
(memory (export "memory") 2 3)
|
|
||||||
|
|
||||||
(func (export "size") (result i32) (memory.size))
|
|
||||||
(func (export "load") (param i32) (result i32) (i32.load8_s (local.get 0)))
|
|
||||||
(func (export "store") (param i32 i32)
|
|
||||||
(i32.store8 (local.get 0) (local.get 1))
|
|
||||||
)
|
|
||||||
|
|
||||||
(data (i32.const 0x1000) "\01\02\03\04")
|
|
||||||
)
|
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
//! Translation of multi example
|
//! Translation of multi example
|
||||||
|
|
||||||
extern crate alloc;
|
|
||||||
|
|
||||||
use alloc::rc::Rc;
|
|
||||||
use anyhow::{ensure, format_err, Context as _, Result};
|
use anyhow::{ensure, format_err, Context as _, Result};
|
||||||
use core::cell::Ref;
|
use std::cell::Ref;
|
||||||
use std::fs::read;
|
use std::rc::Rc;
|
||||||
use wasmtime_api::*;
|
use wasmtime_api::*;
|
||||||
|
|
||||||
struct Callback;
|
struct Callback;
|
||||||
@@ -21,6 +18,31 @@ impl Callable for Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const WAT: &str = r#"
|
||||||
|
(module
|
||||||
|
(func $f (import "" "f") (param i32 i64) (result i64 i32))
|
||||||
|
|
||||||
|
(func $g (export "g") (param i32 i64) (result i64 i32)
|
||||||
|
(call $f (local.get 0) (local.get 1))
|
||||||
|
)
|
||||||
|
|
||||||
|
(func $round_trip_many
|
||||||
|
(export "round_trip_many")
|
||||||
|
(param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
|
||||||
|
(result i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
|
||||||
|
local.get 0
|
||||||
|
local.get 1
|
||||||
|
local.get 2
|
||||||
|
local.get 3
|
||||||
|
local.get 4
|
||||||
|
local.get 5
|
||||||
|
local.get 6
|
||||||
|
local.get 7
|
||||||
|
local.get 8
|
||||||
|
local.get 9)
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Initialize.
|
// Initialize.
|
||||||
println!("Initializing...");
|
println!("Initializing...");
|
||||||
@@ -29,7 +51,7 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
let binary = read("examples/multi.wasm")?;
|
let binary = wat::parse_str(WAT)?;
|
||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
|
|||||||
Binary file not shown.
@@ -1,22 +0,0 @@
|
|||||||
(module
|
|
||||||
(func $f (import "" "f") (param i32 i64) (result i64 i32))
|
|
||||||
|
|
||||||
(func $g (export "g") (param i32 i64) (result i64 i32)
|
|
||||||
(call $f (local.get 0) (local.get 1))
|
|
||||||
)
|
|
||||||
|
|
||||||
(func $round_trip_many
|
|
||||||
(export "round_trip_many")
|
|
||||||
(param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
|
|
||||||
(result i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
|
|
||||||
local.get 0
|
|
||||||
local.get 1
|
|
||||||
local.get 2
|
|
||||||
local.get 3
|
|
||||||
local.get 4
|
|
||||||
local.get 5
|
|
||||||
local.get 6
|
|
||||||
local.get 7
|
|
||||||
local.get 8
|
|
||||||
local.get 9)
|
|
||||||
)
|
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
extern crate alloc;
|
use std::cell::{Ref, RefCell};
|
||||||
|
use std::rc::Rc;
|
||||||
use alloc::rc::Rc;
|
|
||||||
use core::cell::{Ref, RefCell};
|
|
||||||
use std::fs::read;
|
|
||||||
use wasmtime_api::*;
|
use wasmtime_api::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_import_calling_export() {
|
fn test_import_calling_export() {
|
||||||
|
const WAT: &str = r#"
|
||||||
|
(module
|
||||||
|
(type $t0 (func))
|
||||||
|
(import "" "imp" (func $.imp (type $t0)))
|
||||||
|
(func $run call $.imp)
|
||||||
|
(func $other)
|
||||||
|
(export "run" (func $run))
|
||||||
|
(export "other" (func $other))
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
struct Callback {
|
struct Callback {
|
||||||
pub other: RefCell<Option<HostRef<Func>>>,
|
pub other: RefCell<Option<HostRef<Func>>>,
|
||||||
}
|
}
|
||||||
@@ -26,13 +34,8 @@ fn test_import_calling_export() {
|
|||||||
|
|
||||||
let engine = HostRef::new(Engine::default());
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(&engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
let module = HostRef::new(
|
let wasm = wat::parse_str(WAT).unwrap();
|
||||||
Module::new(
|
let module = HostRef::new(Module::new(&store, &wasm).expect("failed to create module"));
|
||||||
&store,
|
|
||||||
&read("tests/import_calling_export.wasm").expect("failed to read wasm file"),
|
|
||||||
)
|
|
||||||
.expect("failed to create module"),
|
|
||||||
);
|
|
||||||
|
|
||||||
let callback = Rc::new(Callback {
|
let callback = Rc::new(Callback {
|
||||||
other: RefCell::new(None),
|
other: RefCell::new(None),
|
||||||
|
|||||||
Binary file not shown.
@@ -30,6 +30,7 @@ more-asserts = "0.2.1"
|
|||||||
lazy_static = "1.2"
|
lazy_static = "1.2"
|
||||||
wat = "1.0.2"
|
wat = "1.0.2"
|
||||||
quickcheck = "0.9.0"
|
quickcheck = "0.9.0"
|
||||||
|
anyhow = "1.0"
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "experimental" }
|
maintenance = { status = "experimental" }
|
||||||
|
|||||||
@@ -1,34 +1,16 @@
|
|||||||
use lightbeam::translate;
|
use lightbeam::translate;
|
||||||
use std::fs::File;
|
|
||||||
use std::io;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
fn read_to_end<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
|
const WAT: &str = r#"
|
||||||
let mut buffer = Vec::new();
|
(module
|
||||||
if path.as_ref() == Path::new("-") {
|
(func (param i32) (param i32) (result i32) (i32.add (get_local 0) (get_local 1)))
|
||||||
let stdin = io::stdin();
|
)
|
||||||
let mut stdin = stdin.lock();
|
"#;
|
||||||
stdin.read_to_end(&mut buffer)?;
|
|
||||||
} else {
|
|
||||||
let mut file = File::open(path)?;
|
|
||||||
file.read_to_end(&mut buffer)?;
|
|
||||||
}
|
|
||||||
Ok(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn maybe_main() -> Result<(), String> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let data = read_to_end("test.wasm").map_err(|e| e.to_string())?;
|
let data = wat::parse_str(WAT)?;
|
||||||
let translated = translate(&data).map_err(|e| e.to_string())?;
|
let translated = translate(&data)?;
|
||||||
let result: u32 = translated.execute_func(0, (5u32, 3u32)).unwrap();
|
let result: u32 = translated.execute_func(0, (5u32, 3u32))?;
|
||||||
println!("f(5, 3) = {}", result);
|
println!("f(5, 3) = {}", result);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
match maybe_main() {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(e) => eprintln!("error: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use cranelift_codegen::{
|
|||||||
};
|
};
|
||||||
use memoffset::offset_of;
|
use memoffset::offset_of;
|
||||||
use more_asserts::assert_le;
|
use more_asserts::assert_le;
|
||||||
|
use thiserror::Error;
|
||||||
use wasmparser::{FuncType, MemoryType, ModuleReader, SectionCode, Type};
|
use wasmparser::{FuncType, MemoryType, ModuleReader, SectionCode, Type};
|
||||||
|
|
||||||
pub trait AsValueType {
|
pub trait AsValueType {
|
||||||
@@ -133,9 +134,11 @@ impl TranslatedModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Error)]
|
||||||
pub enum ExecutionError {
|
pub enum ExecutionError {
|
||||||
|
#[error("function index out of bounds")]
|
||||||
FuncIndexOutOfBounds,
|
FuncIndexOutOfBounds,
|
||||||
|
#[error("type mismatch")]
|
||||||
TypeMismatch,
|
TypeMismatch,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,3 +0,0 @@
|
|||||||
(module
|
|
||||||
(func (param i32) (param i32) (result i32) (i32.add (get_local 0) (get_local 1)))
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user