From 3a4677a6d7d9edf200efc67e884b5df1ad97e4b0 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Wed, 24 May 2023 11:08:50 -0400 Subject: [PATCH] [web] Cleanup assertionsEnabled (flutter/engine#41829) For consistency with the rest of the flutter code, this PR explores the possibility of removing our proprietary `assertionsEnabled`. --- .../src/flutter/lib/web_ui/lib/natives.dart | 10 +- .../web_ui/lib/src/engine/canvas_pool.dart | 16 +- .../src/engine/canvaskit/embedded_views.dart | 40 +- .../lib/src/engine/canvaskit/fonts.dart | 10 +- .../lib/src/engine/canvaskit/image.dart | 16 +- .../src/engine/canvaskit/native_memory.dart | 17 +- .../lib/src/engine/canvaskit/picture.dart | 12 +- .../lib/src/engine/canvaskit/shader.dart | 9 +- .../src/engine/canvaskit/surface_factory.dart | 5 +- .../web_ui/lib/src/engine/canvaskit/text.dart | 16 +- .../web_ui/lib/src/engine/font_fallbacks.dart | 14 +- .../html/debug_canvas_reuse_overlay.dart | 6 +- .../web_ui/lib/src/engine/html/painting.dart | 13 +- .../lib/src/engine/html/path/cubic.dart | 6 +- .../web_ui/lib/src/engine/html/path/path.dart | 12 +- .../lib/src/engine/html/path/path_ref.dart | 52 ++- .../lib/src/engine/html/recording_canvas.dart | 406 ++++++++++-------- .../lib/src/engine/html/render_vertices.dart | 11 +- .../lib/src/engine/html/scene_builder.dart | 13 +- .../lib/src/engine/html/shaders/shader.dart | 6 +- .../engine/html/shaders/shader_builder.dart | 12 +- .../web_ui/lib/src/engine/html/surface.dart | 57 +-- .../lib/src/engine/html_image_codec.dart | 19 +- .../lib/web_ui/lib/src/engine/picture.dart | 12 +- .../lib/src/engine/platform_dispatcher.dart | 18 +- .../lib/src/engine/semantics/semantics.dart | 11 +- .../lib/src/engine/text/canvas_paragraph.dart | 12 +- .../web_ui/lib/src/engine/text/paragraph.dart | 44 +- .../lib/web_ui/lib/src/engine/text/ruler.dart | 5 +- .../src/engine/text_editing/text_editing.dart | 9 +- .../lib/web_ui/lib/src/engine/util.dart | 6 - .../web_ui/lib/src/engine/vector_math.dart | 17 +- .../full_page_embedding_strategy.dart | 7 +- .../test/canvaskit/native_memory_test.dart | 10 +- .../flutter/web_sdk/test/js_access_test.dart | 6 +- 35 files changed, 554 insertions(+), 381 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/natives.dart b/engine/src/flutter/lib/web_ui/lib/natives.dart index 731e36d07f..6cbde5e008 100644 --- a/engine/src/flutter/lib/web_ui/lib/natives.dart +++ b/engine/src/flutter/lib/web_ui/lib/natives.dart @@ -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 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(); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart index c07801a725..cca07a6780 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvas_pool.dart @@ -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; @@ -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) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart index e2d5f19634..70a1a3cb1d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart @@ -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 ??= []; - 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); @@ -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 ??= []; - debugInvalidViewIds.add(viewId); - continue; + debugInvalidViewIds!.add(viewId); } + return true; + }()); + if (isViewInvalid) { + continue; } final DomElement platformViewRoot = _viewClipChains[viewId]!.root; @@ -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 viewsToDispose) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart index 79ee8fc4aa..908c8aa317 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/fonts.dart @@ -36,10 +36,12 @@ class SkiaFontCollection implements FlutterFontCollection { /// /// This should only be used in tests. List? get debugRegisteredFonts { - if (!assertionsEnabled) { - return null; - } - return _registeredFonts; + List? result; + assert(() { + result = _registeredFonts; + return true; + }()); + return result; } final Map> familyToFontMap = >{}; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/image.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/image.dart index 30c1ab2b4d..74220a502e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/image.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/image.dart @@ -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); } @@ -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.'); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart index a8423620a1..35ef4bff1a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart @@ -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 @@ -130,9 +129,10 @@ class CountedRef { /// Creates a counted reference. CountedRef(T nativeObject, R debugReferrer, String debugLabel) { _ref = UniqueRef(this, nativeObject, debugLabel); - if (assertionsEnabled) { + assert(() { debugReferrers.add(debugReferrer); - } + return true; + }()); assert(refCount == debugReferrers.length); } @@ -170,11 +170,18 @@ class CountedRef { /// If asserts are enabled, the [StackTrace]s representing when a reference /// was created. List debugGetStackTraces() { - if (assertionsEnabled) { - return debugReferrers + List? result; + assert(() { + result = debugReferrers .map((R referrer) => referrer.debugStackTrace) .toList(); + return true; + }()); + + if (result != null) { + return result!; } + throw UnsupportedError(''); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart index 19e1d71519..374c24655d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart @@ -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'; @@ -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.'); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/shader.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/shader.dart index 70f6de2469..d09e507a18 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/shader.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/shader.dart @@ -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'; @@ -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; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart index 08e8630743..ee5b001dd8 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart @@ -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. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart index fa2dec9a3f..f177031d6a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart @@ -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.'); } } @@ -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(); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/font_fallbacks.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/font_fallbacks.dart index fc7939b73c..36268c60be 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/font_fallbacks.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/font_fallbacks.dart @@ -81,11 +81,17 @@ class FontFallbackManager { bool _scheduledCodePointCheck = false; Future debugWhenIdle() { - if (assertionsEnabled) { - return _idleFuture; - } else { - throw UnimplementedError(); + Future? 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 diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/debug_canvas_reuse_overlay.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/debug_canvas_reuse_overlay.dart index 348b92914d..87f2b8b798 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/debug_canvas_reuse_overlay.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/debug_canvas_reuse_overlay.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import '../dom.dart'; -import '../util.dart'; DomHTMLElement _createContainer() { final DomHTMLElement container = createDomHTMLDivElement(); @@ -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!; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/painting.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/painting.dart index 1b573a933b..bab36c6740 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/painting.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/painting.dart @@ -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; '); @@ -282,8 +281,12 @@ class SurfacePaintData { buffer.write('colorFilter = $colorFilter; '); } buffer.write('isAntiAlias = $isAntiAlias)'); - return buffer.toString(); - } + result = buffer.toString(); + + return true; + }()); + + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/cubic.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/cubic.dart index 5c06ce7128..81b5dda6fe 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/cubic.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/cubic.dart @@ -5,7 +5,6 @@ import 'dart:math' as math; import 'dart:typed_data'; -import '../../util.dart'; import 'path_utils.dart'; /// Chops cubic at Y extrema points and writes result to [dest]. @@ -45,7 +44,7 @@ QuadRoots _findCubicExtrema(double a, double b, double c, double d) { /// Subdivides cubic curve for a list of t values. void _chopCubicAt( List tValues, Float32List points, Float32List outPts) { - if (assertionsEnabled) { + assert(() { for (int i = 0; i < tValues.length - 1; i++) { final double tValue = tValues[i]; assert(tValue > 0 && tValue < 1, @@ -57,7 +56,8 @@ void _chopCubicAt( assert( nextTValue > tValue, 'Expecting t value to monotonically increase'); } - } + return true; + }()); final int rootCount = tValues.length; if (0 == rootCount) { for (int i = 0; i < 8; i++) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path.dart index ed3b2e7d57..3d2d926fb9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path.dart @@ -7,7 +7,6 @@ import 'dart:typed_data'; import 'package:ui/ui.dart' as ui; -import '../../util.dart'; import '../../validators.dart'; import '../../vector_math.dart'; import 'conic.dart'; @@ -1558,7 +1557,8 @@ class SurfacePath implements ui.Path { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final StringBuffer sb = StringBuffer(); sb.write('Path('); final PathRefIterator iter = PathRefIterator(pathRef); @@ -1589,10 +1589,10 @@ class SurfacePath implements ui.Path { } } sb.write(')'); - return sb.toString(); - } else { - return super.toString(); - } + result = sb.toString(); + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path_ref.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path_ref.dart index b9885d1540..c1dde26155 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path_ref.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/path/path_ref.dart @@ -7,7 +7,6 @@ import 'dart:typed_data'; import 'package:ui/ui.dart' as ui; -import '../../util.dart'; import 'path_utils.dart'; /// Stores the path verbs, points and conic weights. @@ -304,7 +303,7 @@ class PathRef { dx = vector1_0x.abs(); dy = vector1_0y.abs(); } - if (assertionsEnabled) { + assert(() { final int checkCornerIndex = SPath.nearlyEqual(controlPx, bounds.left) ? (SPath.nearlyEqual(controlPy, bounds.top) ? _Corner.kUpperLeft @@ -312,12 +311,12 @@ class PathRef { : (SPath.nearlyEqual(controlPy, bounds.top) ? _Corner.kUpperRight : _Corner.kLowerRight); - assert(checkCornerIndex == cornerIndex); - } + return checkCornerIndex == cornerIndex; + }()); radii.add(ui.Radius.elliptical(dx, dy)); ++cornerIndex; } else { - if (assertionsEnabled) { + assert(() { if (verb == SPath.kLineVerb) { final bool isVerticalOrHorizontal = SPath.nearlyEqual(pts[2], pts[0]) || @@ -329,7 +328,8 @@ class PathRef { } else { assert(verb == SPath.kCloseVerb); } - } + return true; + }()); } } return ui.RRect.fromRectAndCorners(bounds, @@ -634,6 +634,7 @@ class PathRef { debugValidate(); int pCnt; int mask = 0; + String? throwError; switch (verb) { case SPath.kMoveVerb: pCnt = 1; @@ -652,16 +653,21 @@ class PathRef { case SPath.kCloseVerb: pCnt = 0; case SPath.kDoneVerb: - if (assertionsEnabled) { - throw Exception('growForVerb called for kDone'); - } + assert(() { + throwError = 'growForVerb called for kDone'; + return true; + }()); pCnt = 0; default: - if (assertionsEnabled) { - throw Exception('default is not reached'); - } + assert(() { + throwError = 'growForVerb called for unknown verb'; + return true; + }()); pCnt = 0; - break; + } + + if (throwError != null) { + throw Exception(throwError); } fSegmentMask |= mask; @@ -694,6 +700,7 @@ class PathRef { startEdit(); int pCnt; int mask = 0; + String? throwError; switch (verb) { case SPath.kMoveVerb: pCnt = numVbs; @@ -712,16 +719,21 @@ class PathRef { case SPath.kCloseVerb: pCnt = 0; case SPath.kDoneVerb: - if (assertionsEnabled) { - throw Exception('growForVerb called for kDone'); - } + assert(() { + throwError = 'growForVerb called for kDone'; + return true; + }()); pCnt = 0; default: - if (assertionsEnabled) { - throw Exception('default is not reached'); - } + assert(() { + throwError = 'growForVerb called for unknown verb'; + return true; + }()); pCnt = 0; - break; + } + + if (throwError != null) { + throw Exception(throwError); } fSegmentMask |= mask; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart index 11ae75c90d..c323b48066 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart @@ -55,9 +55,16 @@ class RecordingCanvas { /// In debug mode returns the list of recorded paint commands for testing. List get debugPaintCommands { - if (assertionsEnabled) { - return _commands; + List? result; + assert(() { + result = _commands; + return true; + }()); + + if (result != null) { + return result!; } + throw UnsupportedError('For debugging only.'); } @@ -184,15 +191,17 @@ class RecordingCanvas { /// Prints recorded commands. String? debugPrintCommands() { - if (assertionsEnabled) { + String? result; + assert(() { final StringBuffer debugBuf = StringBuffer(); for (int i = 0; i < _commands.length; i++) { final PaintCommand command = _commands[i]; debugBuf.writeln('ctx.$command;'); } - return debugBuf.toString(); - } - return null; + result = debugBuf.toString(); + return true; + }()); + return result; } void save() { @@ -729,11 +738,12 @@ class PaintSave extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'save()'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'save()'; + return true; + }()); + return result; } } @@ -747,11 +757,12 @@ class PaintRestore extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'restore()'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'restore()'; + return true; + }()); + return result; } } @@ -768,11 +779,12 @@ class PaintTranslate extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'translate($dx, $dy)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'translate($dx, $dy)'; + return true; + }()); + return result; } } @@ -789,11 +801,12 @@ class PaintScale extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'scale($sx, $sy)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'scale($sx, $sy)'; + return true; + }()); + return result; } } @@ -809,11 +822,12 @@ class PaintRotate extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'rotate($radians)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'rotate($radians)'; + return true; + }()); + return result; } } @@ -829,11 +843,12 @@ class PaintTransform extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'transform(Matrix4.fromFloat32List(Float32List.fromList([${matrix4.join(', ')}])))'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'transform(Matrix4.fromFloat32List(Float32List.fromList([${matrix4.join(', ')}])))'; + return true; + }()); + return result; } } @@ -850,11 +865,12 @@ class PaintSkew extends PaintCommand { @override String toString() { - if (assertionsEnabled) { - return 'skew($sx, $sy)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'skew($sx, $sy)'; + return true; + }()); + return result; } } @@ -871,11 +887,12 @@ class PaintClipRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'clipRect($rect)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'clipRect($rect)'; + return true; + }()); + return result; } } @@ -891,11 +908,12 @@ class PaintClipRRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'clipRRect($rrect)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'clipRRect($rrect)'; + return true; + }()); + return result; } } @@ -911,11 +929,12 @@ class PaintClipPath extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'clipPath($path)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'clipPath($path)'; + return true; + }()); + return result; } } @@ -932,11 +951,12 @@ class PaintDrawColor extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawColor($color, $blendMode)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawColor($color, $blendMode)'; + return true; + }()); + return result; } } @@ -954,11 +974,12 @@ class PaintDrawLine extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawLine($p1, $p2, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawLine($p1, $p2, $paint)'; + return true; + }()); + return result; } } @@ -974,11 +995,12 @@ class PaintDrawPaint extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawPaint($paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawPaint($paint)'; + return true; + }()); + return result; } } @@ -996,11 +1018,12 @@ class PaintDrawVertices extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawVertices($vertices, $blendMode, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawVertices($vertices, $blendMode, $paint)'; + return true; + }()); + return result; } } @@ -1018,11 +1041,12 @@ class PaintDrawPoints extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawPoints($pointMode, $points, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawPoints($pointMode, $points, $paint)'; + return true; + }()); + return result; } } @@ -1039,11 +1063,12 @@ class PaintDrawRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawRect($rect, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawRect($rect, $paint)'; + return true; + }()); + return result; } } @@ -1060,11 +1085,12 @@ class PaintDrawRRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawRRect($rrect, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawRRect($rrect, $paint)'; + return true; + }()); + return result; } } @@ -1090,11 +1116,12 @@ class PaintDrawDRRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawDRRect($outer, $inner, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawDRRect($outer, $inner, $paint)'; + return true; + }()); + return result; } } @@ -1111,11 +1138,12 @@ class PaintDrawOval extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawOval($rect, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawOval($rect, $paint)'; + return true; + }()); + return result; } } @@ -1133,11 +1161,12 @@ class PaintDrawCircle extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawCircle($c, $radius, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawCircle($c, $radius, $paint)'; + return true; + }()); + return result; } } @@ -1154,11 +1183,12 @@ class PaintDrawPath extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawPath($path, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawPath($path, $paint)'; + return true; + }()); + return result; } } @@ -1178,11 +1208,12 @@ class PaintDrawShadow extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawShadow($path, $color, $elevation, $transparentOccluder)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawShadow($path, $color, $elevation, $transparentOccluder)'; + return true; + }()); + return result; } } @@ -1200,11 +1231,12 @@ class PaintDrawImage extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawImage($image, $offset, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawImage($image, $offset, $paint)'; + return true; + }()); + return result; } } @@ -1223,11 +1255,12 @@ class PaintDrawImageRect extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'drawImageRect($image, $src, $dst, $paint)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'drawImageRect($image, $src, $dst, $paint)'; + return true; + }()); + return result; } } @@ -1244,11 +1277,12 @@ class PaintDrawParagraph extends DrawCommand { @override String toString() { - if (assertionsEnabled) { - return 'DrawParagraph(${paragraph.plainText}, $offset)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'DrawParagraph(${paragraph.plainText}, $offset)'; + return true; + }()); + return result; } } @@ -1276,11 +1310,12 @@ class Subpath { @override String toString() { - if (assertionsEnabled) { - return 'Subpath(${commands.join(', ')})'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'Subpath(${commands.join(', ')})'; + return true; + }()); + return result; } } @@ -1317,11 +1352,12 @@ class MoveTo extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'MoveTo($x, $y)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'MoveTo($x, $y)'; + return true; + }()); + return result; } } @@ -1344,11 +1380,12 @@ class LineTo extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'LineTo($x, $y)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'LineTo($x, $y)'; + return true; + }()); + return result; } } @@ -1476,11 +1513,12 @@ class Ellipse extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'Ellipse($x, $y, $radiusX, $radiusY)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'Ellipse($x, $y, $radiusX, $radiusY)'; + return true; + }()); + return result; } } @@ -1516,11 +1554,12 @@ class QuadraticCurveTo extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'QuadraticCurveTo($x1, $y1, $x2, $y2)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'QuadraticCurveTo($x1, $y1, $x2, $y2)'; + return true; + }()); + return result; } } @@ -1560,11 +1599,12 @@ class BezierCurveTo extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'BezierCurveTo($x1, $y1, $x2, $y2, $x3, $y3)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'BezierCurveTo($x1, $y1, $x2, $y2, $x3, $y3)'; + return true; + }()); + return result; } } @@ -1617,11 +1657,12 @@ class RectCommand extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'Rect($x, $y, $width, $height)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'Rect($x, $y, $width, $height)'; + return true; + }()); + return result; } } @@ -1644,11 +1685,12 @@ class RRectCommand extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return '$rrect'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = '$rrect'; + return true; + }()); + return result; } } @@ -1665,11 +1707,12 @@ class CloseCommand extends PathCommand { @override String toString() { - if (assertionsEnabled) { - return 'Close()'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = 'Close()'; + return true; + }()); + return result; } } @@ -1997,12 +2040,13 @@ class _PaintBounds { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final ui.Rect bounds = computeBounds(); - return '_PaintBounds($bounds of size ${bounds.size})'; - } else { - return super.toString(); - } + result = '_PaintBounds($bounds of size ${bounds.size})'; + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/render_vertices.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/render_vertices.dart index 3d8c42d94c..ca5d2acacf 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/render_vertices.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/render_vertices.dart @@ -64,9 +64,16 @@ class SurfaceVertices implements ui.Vertices { @override bool get debugDisposed { - if (assertionsEnabled) { - return _disposed; + bool? result; + assert(() { + result = _disposed; + return true; + }()); + + if (result != null) { + return result!; } + throw StateError('Vertices.debugDisposed is only available when asserts are enabled.'); } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/scene_builder.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/scene_builder.dart index e596165748..d8e8bb3d24 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/scene_builder.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/scene_builder.dart @@ -63,9 +63,10 @@ class SurfaceSceneBuilder implements ui.SceneBuilder { /// /// This is used by tests. void debugAddSurface(PersistedSurface surface) { - if (assertionsEnabled) { + assert(() { _addSurface(surface); - } + return true; + }()); } void _addSurface(PersistedSurface surface) { @@ -275,10 +276,10 @@ class SurfaceSceneBuilder implements ui.SceneBuilder { void addRetained(ui.EngineLayer retainedLayer) { final PersistedContainerSurface retainedSurface = retainedLayer as PersistedContainerSurface; - if (assertionsEnabled) { - assert(debugAssertSurfaceState(retainedSurface, - PersistedSurfaceState.active, PersistedSurfaceState.released)); - } + assert(debugAssertSurfaceState(retainedSurface, + PersistedSurfaceState.active, + PersistedSurfaceState.released, + )); retainedSurface.tryRetain(); _adoptSurface(retainedSurface); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart index 7907cc6009..9a8fb3ac98 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart @@ -186,9 +186,11 @@ class GradientLinear extends EngineGradient { assert(offsetIsValid(to)), matrix4 = matrix == null ? null : FastMatrix32(matrix), super._() { - if (assertionsEnabled) { + // ignore: prefer_asserts_in_initializer_lists + assert(() { validateColorStops(colors, colorStops); - } + return true; + }()); } final ui.Offset from; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart index bae00fb003..6c971c0832 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart @@ -5,7 +5,6 @@ import 'package:ui/ui.dart' as ui; import '../../browser_detection.dart'; -import '../../util.dart'; /// Creates shader program for target webgl version. /// @@ -250,11 +249,12 @@ class ShaderMethod { } void addStatement(String statement) { - if (assertionsEnabled) { - _statements.add(' ' * _indentLevel + statement); - } else { - _statements.add(statement); - } + String itemToAdd = statement; + assert(() { + itemToAdd = ' ' * _indentLevel + statement; + return true; + }()); + _statements.add(itemToAdd); } /// Adds statements to compute tiling in 0..1 coordinate space. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/surface.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/surface.dart index 425677f2c4..cd76aff0c7 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/surface.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/surface.dart @@ -166,10 +166,12 @@ class PersistedSurfaceException implements Exception { @override String toString() { - if (assertionsEnabled) { - return '${surface.runtimeType}: $message'; - } - return super.toString(); + String result = super.toString(); + assert(() { + result = '${surface.runtimeType}: $message'; + return true; + }()); + return result; } } @@ -356,7 +358,7 @@ abstract class PersistedSurface implements ui.EngineLayer { /// such as on the very first frame. @mustCallSuper void build() { - if (assertionsEnabled) { + assert(() { final DomElement? existingElement = rootElement; if (existingElement != null) { throw PersistedSurfaceException( @@ -365,7 +367,8 @@ abstract class PersistedSurface implements ui.EngineLayer { 'element ${existingElement.tagName}.', ); } - } + return true; + }()); assert(debugAssertSurfaceState(this, PersistedSurfaceState.created)); rootElement = createElement(); assert(rootElement != null); @@ -420,9 +423,10 @@ abstract class PersistedSurface implements ui.EngineLayer { adoptElements(oldSurface); - if (assertionsEnabled) { + assert(() { rootElement!.setAttribute('flt-layer-state', 'updated'); - } + return true; + }()); state = PersistedSurfaceState.active; assert(rootElement != null); } @@ -444,9 +448,10 @@ abstract class PersistedSurface implements ui.EngineLayer { // this surface's DOM elements. retainedSurfaces.add(this); } - if (assertionsEnabled) { + assert(() { rootElement!.setAttribute('flt-layer-state', 'retained'); - } + return true; + }()); if (debugExplainSurfaceStats) { surfaceStatsFor(this).retainSurfaceCount++; } @@ -503,9 +508,10 @@ abstract class PersistedSurface implements ui.EngineLayer { DomElement defaultCreateElement(String tagName) { final DomElement element = createDomElement(tagName); element.style.position = 'absolute'; - if (assertionsEnabled) { + assert(() { element.setAttribute('flt-layer-state', 'new'); - } + return true; + }()); return element; } @@ -595,13 +601,14 @@ abstract class PersistedSurface implements ui.EngineLayer { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final StringBuffer log = StringBuffer(); debugPrint(log, 0); - return log.toString(); - } else { - return super.toString(); - } + result = log.toString(); + return true; + }()); + return result; } } @@ -720,9 +727,10 @@ abstract class PersistedContainerSurface extends PersistedSurface { _updateManyToMany(oldSurface); } - if (assertionsEnabled) { + assert(() { _debugValidateContainerUpdate(oldSurface); - } + return true; + }()); } // Children should override if they are performing clipping. @@ -1212,11 +1220,12 @@ class _PersistedSurfaceMatch { @override String toString() { - if (assertionsEnabled) { - return '_PersistedSurfaceMatch(${newChild!.runtimeType}#${newChild!.hashCode}: $oldChildIndex, quality: $matchQuality)'; - } else { - return super.toString(); - } + String result = super.toString(); + assert(() { + result = '_PersistedSurfaceMatch(${newChild!.runtimeType}#${newChild!.hashCode}: $oldChildIndex, quality: $matchQuality)'; + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart index 212d45942f..5c7935816c 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html_image_codec.dart @@ -10,7 +10,6 @@ import 'package:ui/ui.dart' as ui; import 'browser_detection.dart'; import 'dom.dart'; import 'safe_browser_api.dart'; -import 'util.dart'; Object? get _jsImageDecodeFunction => getJsProperty( getJsProperty( @@ -151,17 +150,25 @@ class HtmlImage implements ui.Image { ui.Image.onDispose?.call(this); // Do nothing. The codec that owns this image should take care of // releasing the object url. - if (assertionsEnabled) { + assert(() { _disposed = true; - } + return true; + }()); } @override bool get debugDisposed { - if (assertionsEnabled) { - return _disposed; + bool? result; + assert(() { + result = _disposed; + return true; + }()); + + if (result != null) { + return result!; } - return throw StateError('Image.debugDisposed is only available when asserts are enabled.'); + + throw StateError('Image.debugDisposed is only available when asserts are enabled.'); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart index 59a1a70aef..db469b7ccc 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/picture.dart @@ -10,7 +10,6 @@ import 'dom.dart'; import 'html/bitmap_canvas.dart'; import 'html/recording_canvas.dart'; import 'html_image_codec.dart'; -import 'util.dart'; /// An implementation of [ui.PictureRecorder] backed by a [RecordingCanvas]. class EnginePictureRecorder implements ui.PictureRecorder { @@ -106,9 +105,16 @@ class EnginePicture implements ui.Picture { @override bool get debugDisposed { - if (assertionsEnabled) { - return _disposed; + bool? result; + assert(() { + result = _disposed; + return true; + }()); + + if (result != null) { + return result!; } + throw StateError('Picture.debugDisposed is only available when asserts are enabled.'); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart index b377363d4a..4dee3ef572 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart @@ -452,7 +452,15 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { ui.PlatformMessageResponseCallback? callback, ) { // In widget tests we want to bypass processing of platform messages. - if (assertionsEnabled && ui.debugEmulateFlutterTesterEnvironment) { + bool returnImmediately = false; + assert(() { + if (ui.debugEmulateFlutterTesterEnvironment) { + returnImmediately = true; + } + return true; + }()); + + if (returnImmediately) { return; } @@ -460,7 +468,13 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { print('Sent platform message on channel: "$name"'); } - if (assertionsEnabled && name == 'flutter/debug-echo') { + bool allowDebugEcho = false; + assert(() { + allowDebugEcho = true; + return true; + }()); + + if (allowDebugEcho && name == 'flutter/debug-echo') { // Echoes back the data unchanged. Used for testing purposes. replyToPlatformMessage(callback, data); return; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart index 51044508fc..50d8d16a20 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart @@ -1512,15 +1512,16 @@ class SemanticsObject { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final String children = _childrenInTraversalOrder != null && _childrenInTraversalOrder!.isNotEmpty ? '[${_childrenInTraversalOrder!.join(', ')}]' : ''; - return '$runtimeType(#$id, children: $children)'; - } else { - return super.toString(); - } + result = '$runtimeType(#$id, children: $children)'; + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart index b9c5ce06a3..fc0678982a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart @@ -8,7 +8,6 @@ import '../dom.dart'; import '../embedder.dart'; import '../html/bitmap_canvas.dart'; import '../profiler.dart'; -import '../util.dart'; import 'layout_fragmenter.dart'; import 'layout_service.dart'; import 'paint_service.dart'; @@ -253,9 +252,16 @@ class CanvasParagraph 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 avialalbe when asserts are enabled.'); } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart index 65f91fc125..5e17a5a5b5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart @@ -89,8 +89,9 @@ class EngineLineMetrics implements ui.LineMetrics { @override String toString() { - if (assertionsEnabled) { - return 'LineMetrics(hardBreak: $hardBreak, ' + String result = super.toString(); + assert(() { + result = 'LineMetrics(hardBreak: $hardBreak, ' 'ascent: $ascent, ' 'descent: $descent, ' 'unscaledAscent: $unscaledAscent, ' @@ -99,9 +100,9 @@ class EngineLineMetrics implements ui.LineMetrics { 'left: $left, ' 'baseline: $baseline, ' 'lineNumber: $lineNumber)'; - } else { - return super.toString(); - } + return true; + }()); + return result; } } @@ -343,10 +344,11 @@ class EngineParagraphStyle implements ui.ParagraphStyle { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final double? fontSize = this.fontSize; final double? height = this.height; - return 'ParagraphStyle(' + result = 'ParagraphStyle(' 'textAlign: ${textAlign ?? "unspecified"}, ' 'textDirection: ${textDirection ?? "unspecified"}, ' 'fontWeight: ${fontWeight ?? "unspecified"}, ' @@ -359,9 +361,9 @@ class EngineParagraphStyle implements ui.ParagraphStyle { 'ellipsis: ${ellipsis != null ? '"$ellipsis"' : "unspecified"}, ' 'locale: ${locale ?? "unspecified"}' ')'; - } else { - return super.toString(); - } + return true; + }()); + return result; } } @@ -469,9 +471,14 @@ class EngineTextStyle implements ui.TextStyle { final String fontFamily = this.fontFamily.isEmpty ? FlutterViewEmbedder.defaultFontFamily : this.fontFamily; // In the flutter tester environment, we use predictable-size test fonts. // This makes widget tests predictable and less flaky. - return assertionsEnabled && ui.debugEmulateFlutterTesterEnvironment && !_testFonts.contains(fontFamily) - ? _testFonts.first - : fontFamily; + String result = fontFamily; + assert(() { + if (ui.debugEmulateFlutterTesterEnvironment && !_testFonts.contains(fontFamily)) { + result = _testFonts.first; + } + return true; + }()); + return result; } String? _cssFontString; @@ -600,11 +607,12 @@ class EngineTextStyle implements ui.TextStyle { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { final List? fontFamilyFallback = this.fontFamilyFallback; final double? fontSize = this.fontSize; final double? height = this.height; - return 'TextStyle(' + result = 'TextStyle(' 'color: ${color ?? "unspecified"}, ' 'decoration: ${decoration ?? "unspecified"}, ' 'decorationColor: ${decorationColor ?? "unspecified"}, ' @@ -626,9 +634,9 @@ class EngineTextStyle implements ui.TextStyle { 'fontFeatures: ${fontFeatures ?? "unspecified"}, ' 'fontVariations: ${fontVariations ?? "unspecified"}' ')'; - } else { - return super.toString(); - } + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart index b18ad73fb8..3007e23020 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart @@ -197,9 +197,10 @@ class TextHeightRuler { ..border = '0' ..padding = '0'; - if (assertionsEnabled) { + assert(() { host.setAttribute('data-ruler', 'line-height'); - } + return true; + }()); _dimensions.applyHeightStyle(textHeightStyle); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart index f8c9127d27..d0ad3a2818 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart @@ -813,9 +813,12 @@ class EditingState { @override String toString() { - return assertionsEnabled - ? 'EditingState("$text", base:$baseOffset, extent:$extentOffset, composingBase:$composingBaseOffset, composingExtent:$composingExtentOffset)' - : super.toString(); + String result = super.toString(); + assert(() { + result = 'EditingState("$text", base:$baseOffset, extent:$extentOffset, composingBase:$composingBaseOffset, composingExtent:$composingExtentOffset)'; + return true; + }()); + return result; } /// Sets the selection values of a DOM element using this [EditingState]. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart index 3b7eca17a5..13f2377d71 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/util.dart @@ -217,12 +217,6 @@ String float64ListToCssTransform3d(List matrix) { } } -bool get assertionsEnabled { - bool k = false; - assert(k = true); - return k; -} - final Float32List _tempRectData = Float32List(4); /// Transforms a [ui.Rect] given the effective [transform]. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart index c50354d217..a6edaa5a61 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/vector_math.dart @@ -1047,18 +1047,19 @@ class Matrix4 { @override String toString() { - if (assertionsEnabled) { + String result = super.toString(); + assert(() { String fmt(int index) { return storage[index].toStringAsFixed(2); } - return '[${fmt(0)}, ${fmt(4)}, ${fmt(8)}, ${fmt(12)}]\n' - '[${fmt(1)}, ${fmt(5)}, ${fmt(9)}, ${fmt(13)}]\n' - '[${fmt(2)}, ${fmt(6)}, ${fmt(10)}, ${fmt(14)}]\n' - '[${fmt(3)}, ${fmt(7)}, ${fmt(11)}, ${fmt(15)}]'; - } else { - return super.toString(); - } + result = '[${fmt(0)}, ${fmt(4)}, ${fmt(8)}, ${fmt(12)}]\n' + '[${fmt(1)}, ${fmt(5)}, ${fmt(9)}, ${fmt(13)}]\n' + '[${fmt(2)}, ${fmt(6)}, ${fmt(10)}, ${fmt(14)}]\n' + '[${fmt(3)}, ${fmt(7)}, ${fmt(11)}, ${fmt(15)}]'; + return true; + }()); + return result; } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/view_embedder/embedding_strategy/full_page_embedding_strategy.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/view_embedder/embedding_strategy/full_page_embedding_strategy.dart index 27dd8052c5..d8db4f82f2 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/view_embedder/embedding_strategy/full_page_embedding_strategy.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/view_embedder/embedding_strategy/full_page_embedding_strategy.dart @@ -3,7 +3,7 @@ // found in the LICENSE file. import 'package:ui/src/engine/dom.dart'; -import 'package:ui/src/engine/util.dart' show assertionsEnabled, setElementStyle; +import 'package:ui/src/engine/util.dart' show setElementStyle; import 'embedding_strategy.dart'; @@ -84,7 +84,7 @@ class FullPageEmbeddingStrategy extends EmbeddingStrategy { void _applyViewportMeta() { for (final DomElement viewportMeta in domDocument.head!.querySelectorAll('meta[name="viewport"]')) { - if (assertionsEnabled) { + assert(() { // Filter out the meta tag that the engine placed on the page. This is // to avoid UI flicker during hot restart. Hot restart will clean up the // old meta tag synchronously with the first post-restart frame. @@ -95,7 +95,8 @@ class FullPageEmbeddingStrategy extends EmbeddingStrategy { 'with Flutter. This tag will be replaced.', ); } - } + return true; + }()); viewportMeta.remove(); } diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/native_memory_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/native_memory_test.dart index ea108bba61..beecd63db1 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/native_memory_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/native_memory_test.dart @@ -252,17 +252,19 @@ class TestJsConstructor implements JsConstructor { class TestCountedRefOwner implements StackTraceDebugger { TestCountedRefOwner(TestSkDeletable nativeObject) { - if (assertionsEnabled) { + assert(() { _debugStackTrace = StackTrace.current; - } + return true; + }()); ref = CountedRef( nativeObject, this, 'TestCountedRefOwner'); } TestCountedRefOwner.cloneOf(this.ref) { - if (assertionsEnabled) { + assert(() { _debugStackTrace = StackTrace.current; - } + return true; + }()); ref.ref(this); } diff --git a/engine/src/flutter/web_sdk/test/js_access_test.dart b/engine/src/flutter/web_sdk/test/js_access_test.dart index 7312569868..0500284dcc 100644 --- a/engine/src/flutter/web_sdk/test/js_access_test.dart +++ b/engine/src/flutter/web_sdk/test/js_access_test.dart @@ -31,13 +31,13 @@ const List _auditedLibraries = [ ]; Future main(List args) async { - bool areAssertionsEnabled = false; + bool shouldThrow = true; assert(() { - areAssertionsEnabled = true; + shouldThrow = false; return true; }()); - if (!areAssertionsEnabled) { + if (shouldThrow) { throw ArgumentError( 'This test must run with --enable-asserts', );