Skip to content

Commit

Permalink
Fixed this binding initialisation. Fixes dop251#415.
Browse files Browse the repository at this point in the history
(cherry picked from commit a070957)
  • Loading branch information
dop251 authored and Gabri3l committed Sep 1, 2022
1 parent 51ae526 commit 5940b05
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ func (p *Program) _dumpCode(indent string, logger func(format string, args ...in
if f.initFields != nil {
dumpInitFields(f.initFields)
}
prg = f.ctor
case *newStaticFieldInit:
if f.initFields != nil {
dumpInitFields(f.initFields)
Expand Down
4 changes: 2 additions & 2 deletions compiler_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,10 +1615,10 @@ func (e *compiledFunctionLiteral) compile() (prg *Program, name unistring.String

stashSize, stackSize := s.finaliseVarAlloc(0)

if stackSize > 0 && thisBinding != nil && thisBinding.inStash {
if thisBinding != nil && thisBinding.inStash && (!s.argsInStash || stackSize > 0) {
delta++
code[preambleLen-delta] = loadStack(0)
}
} // otherwise, 'this' will be at stack[sp-1], no need to load

if !s.strict && thisBinding != nil {
delta++
Expand Down
46 changes: 46 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5502,6 +5502,52 @@ func TestKeywordsAsLabels(t *testing.T) {
testScript(SCRIPT, _undefined, t)
}

func TestThisResolutionWithArg(t *testing.T) {
const SCRIPT = `
let capture;
function f(arg) {
capture = () => this; // move 'this' to stash
return [this, arg];
}
const _this = {};
const arg = {};
const [_this1, arg1] = f.call(_this, arg);
_this1 === _this && arg1 === arg;
`
testScript(SCRIPT, valueTrue, t)
}

func TestThisResolutionArgInStash(t *testing.T) {
const SCRIPT = `
let capture;
function f(arg) {
capture = () => this + arg; // move 'this' and arguments to stash
return [this, arg];
}
const _this = {};
const arg = {};
const [_this1, arg1] = f.call(_this, arg);
_this1 === _this && arg1 === arg;
`
testScript(SCRIPT, valueTrue, t)
}

func TestThisResolutionWithStackVar(t *testing.T) {
const SCRIPT = `
let capture;
function f(arg) {
const _ = 1; // a stack variable
capture = () => this + arg; // move 'this' and arguments to stash
return [this, arg];
}
const _this = {};
const arg = {};
const [_this1, arg1] = f.call(_this, arg);
_this1 === _this && arg1 === arg;
`
testScript(SCRIPT, valueTrue, t)
}

/*
func TestBabel(t *testing.T) {
src, err := ioutil.ReadFile("babel7.js")
Expand Down

0 comments on commit 5940b05

Please sign in to comment.