Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround for various object literal mutation bugs #3266

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ast/nodes/CallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../utils/PathTracker';
import { LiteralValueOrUnknown, UNKNOWN_EXPRESSION, UnknownValue } from '../values';
import Identifier from './Identifier';
import MemberExpression from './MemberExpression';
import * as NodeType from './NodeType';
import { ExpressionEntity } from './shared/Expression';
import { ExpressionNode, INCLUDE_PARAMETERS, IncludeChildren, NodeBase } from './shared/Node';
Expand Down Expand Up @@ -66,6 +67,9 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
this
);
}
if (this.callee instanceof MemberExpression && !this.callee.variable) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny - I had this initially but dismissed it due the higher number of test regressions. But it's the safer route.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if we gonna fix it, we might just as well fix it all the way down. I kept the tests because I REALLY hope to get in a proper solution soon.

this.callee.object.deoptimizePath(UNKNOWN_PATH);
}
for (const argument of this.arguments) {
// This will make sure all properties of parameters behave as "unknown"
argument.deoptimizePath(UNKNOWN_PATH);
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/arrow-function-return-values/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ retained1()();
(() => {
return () => console.log( 'effect' );
})()();

(() => ({ foo: () => {} }))().foo();
(() => ({ foo: () => console.log( 'effect' ) }))().foo();

(() => ({ foo: () => ({ bar: () => ({ baz: () => {} }) }) }))().foo().bar().baz();
(() => ({ foo: () => ({ bar: () => console.log( 'effect' ) }) }))().foo().bar();
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
const object = {};
const propertyIsEnumerable1 = object.propertyIsEnumerable( 'toString' );
const propertyIsEnumerable2 = {}.propertyIsEnumerable( 'toString' );
const propertyIsEnumerable3 = {}.propertyIsEnumerable( 'toString' ).valueOf();

const _hasOwnProperty = {}.hasOwnProperty( 'toString' ).valueOf();
const _isPrototypeOf = {}.isPrototypeOf( {} ).valueOf();
const _propertyIsEnumerable = {}.propertyIsEnumerable( 'toString' ).valueOf();
const _toLocaleString = {}.toLocaleString().trim();
const _toString = {}.toString().trim();
const _valueOf = {}.valueOf();
11 changes: 11 additions & 0 deletions test/form/samples/conditional-expression-paths/_expected.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
var unknownValue = globalThis.unknown();
var foo = { x: () => {}, y: {} };
var bar = { x: () => {}, y: {} };
var baz = { x: () => console.log('effect') };

// unknown branch without side-effects
var a1 = (unknownValue ? foo : bar).y.z;
var b1 = (unknownValue ? foo : bar).x();

// unknown branch with side-effect
var a2 = (unknownValue ? foo : baz).y.z;
var b2 = (unknownValue ? foo : baz).x();

// known branch without side-effects
var a3 = ( foo ).y.z;
var b3 = ( foo).y.z;
var c3 = ( foo ).x();
var d3 = ( foo).x();

// known branch with side-effect
var a4 = ( baz ).y.z;
var b4 = ( baz).y.z;
Expand Down
6 changes: 6 additions & 0 deletions test/form/samples/function-body-return-values/_expected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function removed3 () {
return { x: () => {} };
}

removed3().x();

function retained1 () {
return () => console.log( 'effect' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

({
get foo () {
console.log( 'effect' );
Expand All @@ -11,6 +9,12 @@
return {};
}
}).foo.bar.baz;

({
get foo () {
return () => {};
}
}).foo();
({
get foo () {
console.log( 'effect' );
Expand All @@ -22,6 +26,12 @@
return () => console.log( 'effect' );
}
}).foo();

({
get foo () {
return () => () => {};
}
}).foo()();
({
get foo () {
console.log( 'effect' );
Expand Down
37 changes: 0 additions & 37 deletions test/form/samples/getter-return-values/_expected/amd.js

This file was deleted.

33 changes: 0 additions & 33 deletions test/form/samples/getter-return-values/_expected/es.js

This file was deleted.

38 changes: 0 additions & 38 deletions test/form/samples/getter-return-values/_expected/iife.js

This file was deleted.

42 changes: 0 additions & 42 deletions test/form/samples/getter-return-values/_expected/system.js

This file was deleted.

40 changes: 0 additions & 40 deletions test/form/samples/getter-return-values/_expected/umd.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const x = {
[globalThis.unknown]() {
console.log('effect');
},
a() {}
};

x.a();

const y = {
a() {},
[globalThis.unknown]() {
Expand All @@ -12,3 +21,4 @@ const z = {
};

z.a();
z.hasOwnProperty('a'); // removed
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const x = {
[globalThis.unknown]: () => () => console.log('effect'),
a: () => () => {}
};

x.a()();

const y = {
a: () => () => {},
[globalThis.unknown]: () => () => console.log('effect')
Expand All @@ -12,6 +19,8 @@ const z = {
z.a()();

const v = {};

v.toString().charCodeAt(0); // removed
v.toString().doesNotExist(0); // retained

const w = {
Expand Down
20 changes: 20 additions & 0 deletions test/form/samples/object-literal-property-overwrites/_expected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
const removed1 = {
foo: () => {},
foo: () => {},
['f' + 'oo']: () => {}
};
removed1.foo();

const removed2 = {
foo: () => console.log( 'effect' ),
foo: () => {}
};
removed2.foo();

const removed3 = {
['fo' + 'o']: function () {this.x = 1;},
['f' + 'oo']: () => console.log( 'effect' ),
foo: () => {}
};
removed3.foo();

const retained1 = {
foo: () => {},
foo: function () {this.x = 1;}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
function getReturnExpressionBeforeInit() {
{
const bar = {
[foo.value()]: true
};
if (bar.baz) {
console.log('retained');
}
}

const foo = {
get value() {
return () => 'baz';
}
};

getReturnExpressionBeforeInit();