diff --git a/src/ast/nodes/BinaryExpression.ts b/src/ast/nodes/BinaryExpression.ts index 66e657949ff..4a53a8b6bcd 100644 --- a/src/ast/nodes/BinaryExpression.ts +++ b/src/ast/nodes/BinaryExpression.ts @@ -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'; @@ -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; } diff --git a/test/form/samples/deopt-string-concatenation/_config.js b/test/form/samples/deopt-string-concatenation/_config.js new file mode 100644 index 00000000000..36697ba2394 --- /dev/null +++ b/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' +}; diff --git a/test/form/samples/deopt-string-concatenation/_expected.js b/test/form/samples/deopt-string-concatenation/_expected.js new file mode 100644 index 00000000000..68a30dc4f66 --- /dev/null +++ b/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')); diff --git a/test/form/samples/deopt-string-concatenation/main.js b/test/form/samples/deopt-string-concatenation/main.js new file mode 100644 index 00000000000..68a30dc4f66 --- /dev/null +++ b/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'));