This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_test.go
357 lines (302 loc) · 5.3 KB
/
vm_test.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package goja
import (
"testing"
"github.com/cleancluster/goja/parser"
)
func TestVM1(t *testing.T) {
r := &Runtime{}
r.init()
vm := r.vm
vm.prg = &Program{
values: []Value{valueInt(2), valueInt(3), asciiString("test")},
code: []instruction{
bindName("v"),
newObject,
setGlobal("v"),
loadVal(2),
loadVal(1),
loadVal(0),
add,
setElem,
pop,
getVar1("v"),
halt,
},
}
vm.run()
rv := vm.pop()
if obj, ok := rv.(*Object); ok {
if v := obj.self.getStr("test", nil).ToInteger(); v != 5 {
t.Fatalf("Unexpected property value: %v", v)
}
} else {
t.Fatalf("Unexpected result: %v", rv)
}
}
func TestEvalVar(t *testing.T) {
const SCRIPT = `
function test() {
var a;
return eval("var a = 'yes'; var z = 'no'; a;") === "yes" && a === "yes";
}
test();
`
testScript1(SCRIPT, valueTrue, t)
}
var jumptable = []func(*vm, *instr){
f_jump,
f_halt,
}
func f_jump(vm *vm, i *instr) {
vm.pc += i.prim
}
func f_halt(vm *vm, i *instr) {
vm.halt = true
}
func f_loadVal(vm *vm, i *instr) {
vm.push(vm.prg.values[i.prim])
vm.pc++
}
type instr struct {
code int
prim int
arg interface{}
}
type jumparg struct {
offset int
other string
}
func BenchmarkVmNOP2(b *testing.B) {
prg := []func(*vm){
//loadVal(0).exec,
//loadVal(1).exec,
//add.exec,
jump(1).exec,
halt.exec,
}
r := &Runtime{}
r.init()
vm := r.vm
vm.prg = &Program{
values: []Value{intToValue(2), intToValue(3)},
}
for i := 0; i < b.N; i++ {
vm.halt = false
vm.pc = 0
for !vm.halt {
prg[vm.pc](vm)
}
//vm.sp--
/*r := vm.pop()
if r.ToInteger() != 5 {
b.Fatalf("Unexpected result: %+v", r)
}
if vm.sp != 0 {
b.Fatalf("Unexpected sp: %d", vm.sp)
}*/
}
}
func BenchmarkVmNOP1(b *testing.B) {
prg := []instr{
{code: 2, prim: 0},
{code: 2, prim: 1},
{code: 3},
{code: 1},
}
r := &Runtime{}
r.init()
vm := r.vm
vm.prg = &Program{
values: []Value{intToValue(2), intToValue(3)},
}
for i := 0; i < b.N; i++ {
vm.halt = false
vm.pc = 0
L:
for {
instr := &prg[vm.pc]
//jumptable[instr.code](vm, instr)
switch instr.code {
case 10:
vm.pc += 1
case 11:
vm.pc += 2
case 12:
vm.pc += 3
case 13:
vm.pc += 4
case 14:
vm.pc += 5
case 15:
vm.pc += 6
case 16:
vm.pc += 7
case 17:
vm.pc += 8
case 18:
vm.pc += 9
case 19:
vm.pc += 10
case 20:
vm.pc += 11
case 21:
vm.pc += 12
case 22:
vm.pc += 13
case 23:
vm.pc += 14
case 24:
vm.pc += 15
case 25:
vm.pc += 16
case 0:
//vm.pc += instr.prim
f_jump(vm, instr)
case 1:
break L
case 2:
f_loadVal(vm, instr)
default:
jumptable[instr.code](vm, instr)
}
}
r := vm.pop()
if r.ToInteger() != 5 {
b.Fatalf("Unexpected result: %+v", r)
}
if vm.sp != 0 {
b.Fatalf("Unexpected sp: %d", vm.sp)
}
//vm.sp -= 1
}
}
func BenchmarkVmNOP(b *testing.B) {
r := &Runtime{}
r.init()
vm := r.vm
vm.prg = &Program{
code: []instruction{
jump(1),
//jump(1),
halt,
},
}
for i := 0; i < b.N; i++ {
vm.pc = 0
vm.run()
}
}
func BenchmarkVm1(b *testing.B) {
r := &Runtime{}
r.init()
vm := r.vm
//ins1 := loadVal1(0)
//ins2 := loadVal1(1)
vm.prg = &Program{
values: []Value{valueInt(2), valueInt(3)},
code: []instruction{
loadVal(0),
loadVal(1),
add,
halt,
},
}
for i := 0; i < b.N; i++ {
vm.pc = 0
vm.run()
r := vm.pop()
if r.ToInteger() != 5 {
b.Fatalf("Unexpected result: %+v", r)
}
if vm.sp != 0 {
b.Fatalf("Unexpected sp: %d", vm.sp)
}
}
}
func BenchmarkFib(b *testing.B) {
const TEST_FIB = `
function fib(n) {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}
fib(35);
`
b.StopTimer()
prg, err := parser.ParseFile(nil, "test.js", TEST_FIB, 0)
if err != nil {
b.Fatal(err)
}
c := newCompiler()
c.compile(prg)
c.p.dumpCode(b.Logf)
r := &Runtime{}
r.init()
vm := r.vm
var expectedResult Value = valueInt(9227465)
b.StartTimer()
vm.prg = c.p
vm.run()
v := vm.pop()
b.Logf("stack size: %d", len(vm.stack))
b.Logf("stashAllocs: %d", vm.stashAllocs)
if !v.SameAs(expectedResult) {
b.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
}
}
func BenchmarkEmptyLoop(b *testing.B) {
const SCRIPT = `
function f() {
for (var i = 0; i < 100; i++) {
}
}
f()
`
b.StopTimer()
vm := New()
prg := MustCompile("test.js", SCRIPT, false)
// prg.dumpCode(log.Printf)
b.StartTimer()
for i := 0; i < b.N; i++ {
vm.RunProgram(prg)
}
}
func BenchmarkVMAdd(b *testing.B) {
vm := &vm{}
vm.stack = append(vm.stack, nil, nil)
vm.sp = len(vm.stack)
var v1 Value = valueInt(3)
var v2 Value = valueInt(5)
for i := 0; i < b.N; i++ {
vm.stack[0] = v1
vm.stack[1] = v2
add.exec(vm)
vm.sp++
}
}
func BenchmarkFuncCall(b *testing.B) {
const SCRIPT = `
function f(a, b, c, d) {
}
`
b.StopTimer()
vm := New()
prg := MustCompile("test.js", SCRIPT, false)
vm.RunProgram(prg)
if f, ok := AssertFunction(vm.Get("f")); ok {
b.StartTimer()
for i := 0; i < b.N; i++ {
f(nil, nil, intToValue(1), intToValue(2), intToValue(3), intToValue(4), intToValue(5), intToValue(6))
}
} else {
b.Fatal("f is not a function")
}
}
func BenchmarkAssertInt(b *testing.B) {
var v Value
v = intToValue(42)
for i := 0; i < b.N; i++ {
if i, ok := v.(valueInt); !ok || int64(i) != 42 {
b.Fatal()
}
}
}