Hi, first thanks for your great work.
I met a strange behavior and got confuse whether it is a bug or excepted behavior.
The source is simple cpp codes as
extern "C" {
extern void print(int v);
extern int iadd(int a, int b);
int test(){
int a = iadd(4,10);
print(a);
return 0;
}
}
then I got a .wasm file, I pasted the .wast content below:
(module
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
(type $FUNCSIG$vi (func (param i32)))
(import "env" "iadd" (func $iadd (param i32 i32) (result i32)))
(import "env" "print" (func $print (param i32)))
(table 0 anyfunc)
(memory $0 1)
(data (i32.const 4) "\10@\00\00")
(export "memory" (memory $0))
(export "test" (func $test))
(func $test (result i32)
(call $print
(call $iadd
(i32.const 4)
(i32.const 10)
)
)
(i32.const 0)
)
)
Following your vm_example_test.go
, I tried these ( only Types
, FunctionIndexSpace
, Export
below)
var nativeFunctionList = []string{
"print42",
"print",
"iadd",
}
var nativeFunctionTypes = &wasm.SectionTypes{
Entries: []wasm.FunctionSig{
{
ParamTypes: []wasm.ValueType{},
ReturnTypes: []wasm.ValueType{},
},
{
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{},
},
{
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
},
}
var nativeFunction = []wasm.Function{
{
Sig: &nativeFunctionTypes.Entries[0],
Host: reflect.ValueOf(func(proc *exec.Process) { fmt.Printf("result = 42\n") }),
Body: &wasm.FunctionBody{},
},
{
Sig: &nativeFunctionTypes.Entries[1],
Host: reflect.ValueOf(func(cos *exec.Process, v int32) { fmt.Printf("result = %v \n", v) }),
Body: &wasm.FunctionBody{},
},
{
Sig: &nativeFunctionTypes.Entries[2],
Host: reflect.ValueOf(func(proc *exec.Process, a int32, b int32) int32 { return a + b }),
Body: &wasm.FunctionBody{},
},
}
Look good, right?
But in the runtime, compiler complained: invalid number of arguments to function. And I found the print function, which was passed 0 args.
I tried to replace from int a = iadd(4,10);
to int a = 4;
. It works well.
And I removed print(a);
. It works well too.
Just look as the
(call $iadd
(i32.const 4)
(i32.const 10)
)
doesn't be treated as the parameter to print
function even the return type is int32 ?