Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Unified Diff: lib/src/codegen/js_codegen.dart

Issue 1047023002: use == and != to enable handling null/undefined (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/runtime/dart_runtime.js ('k') | test/codegen/expect/BenchmarkBase.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/codegen/js_codegen.dart
diff --git a/lib/src/codegen/js_codegen.dart b/lib/src/codegen/js_codegen.dart
index 578a4c886bb96b8fe3d04e64311a6163105d1d93..05ed6e6dc0f2710446e6864946b4e5ec41243931 100644
--- a/lib/src/codegen/js_codegen.dart
+++ b/lib/src/codegen/js_codegen.dart
@@ -1447,11 +1447,9 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
var code;
if (op.type.isEqualityOperator) {
// If we statically know LHS or RHS is null we can generate a clean check.
- // We can also do this if the left hand side is a primitive type, because
- // we know then it doesn't have an overridden.
- if (_isNull(left) || _isNull(right) || typeIsPrimitiveInJS(leftType)) {
- // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-equality-comparison
- code = op.type == TokenType.EQ_EQ ? '# === #' : '# !== #';
+ // We can also do this if both sides are the same primitive type.
+ if (_canUsePrimitiveEquality(left, right)) {
+ code = op.type == TokenType.EQ_EQ ? '# == #' : '# != #';
} else {
var bang = op.type == TokenType.BANG_EQ ? '!' : '';
code = '${bang}dart.equals(#, #)';
@@ -1500,6 +1498,22 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
}
}
+ /// If the type [t] is [int] or [double], returns [num].
+ /// Otherwise returns [t].
+ DartType _toJsNumType(DartType t) {
Jacob 2015/03/30 22:44:24 _toJsNumType --> _canonicalizeNumTypes
+ var numType = rules.provider.numType;
+ if (t is InterfaceType && t.superclass == numType) return numType;
+ return t;
+ }
+
+ bool _canUsePrimitiveEquality(Expression left, Expression right) {
+ if (_isNull(left) || _isNull(right)) return true;
+
+ var leftType = _toJsNumType(rules.getStaticType(left));
+ var rightType = _toJsNumType(rules.getStaticType(right));
+ return _isJSBuiltinType(leftType) && leftType == rightType;
+ }
+
bool _isNull(Expression expr) => expr is NullLiteral;
// TODO(jmesserly, vsm): Refactor this logic.
« no previous file with comments | « lib/runtime/dart_runtime.js ('k') | test/codegen/expect/BenchmarkBase.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698