Skip to content

Commit

Permalink
Fixed the use of outer scope 'this' in arrow functions. See dop251#334
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Oct 11, 2021
1 parent bb5631b commit c357d26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions compiler_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,13 +1320,11 @@ func (e *compiledThisExpr) emitGetter(putOnStack bool) {
if putOnStack {
e.addSrcMap()
scope := e.c.scope
for ; scope != nil && !scope.function && !scope.eval; scope = scope.outer {
for ; scope != nil && (scope.arrow || !scope.function && !scope.eval); scope = scope.outer {
}

if scope != nil {
if !scope.arrow {
scope.thisNeeded = true
}
scope.thisNeeded = true
e.c.emit(loadStack(0))
} else {
e.c.emit(loadGlobalObject)
Expand Down
13 changes: 13 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4148,6 +4148,19 @@ func TestArrowUseStrict(t *testing.T) {
}
}

func TestArrowBoxedThis(t *testing.T) {
const SCRIPT = `
var context;
fn = function() {
return (arg) => { var local; context = this; };
};
fn()();
context === this;
`

testScript1(SCRIPT, valueTrue, t)
}
func TestParameterOverride(t *testing.T) {
const SCRIPT = `
function f(arg) {
Expand Down
1 change: 1 addition & 0 deletions tc39_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ var (
"12.2.6.8",
"12.2.8",
"12.2.9",
"12.3.7",
"12.4",
"12.5",
"12.6",
Expand Down

0 comments on commit c357d26

Please sign in to comment.