Cleanup Effects cloning (#2314)

Summary:
Release note: none

This is just a small code cleanup. Instead of cloning an Effects instance by directly calling the constructor of the clone, now call a clone method and supply only the parameter we want to change.

This is a mechanical refactor.
Pull Request resolved: https://github.com/facebook/prepack/pull/2314

Differential Revision: D8975311

Pulled By: hermanventer

fbshipit-source-id: eb57048782527de0756095f9efaee43cbcc1578b
This commit is contained in:
Herman Venter 2018-07-24 10:57:53 -07:00 committed by Facebook Github Bot
parent 789c698358
commit 66115d4377
6 changed files with 21 additions and 44 deletions

View File

@ -19,7 +19,7 @@ export class Completion {
let e = precedingEffects; let e = precedingEffects;
if (e !== undefined) { if (e !== undefined) {
if (e.result === undefined) e.result = this; if (e.result === undefined) e.result = this;
else e = new Effects(this, e.generator, e.modifiedBindings, e.modifiedProperties, e.createdObjects); else e = e.shallowCloneWithResult(this);
} }
this.value = value; this.value = value;
this.effects = e; this.effects = e;
@ -183,13 +183,7 @@ export class ForkedAbruptCompletion extends AbruptCompletion {
updateConsequentKeepingCurrentEffects(newConsequent: AbruptCompletion): AbruptCompletion { updateConsequentKeepingCurrentEffects(newConsequent: AbruptCompletion): AbruptCompletion {
let e = this.consequent.effects; let e = this.consequent.effects;
invariant(e); invariant(e);
newConsequent.effects = new Effects( newConsequent.effects = e.shallowCloneWithResult(newConsequent);
newConsequent,
e.generator,
e.modifiedBindings,
e.modifiedProperties,
e.createdObjects
);
this.consequent = newConsequent; this.consequent = newConsequent;
return this; return this;
} }
@ -197,13 +191,7 @@ export class ForkedAbruptCompletion extends AbruptCompletion {
updateAlternateKeepingCurrentEffects(newAlternate: AbruptCompletion): AbruptCompletion { updateAlternateKeepingCurrentEffects(newAlternate: AbruptCompletion): AbruptCompletion {
let e = this.alternate.effects; let e = this.alternate.effects;
invariant(e); invariant(e);
newAlternate.effects = new Effects( newAlternate.effects = e.shallowCloneWithResult(newAlternate);
newAlternate,
e.generator,
e.modifiedBindings,
e.modifiedProperties,
e.createdObjects
);
this.alternate = newAlternate; this.alternate = newAlternate;
return this; return this;
} }
@ -319,7 +307,7 @@ export class PossiblyNormalCompletion extends NormalCompletion {
updateConsequentKeepingCurrentEffects(newConsequent: Completion): PossiblyNormalCompletion { updateConsequentKeepingCurrentEffects(newConsequent: Completion): PossiblyNormalCompletion {
if (newConsequent instanceof NormalCompletion) this.value = newConsequent.value; if (newConsequent instanceof NormalCompletion) this.value = newConsequent.value;
let e = this.consequentEffects; let e = this.consequentEffects;
let effects = new Effects(newConsequent, e.generator, e.modifiedBindings, e.modifiedProperties, e.createdObjects); let effects = e.shallowCloneWithResult(newConsequent);
this.consequent = effects.result; this.consequent = effects.result;
return this; return this;
} }
@ -327,7 +315,7 @@ export class PossiblyNormalCompletion extends NormalCompletion {
updateAlternateKeepingCurrentEffects(newAlternate: Completion): PossiblyNormalCompletion { updateAlternateKeepingCurrentEffects(newAlternate: Completion): PossiblyNormalCompletion {
if (newAlternate instanceof NormalCompletion) this.value = newAlternate.value; if (newAlternate instanceof NormalCompletion) this.value = newAlternate.value;
let e = this.alternateEffects; let e = this.alternateEffects;
let effects = new Effects(newAlternate, e.generator, e.modifiedBindings, e.modifiedProperties, e.createdObjects); let effects = e.shallowCloneWithResult(newAlternate);
this.alternate = effects.result; this.alternate = effects.result;
return this; return this;
} }

View File

@ -12,7 +12,6 @@
import { CompilerDiagnostic, FatalError } from "../errors.js"; import { CompilerDiagnostic, FatalError } from "../errors.js";
import { AbruptCompletion, Completion, PossiblyNormalCompletion, SimpleNormalCompletion } from "../completions.js"; import { AbruptCompletion, Completion, PossiblyNormalCompletion, SimpleNormalCompletion } from "../completions.js";
import type { Realm } from "../realm.js"; import type { Realm } from "../realm.js";
import { Effects } from "../realm.js";
import { type LexicalEnvironment, type BaseValue, mightBecomeAnObject } from "../environment.js"; import { type LexicalEnvironment, type BaseValue, mightBecomeAnObject } from "../environment.js";
import { EnvironmentRecord } from "../environment.js"; import { EnvironmentRecord } from "../environment.js";
import { TypesDomain, ValuesDomain } from "../domains/index.js"; import { TypesDomain, ValuesDomain } from "../domains/index.js";
@ -184,12 +183,7 @@ function callBothFunctionsAndJoinTheirEffects(
if (r1 instanceof Completion) r1 = r1.shallowCloneWithoutEffects(); if (r1 instanceof Completion) r1 = r1.shallowCloneWithoutEffects();
let r2 = e2.result; let r2 = e2.result;
if (r2 instanceof Completion) r2 = r2.shallowCloneWithoutEffects(); if (r2 instanceof Completion) r2 = r2.shallowCloneWithoutEffects();
let joinedEffects = Join.joinForkOrChoose( let joinedEffects = Join.joinForkOrChoose(realm, cond, e1.shallowCloneWithResult(r1), e2.shallowCloneWithResult(r2));
realm,
cond,
new Effects(r1, e1.generator, e1.modifiedBindings, e1.modifiedProperties, e1.createdObjects),
new Effects(r2, e2.generator, e2.modifiedBindings, e2.modifiedProperties, e2.createdObjects)
);
let completion = joinedEffects.result; let completion = joinedEffects.result;
if (completion instanceof SimpleNormalCompletion) completion = completion.value; if (completion instanceof SimpleNormalCompletion) completion = completion.value;
if (completion instanceof PossiblyNormalCompletion) { if (completion instanceof PossiblyNormalCompletion) {

View File

@ -154,11 +154,11 @@ export class JoinImplementation {
invariant(c.savedEffects === undefined); // the caller should ensure this invariant(c.savedEffects === undefined); // the caller should ensure this
let savedPathConditions = pnc.savedPathConditions; let savedPathConditions = pnc.savedPathConditions;
if (pnc.consequent instanceof AbruptCompletion) { if (pnc.consequent instanceof AbruptCompletion) {
let ae = pnc.alternateEffects;
let na; let na;
if (pnc.alternate instanceof SimpleNormalCompletion) { if (pnc.alternate instanceof SimpleNormalCompletion) {
let { generator, modifiedBindings, modifiedProperties, createdObjects } = pnc.alternateEffects;
na = c.shallowCloneWithoutEffects(); na = c.shallowCloneWithoutEffects();
let newAlternateEffects = new Effects(na, generator, modifiedBindings, modifiedProperties, createdObjects); let newAlternateEffects = ae.shallowCloneWithResult(na);
if (priorEffects) newAlternateEffects = realm.composeEffects(priorEffects, newAlternateEffects); if (priorEffects) newAlternateEffects = realm.composeEffects(priorEffects, newAlternateEffects);
return new PossiblyNormalCompletion( return new PossiblyNormalCompletion(
c.value, c.value,
@ -173,8 +173,7 @@ export class JoinImplementation {
} }
invariant(pnc.alternate instanceof PossiblyNormalCompletion); invariant(pnc.alternate instanceof PossiblyNormalCompletion);
na = this.composePossiblyNormalCompletions(realm, pnc.alternate, c, priorEffects); na = this.composePossiblyNormalCompletions(realm, pnc.alternate, c, priorEffects);
let { generator, modifiedBindings, modifiedProperties, createdObjects } = pnc.alternateEffects; let newAlternateEffects = ae.shallowCloneWithResult(na);
let newAlternateEffects = new Effects(na, generator, modifiedBindings, modifiedProperties, createdObjects);
return new PossiblyNormalCompletion( return new PossiblyNormalCompletion(
c.value, c.value,
pnc.joinCondition, pnc.joinCondition,
@ -186,11 +185,11 @@ export class JoinImplementation {
pnc.savedEffects pnc.savedEffects
); );
} else { } else {
let ce = pnc.consequentEffects;
let nc; let nc;
if (pnc.consequent instanceof SimpleNormalCompletion) { if (pnc.consequent instanceof SimpleNormalCompletion) {
let { generator, modifiedBindings, modifiedProperties, createdObjects } = pnc.consequentEffects;
nc = c.shallowCloneWithoutEffects(); nc = c.shallowCloneWithoutEffects();
let newConsequentEffects = new Effects(nc, generator, modifiedBindings, modifiedProperties, createdObjects); let newConsequentEffects = ce.shallowCloneWithResult(nc);
if (priorEffects) newConsequentEffects = realm.composeEffects(priorEffects, newConsequentEffects); if (priorEffects) newConsequentEffects = realm.composeEffects(priorEffects, newConsequentEffects);
return new PossiblyNormalCompletion( return new PossiblyNormalCompletion(
c.value, c.value,
@ -205,8 +204,7 @@ export class JoinImplementation {
} }
invariant(pnc.consequent instanceof PossiblyNormalCompletion); invariant(pnc.consequent instanceof PossiblyNormalCompletion);
nc = this.composePossiblyNormalCompletions(realm, pnc.consequent, c); nc = this.composePossiblyNormalCompletions(realm, pnc.consequent, c);
let { generator, modifiedBindings, modifiedProperties, createdObjects } = pnc.consequentEffects; let newConsequentEffects = ce.shallowCloneWithResult(nc);
let newConsequentEffects = new Effects(nc, generator, modifiedBindings, modifiedProperties, createdObjects);
return new PossiblyNormalCompletion( return new PossiblyNormalCompletion(
c.value, c.value,
pnc.joinCondition, pnc.joinCondition,
@ -296,13 +294,13 @@ export class JoinImplementation {
): ForkedAbruptCompletion { ): ForkedAbruptCompletion {
let recurse = (xpnc, xe, nac, ne): [ForkedAbruptCompletion, Effects] => { let recurse = (xpnc, xe, nac, ne): [ForkedAbruptCompletion, Effects] => {
let nx = this.replacePossiblyNormalCompletionWithForkedAbruptCompletion(realm, xpnc, nac, ne); let nx = this.replacePossiblyNormalCompletionWithForkedAbruptCompletion(realm, xpnc, nac, ne);
let nxe = new Effects(nx, xe.generator, xe.modifiedBindings, xe.modifiedProperties, xe.createdObjects); let nxe = xe.shallowCloneWithResult(nx);
return [nx, nxe]; return [nx, nxe];
}; };
let cloneEffects = () => { let cloneEffects = () => {
let nac = ac.shallowCloneWithoutEffects(); let nac = ac.shallowCloneWithoutEffects();
let ne = new Effects(nac, e.generator, e.modifiedBindings, e.modifiedProperties, e.createdObjects); let ne = e.shallowCloneWithResult(nac);
return [nac, ne]; return [nac, ne];
}; };

View File

@ -11,7 +11,6 @@
import type { BabelNodeCallExpression, BabelNodeExpression, BabelNodeStatement } from "@babel/types"; import type { BabelNodeCallExpression, BabelNodeExpression, BabelNodeStatement } from "@babel/types";
import type { Realm } from "../realm.js"; import type { Realm } from "../realm.js";
import { Effects } from "../realm.js";
import type { LexicalEnvironment } from "../environment.js"; import type { LexicalEnvironment } from "../environment.js";
import { AbruptCompletion, Completion, PossiblyNormalCompletion, SimpleNormalCompletion } from "../completions.js"; import { AbruptCompletion, Completion, PossiblyNormalCompletion, SimpleNormalCompletion } from "../completions.js";
@ -119,12 +118,7 @@ function callBothFunctionsAndJoinTheirEffects(
); );
let r2 = e2.result.shallowCloneWithoutEffects(); let r2 = e2.result.shallowCloneWithoutEffects();
let joinedEffects = Join.joinForkOrChoose( let joinedEffects = Join.joinForkOrChoose(realm, cond, e1.shallowCloneWithResult(r1), e2.shallowCloneWithResult(r2));
realm,
cond,
new Effects(r1, e1.generator, e1.modifiedBindings, e1.modifiedProperties, e1.createdObjects),
new Effects(r2, e2.generator, e2.modifiedBindings, e2.modifiedProperties, e2.createdObjects)
);
let joinedCompletion = joinedEffects.result; let joinedCompletion = joinedEffects.result;
if (joinedCompletion instanceof PossiblyNormalCompletion) { if (joinedCompletion instanceof PossiblyNormalCompletion) {
// in this case one of the branches may complete abruptly, which means that // in this case one of the branches may complete abruptly, which means that

View File

@ -12,7 +12,6 @@
import type { BabelNodeIfStatement, BabelNodeStatement } from "@babel/types"; import type { BabelNodeIfStatement, BabelNodeStatement } from "@babel/types";
import type { LexicalEnvironment } from "../environment.js"; import type { LexicalEnvironment } from "../environment.js";
import type { Realm } from "../realm.js"; import type { Realm } from "../realm.js";
import { Effects } from "../realm.js";
import { AbruptCompletion, Completion, PossiblyNormalCompletion } from "../completions.js"; import { AbruptCompletion, Completion, PossiblyNormalCompletion } from "../completions.js";
import { Reference } from "../environment.js"; import { Reference } from "../environment.js";
@ -83,8 +82,8 @@ export default function(
let joinedEffects = Join.joinForkOrChoose( let joinedEffects = Join.joinForkOrChoose(
realm, realm,
exprValue, exprValue,
new Effects(cr, ce.generator, ce.modifiedBindings, ce.modifiedProperties, ce.createdObjects), ce.shallowCloneWithResult(cr),
new Effects(ar, ae.generator, ae.modifiedBindings, ae.modifiedProperties, ae.createdObjects) ae.shallowCloneWithResult(ar)
); );
completion = joinedEffects.result; completion = joinedEffects.result;
if (completion instanceof PossiblyNormalCompletion) { if (completion instanceof PossiblyNormalCompletion) {

View File

@ -124,6 +124,10 @@ export class Effects {
canBeApplied: boolean; canBeApplied: boolean;
_id: number; _id: number;
shallowCloneWithResult(result: Completion): Effects {
return new Effects(result, this.generator, this.modifiedBindings, this.modifiedProperties, this.createdObjects);
}
toDisplayString(): string { toDisplayString(): string {
return Utils.jsonToDisplayString(this, 10); return Utils.jsonToDisplayString(this, 10);
} }