OLD | NEW |
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 /* This library defines runtime operations on objects used by the code | 5 /* This library defines runtime operations on objects used by the code |
6 * generator. | 6 * generator. |
7 */ | 7 */ |
8 dart_library.library('dart_runtime/_operations', null, /* Imports */[ | 8 dart_library.library('dart_runtime/_operations', null, /* Imports */[ |
9 ], /* Lazy Imports */[ | 9 ], /* Lazy Imports */[ |
10 'dart/async', | 10 'dart/async', |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 return result; | 56 return result; |
57 } | 57 } |
58 exports.dload = dload; | 58 exports.dload = dload; |
59 | 59 |
60 function dput(obj, field, value) { | 60 function dput(obj, field, value) { |
61 field = _canonicalFieldName(obj, field, [value], field); | 61 field = _canonicalFieldName(obj, field, [value], field); |
62 // TODO(vsm): Implement NSM and type checks. | 62 // TODO(vsm): Implement NSM and type checks. |
63 // See: https://github.com/dart-lang/dev_compiler/issues/170 | 63 // See: https://github.com/dart-lang/dev_compiler/issues/170 |
64 obj[field] = value; | 64 obj[field] = value; |
| 65 return value; |
65 } | 66 } |
66 exports.dput = dput; | 67 exports.dput = dput; |
67 | 68 |
68 | 69 |
69 /// Check that a function of a given type can be applied to | 70 /// Check that a function of a given type can be applied to |
70 /// actuals. | 71 /// actuals. |
71 function checkApply(type, actuals) { | 72 function checkApply(type, actuals) { |
72 if (actuals.length < type.args.length) return false; | 73 if (actuals.length < type.args.length) return false; |
73 let index = 0; | 74 let index = 0; |
74 for(let i = 0; i < type.args.length; ++i) { | 75 for(let i = 0; i < type.args.length; ++i) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return callMethod(obj, method, slice.call(arguments, 2), method); | 162 return callMethod(obj, method, slice.call(arguments, 2), method); |
162 } | 163 } |
163 exports.dsend = dsend; | 164 exports.dsend = dsend; |
164 | 165 |
165 function dindex(obj, index) { | 166 function dindex(obj, index) { |
166 return callMethod(obj, 'get', [index], '[]'); | 167 return callMethod(obj, 'get', [index], '[]'); |
167 } | 168 } |
168 exports.dindex = dindex; | 169 exports.dindex = dindex; |
169 | 170 |
170 function dsetindex(obj, index, value) { | 171 function dsetindex(obj, index, value) { |
171 return callMethod(obj, 'set', [index, value], '[]='); | 172 callMethod(obj, 'set', [index, value], '[]='); |
| 173 return value; |
172 } | 174 } |
173 exports.dsetindex = dsetindex; | 175 exports.dsetindex = dsetindex; |
174 | 176 |
175 function _ignoreTypeFailure(actual, type) { | 177 function _ignoreTypeFailure(actual, type) { |
176 // TODO(vsm): Remove this hack ... | 178 // TODO(vsm): Remove this hack ... |
177 // This is primarily due to the lack of generic methods, | 179 // This is primarily due to the lack of generic methods, |
178 // but we need to triage all the errors. | 180 // but we need to triage all the errors. |
179 let isSubtype = types.isSubtype; | 181 let isSubtype = types.isSubtype; |
180 if (isSubtype(type, core.Iterable) && isSubtype(actual, core.Iterable) || | 182 if (isSubtype(type, core.Iterable) && isSubtype(actual, core.Iterable) || |
181 isSubtype(type, async.Future) && isSubtype(actual, async.Future) || | 183 isSubtype(type, async.Future) && isSubtype(actual, async.Future) || |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 console.log(error.stack ? error.stack : 'No stack trace for: ' + error); | 313 console.log(error.stack ? error.stack : 'No stack trace for: ' + error); |
312 } | 314 } |
313 exports.stackPrint = stackPrint; | 315 exports.stackPrint = stackPrint; |
314 | 316 |
315 function stackTrace(exception) { | 317 function stackTrace(exception) { |
316 var error = getError(exception); | 318 var error = getError(exception); |
317 return _js_helper.getTraceFromException(error); | 319 return _js_helper.getTraceFromException(error); |
318 } | 320 } |
319 exports.stackTrace = stackTrace; | 321 exports.stackTrace = stackTrace; |
320 | 322 |
| 323 /** |
| 324 * Implements a sequence of .? operations. |
| 325 * |
| 326 * Will call each successive callback, unless one returns null, which stops |
| 327 * the sequence. |
| 328 */ |
| 329 function nullSafe(obj /*, ...callbacks*/) { |
| 330 let callbacks = slice.call(arguments, 1); |
| 331 if (obj == null) return obj; |
| 332 for (const callback of callbacks) { |
| 333 obj = callback(obj); |
| 334 if (obj == null) break; |
| 335 } |
| 336 return obj; |
| 337 } |
| 338 exports.nullSafe = nullSafe; |
| 339 |
321 let _value = Symbol('_value'); | 340 let _value = Symbol('_value'); |
322 /** | 341 /** |
323 * Looks up a sequence of [keys] in [map], recursively, and | 342 * Looks up a sequence of [keys] in [map], recursively, and |
324 * returns the result. If the value is not found, [valueFn] will be called to | 343 * returns the result. If the value is not found, [valueFn] will be called to |
325 * add it. For example: | 344 * add it. For example: |
326 * | 345 * |
327 * let map = new Map(); | 346 * let map = new Map(); |
328 * putIfAbsent(map, [1, 2, 'hi ', 'there '], () => 'world'); | 347 * putIfAbsent(map, [1, 2, 'hi ', 'there '], () => 'world'); |
329 * | 348 * |
330 * ... will create a Map with a structure like: | 349 * ... will create a Map with a structure like: |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 next() { | 448 next() { |
430 let i = this.dartIterator; | 449 let i = this.dartIterator; |
431 let done = !i.moveNext(); | 450 let done = !i.moveNext(); |
432 return { done: done, value: done ? void 0 : i.current }; | 451 return { done: done, value: done ? void 0 : i.current }; |
433 } | 452 } |
434 } | 453 } |
435 exports.JsIterator = JsIterator; | 454 exports.JsIterator = JsIterator; |
436 | 455 |
437 | 456 |
438 }); | 457 }); |
OLD | NEW |