mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 22:28:21 +00:00
Added support for Globals
This commit is contained in:
@@ -2,62 +2,56 @@ use wasm_bindgen_test::*;
|
||||
// use anyhow::Result;
|
||||
use wasmer_js::*;
|
||||
|
||||
// #[test]
|
||||
// fn global_new() -> Result<()> {
|
||||
// let store = Store::default();
|
||||
// let global = Global::new(&store, Value::I32(10));
|
||||
// assert_eq!(
|
||||
// *global.ty(),
|
||||
// GlobalType {
|
||||
// ty: Type::I32,
|
||||
// mutability: Mutability::Const
|
||||
// }
|
||||
// );
|
||||
#[wasm_bindgen_test]
|
||||
fn global_new() {
|
||||
let store = Store::default();
|
||||
let global = Global::new(&store, Value::I32(10));
|
||||
assert_eq!(
|
||||
*global.ty(),
|
||||
GlobalType {
|
||||
ty: Type::I32,
|
||||
mutability: Mutability::Const
|
||||
}
|
||||
);
|
||||
|
||||
// let global_mut = Global::new_mut(&store, Value::I32(10));
|
||||
// assert_eq!(
|
||||
// *global_mut.ty(),
|
||||
// GlobalType {
|
||||
// ty: Type::I32,
|
||||
// mutability: Mutability::Var
|
||||
// }
|
||||
// );
|
||||
let global_mut = Global::new_mut(&store, Value::I32(10));
|
||||
assert_eq!(
|
||||
*global_mut.ty(),
|
||||
GlobalType {
|
||||
ty: Type::I32,
|
||||
mutability: Mutability::Var
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
#[wasm_bindgen_test]
|
||||
fn global_get() {
|
||||
let store = Store::default();
|
||||
let global_i32 = Global::new(&store, Value::I32(10));
|
||||
assert_eq!(global_i32.get(), Value::I32(10));
|
||||
// let global_i64 = Global::new(&store, Value::I64(20));
|
||||
// assert_eq!(global_i64.get(), Value::I64(20));
|
||||
let global_f32 = Global::new(&store, Value::F32(10.0));
|
||||
assert_eq!(global_f32.get(), Value::F32(10.0));
|
||||
// let global_f64 = Global::new(&store, Value::F64(20.0));
|
||||
// assert_eq!(global_f64.get(), Value::F64(20.0));
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn global_get() -> Result<()> {
|
||||
// let store = Store::default();
|
||||
// let global_i32 = Global::new(&store, Value::I32(10));
|
||||
// assert_eq!(global_i32.get(), Value::I32(10));
|
||||
// let global_i64 = Global::new(&store, Value::I64(20));
|
||||
// assert_eq!(global_i64.get(), Value::I64(20));
|
||||
// let global_f32 = Global::new(&store, Value::F32(10.0));
|
||||
// assert_eq!(global_f32.get(), Value::F32(10.0));
|
||||
// let global_f64 = Global::new(&store, Value::F64(20.0));
|
||||
// assert_eq!(global_f64.get(), Value::F64(20.0));
|
||||
#[wasm_bindgen_test]
|
||||
fn global_set() {
|
||||
let store = Store::default();
|
||||
let global_i32 = Global::new(&store, Value::I32(10));
|
||||
// Set on a constant should error
|
||||
assert!(global_i32.set(Value::I32(20)).is_err());
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
let global_i32_mut = Global::new_mut(&store, Value::I32(10));
|
||||
// Set on different type should error
|
||||
assert!(global_i32_mut.set(Value::I64(20)).is_err());
|
||||
|
||||
// #[test]
|
||||
// fn global_set() -> Result<()> {
|
||||
// let store = Store::default();
|
||||
// let global_i32 = Global::new(&store, Value::I32(10));
|
||||
// // Set on a constant should error
|
||||
// assert!(global_i32.set(Value::I32(20)).is_err());
|
||||
|
||||
// let global_i32_mut = Global::new_mut(&store, Value::I32(10));
|
||||
// // Set on different type should error
|
||||
// assert!(global_i32_mut.set(Value::I64(20)).is_err());
|
||||
|
||||
// // Set on same type should succeed
|
||||
// global_i32_mut.set(Value::I32(20))?;
|
||||
// assert_eq!(global_i32_mut.get(), Value::I32(20));
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
// Set on same type should succeed
|
||||
global_i32_mut.set(Value::I32(20)).unwrap();
|
||||
assert_eq!(global_i32_mut.get(), Value::I32(20));
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn table_new() {
|
||||
|
||||
@@ -290,3 +290,57 @@ fn test_imported_function_native_with_wasmer_env() {
|
||||
let expected = vec![Val::I32(36)].into_boxed_slice();
|
||||
assert_eq!(exported.call(&[Val::I32(4)]), Ok(expected));
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_imported_exported_global() {
|
||||
let store = Store::default();
|
||||
let mut module = Module::new(
|
||||
&store,
|
||||
br#"
|
||||
(module
|
||||
(global $mut_i32_import (import "" "global") (mut i32))
|
||||
(func (export "getGlobal") (result i32) (global.get $mut_i32_import))
|
||||
(func (export "incGlobal") (global.set $mut_i32_import (
|
||||
i32.add (i32.const 1) (global.get $mut_i32_import)
|
||||
)))
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
module.set_type_hints(ModuleTypeHints {
|
||||
imports: vec![ExternType::Global(GlobalType::new(
|
||||
ValType::I32,
|
||||
Mutability::Var,
|
||||
))],
|
||||
exports: vec![
|
||||
ExternType::Function(FunctionType::new(vec![], vec![Type::I32])),
|
||||
ExternType::Function(FunctionType::new(vec![], vec![])),
|
||||
],
|
||||
});
|
||||
let mut global = Global::new_mut(&store, Value::I32(0));
|
||||
let import_object = imports! {
|
||||
"" => {
|
||||
"global" => global.clone()
|
||||
}
|
||||
};
|
||||
let instance = Instance::new(&module, &import_object).unwrap();
|
||||
|
||||
let get_global = instance.exports.get_function("getGlobal").unwrap();
|
||||
assert_eq!(
|
||||
get_global.call(&[]),
|
||||
Ok(vec![Val::I32(0)].into_boxed_slice())
|
||||
);
|
||||
|
||||
global.set(Value::I32(42));
|
||||
assert_eq!(
|
||||
get_global.call(&[]),
|
||||
Ok(vec![Val::I32(42)].into_boxed_slice())
|
||||
);
|
||||
|
||||
let inc_global = instance.exports.get_function("incGlobal").unwrap();
|
||||
inc_global.call(&[]);
|
||||
assert_eq!(
|
||||
get_global.call(&[]),
|
||||
Ok(vec![Val::I32(43)].into_boxed_slice())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user