Skip to content

Commit

Permalink
Simplify tc39 - extracted from ESM branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jul 4, 2024
1 parent e65d850 commit 28ad10a
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions js/tc39/tc39_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,10 @@ func (ctx *tc39TestCtx) runTC39Test(t testing.TB, name, src string, meta *tc39Me
_ = vm.Set("print", t.Log)
}
var early bool
var origErr error
if meta.hasFlag("module") {
early, origErr, err = ctx.runTC39Module(name, src, meta.Includes, vm)
early, err = ctx.runTC39Module(name, src, meta.Includes, vm)
} else {
early, origErr, err = ctx.runTC39Script(name, src, meta.Includes, vm, meta.Negative.Type != "")
early, err = ctx.runTC39Script(name, src, meta.Includes, vm, meta.Negative.Type != "")
}

if err == nil {
Expand Down Expand Up @@ -447,9 +446,6 @@ func (ctx *tc39TestCtx) runTC39Test(t testing.TB, name, src string, meta *tc39Me
errType := getErrType(name, err, failf)

if errType != "" && errType != meta.Negative.Type {
if meta.Negative.Type == "SyntaxError" && origErr != nil && getErrType(name, origErr, failf) == meta.Negative.Type {
return
}
// vm.vm.prg.dumpCode(t.Logf)
failf("%s: unexpected error type (%s), expected (%s)", name, errType, meta.Negative.Type)
return
Expand Down Expand Up @@ -610,7 +606,7 @@ func (ctx *tc39TestCtx) compile(base, name string) (*sobek.Program, error) {
str := string(b)
comp := ctx.compilerPool.Get()
defer ctx.compilerPool.Put(comp)
comp.Options = compiler.Options{Strict: false, CompatibilityMode: ctx.compatibilityMode}
comp.Options = compiler.Options{CompatibilityMode: ctx.compatibilityMode}
prg, _, err = comp.Compile(str, name, true)
if err != nil {
return nil, err
Expand All @@ -630,22 +626,22 @@ func (ctx *tc39TestCtx) runFile(base, name string, vm *sobek.Runtime) error {
return err
}

func (ctx *tc39TestCtx) runTC39Script(name, src string, includes []string, vm *sobek.Runtime, expectsError bool) (early bool, origErr, err error) {
func (ctx *tc39TestCtx) runTC39Script(name, src string, includes []string, vm *sobek.Runtime, expectsError bool) (early bool, err error) {
early = true
err = ctx.runFile(ctx.base, path.Join("harness", "assert.js"), vm)
if err != nil {
return early, origErr, err
return early, err
}

err = ctx.runFile(ctx.base, path.Join("harness", "sta.js"), vm)
if err != nil {
return early, origErr, err
return early, err
}

for _, include := range includes {
err = ctx.runFile(ctx.base, path.Join("harness", include), vm)
if err != nil {
return early, origErr, err
return early, err
}
}

Expand All @@ -654,59 +650,52 @@ func (ctx *tc39TestCtx) runTC39Script(name, src string, includes []string, vm *s
defer ctx.compilerPool.Put(comp)
comp.Options = compiler.Options{Strict: false, CompatibilityMode: lib.CompatibilityModeBase}
p, _, err = comp.Compile(src, name, true)
origErr = err
if err != nil && !expectsError {
comp.Options.CompatibilityMode = ctx.compatibilityMode
p, _, err = comp.Compile(src, name, true)
}

if err != nil {
return early, origErr, err
return early, err
}

early = false
_, err = vm.RunProgram(p)

return early, origErr, err
return early, err
}

func (ctx *tc39TestCtx) runTC39Module(name, src string, includes []string, vm *sobek.Runtime) (early bool, origErr, err error) {
currentFS := os.DirFS(".")
if err != nil {
panic(err)
}
func (ctx *tc39TestCtx) runTC39Module(name, src string, includes []string, vm *sobek.Runtime) (early bool, err error) {
moduleRuntime := modulestest.NewRuntime(ctx.t)
moduleRuntime.VU.RuntimeField = vm
early = true
err = ctx.runFile(ctx.base, path.Join("harness", "assert.js"), vm)
if err != nil {
return early, origErr, err
return early, err
}

err = ctx.runFile(ctx.base, path.Join("harness", "sta.js"), vm)
if err != nil {
return early, origErr, err
return early, err
}

for _, include := range includes {
err = ctx.runFile(ctx.base, path.Join("harness", include), vm)
if err != nil {
return early, origErr, err
return early, err
}
}

comp := ctx.compilerPool.Get()
defer ctx.compilerPool.Put(comp)
comp.Options = compiler.Options{Strict: false, CompatibilityMode: ctx.compatibilityMode}

u := &url.URL{Scheme: "file", Path: path.Join(ctx.base, name)}
base := u.JoinPath("..")
mr := modules.NewModuleResolver(nil,
func(specifier *url.URL, _ string) ([]byte, error) {
return fs.ReadFile(currentFS, specifier.Path[1:])
return fs.ReadFile(os.DirFS("."), specifier.Path[1:])
},
comp)
u := &url.URL{Scheme: "file", Path: path.Join(ctx.base, name)}

base := u.JoinPath("..")
ms := modules.NewModuleSystem(mr, moduleRuntime.VU)
impl := modules.NewLegacyRequireImpl(moduleRuntime.VU, ms, *base)
require.NoError(ctx.t, vm.Set("require", impl.Require))
Expand All @@ -718,7 +707,7 @@ func (ctx *tc39TestCtx) runTC39Module(name, src string, includes []string, vm *s
URL: u,
})

return early, origErr, err
return early, err
}

func (ctx *tc39TestCtx) runTC39Tests(name string) {
Expand Down

0 comments on commit 28ad10a

Please sign in to comment.