Holds wasm.Code as values on wasm.Module (#1243)

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2023-03-14 22:45:54 -07:00
committed by GitHub
parent 12e80b4bc5
commit e17a85146a
36 changed files with 302 additions and 298 deletions

View File

@@ -126,7 +126,7 @@ func TestModule_Encode(t *testing.T) {
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}},
},
ExportSection: []wasm.Export{
@@ -215,7 +215,7 @@ func TestModule_Encode_HostFunctionSection_Unsupported(t *testing.T) {
captured := require.CapturePanic(func() {
EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{}},
CodeSection: []*wasm.Code{wasm.MustParseGoReflectFuncCode(fn)},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(fn)},
})
})
require.EqualError(t, captured, "BUG: GoFunction is not encodable")

View File

@@ -56,10 +56,11 @@ func EncodeFunctionSection(typeIndices []wasm.Index) []byte {
//
// See encodeCode
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#code-section%E2%91%A0
func encodeCodeSection(code []*wasm.Code) []byte {
func encodeCodeSection(code []wasm.Code) []byte {
contents := leb128.EncodeUint32(uint32(len(code)))
for _, i := range code {
contents = append(contents, encodeCode(i)...)
for i := range code {
c := &code[i]
contents = append(contents, encodeCode(c)...)
}
return encodeSection(wasm.SectionIDCode, contents)
}

View File

@@ -83,7 +83,7 @@ func RunTestEngine_MemoryGrowInRecursiveCall(t *testing.T, et EngineTester) {
m := &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{}, Results: []wasm.ValueType{}}},
FunctionSection: []wasm.Index{0, 0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{
Body: []byte{
// Calls the imported host function, which in turn calls the next in-Wasm function recursively.
@@ -153,7 +153,7 @@ func RunTestModuleEngine_Call(t *testing.T, et EngineTester) {
},
},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeEnd}},
},
}
@@ -205,7 +205,7 @@ func RunTestModuleEngine_LookupFunction(t *testing.T, et EngineTester) {
mod := &wasm.Module{
TypeSection: []wasm.FunctionType{{}, {Params: []wasm.ValueType{wasm.ValueTypeV128}}},
FunctionSection: []wasm.Index{0, 0, 0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{
Body: []byte{wasm.OpcodeEnd},
}, {Body: []byte{wasm.OpcodeEnd}}, {Body: []byte{wasm.OpcodeEnd}},
@@ -313,8 +313,8 @@ func RunTestModuleEngine_Call_HostFn(t *testing.T, et EngineTester) {
runTestModuleEngine_Call_HostFn(t, et, hostDivByWasm)
})
t.Run("go", func(t *testing.T) {
runTestModuleEngine_Call_HostFn(t, et, hostDivByGo)
runTestModuleEngine_Call_HostFn_Mem(t, et, hostReadMemGo)
runTestModuleEngine_Call_HostFn(t, et, &hostDivByGo)
runTestModuleEngine_Call_HostFn_Mem(t, et, &hostReadMemGo)
})
}
@@ -374,7 +374,7 @@ func runTestModuleEngine_Call_HostFn(t *testing.T, et EngineTester, hostDivBy *w
func RunTestModuleEngine_Call_Errors(t *testing.T, et EngineTester) {
e := et.NewEngine(api.CoreFeaturesV1)
_, imported, importing, done := setupCallTests(t, e, hostDivByGo, et.ListenerFactory())
_, imported, importing, done := setupCallTests(t, e, &hostDivByGo, et.ListenerFactory())
defer done()
tests := []struct {
@@ -497,7 +497,7 @@ func RunTestModuleEngine_Memory(t *testing.T, et EngineTester) {
},
},
DataCountSection: &one,
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: []byte{ // "grow"
wasm.OpcodeLocalGet, 0, // how many pages to grow (param)
wasm.OpcodeMemoryGrow, 0, // memory index zero
@@ -644,7 +644,7 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime
hostModule := &wasm.Module{
TypeSection: []wasm.FunctionType{ft},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{divBy},
CodeSection: []wasm.Code{*divBy},
ExportSection: []wasm.Export{{Name: divByGoName, Type: wasm.ExternTypeFunc, Index: 0}},
NameSection: &wasm.NameSection{
ModuleName: "host",
@@ -669,7 +669,7 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime
ImportSection: []wasm.Import{{}},
TypeSection: []wasm.FunctionType{ft},
FunctionSection: []wasm.Index{0, 0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: divByWasm},
{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeCall, byte(0), // Calling imported host function ^.
wasm.OpcodeEnd}},
@@ -708,7 +708,7 @@ func setupCallTests(t *testing.T, e wasm.Engine, divBy *wasm.Code, fnlf experime
TypeSection: []wasm.FunctionType{ft},
ImportSection: []wasm.Import{{}},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeCall, 0 /* only one imported function */, wasm.OpcodeEnd}},
},
ExportSection: []wasm.Export{
@@ -749,7 +749,7 @@ func setupCallMemTests(t *testing.T, e wasm.Engine, readMem *wasm.Code, fnlf exp
hostModule := &wasm.Module{
TypeSection: []wasm.FunctionType{ft},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{readMem},
CodeSection: []wasm.Code{*readMem},
ExportSection: []wasm.Export{
{Name: readMemName, Type: wasm.ExternTypeFunc, Index: 0},
},
@@ -781,7 +781,7 @@ func setupCallMemTests(t *testing.T, e wasm.Engine, readMem *wasm.Code, fnlf exp
ExportSection: []wasm.Export{
{Name: callImportReadMemName, Type: wasm.ExternTypeFunc, Index: 1},
},
CodeSection: []*wasm.Code{
CodeSection: []wasm.Code{
{Body: []byte{wasm.OpcodeCall, 0, wasm.OpcodeEnd}}, // Calling the index 1 = readMemFn.
},
NameSection: &wasm.NameSection{

View File

@@ -73,7 +73,7 @@ func NewModuleBinary(moduleName string, proxyTarget wazero.CompiledModule) []byt
body = append(body, wasm.OpcodeCall)
body = append(body, leb128.EncodeUint32(cnt)...)
body = append(body, wasm.OpcodeEnd)
proxyModule.CodeSection = append(proxyModule.CodeSection, &wasm.Code{Body: body})
proxyModule.CodeSection = append(proxyModule.CodeSection, wasm.Code{Body: body})
proxyFuncIndex := cnt + funcNum
// Assigns the same params name as the imported one.