/
struct.go
74 lines (64 loc) · 1.7 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package vm
import (
"fmt"
"strings"
"github.com/gentee/gentee/core"
)
const (
TRACESTRUCT = iota
TIMESTRUCT
FINFOSTRUCT
HINFOSTRUCT
)
// Struct is used for custom struct types
type Struct struct {
Type *core.StructInfo
Values []interface{} // Values of fields
}
// NewStruct creates a new struct object
func NewStruct(rt *Runtime, sInfo *core.StructInfo) *Struct {
values := make([]interface{}, len(sInfo.Fields))
for i, v := range sInfo.Fields {
if v < core.TYPESTRUCT ||
&rt.Owner.Exec.Structs[(v-core.TYPESTRUCT)>>8] != sInfo {
values[i] = newValue(rt, int(v))
}
}
return &Struct{
Type: sInfo,
Values: values,
}
}
// String interface for Struct
func (pstruct Struct) String() string {
name := pstruct.Type.Name
list := make([]string, len(pstruct.Values))
for i, v := range pstruct.Values {
list[i] = fmt.Sprintf(`%s:%v`, pstruct.Type.Keys[i], fmt.Sprint(v))
}
return name + `[` + strings.Join(list, ` `) + `]`
}
// Len is part of Indexer interface.
func (pstruct *Struct) Len() int {
return len(pstruct.Values)
}
// GetIndex is part of Indexer interface.
func (pstruct *Struct) GetIndex(index interface{}) (interface{}, bool) {
sindex := int(index.(int64))
if sindex < 0 || sindex >= len(pstruct.Values) {
return nil, false
}
return pstruct.Values[sindex], true
}
// SetIndex is part of Indexer interface.
func (pstruct *Struct) SetIndex(index, value interface{}) int {
sindex := int(index.(int64))
if sindex < 0 || sindex >= len(pstruct.Values) {
return core.ErrIndexOut
}
pstruct.Values[sindex] = value
return 0
}