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

Unified Diff: lib/compiler/implementation/lib/js_helper.dart

Issue 10353014: Start implementing checked mode and tools support for using it. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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
Index: lib/compiler/implementation/lib/js_helper.dart
===================================================================
--- lib/compiler/implementation/lib/js_helper.dart (revision 7269)
+++ lib/compiler/implementation/lib/js_helper.dart (working copy)
@@ -777,3 +777,91 @@
if (target === null) return null;
return JS('var', @'#.builtin$typeInfo', target);
}
+
+/**
+ * The following methods are called by the runtime to implement
+ * checked mode. We specialize each primitive type (eg int, bool), and
+ * use the compiler's convention to do is checks on regular objects.
+ */
+stringTypeCheck(value) {
+ if (value === null) return value;
+ if (value is String) return value;
+ throw new TypeError('$value does not implement String');
+}
+
+doubleTypeCheck(value) {
+ if (value === null) return value;
+ if (value is double) return value;
+ throw new TypeError('$value does not implement double');
+}
+
+numTypeCheck(value) {
+ if (value === null) return value;
+ if (value is num) return value;
+ throw new TypeError('$value does not implement num');
+}
+
+boolTypeCheck(value) {
+ if (value === null) return value;
+ if (value is bool) return value;
+ throw new TypeError('$value does not implement bool');
+}
+
+functionTypeCheck(value) {
+ if (value === null) return value;
+ if (value is Function) return value;
+ throw new TypeError('$value does not implement Function');
+}
+
+intTypeCheck(value) {
+ if (value === null) return value;
+ if (value is int) return value;
+ throw new TypeError('$value does not implement int');
+}
+
+stringSuperTypeCheck(value, property) {
+ if (value === null) return value;
+ if (value is String) return value;
+ if (JS('bool', '!!#[#]', value, property)) return value;
+ String name = property.substring(3, property.length);
floitsch 2012/05/07 09:50:07 explain what is cut off. maybe assert that the pro
ahe 2012/05/07 11:59:57 Be careful about asserting anything in this code.
ngeoffray 2012/05/07 13:15:42 Very good point.
+ throw new TypeError('$value does not implement $name');
+}
+
+listTypeCheck(value) {
+ if (value === null) return value;
+ if (value is List) return value;
+ throw new TypeError('$value does not implement List');
+}
+
+listSuperTypeCheck(value, property) {
+ if (value === null) return value;
+ if (value is List) return value;
+ if (JS('bool', '!!#[#]', value, property)) return value;
+ String name = property.substring(3, property.length);
+ throw new TypeError('$value does not implement $name');
+}
+
+listSuperNativeTypeCheck(value, property) {
floitsch 2012/05/07 09:50:07 Explain what this does.
ngeoffray 2012/05/07 13:15:42 Done.
+ if (value === null) return value;
+ if (value is List) return value;
+ if (JS('bool', '#.#()', value, property)) return value;
floitsch 2012/05/07 11:06:56 #.# won't work.
ngeoffray 2012/05/07 13:15:42 Done.
+ String name = property.substring(3, property.length);
+ throw new TypeError('$value does not implement $name');
+}
+
+propertyTypeCheck(value, property) {
floitsch 2012/05/07 09:50:07 ditto.
ngeoffray 2012/05/07 13:15:42 Done.
+ if (value === null) return value;
+ if (JS('bool', '!!#[#]', value, property)) return value;
+ String name = property.substring(3, property.length);
+ throw new TypeError('$value does not implement $name');
+}
+
+callTypeCheck(value, property) {
floitsch 2012/05/07 09:50:07 ditto.
ngeoffray 2012/05/07 13:15:42 Done.
+ if (value === null) return value;
+ if ((JS('String', 'typeof #', value) === 'object')
+ && JS('bool', '#.#()', value, property)) {
floitsch 2012/05/07 11:06:56 #.# won't work.
ngeoffray 2012/05/07 13:15:42 Done.
+ return value;
+ }
+ String name = property.substring(3, property.length);
+ throw new TypeError('$value does not implement $name');
+}

Powered by Google App Engine
This is Rietveld 408576698