Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[web] Cleanup assertionsEnabled #41829

Merged
merged 7 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/web_ui/lib/natives.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
40 changes: 24 additions & 16 deletions lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,19 @@ class HtmlViewEmbedder {
}

for (final int viewId in diffResult.viewsToAdd) {
if (assertionsEnabled) {
if (!platformViewManager.knowsViewId(viewId)) {
bool isViewInvalid = false;
assert(() {
isViewInvalid = !platformViewManager.knowsViewId(viewId);
if (isViewInvalid) {
debugInvalidViewIds ??= <int>[];
debugInvalidViewIds.add(viewId);
continue;
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 +517,17 @@ class HtmlViewEmbedder {
for (int i = 0; i < _compositionOrder.length; i++) {
final int viewId = _compositionOrder[i];

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

final DomElement platformViewRoot = _viewClipChains[viewId]!.root;
Expand All @@ -534,14 +545,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!.isEmpty,
'Cannot render platform views: ${debugInvalidViewIds!.join(', ')}. '
'These views have not been created, or they have been deleted.',
);
}

void disposeViews(Set<int> viewsToDispose) {
Expand Down
10 changes: 6 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,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
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,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 @@ -274,9 +275,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
17 changes: 12 additions & 5 deletions lib/web_ui/lib/src/engine/canvaskit/native_memory.dart
Original file line number Diff line number Diff line change
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,10 @@ 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;
}());
assert(refCount == debugReferrers.length);
}

Expand Down Expand Up @@ -170,11 +170,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
5 changes: 3 additions & 2 deletions lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import '../../engine.dart';
class SurfaceFactory {
SurfaceFactory(int maximumSurfaces)
: maximumSurfaces = math.max(maximumSurfaces, 1) {
if (assertionsEnabled) {
assert(() {
if (maximumSurfaces < 1) {
printWarning('Attempted to create a $SurfaceFactory with '
'$maximumSurfaces maximum surfaces. At least 1 surface is required '
'for rendering.');
}
registerHotRestartListener(debugClear);
}
return true;
}());
}

/// The lazy-initialized singleton surface factory.
Expand Down
16 changes: 12 additions & 4 deletions lib/web_ui/lib/src/engine/canvaskit/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,16 @@ class CkParagraph implements ui.Paragraph {

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

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

throw StateError('Paragraph.debugDisposed is only available when asserts are enabled.');
}
}
Expand Down Expand Up @@ -888,12 +895,13 @@ class CkParagraphBuilder implements ui.ParagraphBuilder {
void pop() {
if (_styleStack.length <= 1) {
// The top-level text style is paragraph-level. We don't pop it off.
if (assertionsEnabled) {
assert(() {
printWarning(
'Cannot pop text style in ParagraphBuilder. '
'Already popped all text styles from the style stack.',
);
}
return true;
}());
return;
}
_styleStack.removeLast();
Expand Down
14 changes: 10 additions & 4 deletions lib/web_ui/lib/src/engine/font_fallbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ class FontFallbackManager {
bool _scheduledCodePointCheck = false;

Future<void> debugWhenIdle() {
if (assertionsEnabled) {
return _idleFuture;
} else {
throw UnimplementedError();
Future<void>? result;
assert(() {
result = _idleFuture;
return true;
}());

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

throw UnimplementedError();
}

/// Determines if the given [text] contains any code points which are not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import '../dom.dart';
import '../util.dart';

DomHTMLElement _createContainer() {
final DomHTMLElement container = createDomHTMLDivElement();
Expand Down Expand Up @@ -77,9 +76,10 @@ class DebugCanvasReuseOverlay {
if (_instance == null) {
// Only call the constructor when assertions are enabled to guard against
// mistakingly including this class in a release build.
if (assertionsEnabled) {
assert(() {
_instance = DebugCanvasReuseOverlay._();
}
return true;
}());
}
return _instance!;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/web_ui/lib/src/engine/html/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ class SurfacePaintData {

@override
String toString() {
if (!assertionsEnabled) {
return super.toString();
} else {
String result = super.toString();
assert(() {
final StringBuffer buffer = StringBuffer('SurfacePaintData(');
if (blendMode != null) {
buffer.write('blendMode = $blendMode; ');
Expand Down Expand Up @@ -282,8 +281,12 @@ class SurfacePaintData {
buffer.write('colorFilter = $colorFilter; ');
}
buffer.write('isAntiAlias = $isAntiAlias)');
return buffer.toString();
}
result = buffer.toString();

return true;
}());

return result;
}
}

Expand Down