Skip to content

Commit

Permalink
Use generic functions in zones.
Browse files Browse the repository at this point in the history
Patch from issue 2893893002 at patchset 300001 (http://crrev.com/2893893002#ps300001)

Additionally, updated sdk/lib/js/dartium/cached_patches.dart for new html_dartium.dart file.

TBR=floitsch@google.com

Review-Url: https://codereview.chromium.org/3014593002 .
  • Loading branch information
terrylucas committed Sep 21, 2017
1 parent dbb7f54 commit 31e9775
Show file tree
Hide file tree
Showing 51 changed files with 451 additions and 412 deletions.
8 changes: 5 additions & 3 deletions pkg/compiler/lib/src/common/tasks.dart
Expand Up @@ -115,7 +115,7 @@ abstract class CompilerTask {
/// of other measuring zones, but we still need to call through the parent
/// chain. Consequently, we use a zone value keyed by [measurer] to see if
/// we should measure or not when delegating.
_run(Zone self, ZoneDelegate parent, Zone zone, f()) {
R _run<R>(Zone self, ZoneDelegate parent, Zone zone, R f()) {
if (zone[measurer] != this) return parent.run(zone, f);
CompilerTask previous = _start();
try {
Expand All @@ -126,7 +126,8 @@ abstract class CompilerTask {
}

/// Same as [run] except that [f] takes one argument, [arg].
_runUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) {
R _runUnary<R, T>(
Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg) {
if (zone[measurer] != this) return parent.runUnary(zone, f, arg);
CompilerTask previous = _start();
try {
Expand All @@ -137,7 +138,8 @@ abstract class CompilerTask {
}

/// Same as [run] except that [f] takes two arguments ([a1] and [a2]).
_runBinary(Zone self, ZoneDelegate parent, Zone zone, f(a1, a2), a1, a2) {
R _runBinary<R, T1, T2>(Zone self, ZoneDelegate parent, Zone zone,
R f(T1 a1, T2 a2), T1 a1, T2 a2) {
if (zone[measurer] != this) return parent.runBinary(zone, f, a1, a2);
CompilerTask previous = _start();
try {
Expand Down
3 changes: 1 addition & 2 deletions pkg/dev_compiler/test/browser/language_tests.js
Expand Up @@ -298,6 +298,7 @@ define(['dart_sdk', 'async_helper', 'expect', 'unittest', 'is', 'require'],
},

'lib/async': {
'async_await_zones_test': fail,
'first_regression_test': async_unittest,
'future_or_bad_type_test_implements_multi': fail,
'future_or_bad_type_test_none_multi': fail,
Expand Down Expand Up @@ -338,9 +339,7 @@ define(['dart_sdk', 'async_helper', 'expect', 'unittest', 'is', 'require'],
'timer_not_available_test': fail,
'timer_repeat_test': async_unittest,
'timer_test': async_unittest,
'zone_bind_callback_test': fail,
'zone_error_callback_test': fail,
'zone_register_callback_test': fail,
'zone_run_unary_test': fail,
},

Expand Down
28 changes: 11 additions & 17 deletions pkg/dev_compiler/tool/input_sdk/lib/html/dart2js/html_dart2js.dart
Expand Up @@ -36968,7 +36968,7 @@ class Window extends EventTarget
@DomName('Window.requestAnimationFrame')
int requestAnimationFrame(FrameRequestCallback callback) {
_ensureRequestAnimationFrame();
return _requestAnimationFrame(_wrapZone/*<num, dynamic>*/(callback));
return _requestAnimationFrame(_wrapZone(callback));
}

/**
Expand Down Expand Up @@ -42906,9 +42906,9 @@ class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {

// TODO(leafp): It would be better to write this as
// _onData = onData == null ? null :
// onData is _wrapZoneCallback<Event, dynamic>
// ? _wrapZone/*<Event, dynamic>*/(onData)
// : _wrapZone/*<Event, dynamic>*/((e) => onData(e as T))
// onData is void Function(Event)
// ? _wrapZone<Event>(onData)
// : _wrapZone<Event>((e) => onData(e as T))
// In order to support existing tests which pass the wrong type of events but
// use a more general listener, without causing as much slowdown for things
// which are typed correctly. But this currently runs afoul of restrictions
Expand All @@ -42917,7 +42917,7 @@ class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {
this._target, this._eventType, void onData(T event), this._useCapture)
: _onData = onData == null
? null
: _wrapZone<Event, dynamic>((e) => (onData as dynamic)(e)) {
: _wrapZone<Event>((e) => (onData as dynamic)(e)) {
_tryResume();
}

Expand All @@ -42939,7 +42939,7 @@ class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {
}
// Remove current event listener.
_unlisten();
_onData = _wrapZone/*<Event, dynamic>*/(handleData);
_onData = _wrapZone<Event>(handleData);
_tryResume();
}

Expand Down Expand Up @@ -46314,24 +46314,18 @@ class _WrappedEvent implements Event {
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// TODO(jacobr): remove these typedefs when dart:async supports generic types.
typedef R _wrapZoneCallback<A, R>(A a);
typedef R _wrapZoneBinaryCallback<A, B, R>(A a, B b);

_wrapZoneCallback/*<A, R>*/ _wrapZone/*<A, R>*/(
_wrapZoneCallback/*<A, R>*/ callback) {
void Function(T) _wrapZone<T>(void Function(T) callback) {
// For performance reasons avoid wrapping if we are in the root zone.
if (Zone.current == Zone.ROOT) return callback;
if (callback == null) return null;
return Zone.current.bindUnaryCallback/*<R, A>*/(callback, runGuarded: true);
return Zone.current.bindUnaryCallbackGuarded(callback);
}

_wrapZoneBinaryCallback/*<A, B, R>*/ _wrapBinaryZone/*<A, B, R>*/(
_wrapZoneBinaryCallback/*<A, B, R>*/ callback) {
void Function(T1, T2) _wrapBinaryZone<T1, T2>(void Function(T1, T2) callback) {
// For performance reasons avoid wrapping if we are in the root zone.
if (Zone.current == Zone.ROOT) return callback;
if (callback == null) return null;
return Zone.current
.bindBinaryCallback/*<R, A, B>*/(callback, runGuarded: true);
return Zone.current.bindBinaryCallbackGuarded(callback);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions runtime/bin/socket_patch.dart
Expand Up @@ -1136,14 +1136,14 @@ class _RawServerSocket extends Stream<RawSocket> implements RawServerSocket {
onCancel: _onSubscriptionStateChange,
onPause: _onPauseStateChange,
onResume: _onPauseStateChange);
_socket.setHandlers(read: zone.bindCallback(() {
_socket.setHandlers(read: zone.bindCallbackGuarded(() {
while (_socket.available > 0) {
var socket = _socket.accept();
if (socket == null) return;
_controller.add(new _RawSocket(socket));
if (_controller.isPaused) return;
}
}), error: zone.bindUnaryCallback((e) {
}), error: zone.bindUnaryCallbackGuarded((e) {
_controller.addError(e);
_controller.close();
}), destroyed: () {
Expand Down Expand Up @@ -1237,7 +1237,7 @@ class _RawSocket extends Stream<RawSocketEvent> implements RawSocket {
_controller.add(RawSocketEvent.CLOSED);
_controller.close();
},
error: zone.bindUnaryCallback((e) {
error: zone.bindUnaryCallbackGuarded((e) {
_controller.addError(e);
_socket.close();
}));
Expand Down Expand Up @@ -1734,7 +1734,7 @@ class _RawDatagramSocket extends Stream implements RawDatagramSocket {
_controller.add(RawSocketEvent.CLOSED);
_controller.close();
},
error: zone.bindUnaryCallback((e) {
error: zone.bindUnaryCallbackGuarded((e) {
_controller.addError(e);
_socket.close();
}));
Expand Down
4 changes: 2 additions & 2 deletions sdk/lib/async/future.dart
Expand Up @@ -519,7 +519,7 @@ abstract class Future<T> {
// context of all the previous iterations' callbacks.
// This avoids, e.g., deeply nested stack traces from the stack trace
// package.
nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) {
nextIteration = Zone.current.bindUnaryCallbackGuarded((bool keepGoing) {
while (keepGoing) {
FutureOr<bool> result;
try {
Expand All @@ -537,7 +537,7 @@ abstract class Future<T> {
keepGoing = result;
}
doneSignal._complete(null);
}, runGuarded: true);
});
nextIteration(true);
return doneSignal;
}
Expand Down
3 changes: 1 addition & 2 deletions sdk/lib/async/schedule_microtask.dart
Expand Up @@ -144,8 +144,7 @@ void scheduleMicrotask(void callback()) {
null, null, currentZone, currentZone.registerCallback(callback));
return;
}
Zone.current
.scheduleMicrotask(Zone.current.bindCallback(callback, runGuarded: true));
Zone.current.scheduleMicrotask(Zone.current.bindCallbackGuarded(callback));
}

class _AsyncRun {
Expand Down
9 changes: 4 additions & 5 deletions sdk/lib/async/stream_impl.dart
Expand Up @@ -332,7 +332,7 @@ class _BufferingStreamSubscription<T>
_checkState(wasInputPaused);
}

void _sendError(var error, StackTrace stackTrace) {
void _sendError(Object error, StackTrace stackTrace) {
assert(!_isCanceled);
assert(!_isPaused);
assert(!_inCallback);
Expand All @@ -345,12 +345,11 @@ class _BufferingStreamSubscription<T>
_state |= _STATE_IN_CALLBACK;
// TODO(floitsch): this dynamic should be 'void'.
if (_onError is ZoneBinaryCallback<dynamic, Object, StackTrace>) {
ZoneBinaryCallback<dynamic, Object, StackTrace> errorCallback = _onError
as Object/*=ZoneBinaryCallback<dynamic, Object, StackTrace>*/;
ZoneBinaryCallback<dynamic, Object, StackTrace> errorCallback =
_onError;
_zone.runBinaryGuarded(errorCallback, error, stackTrace);
} else {
_zone.runUnaryGuarded<dynamic, Object>(
_onError as Object/*=ZoneUnaryCallback<dynamic, Object>*/, error);
_zone.runUnaryGuarded<Object>(_onError, error);
}
_state &= ~_STATE_IN_CALLBACK;
}
Expand Down
9 changes: 3 additions & 6 deletions sdk/lib/async/timer.dart
Expand Up @@ -48,8 +48,8 @@ abstract class Timer {
// be invoked in the root zone.
return Zone.current.createTimer(duration, callback);
}
return Zone.current.createTimer(
duration, Zone.current.bindCallback(callback, runGuarded: true));
return Zone.current
.createTimer(duration, Zone.current.bindCallbackGuarded(callback));
}

/**
Expand All @@ -74,10 +74,7 @@ abstract class Timer {
// be invoked in the root zone.
return Zone.current.createPeriodicTimer(duration, callback);
}
// TODO(floitsch): the return type should be 'void', and the type
// should be inferred.
var boundCallback = Zone.current
.bindUnaryCallback<dynamic, Timer>(callback, runGuarded: true);
var boundCallback = Zone.current.bindUnaryCallbackGuarded<Timer>(callback);
return Zone.current.createPeriodicTimer(duration, boundCallback);
}

Expand Down

0 comments on commit 31e9775

Please sign in to comment.