瀏覽代碼

Fixed the use of outer scope 'this' in arrow functions. See #334

Dmitry Panov 4 年之前
父節點
當前提交
c357d26fb9
共有 3 個文件被更改,包括 16 次插入4 次删除
  1. 2 4
      compiler_expr.go
  2. 13 0
      compiler_test.go
  3. 1 0
      tc39_test.go

+ 2 - 4
compiler_expr.go

@@ -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)

+ 13 - 0
compiler_test.go

@@ -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) {

+ 1 - 0
tc39_test.go

@@ -307,6 +307,7 @@ var (
 		"12.2.6.8",
 		"12.2.8",
 		"12.2.9",
+		"12.3.7",
 		"12.4",
 		"12.5",
 		"12.6",