Skip to content

Commit

Permalink
Fixed callee expressions in optional chains. Fixes dop251#385.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed May 1, 2022
1 parent e1f9dc0 commit e1eca0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,8 @@ func (c *compiler) emitCallee(callee compiledExpr) (calleeName unistring.String)
c.endOptChain()
case *compiledOptional:
c.emitCallee(callee.expr)
c.block.conts = append(c.block.conts, len(c.p.code))
c.emit(nil)
default:
c.emit(loadUndef)
callee.emitGetter(true)
Expand Down Expand Up @@ -2458,6 +2460,9 @@ func (c *compiler) endOptChain() {
for _, item := range c.block.breaks {
c.p.code[item] = jopt(lbl - item)
}
for _, item := range c.block.conts {
c.p.code[item] = joptc(lbl - item)
}
c.block = c.block.outer
}

Expand Down
13 changes: 13 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4716,6 +4716,19 @@ func TestAssignBeforeInit(t *testing.T) {
testScriptWithTestLib(SCRIPT, _undefined, t)
}

func TestOptChainCallee(t *testing.T) {
const SCRIPT = `
var a;
assert.sameValue(a?.(true), undefined);
a = null;
assert.sameValue(a?.(), undefined);
var o = {n: null};
assert.sameValue(o.m?.(true), undefined);
assert.sameValue(o.n?.(true), undefined);
`
testScriptWithTestLib(SCRIPT, _undefined, t)
}

/*
func TestBabel(t *testing.T) {
src, err := ioutil.ReadFile("babel7.js")
Expand Down
13 changes: 13 additions & 0 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,19 @@ func (j jopt) exec(vm *vm) {
}
}

type joptc int32

func (j joptc) exec(vm *vm) {
switch vm.stack[vm.sp-1].(type) {
case valueNull, valueUndefined, memberUnresolved:
vm.sp--
vm.stack[vm.sp-1] = _undefined
vm.pc += int(j)
default:
vm.pc++
}
}

type jcoalesc int32

func (j jcoalesc) exec(vm *vm) {
Expand Down

0 comments on commit e1eca0b

Please sign in to comment.