* Document and update the API of the `externals.rs` module This commit ensures that all public methods and items are documented in the `externals.rs` module, notably all external values that can be imported and exported in WebAssembly. Along the way this also tidies up the API and fixes a few bugs: * `Global::new` now returns a `Result` and fails if the provided value does not match the type of the global. * `Global::set` now returns a `Result` and fails if the global is either immutable or the provided value doesn't match the type of the global. * `Table::new` now fails if the provided initializer does not match the element type. * `Table::get` now returns `Option<Val>` instead of implicitly returning null. * `Table::set` now returns `Result<()>`, returning an error on out of bounds or if the input type is of the wrong type. * `Table::grow` now returns `Result<u32>`, returning the previous number of table elements if succesful or an error if the maximum is reached or the initializer value is of the wrong type. Additionally a bug was fixed here where if the wrong initializer was provided the table would be grown still, but initialization would fail. * `Memory::data` was renamed to `Memory::data_unchecked_mut`. Additionally `Memory::data_unchecked` was added. Lots of caveats were written down about how using the method can go wrong. * `Memory::grow` now returns `Result<u32>`, returning an error if growth fails or the number of pages previous the growth if successful. * Run rustfmt * Fix another test * Update crates/api/src/externals.rs Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com> Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
55 lines
2.3 KiB
Rust
55 lines
2.3 KiB
Rust
use wasmtime::*;
|
|
|
|
#[test]
|
|
fn bad_globals() {
|
|
let ty = GlobalType::new(ValType::I32, Mutability::Var);
|
|
assert!(Global::new(&Store::default(), ty.clone(), Val::I64(0)).is_err());
|
|
assert!(Global::new(&Store::default(), ty.clone(), Val::F32(0)).is_err());
|
|
assert!(Global::new(&Store::default(), ty.clone(), Val::F64(0)).is_err());
|
|
|
|
let ty = GlobalType::new(ValType::I32, Mutability::Const);
|
|
let g = Global::new(&Store::default(), ty.clone(), Val::I32(0)).unwrap();
|
|
assert!(g.set(Val::I32(1)).is_err());
|
|
|
|
let ty = GlobalType::new(ValType::I32, Mutability::Var);
|
|
let g = Global::new(&Store::default(), ty.clone(), Val::I32(0)).unwrap();
|
|
assert!(g.set(Val::I64(0)).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn bad_tables() {
|
|
// i32 not supported yet
|
|
let ty = TableType::new(ValType::I32, Limits::new(0, Some(1)));
|
|
assert!(Table::new(&Store::default(), ty.clone(), Val::I32(0)).is_err());
|
|
|
|
// mismatched initializer
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(0, Some(1)));
|
|
assert!(Table::new(&Store::default(), ty.clone(), Val::I32(0)).is_err());
|
|
|
|
// get out of bounds
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(0, Some(1)));
|
|
let t = Table::new(&Store::default(), ty.clone(), Val::AnyRef(AnyRef::Null)).unwrap();
|
|
assert!(t.get(0).is_none());
|
|
assert!(t.get(u32::max_value()).is_none());
|
|
|
|
// set out of bounds or wrong type
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(1, Some(1)));
|
|
let t = Table::new(&Store::default(), ty.clone(), Val::AnyRef(AnyRef::Null)).unwrap();
|
|
assert!(t.set(0, Val::I32(0)).is_err());
|
|
assert!(t.set(0, Val::AnyRef(AnyRef::Null)).is_ok());
|
|
assert!(t.set(1, Val::AnyRef(AnyRef::Null)).is_err());
|
|
|
|
// grow beyond max
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(1, Some(1)));
|
|
let t = Table::new(&Store::default(), ty.clone(), Val::AnyRef(AnyRef::Null)).unwrap();
|
|
assert!(t.grow(0, Val::AnyRef(AnyRef::Null)).is_ok());
|
|
assert!(t.grow(1, Val::AnyRef(AnyRef::Null)).is_err());
|
|
assert_eq!(t.size(), 1);
|
|
|
|
// grow wrong type
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(1, Some(2)));
|
|
let t = Table::new(&Store::default(), ty.clone(), Val::AnyRef(AnyRef::Null)).unwrap();
|
|
assert!(t.grow(1, Val::I32(0)).is_err());
|
|
assert_eq!(t.size(), 1);
|
|
}
|