This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from mtabini/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
327 lines (302 loc) · 7.07 KB
/
types.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package lua
import (
"fmt"
"math"
"reflect"
"runtime"
"strings"
)
type value interface{}
type float8 int
func debugValue(v value) string {
switch v := v.(type) {
case *table:
entry := func(x value) string {
if t, ok := x.(*table); ok {
return fmt.Sprintf("table %#v", t)
} else {
return debugValue(x)
}
}
s := fmt.Sprintf("table %#v {[", v)
for _, x := range v.array {
s += entry(x) + ", "
}
s += "], {"
for k, x := range v.hash {
s += entry(k) + ": " + entry(x) + ", "
}
return s + "}}"
case string:
return "'" + v + "'"
case float64:
return fmt.Sprintf("%f", v)
case *luaClosure:
return fmt.Sprintf("closure %s:%d %v", v.prototype.source, v.prototype.lineDefined, v)
case *goClosure:
return fmt.Sprintf("go closure %#v", v)
case *goFunction:
pc := reflect.ValueOf(v.Function).Pointer()
f := runtime.FuncForPC(pc)
file, line := f.FileLine(pc)
return fmt.Sprintf("go function %s %s:%d", f.Name(), file, line)
case *userData:
return fmt.Sprintf("userdata %#v", v)
case nil:
return "nil"
case bool:
return fmt.Sprintf("%#v", v)
}
return fmt.Sprintf("unknown %#v %s", v, reflect.TypeOf(v).Name())
}
func stack(s []value) string {
r := fmt.Sprintf("stack (len: %d, cap: %d):\n", len(s), cap(s))
for i, v := range s {
r = fmt.Sprintf("%s %d: %s\n", r, i, debugValue(v))
}
return r
}
func isFalse(s value) bool {
if s == nil {
return true
}
b, isBool := s.(bool)
return isBool && !b
}
type localVariable struct {
name string
startPC, endPC pc
}
type userData struct {
metaTable, env *table
data interface{}
}
type upValueDesc struct {
name string
isLocal bool
index int
}
type stackLocation struct {
state *State
index int
}
type prototype struct {
constants []value
code []instruction
prototypes []prototype
lineInfo []int32
localVariables []localVariable
upValues []upValueDesc
cache *luaClosure
source string
lineDefined, lastLineDefined int
parameterCount, maxStackSize int
isVarArg bool
}
func (p *prototype) upValueName(index int) string {
if s := p.upValues[index].name; s != "" {
return s
}
return "?"
}
func (p *prototype) lastLoad(reg int, lastPC pc) (loadPC pc, found bool) {
var ip, jumpTarget pc
for ; ip < lastPC; ip++ {
i, maybe := p.code[ip], false
switch i.opCode() {
case opLoadNil:
maybe = i.a() <= reg && reg <= i.a()+i.b()
case opTForCall:
maybe = reg >= i.a()+2
case opCall, opTailCall:
maybe = reg >= i.a()
case opJump:
if dest := ip + 1 + pc(i.sbx()); ip < dest && dest <= lastPC && dest > jumpTarget {
jumpTarget = dest
}
case opTest:
maybe = reg == i.a()
default:
maybe = testAMode(i.opCode()) && reg == i.a()
}
if maybe {
if ip < jumpTarget { // Can't know loading instruction because code is conditional.
found = false
} else {
loadPC, found = ip, true
}
}
}
return
}
func (p *prototype) objectName(reg int, lastPC pc) (name, kind string) {
if name, isLocal := p.localName(reg+1, lastPC); isLocal {
return name, "local"
}
if pc, found := p.lastLoad(reg, lastPC); found {
i := p.code[pc]
switch op := i.opCode(); op {
case opMove:
if b := i.b(); b < i.a() {
return p.objectName(b, pc)
}
case opGetTableUp:
name, kind = p.constantName(i.c(), pc), "local"
if p.upValueName(i.b()) == "_ENV" {
kind = "global"
}
return
case opGetTable:
name, kind = p.constantName(i.c(), pc), "local"
if v, ok := p.localName(i.b()+1, pc); ok && v == "_ENV" {
kind = "global"
}
return
case opGetUpValue:
return p.upValueName(i.b()), "upvalue"
case opLoadConstant:
if s, ok := p.constants[i.bx()].(string); ok {
return s, "constant"
}
case opLoadConstantEx:
if s, ok := p.constants[p.code[pc+1].ax()].(string); ok {
return s, "constant"
}
case opSelf:
return p.constantName(i.c(), pc), "method"
}
}
return
}
func (p *prototype) constantName(k int, pc pc) string {
if isConstant(k) {
if s, ok := p.constants[constantIndex(k)].(string); ok {
return s
}
} else if name, kind := p.objectName(k, pc); kind == "c" {
return name
}
return "?"
}
func (p *prototype) localName(index int, pc pc) (string, bool) {
for i := 0; i < len(p.localVariables) && p.localVariables[i].startPC <= pc; i++ {
if pc < p.localVariables[i].endPC {
if index--; index == 0 {
return p.localVariables[i].name, true
}
}
}
return "", false
}
// Converts an integer to a "floating point byte", represented as
// (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
// eeeee != 0 and (xxx) otherwise.
func float8FromInt(x int) float8 {
if x < 8 {
return float8(x)
}
e := 0
for ; x >= 0x10; e++ {
x = (x + 1) >> 1
}
return float8(((e + 1) << 3) | (x - 8))
}
func intFromFloat8(x float8) int {
e := x >> 3 & 0x1f
if e == 0 {
return int(x)
}
return int(x&7+8) << uint(e-1)
}
func arith(op Operator, v1, v2 float64) float64 {
switch op {
case OpAdd:
return v1 + v2
case OpSub:
return v1 - v2
case OpMul:
return v1 * v2
case OpDiv:
return v1 / v2
case OpMod:
return v1 - math.Floor(v1/v2)*v2
case OpPow:
// Golang bug: math.Pow(10.0, 33.0) is incorrect by 1 bit.
if v1 == 10.0 && float64(int(v2)) == v2 {
return math.Pow10(int(v2))
}
return math.Pow(v1, v2)
case OpUnaryMinus:
return -v1
}
panic(fmt.Sprintf("not an arithmetic op code (%d)", op))
}
func (l *State) parseNumber(s string) (v float64, ok bool) { // TODO this is f*cking ugly - scanner.readNumber should be refactored.
if len(strings.Fields(s)) != 1 || strings.ContainsRune(s, 0) {
return
}
scanner := scanner{l: l, r: strings.NewReader(s)}
t := scanner.scan()
if t.t == '-' {
if t := scanner.scan(); t.t == tkNumber {
v, ok = -t.n, true
}
} else if t.t == tkNumber {
v, ok = t.n, true
} else if t.t == '+' {
if t := scanner.scan(); t.t == tkNumber {
v, ok = t.n, true
}
}
if ok && scanner.scan().t != tkEOS {
ok = false
} else if math.IsInf(v, 0) || math.IsNaN(v) {
ok = false
}
return
}
func (l *State) toNumber(r value) (v float64, ok bool) {
if v, ok = r.(float64); ok {
return
}
var s string
if s, ok = r.(string); ok {
if err := l.protectedCall(func() { v, ok = l.parseNumber(strings.TrimSpace(s)) }, l.top, l.errorFunction); err != nil {
l.pop() // Remove error message from the stack.
ok = false
}
}
return
}
func (l *State) toString(index int) (s string, ok bool) {
if s, ok = toString(l.stack[index]); ok {
l.stack[index] = s
}
return
}
func numberToString(f float64) string {
return fmt.Sprintf("%.14g", f)
}
func toString(r value) (string, bool) {
switch r := r.(type) {
case string:
return r, true
case float64:
return numberToString(r), true
}
return "", false
}
func pairAsNumbers(p1, p2 value) (f1, f2 float64, ok bool) {
if f1, ok = p1.(float64); !ok {
return
}
f2, ok = p2.(float64)
return
}
func pairAsStrings(p1, p2 value) (s1, s2 string, ok bool) {
if s1, ok = p1.(string); !ok {
return
}
s2, ok = p2.(string)
return
}