Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 23aae00

Browse files
author
John Messerly
committed
implement notNull checking for num -> int and num -> double
[email protected] Review URL: https://chromereviews.googleplex.com/144347013
1 parent 57dabc5 commit 23aae00

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

lib/runtime/dart_runtime.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ var dart;
8686
}
8787
dart.equals = equals;
8888

89+
/** Checks that `x` is not null or undefined. */
90+
function notNull(x) {
91+
if (x == null) throw 'expected not-null value';
92+
return x;
93+
}
94+
dart.notNull = notNull;
95+
8996
/**
9097
* Defines a lazy property.
9198
* After initial get or set, it will replace itself with a value property.

lib/src/checker/rules.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class TypeRules {
2525
bool isBoolType(DartType t) => t == provider.boolType;
2626
bool isDoubleType(DartType t) => t == provider.doubleType;
2727
bool isIntType(DartType t) => t == provider.intType;
28-
bool isNumType(DartType t) => t.name == "num";
28+
bool isNumType(DartType t) => t == provider.intType.superclass;
2929

3030
StaticInfo checkAssignment(Expression expr, DartType t);
3131

lib/src/codegen/js_codegen.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ var $_libraryName;
9595
/// Conversions that we don't handle end up here.
9696
@override
9797
void visitConversion(Conversion node) {
98+
var from = node.baseType, to = node.convertedType;
99+
100+
// num to int or num to double is just a null check.
101+
if (rules.isNumType(from) &&
102+
(rules.isIntType(to) || rules.isDoubleType(to))) {
103+
// TODO(jmesserly): a lot of these checks are meaningless, as people use
104+
// `num` to mean "any kind of number" rather than "could be null".
105+
// The core libraries especially suffer from this problem, with many of
106+
// the `num` methods returning `num`.
107+
out.write('dart.notNull(');
108+
node.expression.accept(this);
109+
out.write(')');
110+
return;
111+
}
112+
98113
out.write('/* Unimplemented: ');
99114
out.write('${node.description}');
100115
out.write(' */ ');

test/codegen/expect/core/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ var core;
367367
/* Unimplemented FunctionDeclarationStatement: String sixDigits(int n) {if (n >= 100000) return "$n"; if (n >= 10000) return "0$n"; if (n >= 1000) return "00$n"; if (n >= 100) return "000$n"; if (n >= 10) return "0000$n"; return "00000$n";} *//* Unimplemented FunctionDeclarationStatement: String twoDigits(int n) {if (n >= 10) return "$n"; return "0$n";} */if (this.inMicroseconds < 0) {
368368
return "-" + (/* Unimplemented postfix operator: -this */) + "";
369369
}
370-
let twoDigitMinutes = twoDigits(/* Unimplemented: DownCast: num to int */ this.inMinutes.remainder(MINUTES_PER_HOUR));
371-
let twoDigitSeconds = twoDigits(/* Unimplemented: DownCast: num to int */ this.inSeconds.remainder(SECONDS_PER_MINUTE));
372-
let sixDigitUs = sixDigits(/* Unimplemented: DownCast: num to int */ this.inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
370+
let twoDigitMinutes = twoDigits(dart.notNull(this.inMinutes.remainder(MINUTES_PER_HOUR)));
371+
let twoDigitSeconds = twoDigits(dart.notNull(this.inSeconds.remainder(SECONDS_PER_MINUTE)));
372+
let sixDigitUs = sixDigits(dart.notNull(this.inMicroseconds.remainder(MICROSECONDS_PER_SECOND)));
373373
return "" + (this.inHours) + ":" + (twoDigitMinutes) + ":" + (twoDigitSeconds) + "." + (sixDigitUs) + "";
374374
}
375375
get isNegative() { return this._duration < 0; }

0 commit comments

Comments
 (0)