Skip to content

Commit

Permalink
Support implicit type coercion errors in es5-shim
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 10, 2019
1 parent 3ff3db8 commit 1d4d93d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ast/nodes/BinaryExpression.ts
Expand Up @@ -2,6 +2,7 @@ import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ImmutableEntityPathTracker } from '../utils/ImmutableEntityPathTracker';
import { EMPTY_PATH, LiteralValueOrUnknown, ObjectPath, UNKNOWN_VALUE } from '../values';
import ExpressionStatement from './ExpressionStatement';
import { LiteralValue } from './Literal';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
Expand Down Expand Up @@ -60,6 +61,14 @@ export default class BinaryExpression extends NodeBase {
return operatorFn(leftValue as LiteralValue, rightValue as LiteralValue);
}

hasEffects(options: ExecutionPathOptions): boolean {
// support some implicit type coercion runtime errors
if (this.operator === '+' && this.parent instanceof ExpressionStatement) {
return true;
}
return super.hasEffects(options);
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 1;
}
Expand Down
4 changes: 4 additions & 0 deletions test/form/samples/deopt-string-concatenation/_config.js
@@ -0,0 +1,4 @@
module.exports = {
description:
'deoptimize concatenation when used as an expression statement to better support es5-shim'
};
11 changes: 11 additions & 0 deletions test/form/samples/deopt-string-concatenation/_expected.js
@@ -0,0 +1,11 @@
function parseInt(str, radix) {
if (typeof str === 'symbol') {
'' + str;
}

var string = trim(String(str));
var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
return origParseInt(string, defaultedRadix);
}

console.log(parseInt('1'));
11 changes: 11 additions & 0 deletions test/form/samples/deopt-string-concatenation/main.js
@@ -0,0 +1,11 @@
function parseInt(str, radix) {
if (typeof str === 'symbol') {
'' + str;
}

var string = trim(String(str));
var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
return origParseInt(string, defaultedRadix);
}

console.log(parseInt('1'));

0 comments on commit 1d4d93d

Please sign in to comment.