mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-26 13:12:04 +00:00
adding interpreter test for WASM generated from C++
This commit is contained in:
BIN
res/cases/v1/inc_i32.wasm
Normal file
BIN
res/cases/v1/inc_i32.wasm
Normal file
Binary file not shown.
45
res/cases/v1/inc_i32.wast
Normal file
45
res/cases/v1/inc_i32.wast
Normal file
@ -0,0 +1,45 @@
|
||||
;; /// @file inc_i32.cpp
|
||||
;; #include <emscripten.h> // macro EMSCRIPTEN_KEEPALIVE
|
||||
;; #include <stdint.h>
|
||||
;; extern "C" {
|
||||
;; uint32_t EMSCRIPTEN_KEEPALIVE inc_i32(uint32_t param) {
|
||||
;; return ++param;
|
||||
;; }
|
||||
;; } // extern "C"
|
||||
(module
|
||||
(type $0 (func (param i32) (result i32)))
|
||||
(type $1 (func))
|
||||
(import "env" "memoryBase" (global $import$0 i32))
|
||||
(import "env" "memory" (memory $0 256))
|
||||
(import "env" "table" (table 0 anyfunc))
|
||||
(import "env" "tableBase" (global $import$3 i32))
|
||||
(global $global$0 (mut i32) (i32.const 0))
|
||||
(global $global$1 (mut i32) (i32.const 0))
|
||||
(export "_inc_i32" (func $0))
|
||||
(export "__post_instantiate" (func $2))
|
||||
(func $0 (type $0) (param $var$0 i32) (result i32)
|
||||
(i32.add
|
||||
(get_local $var$0)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(func $1 (type $1)
|
||||
(nop)
|
||||
)
|
||||
(func $2 (type $1)
|
||||
(block $label$0
|
||||
(set_global $global$0
|
||||
(get_global $import$0)
|
||||
)
|
||||
(set_global $global$1
|
||||
(i32.add
|
||||
(get_global $global$0)
|
||||
(i32.const 5242880)
|
||||
)
|
||||
)
|
||||
(call $1)
|
||||
)
|
||||
)
|
||||
;; custom section "dylink", size 5
|
||||
)
|
||||
|
@ -1,2 +1,3 @@
|
||||
mod basics;
|
||||
mod wabt;
|
||||
mod wasm;
|
||||
|
37
src/interpreter/tests/wasm.rs
Normal file
37
src/interpreter/tests/wasm.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use elements::deserialize_file;
|
||||
use elements::Module;
|
||||
use interpreter::EnvParams;
|
||||
use interpreter::ExecutionParams;
|
||||
use interpreter::module::ModuleInstanceInterface;
|
||||
use interpreter::program::ProgramInstance;
|
||||
use interpreter::value::RuntimeValue;
|
||||
|
||||
// Name of function contained in WASM file (note the leading underline)
|
||||
const FUNCTION_NAME: &'static str = "_inc_i32";
|
||||
|
||||
// The WASM file containing the module and function
|
||||
const WASM_FILE: &str = &"res/cases/v1/inc_i32.wasm";
|
||||
|
||||
#[test]
|
||||
fn interpreter_inc_i32() {
|
||||
let program = ProgramInstance::with_env_params(EnvParams {
|
||||
total_stack: 128 * 1024,
|
||||
total_memory: 2 * 1024 * 1024,
|
||||
allow_memory_growth: false,
|
||||
}).expect("Failed to instanciate program");
|
||||
|
||||
let module: Module =
|
||||
deserialize_file(WASM_FILE).expect("Failed to deserialize module from buffer");
|
||||
let i32_val = 42;
|
||||
// the functions expects a single i32 parameter
|
||||
let args = vec![RuntimeValue::I32(i32_val)];
|
||||
let exp_retval = Some(RuntimeValue::I32(i32_val + 1));
|
||||
let execution_params = ExecutionParams::from(args);
|
||||
let module = program
|
||||
.add_module("main", module, None)
|
||||
.expect("Failed to initialize module");
|
||||
let retval = module
|
||||
.execute_export(FUNCTION_NAME, execution_params)
|
||||
.expect("");
|
||||
assert_eq!(exp_retval, retval);
|
||||
}
|
Reference in New Issue
Block a user