Skip to content

Commit

Permalink
[web] Cleanup assertionsEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar committed May 9, 2023
1 parent 46544b1 commit bd8c101
Show file tree
Hide file tree
Showing 35 changed files with 594 additions and 412 deletions.
10 changes: 5 additions & 5 deletions lib/web_ui/lib/natives.dart
Expand Up @@ -12,14 +12,14 @@ class DartPluginRegistrant {
/// isolate. This can safely be executed multiple times on the same isolate,
/// but should not be called on the Root isolate.
static void ensureInitialized() {
throw UnimplementedError('`ensureInitialized` is not implemented on the web.');
throw UnimplementedError(
'`ensureInitialized` is not implemented on the web.');
}
}


List<int> saveCompilationTrace() {
if (engine.assertionsEnabled) {
throw UnimplementedError('saveCompilationTrace is not implemented on the web.');
}
assert(
throw UnimplementedError('saveCompilationTrace is not implemented on the web.'),
);
throw UnimplementedError();
}
16 changes: 10 additions & 6 deletions lib/web_ui/lib/src/engine/canvas_pool.dart
Expand Up @@ -963,10 +963,12 @@ class ContextStateHandle {
///
/// [tearDownPaint] must be called after calling this method.
void setUpPaint(SurfacePaintData paint, ui.Rect? shaderBounds) {
if (assertionsEnabled) {
assert(!_debugIsPaintSetUp);
assert(() {
final bool wasPaintSetUp = _debugIsPaintSetUp;
_debugIsPaintSetUp = true;
}
// When setting up paint, the previous paint must be torn down.
return !wasPaintSetUp;
}());

_lastUsedPaint = paint;
lineWidth = paint.strokeWidth ?? 1.0;
Expand Down Expand Up @@ -1059,10 +1061,12 @@ class ContextStateHandle {
///
/// Must be called after calling [setUpPaint].
void tearDownPaint() {
if (assertionsEnabled) {
assert(_debugIsPaintSetUp);
assert(() {
final bool wasPaintSetUp = _debugIsPaintSetUp;
_debugIsPaintSetUp = false;
}
// When tearing down paint, we expect that it was set up before.
return wasPaintSetUp;
}());

final ui.MaskFilter? maskFilter = _lastUsedPaint?.maskFilter;
if (maskFilter != null && _renderMaskFilterForWebkit) {
Expand Down
44 changes: 24 additions & 20 deletions lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart
Expand Up @@ -464,13 +464,17 @@ class HtmlViewEmbedder {
}

for (final int viewId in diffResult.viewsToAdd) {
if (assertionsEnabled) {
if (!platformViewManager.knowsViewId(viewId)) {
debugInvalidViewIds ??= <int>[];
debugInvalidViewIds.add(viewId);
continue;
}
bool isViewInvalid = false;
assert(() {
isViewInvalid = !platformViewManager.knowsViewId(viewId);
debugInvalidViewIds ??= <int>[];
debugInvalidViewIds!.add(viewId);
return true;
}());
if (isViewInvalid) {
continue;
}

if (diffResult.addToBeginning) {
final DomElement platformViewRoot = _viewClipChains[viewId]!.root;
skiaSceneHost.insertBefore(platformViewRoot, elementToInsertBefore);
Expand Down Expand Up @@ -511,12 +515,15 @@ class HtmlViewEmbedder {
for (int i = 0; i < _compositionOrder.length; i++) {
final int viewId = _compositionOrder[i];

if (assertionsEnabled) {
if (!platformViewManager.knowsViewId(viewId)) {
debugInvalidViewIds ??= <int>[];
debugInvalidViewIds.add(viewId);
continue;
}
bool isViewInvalid = false;
assert(() {
isViewInvalid = !platformViewManager.knowsViewId(viewId);
debugInvalidViewIds ??= <int>[];
debugInvalidViewIds!.add(viewId);
return true;
}());
if (isViewInvalid) {
continue;
}

final DomElement platformViewRoot = _viewClipChains[viewId]!.root;
Expand All @@ -534,14 +541,11 @@ class HtmlViewEmbedder {

disposeViews(unusedViews);

if (assertionsEnabled) {
if (debugInvalidViewIds != null && debugInvalidViewIds.isNotEmpty) {
throw AssertionError(
'Cannot render platform views: ${debugInvalidViewIds.join(', ')}. '
'These views have not been created, or they have been deleted.',
);
}
}
assert(
debugInvalidViewIds != null && debugInvalidViewIds!.isNotEmpty,
'Cannot render platform views: ${debugInvalidViewIds!.join(', ')}. '
'These views have not been created, or they have been deleted.',
);
}

void disposeViews(Set<int> viewsToDispose) {
Expand Down
80 changes: 49 additions & 31 deletions lib/web_ui/lib/src/engine/canvaskit/font_fallbacks.dart
Expand Up @@ -394,20 +394,27 @@ class FallbackFontDownloadQueue {
bool get debugIsLoadingFonts => _fontsLoading != null;

Future<void> debugWhenIdle() async {
if (assertionsEnabled) {
await Future<void>.delayed(Duration.zero);
while (isPending) {
if (_fontsLoading != null) {
await _fontsLoading;
}
if (pendingFonts.isNotEmpty) {
await Future<void>.delayed(const Duration(milliseconds: 100));
if (pendingFonts.isEmpty) {
bool isAllowed = false;
assert(() {
isAllowed = true;
() async {
await Future<void>.delayed(Duration.zero);
while (isPending) {
if (_fontsLoading != null) {
await _fontsLoading;
}
if (pendingFonts.isNotEmpty) {
await Future<void>.delayed(const Duration(milliseconds: 100));
if (pendingFonts.isEmpty) {
await Future<void>.delayed(const Duration(milliseconds: 100));
}
}
}
}
} else {
}();
return true;
}());

if (!isAllowed) {
throw UnimplementedError();
}
}
Expand Down Expand Up @@ -480,20 +487,27 @@ class NotoDownloader {
/// Useful in tests to make sure that fonts are loaded before working with
/// text.
Future<void> debugWhenIdle() async {
if (assertionsEnabled) {
// Some downloads begin asynchronously in a microtask or in a Timer.run.
// Let those run before waiting for downloads to finish.
await Future<void>.delayed(Duration.zero);
while (_debugActiveDownloadCount > 0) {
await Future<void>.delayed(const Duration(milliseconds: 100));
// If we started with a non-zero count and hit zero while waiting, wait a
// little more to make sure another download doesn't get chained after
// the last one (e.g. font file download after font CSS download).
if (_debugActiveDownloadCount == 0) {
bool isAllowed = false;
assert(() {
isAllowed = true;
() async {
// Some downloads begin asynchronously in a microtask or in a Timer.run.
// Let those run before waiting for downloads to finish.
await Future<void>.delayed(Duration.zero);
while (_debugActiveDownloadCount > 0) {
await Future<void>.delayed(const Duration(milliseconds: 100));
// If we started with a non-zero count and hit zero while waiting, wait a
// little more to make sure another download doesn't get chained after
// the last one (e.g. font file download after font CSS download).
if (_debugActiveDownloadCount == 0) {
await Future<void>.delayed(const Duration(milliseconds: 100));
}
}
}
} else {
}();
return true;
}());

if (!isAllowed) {
throw UnimplementedError();
}
}
Expand All @@ -502,31 +516,35 @@ class NotoDownloader {
///
/// Override this for testing.
Future<ByteBuffer> downloadAsBytes(String url, {String? debugDescription}) async {
if (assertionsEnabled) {
assert(() {
_debugActiveDownloadCount += 1;
}
return true;
}());
final Future<ByteBuffer> data = httpFetchByteBuffer('$fallbackFontUrlPrefix$url');
if (assertionsEnabled) {
assert(() {
unawaited(data.whenComplete(() {
_debugActiveDownloadCount -= 1;
}));
}
return true;
}());
return data;
}

/// Downloads the [url] and returns is as a [String].
///
/// Override this for testing.
Future<String> downloadAsString(String url, {String? debugDescription}) async {
if (assertionsEnabled) {
assert(() {
_debugActiveDownloadCount += 1;
}
return true;
}());
final Future<String> data = httpFetchText('$fallbackFontUrlPrefix$url');
if (assertionsEnabled) {
assert(() {
unawaited(data.whenComplete(() {
_debugActiveDownloadCount -= 1;
}));
}
return true;
}());
return data;
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Expand Up @@ -31,10 +31,12 @@ class SkiaFontCollection implements FlutterFontCollection {
///
/// This should only be used in tests.
List<RegisteredFont>? get debugRegisteredFonts {
if (!assertionsEnabled) {
return null;
}
return _registeredFonts;
List<RegisteredFont>? result;
assert(() {
result = _registeredFonts;
return true;
}());
return result;
}

final Map<String, List<SkFont>> familyToFontMap = <String, List<SkFont>>{};
Expand Down
16 changes: 12 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/image.dart
Expand Up @@ -228,9 +228,10 @@ class CkImage implements ui.Image, StackTraceDebugger {
}

void _init() {
if (assertionsEnabled) {
assert(() {
_debugStackTrace = StackTrace.current;
}
return true;
}());
ui.Image.onCreate?.call(this);
}

Expand Down Expand Up @@ -276,9 +277,16 @@ class CkImage implements ui.Image, StackTraceDebugger {

@override
bool get debugDisposed {
if (assertionsEnabled) {
return _disposed;
bool? result;
assert(() {
result = _disposed;
return true;
}());

if (result != null) {
return result!;
}

throw StateError(
'Image.debugDisposed is only available when asserts are enabled.');
}
Expand Down
18 changes: 13 additions & 5 deletions lib/web_ui/lib/src/engine/canvaskit/native_memory.dart
Expand Up @@ -6,7 +6,6 @@ import 'dart:js_interop';
import 'package:meta/meta.dart';

import '../../engine.dart' show Instrumentation;
import '../util.dart';
import 'canvaskit_api.dart';

/// Collects native objects that weren't explicitly disposed of using
Expand Down Expand Up @@ -130,9 +129,11 @@ class CountedRef<R extends StackTraceDebugger, T extends Object> {
/// Creates a counted reference.
CountedRef(T nativeObject, R debugReferrer, String debugLabel) {
_ref = UniqueRef<T>(this, nativeObject, debugLabel);
if (assertionsEnabled) {
assert(() {
debugReferrers.add(debugReferrer);
}
return true;
}());
// TODO: @yjbanov what's the purpose of this? It's always true.
assert(refCount == debugReferrers.length);
}

Expand Down Expand Up @@ -170,11 +171,18 @@ class CountedRef<R extends StackTraceDebugger, T extends Object> {
/// If asserts are enabled, the [StackTrace]s representing when a reference
/// was created.
List<StackTrace> debugGetStackTraces() {
if (assertionsEnabled) {
return debugReferrers
List<StackTrace>? result;
assert(() {
result = debugReferrers
.map<StackTrace>((R referrer) => referrer.debugStackTrace)
.toList();
return true;
}());

if (result != null) {
return result!;
}

throw UnsupportedError('');
}

Expand Down
12 changes: 9 additions & 3 deletions lib/web_ui/lib/src/engine/canvaskit/picture.dart
Expand Up @@ -6,7 +6,6 @@ import 'dart:typed_data';

import 'package:ui/ui.dart' as ui;

import '../util.dart';
import 'canvas.dart';
import 'canvaskit_api.dart';
import 'image.dart';
Expand All @@ -30,9 +29,16 @@ class CkPicture implements ui.Picture {

@override
bool get debugDisposed {
if (assertionsEnabled) {
return _isDisposed;
bool? result;
assert(() {
result = _isDisposed;
return true;
}());

if (result != null) {
return result!;
}

throw StateError('Picture.debugDisposed is only available when asserts are enabled.');
}

Expand Down
9 changes: 5 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/shader.dart
Expand Up @@ -8,7 +8,6 @@ import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:ui/ui.dart' as ui;

import '../util.dart';
import '../validators.dart';
import 'canvaskit_api.dart';
import 'image.dart';
Expand Down Expand Up @@ -105,10 +104,12 @@ class CkGradientLinear extends SimpleCkShader implements ui.Gradient {
) : assert(offsetIsValid(from)),
assert(offsetIsValid(to)),
matrix4 = matrix {
if (assertionsEnabled) {
assert(matrix4 == null || matrix4IsValid(matrix4!));
assert(matrix4 == null || matrix4IsValid(matrix4!));
// ignore: prefer_asserts_in_initializer_lists
assert(() {
validateColorStops(colors, colorStops);
}
return true;
}());
}

final ui.Offset from;
Expand Down

0 comments on commit bd8c101

Please sign in to comment.