From 41d2084bbd248a40a330f18b5bbc0ee3d795e144 Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Sat, 29 Aug 2020 12:40:59 -0500 Subject: [PATCH] js: Refactor non-zero constant checking out of op.equals --- src/compiler/jsgen.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/compiler/jsgen.js b/src/compiler/jsgen.js index 17d02d8b..6ed577a0 100644 --- a/src/compiler/jsgen.js +++ b/src/compiler/jsgen.js @@ -178,6 +178,19 @@ disableToString(TypedInput.prototype.asString); disableToString(TypedInput.prototype.asBoolean); disableToString(TypedInput.prototype.asUnknown); +/** + * Determine if an input is a constant that is a non-zero number. + * @param {Input} input The input to examine. + * @returns {boolean} true if the input is a constant non-zero number + */ +const isNonZeroNumberConstant = input => { + if (typeof input.constantValue === 'undefined') { + return false; + } + const value = +input.constantValue; + return value !== 0; +}; + class ScriptCompiler { constructor (script, ast, target) { this.script = script; @@ -290,19 +303,11 @@ class ScriptCompiler { } // When one operand is known to be a non-zero constant, we can use === // 0 is not allowed here as NaN will get converted to zero, and "apple or any other NaN value = 0" should not return true. - if (left.isAlwaysNumber()) { - if (typeof left.constantValue !== 'undefined') { - if (+left.constantValue !== 0) { - return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); - } - } + if (left.isAlwaysNumber() && isNonZeroNumberConstant(left)) { + return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); } - if (right.isAlwaysNumber()) { - if (typeof right.constantValue !== 'undefined') { - if (+right.constantValue !== 0) { - return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); - } - } + if (right.isAlwaysNumber() && isNonZeroNumberConstant(right)) { + return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); } return new TypedInput(`compareEqual(${left.asUnknown()}, ${right.asUnknown()})`, TYPE_BOOLEAN); }