Skip to content

Commit 5aca85c

Browse files
authored
Merge pull request #7 from Mooean/master
add encode test,add validate , add stack.
2 parents 6604ac7 + 8079b2d commit 5aca85c

File tree

7 files changed

+798
-0
lines changed

7 files changed

+798
-0
lines changed

pvm/engine/internal/stack/stack.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2017 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 stack implements a growable uint64 stack
6+
package stack
7+
8+
type Stack struct {
9+
slice []uint64
10+
}
11+
12+
func (s *Stack) Push(b uint64) {
13+
s.slice = append(s.slice, b)
14+
}
15+
16+
func (s *Stack) Pop() uint64 {
17+
v := s.Top()
18+
s.slice = s.slice[:len(s.slice)-1]
19+
return v
20+
}
21+
22+
func (s *Stack) SetTop(v uint64) {
23+
s.slice[len(s.slice)-1] = v
24+
}
25+
26+
func (s *Stack) Top() uint64 {
27+
return s.slice[len(s.slice)-1]
28+
}
29+
30+
func (s *Stack) Get(i int) uint64 {
31+
return s.slice[i]
32+
}
33+
34+
func (s *Stack) Set(i int, v uint64) {
35+
s.slice[i] = v
36+
}
37+
38+
func (s *Stack) Len() int {
39+
return len(s.slice)
40+
}

pvm/engine/validate/error.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2017 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 validate
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
11+
"paradigm-vm/pvm/engine/wasm"
12+
ops "paradigm-vm/pvm/engine/wasm/operators"
13+
)
14+
15+
type Error struct {
16+
Offset int // Byte offset in the bytecode vector where the error occurs.
17+
Function int // Index into the function index space for the offending function.
18+
Err error
19+
}
20+
21+
func (e Error) Error() string {
22+
return fmt.Sprintf("error while validating function %d at offset %d: %v", e.Function, e.Offset, e.Err)
23+
}
24+
25+
var ErrStackUnderflow = errors.New("validate: stack underflow")
26+
27+
type InvalidImmediateError struct {
28+
ImmType string
29+
OpName string
30+
}
31+
32+
func (e InvalidImmediateError) Error() string {
33+
return fmt.Sprintf("invalid immediate for op %s at (should be %s)", e.OpName, e.ImmType)
34+
}
35+
36+
type UnmatchedOpError byte
37+
38+
func (e UnmatchedOpError) Error() string {
39+
n1, _ := ops.New(byte(e))
40+
return fmt.Sprintf("encountered unmatched %s", n1.Name)
41+
}
42+
43+
type InvalidLabelError uint32
44+
45+
func (e InvalidLabelError) Error() string {
46+
return fmt.Sprintf("invalid nesting depth %d", uint32(e))
47+
}
48+
49+
type InvalidLocalIndexError uint32
50+
51+
func (e InvalidLocalIndexError) Error() string {
52+
return fmt.Sprintf("invalid index for local variable %d", uint32(e))
53+
}
54+
55+
type InvalidTypeError struct {
56+
Wanted wasm.ValueType
57+
Got wasm.ValueType
58+
}
59+
60+
func (e InvalidTypeError) Error() string {
61+
return fmt.Sprintf("invalid type, got: %v, wanted: %v", e.Got, e.Wanted)
62+
}
63+
64+
type InvalidElementIndexError uint32
65+
66+
func (e InvalidElementIndexError) Error() string {
67+
return fmt.Sprintf("invalid element index %d", uint32(e))
68+
}
69+
70+
type NoSectionError wasm.SectionID
71+
72+
func (e NoSectionError) Error() string {
73+
return fmt.Sprintf("reference to non existant section (id %d) in module", wasm.SectionID(e))
74+
}

pvm/engine/validate/log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 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 validate
6+
7+
import (
8+
"io/ioutil"
9+
"log"
10+
"os"
11+
)
12+
13+
var PrintDebugInfo = false
14+
15+
var logger *log.Logger
16+
17+
func init() {
18+
w := ioutil.Discard
19+
20+
if PrintDebugInfo {
21+
w = os.Stderr
22+
}
23+
24+
logger = log.New(w, "", log.Lshortfile)
25+
log.SetFlags(log.Lshortfile)
26+
}

pvm/engine/validate/operand.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 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 validate
6+
7+
import (
8+
"paradigm-vm/pvm/engine/wasm"
9+
)
10+
11+
type operand struct {
12+
Type wasm.ValueType
13+
}

0 commit comments

Comments
 (0)