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

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, 7 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/compiler/implementation/dart2js.dart ('k') | lib/compiler/implementation/lib/mock.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/lib/js_helper.dart
===================================================================
--- lib/compiler/implementation/lib/js_helper.dart (revision 7371)
+++ lib/compiler/implementation/lib/js_helper.dart (working copy)
@@ -261,8 +261,6 @@
}
String stringConcat(String receiver, String other) {
- assert(receiver is String);
- assert(other is String);
return JS('String', @'# + #', receiver, other);
}
@@ -779,3 +777,117 @@
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');
+}
+
+void propertyTypeError(value, property) {
+ // Cuts the property name to the class name.
+ String name = property.substring(3, property.length);
+ throw new TypeError('$value does not implement $name');
+}
+
+/**
+ * For types that are not supertypes of native (eg DOM) types,
+ * we emit a simple property check to check that an object implements
+ * that type.
+ */
+propertyTypeCheck(value, property) {
+ if (value === null) return value;
+ if (JS('bool', '!!#[#]', value, property)) return value;
+ propertyTypeError(value, property);
+}
+
+/**
+ * For types that are supertypes of native (eg DOM) types, we emit a
+ * call because we cannot add a JS property to their prototype at load
+ * time.
+ */
+callTypeCheck(value, property) {
+ if (value === null) return value;
+ if ((JS('String', 'typeof #', value) === 'object')
+ && JS('bool', '#[#]()', value, property)) {
+ return value;
+ }
+ propertyTypeError(value, property);
+}
+
+/**
+ * Specialization of the type check for String and its supertype
+ * since [value] can be a JS primitive.
+ */
+stringSuperTypeCheck(value, property) {
+ if (value === null) return value;
+ if (value is String) return value;
+ if (JS('bool', '!!#[#]', value, property)) return value;
+ propertyTypeError(value, property);
+}
+
+stringSuperNativeTypeCheck(value, property) {
+ if (value === null) return value;
+ if (value is String) return value;
+ if (JS('bool', '#[#]()', value, property)) return value;
+ propertyTypeError(value, property);
+}
+
+/**
+ * Specialization of the type check for List and its supertypes,
+ * since [value] can be a JS array.
+ */
+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;
+ propertyTypeError(value, property);
+}
+
+listSuperNativeTypeCheck(value, property) {
+ if (value === null) return value;
+ if (value is List) return value;
+ if (JS('bool', '#[#]()', value, property)) return value;
+ propertyTypeError(value, property);
+}
« no previous file with comments | « lib/compiler/implementation/dart2js.dart ('k') | lib/compiler/implementation/lib/mock.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698