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

Side by Side Diff: lib/runtime/dart_runtime.js

Issue 1177233004: Clean up runtime type code (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Format Created 5 years, 6 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 var dart, dartx; 5 var dart, dartx;
6 (function (dart) { 6 (function (dart) {
7 'use strict'; 7 'use strict';
8 8
9 const defineProperty = Object.defineProperty; 9 const defineProperty = Object.defineProperty;
10 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; 10 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 function dindex(obj, index) { 130 function dindex(obj, index) {
131 return callMethod(obj, 'get', [index], '[]'); 131 return callMethod(obj, 'get', [index], '[]');
132 } 132 }
133 dart.dindex = dindex; 133 dart.dindex = dindex;
134 134
135 function dsetindex(obj, index, value) { 135 function dsetindex(obj, index, value) {
136 return callMethod(obj, 'set', [index, value], '[]='); 136 return callMethod(obj, 'set', [index, value], '[]=');
137 } 137 }
138 dart.dsetindex = dsetindex; 138 dart.dsetindex = dsetindex;
139 139
140 function typeToString(type) { 140 function _typeName(type) {
141 if (typeof(type) == "function") { 141 if (typeof(type) == "function") {
142 let name = type.name; 142 let name = type.name;
143 let args = type[dart.typeArguments]; 143 let args = type[dart.typeArguments];
144 if (args) { 144 if (args) {
145 name += '<'; 145 name += '<';
146 for (let i = 0; i < args.length; ++i) { 146 for (let i = 0; i < args.length; ++i) {
147 if (i > 0) name += ', '; 147 if (i > 0) name += ', ';
148 name += typeToString(args[i]); 148 name += _typeName(args[i]);
149 } 149 }
150 name += '>'; 150 name += '>';
151 } 151 }
152 return name; 152 return name;
153 } else { 153 } else {
154 return type.toString(); 154 return type.toString();
155 } 155 }
156 } 156 }
157 dart.typeName = typeToString; 157 dart.typeName = _typeName;
158
159 function _ignoreTypeFailure(actual, type) {
160 // TODO(vsm): Remove this hack ...
161 // This is primarily due to the lack of generic methods,
162 // but we need to triage all the errors.
163 if (isSubtype(type, core.Iterable) && isSubtype(actual, core.Iterable) ||
vsm 2015/06/11 16:30:26 Ideally, this would do still do a regular Dart sub
164 isSubtype(type, async.Future) && isSubtype(actual, async.Future) ||
165 isSubtype(type, core.Map) && isSubtype(actual, core.Map) ||
166 isSubtype(type, core.Function) && isSubtype(actual, core.Function)) {
167 console.error('Ignoring cast fail from ' + _typeName(actual) +
168 ' to ' + _typeName(type));
169 return true;
170 }
171 return false;
172 }
158 173
159 function cast(obj, type) { 174 function cast(obj, type) {
160 // TODO(vsm): handle non-nullable types 175 // TODO(vsm): handle non-nullable types
161 if (obj == null) return obj; 176 if (obj == null) return obj;
162 let actual = realRuntimeType(obj); 177 let actual = realRuntimeType(obj);
163 if (isSubtype(actual, type)) return obj; 178 if (isSubtype(actual, type)) return obj;
164 // TODO(vsm): Remove this hack ... due to 179 if (_ignoreTypeFailure(actual, type)) return obj;
165 // lack of generic methods.
166 if (isSubtype(type, core.Iterable) && isSubtype(actual, core.Iterable) ||
167 isSubtype(type, async.Future) && isSubtype(actual, async.Future) ||
168 isSubtype(type, core.Map) && isSubtype(actual, core.Map)) {
169 console.log('Warning: ignoring cast fail from ' + typeToString(actual) + ' to ' + typeToString(type));
170 return obj;
171 }
172 // console.log('Error: cast fail from ' + typeToString(actual) + ' to ' + ty peToString(type));
173 throw new _js_helper.CastErrorImplementation(actual, type); 180 throw new _js_helper.CastErrorImplementation(actual, type);
174 } 181 }
175 dart.as = cast; 182 dart.as = cast;
176 183
177 184
178 // TODO(vsm): How should we encode the runtime type? 185 // TODO(vsm): How should we encode the runtime type?
179 const _runtimeType = Symbol('_runtimeType'); 186 const _runtimeType = Symbol('_runtimeType');
180 187
181 function checkPrimitiveType(obj) { 188 function checkPrimitiveType(obj) {
182 switch (typeof obj) { 189 switch (typeof obj) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 return result; 224 return result;
218 } 225 }
219 dart.realRuntimeType = realRuntimeType; 226 dart.realRuntimeType = realRuntimeType;
220 227
221 function instanceOf(obj, type) { 228 function instanceOf(obj, type) {
222 return isSubtype(realRuntimeType(obj), type); 229 return isSubtype(realRuntimeType(obj), type);
223 } 230 }
224 dart.is = instanceOf; 231 dart.is = instanceOf;
225 232
226 function instanceOfOrNull(obj, type) { 233 function instanceOfOrNull(obj, type) {
227 return (obj == null) || instanceOf(obj, type); 234 // FIXME(vsm): This is used only in checkApply.
235 // Just log failures due to generics for now.
236 if ((obj == null) || instanceOf(obj, type)) return true;
237 let actual = realRuntimeType(obj);
238 if (_ignoreTypeFailure(actual, type)) return true;
239 return false;
228 } 240 }
229 241
230 /** 242 /**
231 * Computes the canonical type. 243 * Computes the canonical type.
232 * This maps JS types onto their corresponding Dart Type. 244 * This maps JS types onto their corresponding Dart Type.
233 */ 245 */
234 // TODO(jmesserly): lots more needs to be done here. 246 // TODO(jmesserly): lots more needs to be done here.
235 function canonicalType(t) { 247 function canonicalType(t) {
236 if (t === Object) return core.Object; 248 if (t === Object) return core.Object;
237 if (t === Function) return core.Function; 249 if (t === Function) return core.Function;
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 } 439 }
428 dart.equals = equals; 440 dart.equals = equals;
429 441
430 /** Checks that `x` is not null or undefined. */ 442 /** Checks that `x` is not null or undefined. */
431 function notNull(x) { 443 function notNull(x) {
432 if (x == null) throwRuntimeError('expected not-null value'); 444 if (x == null) throwRuntimeError('expected not-null value');
433 return x; 445 return x;
434 } 446 }
435 dart.notNull = notNull; 447 dart.notNull = notNull;
436 448
437 function _typeName(type) {
438 if (type === void 0) throwRuntimeError('Undefined type');
439 let name = type.name;
440 if (!name) throwRuntimeError('Unexpected type: ' + type);
441 return name;
442 }
443
444 class AbstractFunctionType { 449 class AbstractFunctionType {
445 constructor() { 450 constructor() {
446 this._stringValue = null; 451 this._stringValue = null;
447 } 452 }
448 453
449 /// Check that a function of this type can be applied to 454 /// Check that a function of this type can be applied to
450 /// actuals. 455 /// actuals.
451 checkApply(actuals) { 456 checkApply(actuals) {
452 if (actuals.length < this.args.length) return false; 457 if (actuals.length < this.args.length) return false;
453 let index = 0; 458 let index = 0;
(...skipping 980 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 1439
1435 // TODO(vsm): This is referenced (as init.globalState) from 1440 // TODO(vsm): This is referenced (as init.globalState) from
1436 // isolate_helper.dart. Where should it go? 1441 // isolate_helper.dart. Where should it go?
1437 // See: https://github.com/dart-lang/dev_compiler/issues/164 1442 // See: https://github.com/dart-lang/dev_compiler/issues/164
1438 dart.globalState = null; 1443 dart.globalState = null;
1439 1444
1440 /** Dart extension members. */ 1445 /** Dart extension members. */
1441 dartx = dartx || {}; 1446 dartx = dartx || {};
1442 } 1447 }
1443 })(dart || (dart = {})); 1448 })(dart || (dart = {}));
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698