From c0b3efbbb4709cc074c34862b506140248a3ec76 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 1 Aug 2014 11:16:09 -0700 Subject: [PATCH] emit this capturing as a first action in the constructor --- src/compiler/emitter.ts | 4 +-- .../reference/arrowFunctionContexts.js | 4 +-- .../reference/captureThisInSuperCall.js | 32 +++++++++++++++++++ .../classMemberInitializerWithLamdaScoping.js | 2 +- ...ionAndPropertyNameAsConstuctorParameter.js | 4 +-- .../derivedClassSuperCallsWithThisArg.js | 2 +- tests/baselines/reference/lambdaPropSelf.js | 2 +- tests/baselines/reference/statics.js | 2 +- .../reference/validUseOfThisInSuper.js | 2 +- .../cases/compiler/captureThisInSuperCall.ts | 8 +++++ 10 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 tests/baselines/reference/captureThisInSuperCall.js create mode 100644 tests/cases/compiler/captureThisInSuperCall.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index a80845d852..b867a555ef 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1371,6 +1371,7 @@ module ts { write(" {"); scopeEmitStart(node, "constructor"); increaseIndent(); + emitCaptureThisForNodeIfNecessary(node); if (ctor) { emitDefaultValueAssignments(ctor); emitRestParameter(ctor); @@ -1390,8 +1391,7 @@ module ts { write("_super.apply(this, arguments);"); emitEnd(node.baseType); } - } - emitCaptureThisForNodeIfNecessary(node); + } emitMemberAssignments(node, /*nonstatic*/0); if (ctor) { var statements: Node[] = (ctor.body).statements; diff --git a/tests/baselines/reference/arrowFunctionContexts.js b/tests/baselines/reference/arrowFunctionContexts.js index 24b928c784..d0b43e61c8 100644 --- a/tests/baselines/reference/arrowFunctionContexts.js +++ b/tests/baselines/reference/arrowFunctionContexts.js @@ -114,8 +114,8 @@ var Base = (function () { var Derived = (function (_super) { __extends(Derived, _super); function Derived() { - _super.call(this, function () { return _this; }); var _this = this; + _super.call(this, function () { return _this; }); } return Derived; })(Base); @@ -147,8 +147,8 @@ var M2; var Derived = (function (_super) { __extends(Derived, _super); function Derived() { - _super.call(this, function () { return _this; }); var _this = this; + _super.call(this, function () { return _this; }); } return Derived; })(Base); diff --git a/tests/baselines/reference/captureThisInSuperCall.js b/tests/baselines/reference/captureThisInSuperCall.js new file mode 100644 index 0000000000..3f60fcd3cb --- /dev/null +++ b/tests/baselines/reference/captureThisInSuperCall.js @@ -0,0 +1,32 @@ +//// [captureThisInSuperCall.ts] +class A { + constructor(p:any) {} +} + +class B extends A { + constructor() { super({ test: () => this.someMethod()}); } + someMethod() {} +} + +//// [captureThisInSuperCall.js] +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var A = (function () { + function A(p) { + } + return A; +})(); +var B = (function (_super) { + __extends(B, _super); + function B() { + var _this = this; + _super.call(this, { test: function () { return _this.someMethod(); } }); + } + B.prototype.someMethod = function () { + }; + return B; +})(A); diff --git a/tests/baselines/reference/classMemberInitializerWithLamdaScoping.js b/tests/baselines/reference/classMemberInitializerWithLamdaScoping.js index f246ea3870..9eb0eb8307 100644 --- a/tests/baselines/reference/classMemberInitializerWithLamdaScoping.js +++ b/tests/baselines/reference/classMemberInitializerWithLamdaScoping.js @@ -33,8 +33,8 @@ class Test1 { //// [classMemberInitializerWithLamdaScoping.js] var Test = (function () { function Test(field) { - this.field = field; var _this = this; + this.field = field; this.messageHandler = function () { var field = _this.field; console.log(field); diff --git a/tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.js b/tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.js index 813295bb88..566bb552a7 100644 --- a/tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.js +++ b/tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.js @@ -47,8 +47,8 @@ var Foo2 = (function () { })(); var Foo3 = (function () { function Foo3(_this) { - this._this = _this; var _this = this; + this._this = _this; var lambda = function () { return function (x) { return _this; }; }; @@ -66,8 +66,8 @@ var Foo4 = (function () { })(); var Foo5 = (function () { function Foo5(_this) { - this._this = _this; var _this = this; + this._this = _this; var lambda = function () { return function (x) { return _this; }; }; diff --git a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.js b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.js index 163337ae12..0616d94cdf 100644 --- a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.js +++ b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.js @@ -58,9 +58,9 @@ var Derived2 = (function (_super) { var Derived3 = (function (_super) { __extends(Derived3, _super); function Derived3(a) { + var _this = this; _super.call(this, function () { return _this; }); this.a = a; - var _this = this; } return Derived3; })(Base); diff --git a/tests/baselines/reference/lambdaPropSelf.js b/tests/baselines/reference/lambdaPropSelf.js index 1630bd0c5d..8d1b16e143 100644 --- a/tests/baselines/reference/lambdaPropSelf.js +++ b/tests/baselines/reference/lambdaPropSelf.js @@ -26,8 +26,8 @@ module M { //// [lambdaPropSelf.js] var Person = (function () { function Person(name, children) { - this.name = name; var _this = this; + this.name = name; this.addChild = function () { return _this.children.push("New child"); }; this.children = ko.observableArray(children); } diff --git a/tests/baselines/reference/statics.js b/tests/baselines/reference/statics.js index 4266725bec..c69fa0aabd 100644 --- a/tests/baselines/reference/statics.js +++ b/tests/baselines/reference/statics.js @@ -36,9 +36,9 @@ var M; (function (M) { var C = (function () { function C(c1, c2, c3) { + var _this = this; this.c1 = c1; this.c2 = c2; - var _this = this; this.x = C.y + this.c1 + this.c2 + c3; this.g = function (v) { return C.f(_this.x + C.y + v + _this.c1 + _this.c2 + C.pub); }; } diff --git a/tests/baselines/reference/validUseOfThisInSuper.js b/tests/baselines/reference/validUseOfThisInSuper.js index 9ff9bb0842..a57f293443 100644 --- a/tests/baselines/reference/validUseOfThisInSuper.js +++ b/tests/baselines/reference/validUseOfThisInSuper.js @@ -25,8 +25,8 @@ var Base = (function () { var Super = (function (_super) { __extends(Super, _super); function Super() { - _super.call(this, (function () { return _this; })()); var _this = this; + _super.call(this, (function () { return _this; })()); } return Super; })(Base); diff --git a/tests/cases/compiler/captureThisInSuperCall.ts b/tests/cases/compiler/captureThisInSuperCall.ts new file mode 100644 index 0000000000..8037aa50b4 --- /dev/null +++ b/tests/cases/compiler/captureThisInSuperCall.ts @@ -0,0 +1,8 @@ +class A { + constructor(p:any) {} +} + +class B extends A { + constructor() { super({ test: () => this.someMethod()}); } + someMethod() {} +} \ No newline at end of file