| OLD | NEW |
| (Empty) | |
| 1 library angular.core.parser.utils; |
| 2 |
| 3 import 'package:angular/core/parser/syntax.dart' show Expression; |
| 4 export 'package:angular/utils.dart' show relaxFnApply, relaxFnArgs, toBool; |
| 5 |
| 6 /// Marker for an uninitialized value. |
| 7 const UNINITIALIZED = const _Uninitialized(); |
| 8 class _Uninitialized { const _Uninitialized(); } |
| 9 |
| 10 class EvalError { |
| 11 final String message; |
| 12 EvalError(this.message); |
| 13 |
| 14 String unwrap(String input, stack) { |
| 15 String location = (stack == null) ? '' : '\n\nFROM:\n$stack'; |
| 16 return 'Eval Error: $message while evaling [$input]$location'; |
| 17 } |
| 18 } |
| 19 |
| 20 /// Evaluate the [list] in context of the [scope]. |
| 21 List evalList(scope, List<Expression> list) { |
| 22 int length = list.length; |
| 23 for(int cacheLength = _evalListCache.length; cacheLength <= length; cacheLengt
h++) { |
| 24 _evalListCache.add(new List(cacheLength)); |
| 25 } |
| 26 List result = _evalListCache[length]; |
| 27 for (int i = 0; i < length; i++) { |
| 28 result[i] = list[i].eval(scope); |
| 29 } |
| 30 return result; |
| 31 } |
| 32 final List _evalListCache = [[],[0],[0,0],[0,0,0],[0,0,0,0],[0,0,0,0,0]]; |
| 33 |
| 34 /// Add the two arguments with automatic type conversion. |
| 35 autoConvertAdd(a, b) { |
| 36 if (a != null && b != null) { |
| 37 // TODO(deboer): Support others. |
| 38 if (a is String && b is! String) { |
| 39 return a + b.toString(); |
| 40 } |
| 41 if (a is! String && b is String) { |
| 42 return a.toString() + b; |
| 43 } |
| 44 return a + b; |
| 45 } |
| 46 if (a != null) return a; |
| 47 if (b != null) return b; |
| 48 return null; |
| 49 } |
| 50 |
| 51 /** |
| 52 * Ensures that the given [function] is a function and return it. Throws |
| 53 * an [EvalError] if it isn't. |
| 54 */ |
| 55 Function ensureFunction(function, String name) { |
| 56 if (function is Function) return function; |
| 57 if (function == null) { |
| 58 throw new EvalError("Undefined function $name"); |
| 59 } else { |
| 60 throw new EvalError("$name is not a function"); |
| 61 } |
| 62 } |
| 63 |
| 64 /** |
| 65 * Ensures that the map entry with the given [name] is a function and |
| 66 * return it. Throws an [EvalError] if it isn't. |
| 67 */ |
| 68 Function ensureFunctionFromMap(Map map, String name) { |
| 69 return ensureFunction(map[name], name); |
| 70 } |
| 71 |
| 72 /// Get a keyed element from the given [object]. |
| 73 getKeyed(object, key) { |
| 74 if (object is List) { |
| 75 return object[key.toInt()]; |
| 76 } else if (object is Map) { |
| 77 return object["$key"]; // toString dangerous? |
| 78 } else if (object == null) { |
| 79 throw new EvalError('Accessing null object'); |
| 80 } |
| 81 throw new EvalError("Attempted field access on a non-list, non-map"); |
| 82 } |
| 83 |
| 84 /// Set a keyed element in the given [object]. |
| 85 setKeyed(object, key, value) { |
| 86 if (object is List) { |
| 87 int index = key.toInt(); |
| 88 if (object.length <= index) object.length = index + 1; |
| 89 object[index] = value; |
| 90 } else if (object is Map) { |
| 91 object["$key"] = value; // toString dangerous? |
| 92 } else { |
| 93 throw new EvalError("Attempting to set a field on a non-list, non-map"); |
| 94 } |
| 95 return value; |
| 96 } |
| OLD | NEW |