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

Side by Side Diff: test/generated_sdk/lib/async/zone.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 part of dart.async;
6
7 typedef dynamic ZoneCallback();
8 typedef dynamic ZoneUnaryCallback(arg);
9 typedef dynamic ZoneBinaryCallback(arg1, arg2);
10
11 typedef dynamic HandleUncaughtErrorHandler(
12 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace);
13 typedef dynamic RunHandler(Zone self, ZoneDelegate parent, Zone zone, f());
14 typedef dynamic RunUnaryHandler(
15 Zone self, ZoneDelegate parent, Zone zone, f(arg), arg);
16 typedef dynamic RunBinaryHandler(
17 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2), arg1, arg2);
18 typedef ZoneCallback RegisterCallbackHandler(
19 Zone self, ZoneDelegate parent, Zone zone, f());
20 typedef ZoneUnaryCallback RegisterUnaryCallbackHandler(
21 Zone self, ZoneDelegate parent, Zone zone, f(arg));
22 typedef ZoneBinaryCallback RegisterBinaryCallbackHandler(
23 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2));
24 typedef AsyncError ErrorCallbackHandler(Zone self, ZoneDelegate parent,
25 Zone zone, Object error, StackTrace stackTrace);
26 typedef void ScheduleMicrotaskHandler(
27 Zone self, ZoneDelegate parent, Zone zone, f());
28 typedef Timer CreateTimerHandler(
29 Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f());
30 typedef Timer CreatePeriodicTimerHandler(
31 Zone self, ZoneDelegate parent, Zone zone,
32 Duration period, void f(Timer timer));
33 typedef void PrintHandler(
34 Zone self, ZoneDelegate parent, Zone zone, String line);
35 typedef Zone ForkHandler(Zone self, ZoneDelegate parent, Zone zone,
36 ZoneSpecification specification,
37 Map zoneValues);
38
39 /** Pair of error and stack trace. Returned by [Zone.errorCallback]. */
40 class AsyncError implements Error {
41 final error;
42 final StackTrace stackTrace;
43
44 AsyncError(this.error, this.stackTrace);
45
46 String toString() => error.toString();
47 }
48
49
50 class _ZoneFunction {
51 final _Zone zone;
52 final Function function;
53 const _ZoneFunction(this.zone, this.function);
54 }
55
56 /**
57 * This class provides the specification for a forked zone.
58 *
59 * When forking a new zone (see [Zone.fork]) one can override the default
60 * behavior of the zone by providing callbacks. These callbacks must be
61 * given in an instance of this class.
62 *
63 * Handlers have the same signature as the same-named methods on [Zone] but
64 * receive three additional arguments:
65 *
66 * 1. the zone the handlers are attached to (the "self" zone).
67 * 2. a [ZoneDelegate] to the parent zone.
68 * 3. the zone that first received the request (before the request was
69 * bubbled up).
70 *
71 * Handlers can either stop propagation the request (by simply not calling the
72 * parent handler), or forward to the parent zone, potentially modifying the
73 * arguments on the way.
74 */
75 abstract class ZoneSpecification {
76 /**
77 * Creates a specification with the provided handlers.
78 */
79 const factory ZoneSpecification({
80 dynamic handleUncaughtError(Zone self, ZoneDelegate parent, Zone zone,
81 error, StackTrace stackTrace),
82 dynamic run(Zone self, ZoneDelegate parent, Zone zone, f()),
83 dynamic runUnary(
84 Zone self, ZoneDelegate parent, Zone zone, f(arg), arg),
85 dynamic runBinary(Zone self, ZoneDelegate parent, Zone zone,
86 f(arg1, arg2), arg1, arg2),
87 ZoneCallback registerCallback(
88 Zone self, ZoneDelegate parent, Zone zone, f()),
89 ZoneUnaryCallback registerUnaryCallback(
90 Zone self, ZoneDelegate parent, Zone zone, f(arg)),
91 ZoneBinaryCallback registerBinaryCallback(
92 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2)),
93 AsyncError errorCallback(Zone self, ZoneDelegate parent, Zone zone,
94 Object error, StackTrace stackTrace),
95 void scheduleMicrotask(
96 Zone self, ZoneDelegate parent, Zone zone, f()),
97 Timer createTimer(Zone self, ZoneDelegate parent, Zone zone,
98 Duration duration, void f()),
99 Timer createPeriodicTimer(Zone self, ZoneDelegate parent, Zone zone,
100 Duration period, void f(Timer timer)),
101 void print(Zone self, ZoneDelegate parent, Zone zone, String line),
102 Zone fork(Zone self, ZoneDelegate parent, Zone zone,
103 ZoneSpecification specification, Map zoneValues)
104 }) = _ZoneSpecification;
105
106 /**
107 * Creates a specification from [other] with the provided handlers overriding
108 * the ones in [other].
109 */
110 factory ZoneSpecification.from(ZoneSpecification other, {
111 dynamic handleUncaughtError(Zone self, ZoneDelegate parent, Zone zone,
112 error, StackTrace stackTrace): null,
113 dynamic run(Zone self, ZoneDelegate parent, Zone zone, f()): null,
114 dynamic runUnary(
115 Zone self, ZoneDelegate parent, Zone zone, f(arg), arg): null,
116 dynamic runBinary(Zone self, ZoneDelegate parent, Zone zone,
117 f(arg1, arg2), arg1, arg2): null,
118 ZoneCallback registerCallback(
119 Zone self, ZoneDelegate parent, Zone zone, f()): null,
120 ZoneUnaryCallback registerUnaryCallback(
121 Zone self, ZoneDelegate parent, Zone zone, f(arg)): null,
122 ZoneBinaryCallback registerBinaryCallback(
123 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2)): null,
124 AsyncError errorCallback(Zone self, ZoneDelegate parent, Zone zone,
125 Object error, StackTrace stackTrace),
126 void scheduleMicrotask(
127 Zone self, ZoneDelegate parent, Zone zone, f()): null,
128 Timer createTimer(Zone self, ZoneDelegate parent, Zone zone,
129 Duration duration, void f()): null,
130 Timer createPeriodicTimer(Zone self, ZoneDelegate parent, Zone zone,
131 Duration period, void f(Timer timer)): null,
132 void print(Zone self, ZoneDelegate parent, Zone zone, String line): null,
133 Zone fork(Zone self, ZoneDelegate parent, Zone zone,
134 ZoneSpecification specification,
135 Map zoneValues): null
136 }) {
137 return new ZoneSpecification(
138 handleUncaughtError: handleUncaughtError != null
139 ? handleUncaughtError
140 : other.handleUncaughtError,
141 run: run != null ? run : other.run,
142 runUnary: runUnary != null ? runUnary : other.runUnary,
143 runBinary: runBinary != null ? runBinary : other.runBinary,
144 registerCallback: registerCallback != null
145 ? registerCallback
146 : other.registerCallback,
147 registerUnaryCallback: registerUnaryCallback != null
148 ? registerUnaryCallback
149 : other.registerUnaryCallback,
150 registerBinaryCallback: registerBinaryCallback != null
151 ? registerBinaryCallback
152 : other.registerBinaryCallback,
153 errorCallback: errorCallback != null
154 ? errorCallback
155 : other.errorCallback,
156 scheduleMicrotask: scheduleMicrotask != null
157 ? scheduleMicrotask
158 : other.scheduleMicrotask,
159 createTimer : createTimer != null ? createTimer : other.createTimer,
160 createPeriodicTimer: createPeriodicTimer != null
161 ? createPeriodicTimer
162 : other.createPeriodicTimer,
163 print : print != null ? print : other.print,
164 fork: fork != null ? fork : other.fork);
165 }
166
167 HandleUncaughtErrorHandler get handleUncaughtError;
168 RunHandler get run;
169 RunUnaryHandler get runUnary;
170 RunBinaryHandler get runBinary;
171 RegisterCallbackHandler get registerCallback;
172 RegisterUnaryCallbackHandler get registerUnaryCallback;
173 RegisterBinaryCallbackHandler get registerBinaryCallback;
174 ErrorCallbackHandler get errorCallback;
175 ScheduleMicrotaskHandler get scheduleMicrotask;
176 CreateTimerHandler get createTimer;
177 CreatePeriodicTimerHandler get createPeriodicTimer;
178 PrintHandler get print;
179 ForkHandler get fork;
180 }
181
182 /**
183 * Internal [ZoneSpecification] class.
184 *
185 * The implementation wants to rely on the fact that the getters cannot change
186 * dynamically. We thus require users to go through the redirecting
187 * [ZoneSpecification] constructor which instantiates this class.
188 */
189 class _ZoneSpecification implements ZoneSpecification {
190 const _ZoneSpecification({
191 this.handleUncaughtError: null,
192 this.run: null,
193 this.runUnary: null,
194 this.runBinary: null,
195 this.registerCallback: null,
196 this.registerUnaryCallback: null,
197 this.registerBinaryCallback: null,
198 this.errorCallback: null,
199 this.scheduleMicrotask: null,
200 this.createTimer: null,
201 this.createPeriodicTimer: null,
202 this.print: null,
203 this.fork: null
204 });
205
206 // TODO(13406): Enable types when dart2js supports it.
207 final /*HandleUncaughtErrorHandler*/ handleUncaughtError;
208 final /*RunHandler*/ run;
209 final /*RunUnaryHandler*/ runUnary;
210 final /*RunBinaryHandler*/ runBinary;
211 final /*RegisterCallbackHandler*/ registerCallback;
212 final /*RegisterUnaryCallbackHandler*/ registerUnaryCallback;
213 final /*RegisterBinaryCallbackHandler*/ registerBinaryCallback;
214 final /*ErrorCallbackHandler*/ errorCallback;
215 final /*ScheduleMicrotaskHandler*/ scheduleMicrotask;
216 final /*CreateTimerHandler*/ createTimer;
217 final /*CreatePeriodicTimerHandler*/ createPeriodicTimer;
218 final /*PrintHandler*/ print;
219 final /*ForkHandler*/ fork;
220 }
221
222 /**
223 * This class wraps zones for delegation.
224 *
225 * When forwarding to parent zones one can't just invoke the parent zone's
226 * exposed functions (like [Zone.run]), but one needs to provide more
227 * information (like the zone the `run` was initiated). Zone callbacks thus
228 * receive more information including this [ZoneDelegate] class. When delegating
229 * to the parent zone one should go through the given instance instead of
230 * directly invoking the parent zone.
231 */
232 abstract class ZoneDelegate {
233 dynamic handleUncaughtError(Zone zone, error, StackTrace stackTrace);
234 dynamic run(Zone zone, f());
235 dynamic runUnary(Zone zone, f(arg), arg);
236 dynamic runBinary(Zone zone, f(arg1, arg2), arg1, arg2);
237 ZoneCallback registerCallback(Zone zone, f());
238 ZoneUnaryCallback registerUnaryCallback(Zone zone, f(arg));
239 ZoneBinaryCallback registerBinaryCallback(Zone zone, f(arg1, arg2));
240 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace);
241 void scheduleMicrotask(Zone zone, f());
242 Timer createTimer(Zone zone, Duration duration, void f());
243 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer));
244 void print(Zone zone, String line);
245 Zone fork(Zone zone, ZoneSpecification specification, Map zoneValues);
246 }
247
248 /**
249 * A Zone represents the asynchronous version of a dynamic extent. Asynchronous
250 * callbacks are executed in the zone they have been queued in. For example,
251 * the callback of a `future.then` is executed in the same zone as the one where
252 * the `then` was invoked.
253 */
254 abstract class Zone {
255 // Private constructor so that it is not possible instantiate a Zone class.
256 Zone._();
257
258 /** The root zone that is implicitly created. */
259 static const Zone ROOT = _ROOT_ZONE;
260
261 /** The currently running zone. */
262 static Zone _current = _ROOT_ZONE;
263
264 static Zone get current => _current;
265
266 dynamic handleUncaughtError(error, StackTrace stackTrace);
267
268 /**
269 * Returns the parent zone.
270 *
271 * Returns `null` if `this` is the [ROOT] zone.
272 */
273 Zone get parent;
274
275 /**
276 * The error zone is the one that is responsible for dealing with uncaught
277 * errors.
278 * Errors are not allowed to cross between zones with different error-zones.
279 *
280 * This is the closest parent or ancestor zone of this zone that has a custom
281 * [handleUncaughtError] method.
282 */
283 Zone get errorZone;
284
285 /**
286 * Returns true if `this` and [otherZone] are in the same error zone.
287 *
288 * Two zones are in the same error zone if they inherit their
289 * [handleUncaughtError] callback from the same [errorZone].
290 */
291 bool inSameErrorZone(Zone otherZone);
292
293 /**
294 * Creates a new zone as a child of `this`.
295 *
296 * The new zone will have behavior like the current zone, except where
297 * overridden by functions in [specification].
298 *
299 * The new zone will have the same stored values (accessed through
300 * `operator []`) as this zone, but updated with the keys and values
301 * in [zoneValues]. If a key is in both this zone's values and in
302 * `zoneValues`, the new zone will use the value from `zoneValues``.
303 */
304 Zone fork({ ZoneSpecification specification,
305 Map zoneValues });
306
307 /**
308 * Executes the given function [f] in this zone.
309 */
310 dynamic run(f());
311
312 /**
313 * Executes the given callback [f] with argument [arg] in this zone.
314 */
315 dynamic runUnary(f(arg), var arg);
316
317 /**
318 * Executes the given callback [f] with argument [arg1] and [arg2] in this
319 * zone.
320 */
321 dynamic runBinary(f(arg1, arg2), var arg1, var arg2);
322
323 /**
324 * Executes the given function [f] in this zone.
325 *
326 * Same as [run] but catches uncaught errors and gives them to
327 * [handleUncaughtError].
328 */
329 dynamic runGuarded(f());
330
331 /**
332 * Executes the given callback [f] in this zone.
333 *
334 * Same as [runUnary] but catches uncaught errors and gives them to
335 * [handleUncaughtError].
336 */
337 dynamic runUnaryGuarded(f(arg), var arg);
338
339 /**
340 * Executes the given callback [f] in this zone.
341 *
342 * Same as [runBinary] but catches uncaught errors and gives them to
343 * [handleUncaughtError].
344 */
345 dynamic runBinaryGuarded(f(arg1, arg2), var arg1, var arg2);
346
347 /**
348 * Registers the given callback in this zone.
349 *
350 * It is good practice to register asynchronous or delayed callbacks before
351 * invoking [run]. This gives the zone a chance to wrap the callback and
352 * to store information with the callback. For example, a zone may decide
353 * to store the stack trace (at the time of the registration) with the
354 * callback.
355 *
356 * Returns a potentially new callback that should be used in place of the
357 * given [callback].
358 */
359 ZoneCallback registerCallback(callback());
360
361 /**
362 * Registers the given callback in this zone.
363 *
364 * Similar to [registerCallback] but with a unary callback.
365 */
366 ZoneUnaryCallback registerUnaryCallback(callback(arg));
367
368 /**
369 * Registers the given callback in this zone.
370 *
371 * Similar to [registerCallback] but with a unary callback.
372 */
373 ZoneBinaryCallback registerBinaryCallback(callback(arg1, arg2));
374
375 /**
376 * Equivalent to:
377 *
378 * ZoneCallback registered = registerCallback(f);
379 * if (runGuarded) return () => this.runGuarded(registered);
380 * return () => this.run(registered);
381 *
382 */
383 ZoneCallback bindCallback(f(), { bool runGuarded: true });
384
385 /**
386 * Equivalent to:
387 *
388 * ZoneCallback registered = registerUnaryCallback(f);
389 * if (runGuarded) return (arg) => this.runUnaryGuarded(registered, arg);
390 * return (arg) => thin.runUnary(registered, arg);
391 */
392 ZoneUnaryCallback bindUnaryCallback(f(arg), { bool runGuarded: true });
393
394 /**
395 * Equivalent to:
396 *
397 * ZoneCallback registered = registerBinaryCallback(f);
398 * if (runGuarded) {
399 * return (arg1, arg2) => this.runBinaryGuarded(registered, arg);
400 * }
401 * return (arg1, arg2) => thin.runBinary(registered, arg1, arg2);
402 */
403 ZoneBinaryCallback bindBinaryCallback(
404 f(arg1, arg2), { bool runGuarded: true });
405
406 /**
407 * Intercepts errors when added programmtically to a `Future` or `Stream`.
408 *
409 * When caling [Completer.completeError], [Stream.addError],
410 * or [Future] constructors that take an error or a callback that may throw,
411 * the current zone is allowed to intercept and replace the error.
412 *
413 * When other libraries use intermediate controllers or completers, such
414 * calls may contain errors that have already been processed.
415 *
416 * Return `null` if no replacement is desired.
417 * The original error is used unchanged in that case.
418 * Otherwise return an instance of [AsyncError] holding
419 * the new pair of error and stack trace.
420 * If the [AsyncError.error] is `null`, it is replaced by a [NullThrownError].
421 */
422 AsyncError errorCallback(Object error, StackTrace stackTrace);
423
424 /**
425 * Runs [f] asynchronously in this zone.
426 */
427 void scheduleMicrotask(void f());
428
429 /**
430 * Creates a Timer where the callback is executed in this zone.
431 */
432 Timer createTimer(Duration duration, void callback());
433
434 /**
435 * Creates a periodic Timer where the callback is executed in this zone.
436 */
437 Timer createPeriodicTimer(Duration period, void callback(Timer timer));
438
439 /**
440 * Prints the given [line].
441 */
442 void print(String line);
443
444 /**
445 * Call to enter the Zone.
446 *
447 * The previous current zone is returned.
448 */
449 static Zone _enter(Zone zone) {
450 assert(zone != null);
451 assert(!identical(zone, _current));
452 Zone previous = _current;
453 _current = zone;
454 return previous;
455 }
456
457 /**
458 * Call to leave the Zone.
459 *
460 * The previous Zone must be provided as `previous`.
461 */
462 static void _leave(Zone previous) {
463 assert(previous != null);
464 Zone._current = previous;
465 }
466
467 /**
468 * Retrieves the zone-value associated with [key].
469 *
470 * If this zone does not contain the value looks up the same key in the
471 * parent zone. If the [key] is not found returns `null`.
472 *
473 * Any object can be used as key, as long as it has compatible `operator ==`
474 * and `hashCode` implementations.
475 * By controlling access to the key, a zone can grant or deny access to the
476 * zone value.
477 */
478 operator [](Object key);
479 }
480
481 ZoneDelegate _parentDelegate(_Zone zone) {
482 if (zone.parent == null) return null;
483 return zone.parent._delegate;
484 }
485
486 class _ZoneDelegate implements ZoneDelegate {
487 final _Zone _delegationTarget;
488
489 _ZoneDelegate(this._delegationTarget);
490
491 dynamic handleUncaughtError(Zone zone, error, StackTrace stackTrace) {
492 _ZoneFunction implementation = _delegationTarget._handleUncaughtError;
493 _Zone implZone = implementation.zone;
494 return (implementation.function)(
495 implZone, _parentDelegate(implZone), zone, error, stackTrace);
496 }
497
498 dynamic run(Zone zone, f()) {
499 _ZoneFunction implementation = _delegationTarget._run;
500 _Zone implZone = implementation.zone;
501 return (implementation.function)(
502 implZone, _parentDelegate(implZone), zone, f);
503 }
504
505 dynamic runUnary(Zone zone, f(arg), arg) {
506 _ZoneFunction implementation = _delegationTarget._runUnary;
507 _Zone implZone = implementation.zone;
508 return (implementation.function)(
509 implZone, _parentDelegate(implZone), zone, f, arg);
510 }
511
512 dynamic runBinary(Zone zone, f(arg1, arg2), arg1, arg2) {
513 _ZoneFunction implementation = _delegationTarget._runBinary;
514 _Zone implZone = implementation.zone;
515 return (implementation.function)(
516 implZone, _parentDelegate(implZone), zone, f, arg1, arg2);
517 }
518
519 ZoneCallback registerCallback(Zone zone, f()) {
520 _ZoneFunction implementation = _delegationTarget._registerCallback;
521 _Zone implZone = implementation.zone;
522 return (implementation.function)(
523 implZone, _parentDelegate(implZone), zone, f);
524 }
525
526 ZoneUnaryCallback registerUnaryCallback(Zone zone, f(arg)) {
527 _ZoneFunction implementation = _delegationTarget._registerUnaryCallback;
528 _Zone implZone = implementation.zone;
529 return (implementation.function)(
530 implZone, _parentDelegate(implZone), zone, f);
531 }
532
533 ZoneBinaryCallback registerBinaryCallback(Zone zone, f(arg1, arg2)) {
534 _ZoneFunction implementation = _delegationTarget._registerBinaryCallback;
535 _Zone implZone = implementation.zone;
536 return (implementation.function)(
537 implZone, _parentDelegate(implZone), zone, f);
538 }
539
540 AsyncError errorCallback(Zone zone, Object error, StackTrace stackTrace) {
541 _ZoneFunction implementation = _delegationTarget._errorCallback;
542 _Zone implZone = implementation.zone;
543 if (identical(implZone, _ROOT_ZONE)) return null;
544 return (implementation.function)(implZone, _parentDelegate(implZone), zone,
545 error, stackTrace);
546 }
547
548 void scheduleMicrotask(Zone zone, f()) {
549 _ZoneFunction implementation = _delegationTarget._scheduleMicrotask;
550 _Zone implZone = implementation.zone;
551 (implementation.function)(
552 implZone, _parentDelegate(implZone), zone, f);
553 }
554
555 Timer createTimer(Zone zone, Duration duration, void f()) {
556 _ZoneFunction implementation = _delegationTarget._createTimer;
557 _Zone implZone = implementation.zone;
558 return (implementation.function)(
559 implZone, _parentDelegate(implZone), zone, duration, f);
560 }
561
562 Timer createPeriodicTimer(Zone zone, Duration period, void f(Timer timer)) {
563 _ZoneFunction implementation = _delegationTarget._createPeriodicTimer;
564 _Zone implZone = implementation.zone;
565 return (implementation.function)(
566 implZone, _parentDelegate(implZone), zone, period, f);
567 }
568
569 void print(Zone zone, String line) {
570 _ZoneFunction implementation = _delegationTarget._print;
571 _Zone implZone = implementation.zone;
572 (implementation.function)(
573 implZone, _parentDelegate(implZone), zone, line);
574 }
575
576 Zone fork(Zone zone, ZoneSpecification specification,
577 Map zoneValues) {
578 _ZoneFunction implementation = _delegationTarget._fork;
579 _Zone implZone = implementation.zone;
580 return (implementation.function)(
581 implZone, _parentDelegate(implZone), zone, specification, zoneValues);
582 }
583 }
584
585
586 /**
587 * Base class for Zone implementations.
588 */
589 abstract class _Zone implements Zone {
590 const _Zone();
591
592 _ZoneFunction get _runUnary;
593 _ZoneFunction get _run;
594 _ZoneFunction get _runBinary;
595 _ZoneFunction get _registerCallback;
596 _ZoneFunction get _registerUnaryCallback;
597 _ZoneFunction get _registerBinaryCallback;
598 _ZoneFunction get _errorCallback;
599 _ZoneFunction get _scheduleMicrotask;
600 _ZoneFunction get _createTimer;
601 _ZoneFunction get _createPeriodicTimer;
602 _ZoneFunction get _print;
603 _ZoneFunction get _fork;
604 _ZoneFunction get _handleUncaughtError;
605 _Zone get parent;
606 ZoneDelegate get _delegate;
607 Map get _map;
608
609 bool inSameErrorZone(Zone otherZone) {
610 return identical(this, otherZone) ||
611 identical(errorZone, otherZone.errorZone);
612 }
613 }
614
615 class _CustomZone extends _Zone {
616 // The actual zone and implementation of each of these
617 // inheritable zone functions.
618 _ZoneFunction _runUnary;
619 _ZoneFunction _run;
620 _ZoneFunction _runBinary;
621 _ZoneFunction _registerCallback;
622 _ZoneFunction _registerUnaryCallback;
623 _ZoneFunction _registerBinaryCallback;
624 _ZoneFunction _errorCallback;
625 _ZoneFunction _scheduleMicrotask;
626 _ZoneFunction _createTimer;
627 _ZoneFunction _createPeriodicTimer;
628 _ZoneFunction _print;
629 _ZoneFunction _fork;
630 _ZoneFunction _handleUncaughtError;
631
632 // A cached delegate to this zone.
633 ZoneDelegate _delegateCache;
634
635 /// The parent zone.
636 final _Zone parent;
637
638 /// The zone's scoped value declaration map.
639 ///
640 /// This is always a [HashMap].
641 final Map _map;
642
643 ZoneDelegate get _delegate {
644 if (_delegateCache != null) return _delegateCache;
645 _delegateCache = new _ZoneDelegate(this);
646 return _delegateCache;
647 }
648
649 _CustomZone(this.parent, ZoneSpecification specification, this._map) {
650 // The root zone will have implementations of all parts of the
651 // specification, so it will never try to access the (null) parent.
652 // All other zones have a non-null parent.
653 _run = (specification.run != null)
654 ? new _ZoneFunction(this, specification.run)
655 : parent._run;
656 _runUnary = (specification.runUnary != null)
657 ? new _ZoneFunction(this, specification.runUnary)
658 : parent._runUnary;
659 _runBinary = (specification.runBinary != null)
660 ? new _ZoneFunction(this, specification.runBinary)
661 : parent._runBinary;
662 _registerCallback = (specification.registerCallback != null)
663 ? new _ZoneFunction(this, specification.registerCallback)
664 : parent._registerCallback;
665 _registerUnaryCallback = (specification.registerUnaryCallback != null)
666 ? new _ZoneFunction(this, specification.registerUnaryCallback)
667 : parent._registerUnaryCallback;
668 _registerBinaryCallback = (specification.registerBinaryCallback != null)
669 ? new _ZoneFunction(this, specification.registerBinaryCallback)
670 : parent._registerBinaryCallback;
671 _errorCallback = (specification.errorCallback != null)
672 ? new _ZoneFunction(this, specification.errorCallback)
673 : parent._errorCallback;
674 _scheduleMicrotask = (specification.scheduleMicrotask != null)
675 ? new _ZoneFunction(this, specification.scheduleMicrotask)
676 : parent._scheduleMicrotask;
677 _createTimer = (specification.createTimer != null)
678 ? new _ZoneFunction(this, specification.createTimer)
679 : parent._createTimer;
680 _createPeriodicTimer = (specification.createPeriodicTimer != null)
681 ? new _ZoneFunction(this, specification.createPeriodicTimer)
682 : parent._createPeriodicTimer;
683 _print = (specification.print != null)
684 ? new _ZoneFunction(this, specification.print)
685 : parent._print;
686 _fork = (specification.fork != null)
687 ? new _ZoneFunction(this, specification.fork)
688 : parent._fork;
689 _handleUncaughtError = (specification.handleUncaughtError != null)
690 ? new _ZoneFunction(this, specification.handleUncaughtError)
691 : parent._handleUncaughtError;
692 }
693
694 /**
695 * The closest error-handling zone.
696 *
697 * Returns `this` if `this` has an error-handler. Otherwise returns the
698 * parent's error-zone.
699 */
700 Zone get errorZone => _handleUncaughtError.zone;
701
702 dynamic runGuarded(f()) {
703 try {
704 return run(f);
705 } catch (e, s) {
706 return handleUncaughtError(e, s);
707 }
708 }
709
710 dynamic runUnaryGuarded(f(arg), arg) {
711 try {
712 return runUnary(f, arg);
713 } catch (e, s) {
714 return handleUncaughtError(e, s);
715 }
716 }
717
718 dynamic runBinaryGuarded(f(arg1, arg2), arg1, arg2) {
719 try {
720 return runBinary(f, arg1, arg2);
721 } catch (e, s) {
722 return handleUncaughtError(e, s);
723 }
724 }
725
726 ZoneCallback bindCallback(f(), { bool runGuarded: true }) {
727 ZoneCallback registered = registerCallback(f);
728 if (runGuarded) {
729 return () => this.runGuarded(registered);
730 } else {
731 return () => this.run(registered);
732 }
733 }
734
735 ZoneUnaryCallback bindUnaryCallback(f(arg), { bool runGuarded: true }) {
736 ZoneUnaryCallback registered = registerUnaryCallback(f);
737 if (runGuarded) {
738 return (arg) => this.runUnaryGuarded(registered, arg);
739 } else {
740 return (arg) => this.runUnary(registered, arg);
741 }
742 }
743
744 ZoneBinaryCallback bindBinaryCallback(
745 f(arg1, arg2), { bool runGuarded: true }) {
746 ZoneBinaryCallback registered = registerBinaryCallback(f);
747 if (runGuarded) {
748 return (arg1, arg2) => this.runBinaryGuarded(registered, arg1, arg2);
749 } else {
750 return (arg1, arg2) => this.runBinary(registered, arg1, arg2);
751 }
752 }
753
754 operator [](Object key) {
755 var result = _map[key];
756 if (result != null || _map.containsKey(key)) return result;
757 // If we are not the root zone, look up in the parent zone.
758 if (parent != null) {
759 // We do not optimize for repeatedly looking up a key which isn't
760 // there. That would require storing the key and keeping it alive.
761 // Copying the key/value from the parent does not keep any new values
762 // alive.
763 var value = parent[key];
764 if (value != null) {
765 _map[key] = value;
766 }
767 return value;
768 }
769 assert(this == _ROOT_ZONE);
770 return null;
771 }
772
773 // Methods that can be customized by the zone specification.
774
775 dynamic handleUncaughtError(error, StackTrace stackTrace) {
776 _ZoneFunction implementation = this._handleUncaughtError;
777 assert(implementation != null);
778 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
779 return (implementation.function)(
780 implementation.zone, parentDelegate, this, error, stackTrace);
781 }
782
783 Zone fork({ZoneSpecification specification, Map zoneValues}) {
784 _ZoneFunction implementation = this._fork;
785 assert(implementation != null);
786 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
787 return (implementation.function)(
788 implementation.zone, parentDelegate, this,
789 specification, zoneValues);
790 }
791
792 dynamic run(f()) {
793 _ZoneFunction implementation = this._run;
794 assert(implementation != null);
795 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
796 return (implementation.function)(
797 implementation.zone, parentDelegate, this, f);
798 }
799
800 dynamic runUnary(f(arg), arg) {
801 _ZoneFunction implementation = this._runUnary;
802 assert(implementation != null);
803 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
804 return (implementation.function)(
805 implementation.zone, parentDelegate, this, f, arg);
806 }
807
808 dynamic runBinary(f(arg1, arg2), arg1, arg2) {
809 _ZoneFunction implementation = this._runBinary;
810 assert(implementation != null);
811 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
812 return (implementation.function)(
813 implementation.zone, parentDelegate, this, f, arg1, arg2);
814 }
815
816 ZoneCallback registerCallback(f()) {
817 _ZoneFunction implementation = this._registerCallback;
818 assert(implementation != null);
819 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
820 return (implementation.function)(
821 implementation.zone, parentDelegate, this, f);
822 }
823
824 ZoneUnaryCallback registerUnaryCallback(f(arg)) {
825 _ZoneFunction implementation = this._registerUnaryCallback;
826 assert(implementation != null);
827 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
828 return (implementation.function)(
829 implementation.zone, parentDelegate, this, f);
830 }
831
832 ZoneBinaryCallback registerBinaryCallback(f(arg1, arg2)) {
833 _ZoneFunction implementation = this._registerBinaryCallback;
834 assert(implementation != null);
835 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
836 return (implementation.function)(
837 implementation.zone, parentDelegate, this, f);
838 }
839
840 AsyncError errorCallback(Object error, StackTrace stackTrace) {
841 final _ZoneFunction implementation = this._errorCallback;
842 assert(implementation != null);
843 final Zone implementationZone = implementation.zone;
844 if (identical(implementationZone, _ROOT_ZONE)) return null;
845 final ZoneDelegate parentDelegate = _parentDelegate(implementationZone);
846 return (implementation.function)(
847 implementationZone, parentDelegate, this, error, stackTrace);
848 }
849
850 void scheduleMicrotask(void f()) {
851 _ZoneFunction implementation = this._scheduleMicrotask;
852 assert(implementation != null);
853 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
854 return (implementation.function)(
855 implementation.zone, parentDelegate, this, f);
856 }
857
858 Timer createTimer(Duration duration, void f()) {
859 _ZoneFunction implementation = this._createTimer;
860 assert(implementation != null);
861 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
862 return (implementation.function)(
863 implementation.zone, parentDelegate, this, duration, f);
864 }
865
866 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
867 _ZoneFunction implementation = this._createPeriodicTimer;
868 assert(implementation != null);
869 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
870 return (implementation.function)(
871 implementation.zone, parentDelegate, this, duration, f);
872 }
873
874 void print(String line) {
875 _ZoneFunction implementation = this._print;
876 assert(implementation != null);
877 ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
878 return (implementation.function)(
879 implementation.zone, parentDelegate, this, line);
880 }
881 }
882
883 void _rootHandleUncaughtError(
884 Zone self, ZoneDelegate parent, Zone zone, error, StackTrace stackTrace) {
885 _schedulePriorityAsyncCallback(() {
886 throw new _UncaughtAsyncError(error, stackTrace);
887 });
888 }
889
890 dynamic _rootRun(Zone self, ZoneDelegate parent, Zone zone, f()) {
891 if (Zone._current == zone) return f();
892
893 Zone old = Zone._enter(zone);
894 try {
895 return f();
896 } finally {
897 Zone._leave(old);
898 }
899 }
900
901 dynamic _rootRunUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) {
902 if (Zone._current == zone) return f(arg);
903
904 Zone old = Zone._enter(zone);
905 try {
906 return f(arg);
907 } finally {
908 Zone._leave(old);
909 }
910 }
911
912 dynamic _rootRunBinary(Zone self, ZoneDelegate parent, Zone zone,
913 f(arg1, arg2), arg1, arg2) {
914 if (Zone._current == zone) return f(arg1, arg2);
915
916 Zone old = Zone._enter(zone);
917 try {
918 return f(arg1, arg2);
919 } finally {
920 Zone._leave(old);
921 }
922 }
923
924 ZoneCallback _rootRegisterCallback(
925 Zone self, ZoneDelegate parent, Zone zone, f()) {
926 return f;
927 }
928
929 ZoneUnaryCallback _rootRegisterUnaryCallback(
930 Zone self, ZoneDelegate parent, Zone zone, f(arg)) {
931 return f;
932 }
933
934 ZoneBinaryCallback _rootRegisterBinaryCallback(
935 Zone self, ZoneDelegate parent, Zone zone, f(arg1, arg2)) {
936 return f;
937 }
938
939 AsyncError _rootErrorCallback(Zone self, ZoneDelegate parent, Zone zone,
940 Object error, StackTrace stackTrace) => null;
941
942 void _rootScheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, f()) {
943 if (!identical(_ROOT_ZONE, zone)) {
944 bool hasErrorHandler = !_ROOT_ZONE.inSameErrorZone(zone);
945 f = zone.bindCallback(f, runGuarded: hasErrorHandler);
946 }
947 _scheduleAsyncCallback(f);
948 }
949
950 Timer _rootCreateTimer(Zone self, ZoneDelegate parent, Zone zone,
951 Duration duration, void callback()) {
952 if (!identical(_ROOT_ZONE, zone)) {
953 callback = zone.bindCallback(callback);
954 }
955 return Timer._createTimer(duration, callback);
956 }
957
958 Timer _rootCreatePeriodicTimer(
959 Zone self, ZoneDelegate parent, Zone zone,
960 Duration duration, void callback(Timer timer)) {
961 if (!identical(_ROOT_ZONE, zone)) {
962 callback = zone.bindUnaryCallback(callback);
963 }
964 return Timer._createPeriodicTimer(duration, callback);
965 }
966
967 void _rootPrint(Zone self, ZoneDelegate parent, Zone zone, String line) {
968 printToConsole(line);
969 }
970
971 void _printToZone(String line) {
972 Zone.current.print(line);
973 }
974
975 Zone _rootFork(Zone self, ZoneDelegate parent, Zone zone,
976 ZoneSpecification specification,
977 Map zoneValues) {
978 // TODO(floitsch): it would be nice if we could get rid of this hack.
979 // Change the static zoneOrDirectPrint function to go through zones
980 // from now on.
981 printToZone = _printToZone;
982
983 if (specification == null) {
984 specification = const ZoneSpecification();
985 } else if (specification is! _ZoneSpecification) {
986 throw new ArgumentError("ZoneSpecifications must be instantiated"
987 " with the provided constructor.");
988 }
989 Map valueMap;
990 if (zoneValues == null) {
991 if (zone is _Zone) {
992 valueMap = zone._map;
993 } else {
994 valueMap = new HashMap();
995 }
996 } else {
997 valueMap = new HashMap.from(zoneValues);
998 }
999 return new _CustomZone(zone, specification, valueMap);
1000 }
1001
1002 class _RootZoneSpecification implements ZoneSpecification {
1003 HandleUncaughtErrorHandler get handleUncaughtError =>
1004 _rootHandleUncaughtError;
1005 RunHandler get run => _rootRun;
1006 RunUnaryHandler get runUnary => _rootRunUnary;
1007 RunBinaryHandler get runBinary => _rootRunBinary;
1008 RegisterCallbackHandler get registerCallback => _rootRegisterCallback;
1009 RegisterUnaryCallbackHandler get registerUnaryCallback =>
1010 _rootRegisterUnaryCallback;
1011 RegisterBinaryCallbackHandler get registerBinaryCallback =>
1012 _rootRegisterBinaryCallback;
1013 ErrorCallbackHandler get errorCallback => _rootErrorCallback;
1014 ScheduleMicrotaskHandler get scheduleMicrotask => _rootScheduleMicrotask;
1015 CreateTimerHandler get createTimer => _rootCreateTimer;
1016 CreatePeriodicTimerHandler get createPeriodicTimer =>
1017 _rootCreatePeriodicTimer;
1018 PrintHandler get print => _rootPrint;
1019 ForkHandler get fork => _rootFork;
1020 }
1021
1022 class _RootZone extends _Zone {
1023 const _RootZone();
1024
1025 _ZoneFunction get _run =>
1026 const _ZoneFunction(_ROOT_ZONE, _rootRun);
1027 _ZoneFunction get _runUnary =>
1028 const _ZoneFunction(_ROOT_ZONE, _rootRunUnary);
1029 _ZoneFunction get _runBinary =>
1030 const _ZoneFunction(_ROOT_ZONE, _rootRunBinary);
1031 _ZoneFunction get _registerCallback =>
1032 const _ZoneFunction(_ROOT_ZONE, _rootRegisterCallback);
1033 _ZoneFunction get _registerUnaryCallback =>
1034 const _ZoneFunction(_ROOT_ZONE, _rootRegisterUnaryCallback);
1035 _ZoneFunction get _registerBinaryCallback =>
1036 const _ZoneFunction(_ROOT_ZONE, _rootRegisterBinaryCallback);
1037 _ZoneFunction get _errorCallback =>
1038 const _ZoneFunction(_ROOT_ZONE, _rootErrorCallback);
1039 _ZoneFunction get _scheduleMicrotask =>
1040 const _ZoneFunction(_ROOT_ZONE, _rootScheduleMicrotask);
1041 _ZoneFunction get _createTimer =>
1042 const _ZoneFunction(_ROOT_ZONE, _rootCreateTimer);
1043 _ZoneFunction get _createPeriodicTimer =>
1044 const _ZoneFunction(_ROOT_ZONE, _rootCreatePeriodicTimer);
1045 _ZoneFunction get _print =>
1046 const _ZoneFunction(_ROOT_ZONE, _rootPrint);
1047 _ZoneFunction get _fork =>
1048 const _ZoneFunction(_ROOT_ZONE, _rootFork);
1049 _ZoneFunction get _handleUncaughtError =>
1050 const _ZoneFunction(_ROOT_ZONE, _rootHandleUncaughtError);
1051
1052 // The parent zone.
1053 _Zone get parent => null;
1054
1055 /// The zone's scoped value declaration map.
1056 ///
1057 /// This is always a [HashMap].
1058 Map get _map => _rootMap;
1059
1060 static Map _rootMap = new HashMap();
1061
1062 static ZoneDelegate _rootDelegate;
1063
1064 ZoneDelegate get _delegate {
1065 if (_rootDelegate != null) return _rootDelegate;
1066 return _rootDelegate = new _ZoneDelegate(this);
1067 }
1068
1069 /**
1070 * The closest error-handling zone.
1071 *
1072 * Returns `this` if `this` has an error-handler. Otherwise returns the
1073 * parent's error-zone.
1074 */
1075 Zone get errorZone => this;
1076
1077 // Zone interface.
1078
1079 dynamic runGuarded(f()) {
1080 try {
1081 if (identical(_ROOT_ZONE, Zone._current)) {
1082 return f();
1083 }
1084 return _rootRun(null, null, this, f);
1085 } catch (e, s) {
1086 return handleUncaughtError(e, s);
1087 }
1088 }
1089
1090 dynamic runUnaryGuarded(f(arg), arg) {
1091 try {
1092 if (identical(_ROOT_ZONE, Zone._current)) {
1093 return f(arg);
1094 }
1095 return _rootRunUnary(null, null, this, f, arg);
1096 } catch (e, s) {
1097 return handleUncaughtError(e, s);
1098 }
1099 }
1100
1101 dynamic runBinaryGuarded(f(arg1, arg2), arg1, arg2) {
1102 try {
1103 if (identical(_ROOT_ZONE, Zone._current)) {
1104 return f(arg1, arg2);
1105 }
1106 return _rootRunBinary(null, null, this, f, arg1, arg2);
1107 } catch (e, s) {
1108 return handleUncaughtError(e, s);
1109 }
1110 }
1111
1112 ZoneCallback bindCallback(f(), { bool runGuarded: true }) {
1113 if (runGuarded) {
1114 return () => this.runGuarded(f);
1115 } else {
1116 return () => this.run(f);
1117 }
1118 }
1119
1120 ZoneUnaryCallback bindUnaryCallback(f(arg), { bool runGuarded: true }) {
1121 if (runGuarded) {
1122 return (arg) => this.runUnaryGuarded(f, arg);
1123 } else {
1124 return (arg) => this.runUnary(f, arg);
1125 }
1126 }
1127
1128 ZoneBinaryCallback bindBinaryCallback(
1129 f(arg1, arg2), { bool runGuarded: true }) {
1130 if (runGuarded) {
1131 return (arg1, arg2) => this.runBinaryGuarded(f, arg1, arg2);
1132 } else {
1133 return (arg1, arg2) => this.runBinary(f, arg1, arg2);
1134 }
1135 }
1136
1137 operator [](Object key) => null;
1138
1139 // Methods that can be customized by the zone specification.
1140
1141 dynamic handleUncaughtError(error, StackTrace stackTrace) {
1142 return _rootHandleUncaughtError(null, null, this, error, stackTrace);
1143 }
1144
1145 Zone fork({ZoneSpecification specification, Map zoneValues}) {
1146 return _rootFork(null, null, this, specification, zoneValues);
1147 }
1148
1149 dynamic run(f()) {
1150 if (identical(Zone._current, _ROOT_ZONE)) return f();
1151 return _rootRun(null, null, this, f);
1152 }
1153
1154 dynamic runUnary(f(arg), arg) {
1155 if (identical(Zone._current, _ROOT_ZONE)) return f(arg);
1156 return _rootRunUnary(null, null, this, f, arg);
1157 }
1158
1159 dynamic runBinary(f(arg1, arg2), arg1, arg2) {
1160 if (identical(Zone._current, _ROOT_ZONE)) return f(arg1, arg2);
1161 return _rootRunBinary(null, null, this, f, arg1, arg2);
1162 }
1163
1164 ZoneCallback registerCallback(f()) => f;
1165
1166 ZoneUnaryCallback registerUnaryCallback(f(arg)) => f;
1167
1168 ZoneBinaryCallback registerBinaryCallback(f(arg1, arg2)) => f;
1169
1170 AsyncError errorCallback(Object error, StackTrace stackTrace) => null;
1171
1172 void scheduleMicrotask(void f()) {
1173 _rootScheduleMicrotask(null, null, this, f);
1174 }
1175
1176 Timer createTimer(Duration duration, void f()) {
1177 return Timer._createTimer(duration, f);
1178 }
1179
1180 Timer createPeriodicTimer(Duration duration, void f(Timer timer)) {
1181 return Timer._createPeriodicTimer(duration, f);
1182 }
1183
1184 void print(String line) {
1185 printToConsole(line);
1186 }
1187 }
1188
1189 const _ROOT_ZONE = const _RootZone();
1190
1191 /**
1192 * Runs [body] in its own zone.
1193 *
1194 * If [onError] is non-null the zone is considered an error zone. All uncaught
1195 * errors, synchronous or asynchronous, in the zone are caught and handled
1196 * by the callback.
1197 *
1198 * Errors may never cross error-zone boundaries. This is intuitive for leaving
1199 * a zone, but it also applies for errors that would enter an error-zone.
1200 * Errors that try to cross error-zone boundaries are considered uncaught.
1201 *
1202 * var future = new Future.value(499);
1203 * runZoned(() {
1204 * future = future.then((_) { throw "error in first error-zone"; });
1205 * runZoned(() {
1206 * future = future.catchError((e) { print("Never reached!"); });
1207 * }, onError: (e) { print("unused error handler"); });
1208 * }, onError: (e) { print("catches error of first error-zone."); });
1209 *
1210 * Example:
1211 *
1212 * runZoned(() {
1213 * new Future(() { throw "asynchronous error"; });
1214 * }, onError: print); // Will print "asynchronous error".
1215 */
1216 dynamic runZoned(body(),
1217 { Map zoneValues,
1218 ZoneSpecification zoneSpecification,
1219 Function onError }) {
1220 HandleUncaughtErrorHandler errorHandler;
1221 if (onError != null) {
1222 errorHandler = (Zone self, ZoneDelegate parent, Zone zone,
1223 error, StackTrace stackTrace) {
1224 try {
1225 if (onError is ZoneBinaryCallback) {
1226 return self.parent.runBinary(onError, error, stackTrace);
1227 }
1228 return self.parent.runUnary(onError, error);
1229 } catch(e, s) {
1230 if (identical(e, error)) {
1231 return parent.handleUncaughtError(zone, error, stackTrace);
1232 } else {
1233 return parent.handleUncaughtError(zone, e, s);
1234 }
1235 }
1236 };
1237 }
1238 if (zoneSpecification == null) {
1239 zoneSpecification =
1240 new ZoneSpecification(handleUncaughtError: errorHandler);
1241 } else if (errorHandler != null) {
1242 zoneSpecification =
1243 new ZoneSpecification.from(zoneSpecification,
1244 handleUncaughtError: errorHandler);
1245 }
1246 Zone zone = Zone.current.fork(specification: zoneSpecification,
1247 zoneValues: zoneValues);
1248 if (onError != null) {
1249 return zone.runGuarded(body);
1250 } else {
1251 return zone.run(body);
1252 }
1253 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/async/timer.dart ('k') | test/generated_sdk/lib/collection/collection.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698