Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Expand x==null using a disjunction (#2572)
Browse files Browse the repository at this point in the history
Summary:
Fixes #2563, #2564
Pull Request resolved: #2572

Differential Revision: D10027955

Pulled By: sb98052

fbshipit-source-id: 4023e3c32362cb95f477d4d990dd9b8edb5a963c
  • Loading branch information
Sapan Bhatia authored and facebook-github-bot committed Sep 25, 2018
1 parent 7f3b5d4 commit 9681e2e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,24 @@ function pushPathCondition(condition: Value): void {
let right = condition.args[1];
if (left instanceof ConcreteValue && right instanceof AbstractValue) [left, right] = [right, left];
if (left instanceof AbstractValue && (right instanceof UndefinedValue || right instanceof NullValue)) {
let op = condition.kind === "!=" ? "!==" : "===";
if (op === "!==") pushPathCondition(left);
else pushInversePathCondition(left);
let leftNeNull = AbstractValue.createFromBinaryOp(realm, op, left, realm.intrinsics.null);
if (leftNeNull.mightNotBeFalse()) pushPathCondition(leftNeNull);
let leftNeUndefined = AbstractValue.createFromBinaryOp(realm, op, left, realm.intrinsics.undefined);
if (leftNeUndefined.mightNotBeFalse()) pushPathCondition(leftNeUndefined);
if (condition.kind === "!=") {
// x != null => x!==null && x!==undefined
pushPathCondition(left);
let leftNeNull = AbstractValue.createFromBinaryOp(realm, "!==", left, realm.intrinsics.null);
let leftNeUndefined = AbstractValue.createFromBinaryOp(realm, "!==", left, realm.intrinsics.undefined);
pushPathCondition(leftNeNull);
pushPathCondition(leftNeUndefined);
} else if (condition.kind === "==") {
// x == null => x===null || x===undefined
pushInversePathCondition(left);
let leftEqNull = AbstractValue.createFromBinaryOp(realm, "===", left, realm.intrinsics.null);
let leftEqUndefined = AbstractValue.createFromBinaryOp(realm, "===", left, realm.intrinsics.undefined);
let c;
if (!leftEqNull.mightNotBeFalse()) c = leftEqUndefined;
else if (!leftEqUndefined.mightNotBeFalse()) c = leftEqNull;
else c = AbstractValue.createFromLogicalOp(realm, "||", leftEqNull, leftEqUndefined);
pushPathCondition(c);
}
return;
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/serializer/optimizations/simplifyCompNullOrUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let x = global.__abstract ? __abstract(undefined, "undefined") : undefined;

if (x == null) global.result = x === null;

inspect = () => global.result;
7 changes: 7 additions & 0 deletions test/serializer/optimizations/simplifyCompNullOrUndefined2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// does contain result = false

let x = global.__abstract ? __abstract(undefined, "undefined") : undefined;

if (x != null) global.result = x === undefined;

inspect = () => global.result;
8 changes: 8 additions & 0 deletions test/serializer/optimizations/simplifyCompNullOrUndefined3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function f(obj) {
let y = obj.foo;
if (y == null) {
return y === undefined;
}
}
global.__optimize && __optimize(f);
inspect = () => f({ foo: null });

0 comments on commit 9681e2e

Please sign in to comment.