Skip to content
This repository was archived by the owner on May 11, 2020. It is now read-only.

Commit a6163ff

Browse files
author
张志强
authored
wasm: fix marshaling of LocalNames and NameMap
1 parent 8dd99d5 commit a6163ff

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

wasm/section.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,9 @@ func (s *LocalNames) UnmarshalWASM(r io.Reader) error {
11891189
}
11901190

11911191
func (s *LocalNames) MarshalWASM(w io.Writer) error {
1192+
if _, err := leb128.WriteVarUint32(w, uint32(len(s.Funcs))); err != nil {
1193+
return err
1194+
}
11921195
keys := make([]uint32, 0, len(s.Funcs))
11931196
for k := range s.Funcs {
11941197
keys = append(keys, k)
@@ -1235,6 +1238,9 @@ func (m NameMap) UnmarshalWASM(r io.Reader) error {
12351238
return nil
12361239
}
12371240
func (m NameMap) MarshalWASM(w io.Writer) error {
1241+
if _, err := leb128.WriteVarUint32(w, uint32(len(m))); err != nil {
1242+
return err
1243+
}
12381244
keys := make([]uint32, 0, len(m))
12391245
for k := range m {
12401246
keys = append(keys, k)

wasm/section_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2020 The go-interpreter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package wasm_test
6+
7+
import (
8+
"bytes"
9+
"io/ioutil"
10+
"path/filepath"
11+
"testing"
12+
13+
"github.com/go-interpreter/wagon/wasm"
14+
)
15+
16+
func TestSectionCustom(t *testing.T) {
17+
fname := "testdata/custom_funcs_locals.wasm"
18+
19+
t.Run(filepath.Base(fname), func(t *testing.T) {
20+
raw, err := ioutil.ReadFile(fname)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
25+
r := bytes.NewReader(raw)
26+
m, err := wasm.DecodeModule(r)
27+
if err != nil {
28+
t.Fatalf("error reading module %v", err)
29+
}
30+
31+
nameCustom := m.Custom("name")
32+
if nameCustom == nil {
33+
t.Fatal("can not find name custom section")
34+
}
35+
36+
var nSec wasm.NameSection
37+
err = nSec.UnmarshalWASM(bytes.NewReader(nameCustom.Data))
38+
if err != nil {
39+
t.Fatalf("error name Section Unmarshal %v", err)
40+
}
41+
42+
// check FunctionNames MarshalWASM func
43+
if len(nSec.Types[wasm.NameFunction]) == 0 {
44+
t.Fatalf("%s doesn't have custom FunctionNames section", fname)
45+
}
46+
47+
sub, err := nSec.Decode(wasm.NameFunction)
48+
if err != nil {
49+
t.Fatalf("error NameSection Decode NameFunction %v", err)
50+
}
51+
52+
funcNames, ok := sub.(*wasm.FunctionNames)
53+
if !ok {
54+
t.Fatal("error NameSubsection type")
55+
}
56+
57+
buf := new(bytes.Buffer)
58+
err = funcNames.MarshalWASM(buf)
59+
if err != nil {
60+
t.Fatalf("error FunctionNames Marshal %v", err)
61+
}
62+
if !bytes.Equal(buf.Bytes(), nSec.Types[wasm.NameFunction]) {
63+
t.Fatal("error Marshal and Unmarshal FunctionNames")
64+
}
65+
66+
// check LocalNames MarshalWASM func
67+
if len(nSec.Types[wasm.NameLocal]) == 0 {
68+
t.Fatalf("%s doesn't have custom LocalNames section", fname)
69+
}
70+
71+
sub, err = nSec.Decode(wasm.NameLocal)
72+
if err != nil {
73+
t.Fatalf("error NameSection Decode NameFunction %v", err)
74+
}
75+
76+
localNames, ok := sub.(*wasm.LocalNames)
77+
if !ok {
78+
t.Fatal("error NameSubsection type")
79+
}
80+
81+
buf = new(bytes.Buffer)
82+
err = localNames.MarshalWASM(buf)
83+
if err != nil {
84+
t.Fatalf("error LocalNames Marshal %v", err)
85+
}
86+
if !bytes.Equal(buf.Bytes(), nSec.Types[wasm.NameLocal]) {
87+
t.Fatal("error Marshal and Unmarshal LocalNames")
88+
}
89+
90+
})
91+
}
134 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)