mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-27 05:32:07 +00:00
another tests added
This commit is contained in:
@ -38,8 +38,8 @@ fn import_function() {
|
||||
.build();
|
||||
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
let external_module = program.add_module("external_module", module1).unwrap();
|
||||
let main_module = program.add_module("main", module2).unwrap();
|
||||
let external_module = program.add_module("external_module", module1, None).unwrap();
|
||||
let main_module = program.add_module("main", module2, None).unwrap();
|
||||
|
||||
assert_eq!(external_module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(3));
|
||||
assert_eq!(main_module.execute_index(1, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(10));
|
||||
@ -72,10 +72,8 @@ fn wrong_import() {
|
||||
.build();
|
||||
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
let _side_module_instance = program.add_module("side_module", side_module).unwrap();
|
||||
let module_instance = program.add_module("main", module).unwrap();
|
||||
|
||||
assert!(module_instance.execute_index(1, vec![].into()).is_err());
|
||||
let _side_module_instance = program.add_module("side_module", side_module, None).unwrap();
|
||||
assert!(program.add_module("main", module, None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -117,7 +115,7 @@ fn global_get_set() {
|
||||
.build();
|
||||
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
let module = program.add_module_without_validation("main", module).unwrap(); // validation is failing (accessing immutable global)
|
||||
let module = program.add_module_without_validation("main", module, None).unwrap(); // validation is failing (accessing immutable global)
|
||||
assert_eq!(module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(50));
|
||||
assert_eq!(module.execute_index(1, vec![].into()).unwrap_err(), Error::Variable("trying to update immutable variable".into()));
|
||||
assert_eq!(module.execute_index(2, vec![].into()).unwrap_err(), Error::Variable("trying to update variable of type I32 with value of type Some(I64)".into()));
|
||||
@ -162,36 +160,12 @@ fn single_program_different_modules() {
|
||||
// => linear memory is created
|
||||
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
|
||||
|
||||
let module = module()
|
||||
.with_import(ImportEntry::new("env".into(), "add".into(), External::Function(0)))
|
||||
.with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0)))
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::Call(0),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::Call(1),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.build();
|
||||
|
||||
// load module
|
||||
let module_instance = program.add_module("main", module).unwrap();
|
||||
|
||||
// create native env module executor
|
||||
let mut executor = FunctionExecutor {
|
||||
memory: env_memory.clone(),
|
||||
values: Vec::new(),
|
||||
};
|
||||
{
|
||||
// create native env module with native add && sub implementations
|
||||
let functions: UserFunctions = UserFunctions {
|
||||
executor: &mut executor,
|
||||
functions: vec![UserFunction {
|
||||
@ -205,13 +179,37 @@ fn single_program_different_modules() {
|
||||
}],
|
||||
};
|
||||
let native_env_instance = Arc::new(env_native_module(env_instance, functions).unwrap());
|
||||
|
||||
// execute functions
|
||||
let params = ExecutionParams::with_external("env".into(), native_env_instance);
|
||||
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(7))).unwrap().unwrap(), RuntimeValue::I32(7));
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(50))).unwrap().unwrap(), RuntimeValue::I32(57));
|
||||
assert_eq!(module_instance.execute_index(3, params.clone().add_argument(RuntimeValue::I32(15))).unwrap().unwrap(), RuntimeValue::I32(42));
|
||||
let module = module()
|
||||
.with_import(ImportEntry::new("env".into(), "add".into(), External::Function(0)))
|
||||
.with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0)))
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::Call(0),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::Call(1),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.build();
|
||||
|
||||
// load module
|
||||
let module_instance = program.add_module("main", module, Some(¶ms.externals)).unwrap();
|
||||
|
||||
{
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(7))).unwrap().unwrap(), RuntimeValue::I32(7));
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(50))).unwrap().unwrap(), RuntimeValue::I32(57));
|
||||
assert_eq!(module_instance.execute_index(3, params.clone().add_argument(RuntimeValue::I32(15))).unwrap().unwrap(), RuntimeValue::I32(42));
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(executor.memory.get(0, 1).unwrap()[0], 42);
|
||||
|
Reference in New Issue
Block a user