forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc39_test.go
538 lines (471 loc) · 15 KB
/
tc39_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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package goja
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"sync"
"testing"
"time"
)
const (
tc39BASE = "testdata/test262"
)
var (
invalidFormatError = errors.New("Invalid file format")
ignorableTestError = newSymbol(stringEmpty)
sabStub = MustCompile("sabStub.js", `
Object.defineProperty(this, "SharedArrayBuffer", {
get: function() {
throw IgnorableTestError;
}
});`,
false)
)
var (
skipList = map[string]bool{
"test/built-ins/Date/prototype/toISOString/15.9.5.43-0-8.js": true, // timezone
"test/built-ins/Date/prototype/toISOString/15.9.5.43-0-9.js": true, // timezone
"test/built-ins/Date/prototype/toISOString/15.9.5.43-0-10.js": true, // timezone
"test/annexB/built-ins/escape/escape-above-astral.js": true, // \u{xxxxx}
// class
"test/language/statements/class/subclass/builtin-objects/Symbol/symbol-valid-as-extends-value.js": true,
"test/language/statements/class/subclass/builtin-objects/Symbol/new-symbol-with-super-throws.js": true,
"test/language/statements/class/subclass/builtin-objects/WeakSet/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/WeakSet/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/WeakMap/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/WeakMap/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/Map/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/Map/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/Set/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/Set/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/Object/replacing-prototype.js": true,
"test/language/statements/class/subclass/builtin-objects/Object/regular-subclassing.js": true,
"test/built-ins/Array/prototype/concat/Array.prototype.concat_non-array.js": true,
"test/language/statements/class/subclass/builtin-objects/Array/length.js": true,
"test/language/statements/class/subclass/builtin-objects/TypedArray/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/TypedArray/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/DataView/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/DataView/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/String/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/String/regular-subclassing.js": true,
"test/language/statements/class/subclass/builtin-objects/String/length.js": true,
"test/language/statements/class/subclass/builtin-objects/Date/super-must-be-called.js": true,
"test/language/statements/class/subclass/builtin-objects/Date/regular-subclassing.js": true,
// full unicode regexp flag
"test/built-ins/RegExp/prototype/Symbol.match/u-advance-after-empty.js": true,
"test/built-ins/RegExp/prototype/Symbol.match/get-unicode-error.js": true,
"test/built-ins/RegExp/prototype/Symbol.match/builtin-success-u-return-val-groups.js": true,
"test/built-ins/RegExp/prototype/Symbol.match/builtin-infer-unicode.js": true,
"test/built-ins/RegExp/unicode_identity_escape.js": true,
// object literals
"test/built-ins/Array/from/source-object-iterator-1.js": true,
"test/built-ins/Array/from/source-object-iterator-2.js": true,
"test/built-ins/TypedArray/prototype/fill/fill-values-conversion-once.js": true,
"test/built-ins/TypedArrays/of/this-is-not-constructor.js": true,
"test/built-ins/TypedArrays/of/argument-number-value-throws.js": true,
"test/built-ins/TypedArrays/from/this-is-not-constructor.js": true,
"test/built-ins/TypedArrays/from/set-value-abrupt-completion.js": true,
"test/built-ins/TypedArrays/from/property-abrupt-completion.js": true,
"test/built-ins/TypedArray/of/this-is-not-constructor.js": true,
"test/built-ins/TypedArray/from/this-is-not-constructor.js": true,
"test/built-ins/DataView/custom-proto-access-throws.js": true,
"test/built-ins/DataView/custom-proto-access-throws-sab.js": true,
// arrow-function
"test/built-ins/Object/prototype/toString/proxy-function.js": true,
// template strings
"test/built-ins/String/raw/zero-literal-segments.js": true,
"test/built-ins/String/raw/template-substitutions-are-appended-on-same-index.js": true,
"test/built-ins/String/raw/special-characters.js": true,
"test/built-ins/String/raw/return-the-string-value-from-template.js": true,
}
featuresBlackList = []string{
"arrow-function",
}
es6WhiteList = map[string]bool{}
es6IdWhiteList = []string{
"8.1.2.1",
"9.5",
"12.9.3",
"12.9.4",
"19.1",
"19.4",
"20.3",
"21.1",
"21.2.5.6",
"22.1.2.1",
"22.1.2.3",
"22.1.2.5",
"22.1.3",
"22.1.4",
"22.2",
"23.1",
"23.2",
"23.3",
"23.4",
"24.2",
"25.1.2",
"26.1",
"26.2",
"B.2.1",
"B.2.2",
}
esIdPrefixWhiteList = []string{
"sec-array.prototype.includes",
"sec-%typedarray%",
"sec-string.prototype",
"sec-date",
}
)
type tc39Test struct {
name string
f func(t *testing.T)
}
type tc39BenchmarkItem struct {
name string
duration time.Duration
}
type tc39BenchmarkData []tc39BenchmarkItem
type tc39TestCtx struct {
base string
t *testing.T
prgCache map[string]*Program
prgCacheLock sync.Mutex
enableBench bool
benchmark tc39BenchmarkData
benchLock sync.Mutex
testQueue []tc39Test
}
type TC39MetaNegative struct {
Phase, Type string
}
type tc39Meta struct {
Negative TC39MetaNegative
Includes []string
Flags []string
Features []string
Es5id string
Es6id string
Esid string
}
func (m *tc39Meta) hasFlag(flag string) bool {
for _, f := range m.Flags {
if f == flag {
return true
}
}
return false
}
func parseTC39File(name string) (*tc39Meta, string, error) {
f, err := os.Open(name)
if err != nil {
return nil, "", err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, "", err
}
str := string(b)
metaStart := strings.Index(str, "/*---")
if metaStart == -1 {
return nil, "", invalidFormatError
} else {
metaStart += 5
}
metaEnd := strings.Index(str, "---*/")
if metaEnd == -1 || metaEnd <= metaStart {
return nil, "", invalidFormatError
}
var meta tc39Meta
err = yaml.Unmarshal([]byte(str[metaStart:metaEnd]), &meta)
if err != nil {
return nil, "", err
}
if meta.Negative.Type != "" && meta.Negative.Phase == "" {
return nil, "", errors.New("negative type is set, but phase isn't")
}
return &meta, str, nil
}
func (*tc39TestCtx) detachArrayBuffer(call FunctionCall) Value {
if obj, ok := call.Argument(0).(*Object); ok {
if buf, ok := obj.self.(*arrayBufferObject); ok {
buf.detach()
return _undefined
}
}
panic(typeError("detachArrayBuffer() is called with incompatible argument"))
}
func (*tc39TestCtx) throwIgnorableTestError(FunctionCall) Value {
panic(ignorableTestError)
}
func (ctx *tc39TestCtx) runTC39Test(name, src string, meta *tc39Meta, t testing.TB) {
defer func() {
if x := recover(); x != nil {
panic(fmt.Sprintf("panic while running %s: %v", name, x))
}
}()
vm := New()
_262 := vm.NewObject()
_262.Set("detachArrayBuffer", ctx.detachArrayBuffer)
_262.Set("createRealm", ctx.throwIgnorableTestError)
vm.Set("$262", _262)
vm.Set("IgnorableTestError", ignorableTestError)
vm.RunProgram(sabStub)
err, early := ctx.runTC39Script(name, src, meta.Includes, vm)
if err != nil {
if meta.Negative.Type == "" {
if err, ok := err.(*Exception); ok {
if err.Value() == ignorableTestError {
t.Skip("Test threw IgnorableTestError")
}
}
t.Fatalf("%s: %v", name, err)
} else {
if meta.Negative.Phase == "early" && !early || meta.Negative.Phase == "runtime" && early {
t.Fatalf("%s: error %v happened at the wrong phase (expected %s)", name, err, meta.Negative.Phase)
}
var errType string
switch err := err.(type) {
case *Exception:
if o, ok := err.Value().(*Object); ok {
if c := o.Get("constructor"); c != nil {
if c, ok := c.(*Object); ok {
errType = c.Get("name").String()
} else {
t.Fatalf("%s: error constructor is not an object (%v)", name, o)
}
} else {
t.Fatalf("%s: error does not have a constructor (%v)", name, o)
}
} else {
t.Fatalf("%s: error is not an object (%v)", name, err.Value())
}
case *CompilerSyntaxError:
errType = "SyntaxError"
case *CompilerReferenceError:
errType = "ReferenceError"
default:
t.Fatalf("%s: error is not a JS error: %v", name, err)
}
if errType != meta.Negative.Type {
vm.vm.prg.dumpCode(t.Logf)
t.Fatalf("%s: unexpected error type (%s), expected (%s)", name, errType, meta.Negative.Type)
}
}
} else {
if meta.Negative.Type != "" {
vm.vm.prg.dumpCode(t.Logf)
t.Fatalf("%s: Expected error: %v", name, err)
}
}
}
func (ctx *tc39TestCtx) runTC39File(name string, t testing.TB) {
if skipList[name] {
t.Skip("Excluded")
}
p := path.Join(ctx.base, name)
meta, src, err := parseTC39File(p)
if err != nil {
//t.Fatalf("Could not parse %s: %v", name, err)
t.Errorf("Could not parse %s: %v", name, err)
return
}
if meta.Es5id == "" {
skip := true
//t.Logf("%s: Not ES5, skipped", name)
if es6WhiteList[name] {
skip = false
} else {
if meta.Es6id != "" {
for _, prefix := range es6IdWhiteList {
if strings.HasPrefix(meta.Es6id, prefix) &&
(len(meta.Es6id) == len(prefix) || meta.Es6id[len(prefix)] == '.') {
skip = false
break
}
}
}
}
if skip {
if meta.Esid != "" {
for _, prefix := range esIdPrefixWhiteList {
if strings.HasPrefix(meta.Esid, prefix) &&
(len(meta.Esid) == len(prefix) || meta.Esid[len(prefix)] == '.') {
skip = false
break
}
}
}
}
if skip {
t.Skip("Not ES5")
}
for _, feature := range meta.Features {
for _, bl := range featuresBlackList {
if feature == bl {
t.Skip("Blacklisted feature")
}
}
}
}
var startTime time.Time
if ctx.enableBench {
startTime = time.Now()
}
hasRaw := meta.hasFlag("raw")
if hasRaw || !meta.hasFlag("onlyStrict") {
//log.Printf("Running normal test: %s", name)
//t.Logf("Running normal test: %s", name)
ctx.runTC39Test(name, src, meta, t)
}
if !hasRaw && !meta.hasFlag("noStrict") {
//log.Printf("Running strict test: %s", name)
//t.Logf("Running strict test: %s", name)
ctx.runTC39Test(name, "'use strict';\n"+src, meta, t)
}
if ctx.enableBench {
ctx.benchLock.Lock()
ctx.benchmark = append(ctx.benchmark, tc39BenchmarkItem{
name: name,
duration: time.Since(startTime),
})
ctx.benchLock.Unlock()
}
}
func (ctx *tc39TestCtx) init() {
ctx.prgCache = make(map[string]*Program)
}
func (ctx *tc39TestCtx) compile(base, name string) (*Program, error) {
ctx.prgCacheLock.Lock()
defer ctx.prgCacheLock.Unlock()
prg := ctx.prgCache[name]
if prg == nil {
fname := path.Join(base, name)
f, err := os.Open(fname)
if err != nil {
return nil, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
str := string(b)
prg, err = Compile(name, str, false)
if err != nil {
return nil, err
}
ctx.prgCache[name] = prg
}
return prg, nil
}
func (ctx *tc39TestCtx) runFile(base, name string, vm *Runtime) error {
prg, err := ctx.compile(base, name)
if err != nil {
return err
}
_, err = vm.RunProgram(prg)
return err
}
func (ctx *tc39TestCtx) runTC39Script(name, src string, includes []string, vm *Runtime) (err error, early bool) {
early = true
err = ctx.runFile(ctx.base, path.Join("harness", "assert.js"), vm)
if err != nil {
return
}
err = ctx.runFile(ctx.base, path.Join("harness", "sta.js"), vm)
if err != nil {
return
}
for _, include := range includes {
err = ctx.runFile(ctx.base, path.Join("harness", include), vm)
if err != nil {
return
}
}
var p *Program
p, err = Compile(name, src, false)
if err != nil {
return
}
early = false
_, err = vm.RunProgram(p)
return
}
func (ctx *tc39TestCtx) runTC39Tests(name string) {
files, err := ioutil.ReadDir(path.Join(ctx.base, name))
if err != nil {
ctx.t.Fatal(err)
}
for _, file := range files {
if file.Name()[0] == '.' {
continue
}
if file.IsDir() {
ctx.runTC39Tests(path.Join(name, file.Name()))
} else {
if strings.HasSuffix(file.Name(), ".js") {
name := path.Join(name, file.Name())
ctx.runTest(name, func(t *testing.T) {
ctx.runTC39File(name, t)
})
}
}
}
}
func TestTC39(t *testing.T) {
if testing.Short() {
t.Skip()
}
if _, err := os.Stat(tc39BASE); err != nil {
t.Skipf("If you want to run tc39 tests, download them from https://github.com/tc39/test262 and put into %s. The last working commit is 1ba3a7c4a93fc93b3d0d7e4146f59934a896837d. (%v)", tc39BASE, err)
}
ctx := &tc39TestCtx{
base: tc39BASE,
}
ctx.init()
//ctx.enableBench = true
t.Run("tc39", func(t *testing.T) {
ctx.t = t
//ctx.runTC39File("test/language/types/number/8.5.1.js", t)
//ctx.runTC39Tests("test/language")
ctx.runTC39Tests("test/language/expressions")
ctx.runTC39Tests("test/language/arguments-object")
ctx.runTC39Tests("test/language/asi")
ctx.runTC39Tests("test/language/directive-prologue")
ctx.runTC39Tests("test/language/function-code")
ctx.runTC39Tests("test/language/eval-code")
ctx.runTC39Tests("test/language/global-code")
ctx.runTC39Tests("test/language/identifier-resolution")
ctx.runTC39Tests("test/language/identifiers")
//ctx.runTC39Tests("test/language/literals") // octal sequences in strict mode
ctx.runTC39Tests("test/language/punctuators")
ctx.runTC39Tests("test/language/reserved-words")
ctx.runTC39Tests("test/language/source-text")
ctx.runTC39Tests("test/language/statements")
ctx.runTC39Tests("test/language/types")
ctx.runTC39Tests("test/language/white-space")
ctx.runTC39Tests("test/built-ins")
ctx.runTC39Tests("test/annexB/built-ins/String/prototype/substr")
ctx.runTC39Tests("test/annexB/built-ins/escape")
ctx.runTC39Tests("test/annexB/built-ins/unescape")
ctx.flush()
})
if ctx.enableBench {
sort.Slice(ctx.benchmark, func(i, j int) bool {
return ctx.benchmark[i].duration > ctx.benchmark[j].duration
})
bench := ctx.benchmark
if len(bench) > 50 {
bench = bench[:50]
}
for _, item := range bench {
fmt.Printf("%s\t%d\n", item.name, item.duration/time.Millisecond)
}
}
}