Skip to content

Commit ec9f2ca

Browse files
Call Object.getPrototypeOf twice to avoid referencing this.constructor
1 parent cfa4af4 commit ec9f2ca

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

src/com/google/javascript/jscomp/RewriteAsyncFunctions.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,21 @@ private void convertAsyncFunction(NodeTraversal t, LexicalContext functionContex
246246
boolean needsSuperTranspilation = compiler.getOptions().needsTranspilationFrom(FeatureSet.ES6);
247247
for (String replacedMethodName : functionContext.replacedSuperProperties) {
248248
// MS Edge 17 cannot properly capture references to "super" in an arrow function.
249-
// If we are not transpiling classes, switch to using Object.getPrototypeOf(this.constructor)
249+
// If we are not transpiling classes, switch to using Object.getPrototypeOf(this)
250250
// as a replacement for super.
251251
// If we are transpiling classes, the super reference will be handled elsewhere.
252252
Node superReference;
253253
if (needsSuperTranspilation) {
254254
superReference = IR.superNode();
255-
} else if (originalFunction.getParent().isStaticMember()) {
255+
} else {
256256
// static super: Object.getPrototypeOf(this);
257257
superReference =
258258
IR.call(IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")), IR.thisNode());
259-
} else {
260-
// instance super: Object.getPrototypeOf(this.constructor).prototype
261-
superReference =
262-
IR.getprop(
263-
IR.call(
264-
IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")),
265-
IR.getprop(IR.thisNode(), IR.string("constructor"))),
266-
IR.string("prototype"));
259+
if (!originalFunction.getParent().isStaticMember()) {
260+
// instance super: Object.getPrototypeOf(Object.getPrototypeOf(this))
261+
superReference =
262+
IR.call(IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")), superReference);
263+
}
267264
}
268265

269266
// const super$get$x = () => super.x;

src/com/google/javascript/jscomp/RewriteAsyncIteration.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -566,22 +566,21 @@ private void prependTempVarDeclarations(LexicalContext ctx, NodeTraversal t) {
566566
// }
567567
//
568568
// MS Edge 17 cannot properly capture references to "super" in an arrow function.
569-
// If we are not transpiling classes, switch to using Object.getPrototypeOf(this.constructor)
569+
// If we are not transpiling classes, switch to using Object.getPrototypeOf(this)
570570
// as a replacement for super.
571571
// If we are transpiling classes, the super reference will be handled elsewhere.
572572
Node superReference;
573573
if (needsSuperTranspilation) {
574574
superReference = IR.superNode();
575-
} else if (ctx.function.getParent().isStaticMember()) {
576-
// static super: Object.getPrototypeOf(this.constructor);
577-
superReference =
578-
IR.call(
579-
IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")),
580-
IR.getprop(IR.thisNode(), IR.string("constructor")));
581575
} else {
582-
// instance super: Object.getPrototypeOf(this)
576+
// static super: Object.getPrototypeOf(this);
583577
superReference =
584578
IR.call(IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")), IR.thisNode());
579+
if (!ctx.function.getParent().isStaticMember()) {
580+
// instance super: Object.getPrototypeOf(Object.getPrototypeOf(this))
581+
superReference =
582+
IR.call(IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")), superReference);
583+
}
585584
}
586585

587586
Node arrowFunction =

test/com/google/javascript/jscomp/IntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,7 +3989,7 @@ public void testAsyncFunctionSuper() {
39893989
"class Baz extends Foo {",
39903990
" bar() {",
39913991
" const $jscomp$async$this = this, $jscomp$async$super$get$bar =",
3992-
" () => Object.getPrototypeOf(this.constructor).prototype.bar;",
3992+
" () => Object.getPrototypeOf(Object.getPrototypeOf(this)).bar;",
39933993
" return $jscomp.asyncExecutePromiseGeneratorFunction(function*() {",
39943994
" yield Promise.resolve();",
39953995
" $jscomp$async$super$get$bar().call($jscomp$async$this);",
@@ -4043,7 +4043,7 @@ public void testAsyncIterationSuper() {
40434043
" bar() {",
40444044
" const $jscomp$asyncIter$this = this,",
40454045
" $jscomp$asyncIter$super$get$bar =",
4046-
" () => Object.getPrototypeOf(this.constructor).prototype.bar;",
4046+
" () => Object.getPrototypeOf(Object.getPrototypeOf(this)).bar;",
40474047
" return new $jscomp.AsyncGeneratorWrapper(function*() {",
40484048
" $jscomp$asyncIter$super$get$bar().call($jscomp$asyncIter$this).next();",
40494049
" }());",

test/com/google/javascript/jscomp/RewriteAsyncFunctionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void testInnerSuperCallEs2015Out() {
167167
" m() {",
168168
" const $jscomp$async$this = this;",
169169
" const $jscomp$async$super$get$m =",
170-
" () => Object.getPrototypeOf(this.constructor).prototype.m;",
170+
" () => Object.getPrototypeOf(Object.getPrototypeOf(this)).m;",
171171
" return $jscomp.asyncExecutePromiseGeneratorFunction(",
172172
" function* () {",
173173
" return $jscomp$async$super$get$m().call($jscomp$async$this);",

test/com/google/javascript/jscomp/RewriteAsyncIterationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public void testInnerSuperReferenceInAsyncGenerator() {
226226
"class X extends A {",
227227
" m() {",
228228
" const $jscomp$asyncIter$super$get$m =",
229-
" () => Object.getPrototypeOf(this.constructor).prototype.m;",
229+
" () => Object.getPrototypeOf(Object.getPrototypeOf(this)).m;",
230230
" return new $jscomp.AsyncGeneratorWrapper(",
231231
" function* () {",
232232
" const tmp = $jscomp$asyncIter$super$get$m();",

0 commit comments

Comments
 (0)