Skip to content

Commit

Permalink
Enabled more tests, fixed bugs. See #460.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Dec 20, 2022
1 parent a008913 commit b449afd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
9 changes: 6 additions & 3 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,16 @@ func (g *generator) step() (res Value, resultType resultType, ex *Exception) {
return
}

func (g *generator) next(v Value) (Value, resultType, *Exception) {
func (g *generator) enterNext() {
g.vm.pushCtx()
g.vm.pushTryFrame(tryPanicMarker, -1)
g.vm.callStack = append(g.vm.callStack, context{pc: -2}) // extra frame so that vm.run() halts after ret
g.storeLengths()
g.vm.resume(&g.ctx)
}

func (g *generator) next(v Value) (Value, resultType, *Exception) {
g.enterNext()
if v != nil {
g.vm.push(v)
}
Expand All @@ -718,8 +722,7 @@ func (g *generator) next(v Value) (Value, resultType, *Exception) {
}

func (g *generator) nextThrow(v Value) (Value, resultType, *Exception) {
g.enter()
g.vm.resume(&g.ctx)
g.enterNext()
ex := g.vm.handleThrow(v)
if ex != nil {
g.vm.popTryFrame()
Expand Down
4 changes: 1 addition & 3 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,9 +1270,7 @@ func (self *_parser) parseAssignmentExpression() ast.Expression {
self.scope.allowAwait = false
}()
}
if c, ok := left.(*ast.CallExpression); ok && len(self.errors) == 0 {
paramList = self.reinterpretSequenceAsArrowFuncParams(c.ArgumentList)
} else {
if _, ok := left.(*ast.CallExpression); ok {
self.restore(&state)
self.next() // skip "async"
paramList = self.parseFunctionParameterList()
Expand Down
37 changes: 37 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,43 @@ func TestPanicPassthrough(t *testing.T) {
t.Fatal("Should not reach here")
}

func TestSuspendResumeRelStackLen(t *testing.T) {
const SCRIPT = `
let result;
async function f2() {
throw new Error("test");
}
async function f1() {
let a = [1];
for (let i of a) {
try {
await f2();
} catch {
return true;
}
}
}
async function f() {
let a = [1];
for (let i of a) {
return await f1();
}
}
f().then(v => {result = v});
`
vm := New()
_, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if result := vm.Get("result").Export(); result != true {
t.Fatal(result)
}
}

/*
func TestArrayConcatSparse(t *testing.T) {
function foo(a,b,c)
Expand Down
54 changes: 29 additions & 25 deletions tc39_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ var (
"test/language/statements/class/elements/private-generator-method-name.js": true,
"test/language/expressions/in/private-field-rhs-yield-present.js": true,
"test/language/expressions/class/elements/private-static-generator-method-name.js": true,
"test/language/expressions/class/elements/private-static-async-generator-method-name.js": true,
"test/language/computed-property-names/class/static/generator-prototype.js": true,
"test/language/computed-property-names/class/method/constructor-can-be-generator.js": true,
"test/language/computed-property-names/class/static/generator-constructor.js": true,
Expand Down Expand Up @@ -188,6 +187,7 @@ var (
"test/language/expressions/object/method-definition/static-init-await-binding-generator.js": true,
"test/language/expressions/class/elements/private-async-generator-method-name.js": true,
"test/language/comments/hashbang/function-constructor.js": true,
"test/language/expressions/async-generator/name.js": true,

// async iterator
"test/language/expressions/optional-chaining/iteration-statement-for-await-of.js": true,
Expand Down Expand Up @@ -273,31 +273,35 @@ func init() {
"test/language/identifiers/start-unicode-14.",
"test/language/identifiers/part-unicode-14.",

// async
"test/language/eval-code/direct/async-",
"test/language/expressions/async-",
"test/language/expressions/await/",
"test/language/statements/async-function/",
// generators and async generators (harness/hidden-constructors.js)
"test/built-ins/Async",
"test/language/statements/class/elements/private-static-async-",
"test/language/statements/class/elements/wrapped-in-sc-rs-static-async-",
"test/language/expressions/class/elements/wrapped-in-sc-rs-static-async-",
"test/language/statements/class/elements/after-same-line-static-method-rs-static-async-",
"test/language/expressions/class/elements/after-same-line-static-method-rs-static-async-",
"test/language/statements/class/elements/after-same-line-method-rs-static-async-",
"test/language/expressions/class/elements/after-same-line-method-rs-static-async-",
"test/language/statements/class/elements/new-sc-line-method-rs-static-async-",
"test/language/expressions/class/elements/new-sc-line-method-rs-static-async-",
"test/language/statements/class/elements/new-no-sc-line-method-rs-static-async-",
"test/language/expressions/class/elements/new-no-sc-line-method-rs-static-async-",
"test/language/statements/class/elements/same-line-method-rs-static-async-",
"test/language/expressions/class/elements/same-line-method-rs-static-async-",
"test/language/statements/class/elements/regular-definitions-rs-static-async-",
"test/language/expressions/class/elements/regular-definitions-rs-static-async-",
"test/language/statements/class/elements/multiple-stacked-definitions-rs-static-async-",
"test/language/expressions/class/elements/multiple-stacked-definitions-rs-static-async-",
"test/language/statements/class/elements/multiple-definitions-rs-static-async-",
"test/language/expressions/class/elements/multiple-definitions-rs-static-async-",

// async generators
"test/language/statements/class/elements/wrapped-in-sc-rs-static-async-generator-",
"test/language/statements/class/elements/same-line-method-rs-static-async-generator-",
"test/language/statements/class/elements/regular-definitions-rs-static-async-generator-",
"test/language/statements/class/elements/private-static-async-generator-",
"test/language/statements/class/elements/new-sc-line-method-rs-static-async-generator-",
"test/language/statements/class/elements/multiple-stacked-definitions-rs-static-async-generator-",
"test/language/statements/class/elements/new-no-sc-line-method-rs-static-async-generator-",
"test/language/statements/class/elements/multiple-definitions-rs-static-async-generator-",
"test/language/statements/class/elements/after-same-line-static-method-rs-static-async-generator-",
"test/language/statements/class/elements/after-same-line-method-rs-static-async-generator-",
"test/language/statements/class/elements/after-same-line-static-method-rs-static-async-generator-",

"test/language/expressions/class/elements/wrapped-in-sc-rs-static-async-generator-",
"test/language/expressions/class/elements/same-line-method-rs-static-async-generator-",
"test/language/expressions/class/elements/regular-definitions-rs-static-async-generator-",
"test/language/expressions/class/elements/private-static-async-generator-",
"test/language/expressions/class/elements/new-sc-line-method-rs-static-async-generator-",
"test/language/expressions/class/elements/multiple-stacked-definitions-rs-static-async-generator-",
"test/language/expressions/class/elements/new-no-sc-line-method-rs-static-async-generator-",
"test/language/expressions/class/elements/multiple-definitions-rs-static-async-generator-",
"test/language/expressions/class/elements/after-same-line-static-method-rs-static-async-generator-",
"test/language/expressions/class/elements/after-same-line-method-rs-static-async-generator-",
"test/language/expressions/class/elements/after-same-line-static-method-rs-static-async-generator-",

"test/language/eval-code/direct/async-gen-",

// generators
"test/language/eval-code/direct/gen-",
Expand Down
11 changes: 11 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (vm *vm) suspend(ectx *execCtx, tryStackLen, iterStackLen, refStackLen uint
l := len(vm.tryStack) - int(tryStackLen)
ectx.tryStack = append(ectx.tryStack[:0], vm.tryStack[l:]...)
vm.tryStack = vm.tryStack[:l]
for i := range ectx.tryStack {
tf := &ectx.tryStack[i]
tf.iterLen -= iterStackLen
tf.refLen -= refStackLen
}
}
if iterStackLen > 0 {
l := len(vm.iterStack) - int(iterStackLen)
Expand All @@ -90,6 +95,12 @@ func (vm *vm) resume(ctx *execCtx) {
vm.stack.expand(vm.sp + len(ctx.stack))
copy(vm.stack[vm.sp:], ctx.stack)
vm.sp += len(ctx.stack)
for i := range ctx.tryStack {
tf := &ctx.tryStack[i]
tf.callStackLen = uint32(len(vm.callStack))
tf.iterLen += uint32(len(vm.iterStack))
tf.refLen += uint32(len(vm.refStack))
}
vm.tryStack = append(vm.tryStack, ctx.tryStack...)
vm.iterStack = append(vm.iterStack, ctx.iterStack...)
vm.refStack = append(vm.refStack, ctx.refStack...)
Expand Down

0 comments on commit b449afd

Please sign in to comment.