Repository navigation
Expand file tree
/
Copy pathsharedbytecode_test.go
More file actions
190 lines (176 loc) · 5.06 KB
/
Copy pathsharedbytecode_test.go
File metadata and controls
190 lines (176 loc) · 5.06 KB
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
// Copyright (c) 2026 The slowjs Authors.
// SPDX-License-Identifier: MIT
// Use of this source code is governed by the MIT license in LICENSE-MIT at the
// root of this repository.
package slowjs // import "github.com/liustack/slowjs"
import (
"bytes"
"strings"
"sync"
"testing"
)
// sharedPad is the size of the comment in big, so that its source is far
// larger than anything else a VM of sharedScript holds.
const sharedPad = 256 << 10
// sharedScript has functions of every kind, nested ones among them, one that
// throws two calls down and one that makes a stack, and big, whose source is
// sharedPad bytes of comment.
var sharedScript = `function outer(a, b) {
// a comment the source keeps
const inner = (x) => x + a;
class Point {
constructor(x) { this.x = x }
get double() { return this.x * 2 }
static make() { return new Point(1) }
}
return { inner, Point, method() { return b }, async run() { await 0; return a } };
}
function thrower() {
const f = () => { throw new Error('deep') };
return f();
}
function stackHere() { return new Error('here').stack }
function big() { /*` + strings.Repeat("x", sharedPad) + `*/ return 1 }
globalThis.shared = { outer, thrower, stackHere, big, o: outer(1, 2) };
`
// sharedProbe is what a VM of sharedScript shows of its functions: their
// text, a stack thrown two calls down and a stack made in a function.
const sharedProbe = `JSON.stringify([
String(shared.outer), String(shared.o.inner), String(shared.o.Point),
String(shared.o.method), String(shared.o.run), String(shared.o.Point.make),
String(Object.getOwnPropertyDescriptor(shared.o.Point.prototype, 'double').get),
String(shared.thrower), String(shared.big),
(() => { try { shared.thrower() } catch (e) { return e.stack } })(),
shared.stackHere(),
new Function('return 1').toString(),
])`
func compileShared(t testing.TB) []byte {
t.Helper()
m, err := NewVM()
if err != nil {
t.Fatal(err)
}
defer m.Close()
code, err := m.Compile(sharedScript, EvalGlobal)
if err != nil {
t.Fatal(err)
}
return code
}
func newSharedBytecode(t testing.TB, code []byte) *SharedBytecode {
t.Helper()
b, err := NewSharedBytecode(code)
if err != nil {
t.Fatal(err)
}
return b
}
// The bytes stay while a VM that loaded them is open, after Close of the
// SharedBytecode, and go with the last of them. Loading after Close fails.
func TestSharedBytecodeLifetime(t *testing.T) {
code := compileShared(t)
shared := newSharedBytecode(t, code)
if got := shared.Len(); got != len(code) {
t.Fatalf("Len %d, want %d", got, len(code))
}
want := evalString(t, newTestVMWith(t, code), sharedProbe)
var vms []*VM
for range 2 {
m, err := NewVM()
if err != nil {
t.Fatal(err)
}
if _, err := m.EvalSharedBytecode(shared); err != nil {
t.Fatal(err)
}
vms = append(vms, m)
}
if err := shared.Close(); err != nil {
t.Fatal(err)
}
if err := shared.Close(); err != nil {
t.Fatalf("a second Close: %v", err)
}
if shared.p == 0 {
t.Fatal("Close freed the bytes while VMs that loaded them are open")
}
m, err := NewVM()
if err != nil {
t.Fatal(err)
}
if _, err := m.EvalSharedBytecode(shared); err == nil {
t.Error("EvalSharedBytecode after Close did not fail")
}
m.Close()
for i, m := range vms {
if got := evalString(t, m, sharedProbe); got != want {
t.Errorf("VM %d after Close of the bytecode shows\n%s\nwant\n%s", i, got, want)
}
if err := m.Close(); err != nil {
t.Fatal(err)
}
}
if shared.p != 0 || shared.refs != 0 {
t.Fatalf("after the last VM closed: p %#x, refs %d", shared.p, shared.refs)
}
}
func newTestVMWith(t *testing.T, code []byte) *VM {
t.Helper()
m := newTestVM(t)
if _, err := m.EvalBytecode(code); err != nil {
t.Fatal(err)
}
return m
}
// Bytecode that ends inside a source text fails as a copy of it fails.
func TestSharedBytecodeTruncated(t *testing.T) {
code := compileShared(t)
at := bytes.Index(code, []byte(strings.Repeat("x", 64)))
if at < 0 {
t.Fatal("no source of big in the bytecode")
}
short := code[:at+sharedPad/2]
copied := newTestVM(t)
_, want := copied.EvalBytecode(short)
if want == nil {
t.Fatal("a copy of truncated bytecode loaded")
}
shared := newSharedBytecode(t, short)
defer shared.Close()
m := newTestVM(t)
_, err := m.EvalSharedBytecode(shared)
if err == nil || err.Error() != want.Error() {
t.Fatalf("got %v, want %v", err, want)
}
}
// VMs on several goroutines load and read the same bytes at once.
func TestSharedBytecodeConcurrent(t *testing.T) {
code := compileShared(t)
shared := newSharedBytecode(t, code)
want := evalString(t, newTestVMWith(t, code), sharedProbe)
var wg sync.WaitGroup
for range 4 {
wg.Go(func() {
for range 4 {
m, err := NewVM()
if err != nil {
t.Error(err)
return
}
if _, err := m.EvalSharedBytecode(shared); err != nil {
t.Error(err)
} else if v, err := m.Eval(sharedProbe, EvalGlobal); err != nil || v != want {
t.Errorf("got %v, %v", v, err)
}
m.Close()
}
})
}
wg.Wait()
if err := shared.Close(); err != nil {
t.Fatal(err)
}
if shared.p != 0 {
t.Fatal("the bytes stayed after the last VM and Close")
}
}