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

Side by Side Diff: frog/value.dart

Issue 8534001: Adds typechecking of return values (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merged Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 /** 5 /**
6 * Represents a meta-value for code generation. 6 * Represents a meta-value for code generation.
7 */ 7 */
8 class Value { 8 class Value {
9 /** The [Type] of the [Value]. */ 9 /** The [Type] of the [Value]. */
10 Type type; 10 Type type;
(...skipping 19 matching lines...) Expand all
30 30
31 Value(this.type, this.code, this.span, 31 Value(this.type, this.code, this.span,
32 // TODO(sigmund): reorder, so that needsTemp comes first. 32 // TODO(sigmund): reorder, so that needsTemp comes first.
33 [this.isSuper = false, this.needsTemp = true, this.isType = false]) { 33 [this.isSuper = false, this.needsTemp = true, this.isType = false]) {
34 if (type == null) type = world.varType; 34 if (type == null) type = world.varType;
35 } 35 }
36 36
37 /** Is this value a constant expression? */ 37 /** Is this value a constant expression? */
38 bool get isConst() => false; 38 bool get isConst() => false;
39 39
40 /**
41 * A canonicalized form of the code. Two const expressions that result in the
42 * same instance should have the same [canonicalCode].
43 */
44 String get canonicalCode() => null;
Siggi Cherem (dart-lang) 2011/11/11 08:23:53 maybe this should throw an error in this case?
45
40 // TODO(jimhug): Fix these names once get/set are truly pseudo-keywords. 46 // TODO(jimhug): Fix these names once get/set are truly pseudo-keywords.
41 // See issue #379. 47 // See issue #379.
42 Value get_(MethodGenerator context, String name, Node node) { 48 Value get_(MethodGenerator context, String name, Node node) {
43 final member = _resolveMember(context, name, node); 49 final member = _resolveMember(context, name, node);
44 if (member != null) { 50 if (member != null) {
45 return member._get(context, node, this); 51 return member._get(context, node, this);
46 } else { 52 } else {
47 return invokeNoSuchMethod(context, 'get:$name', node); 53 return invokeNoSuchMethod(context, 'get:$name', node);
48 } 54 }
49 } 55 }
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 // TODO(jmesserly): better assert for integers? 327 // TODO(jmesserly): better assert for integers?
322 if (toType.isNum) toType = world.numType; 328 if (toType.isNum) toType = world.numType;
323 329
324 // Generate a check like these: 330 // Generate a check like these:
325 // obj && obj.is$TypeName() 331 // obj && obj.is$TypeName()
326 // $assert_int(obj) 332 // $assert_int(obj)
327 // 333 //
328 // We rely on the fact that calling an undefined method produces a JS 334 // We rely on the fact that calling an undefined method produces a JS
329 // TypeError. Alternatively we could define fallbacks on Object that throw. 335 // TypeError. Alternatively we could define fallbacks on Object that throw.
330 String check; 336 String check;
331 if (toType.library.isCore && toType.typeofName != null) { 337 if (toType.isVoid) {
338 check = '\$assert_void($code)';
339 if (toType.typeCheckCode == null) {
jimhug 2011/11/11 15:02:02 Hmm, I'd rather not see this code sitting in value
340 toType.typeCheckCode = '''
341 function \$assert_void(x) {
342 return x == null ? x : x.is\$void(); // throws TypeError
343 }''';
344 }
345 } else if (toType.library.isCore && toType.typeofName != null) {
332 check = '\$assert_${toType.name}($code)'; 346 check = '\$assert_${toType.name}($code)';
333 347
334 if (toType.typeCheckCode == null) { 348 if (toType.typeCheckCode == null) {
335 toType.typeCheckCode = ''' 349 toType.typeCheckCode = '''
336 function \$assert_${toType.name}(x) { 350 function \$assert_${toType.name}(x) {
337 if (x == null || typeof(x) == "${toType.typeofName}") return x; 351 if (x == null || typeof(x) == "${toType.typeofName}") return x;
338 throw new TypeError("'" + x + "' is not a ${toType.name}."); 352 throw new TypeError("'" + x + "' is not a ${toType.name}.");
339 }'''; 353 }''';
340 } 354 }
341 } else { 355 } else {
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 679
666 // Then look for members in my library. 680 // Then look for members in my library.
667 member = home.library.lookup(name, span); 681 member = home.library.lookup(name, span);
668 if (member != null) { 682 if (member != null) {
669 return member; 683 return member;
670 } 684 }
671 685
672 return null; 686 return null;
673 } 687 }
674 } 688 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698