diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart index 9ee1a22bc6..6e8d58c1b8 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart @@ -13,7 +13,7 @@ library canvaskit_api; import 'dart:async'; -import 'dart:js' as js; +import 'dart:js_util' as js_util; import 'dart:typed_data'; import 'package:js/js.dart'; @@ -2275,7 +2275,7 @@ abstract class Collector { class ProductionCollector implements Collector { ProductionCollector() { _skObjectFinalizationRegistry = - SkObjectFinalizationRegistry(js.allowInterop((SkDeletable deletable) { + SkObjectFinalizationRegistry(allowInterop((SkDeletable deletable) { // This is called when GC decides to collect the wrapper object and // notify us, which may happen after the object is already deleted // explicitly, e.g. when its ref count drops to zero. When that happens @@ -2530,6 +2530,28 @@ extension SkPartialImageInfoExtension on SkPartialImageInfo { external int get width; } +/// Helper interop methods for [patchCanvasKitModule]. +@JS() +external set _flutterWebCachedModule(Object? module); + +@JS() +external Object? get _flutterWebCachedModule; + +@JS() +external set _flutterWebCachedExports(Object? exports); + +@JS() +external Object? get _flutterWebCachedExports; + +@JS('Object') +external Object get objectConstructor; + +@JS() +external Object? get exports; + +@JS() +external Object? get module; + /// Monkey-patch the top-level `module` and `exports` objects so that /// CanvasKit doesn't attempt to register itself as an anonymous module. /// @@ -2553,40 +2575,39 @@ extension SkPartialImageInfoExtension on SkPartialImageInfo { void patchCanvasKitModule(DomHTMLScriptElement canvasKitScript) { // First check if `exports` and `module` are already defined. If so, then // CommonJS is being used, and we shouldn't have any problems. - final js.JsFunction objectConstructor = js.context['Object'] as js.JsFunction; - if (js.context['exports'] == null) { - final js.JsObject exportsAccessor = js.JsObject.jsify({ + if (exports == null) { + final Object? exportsAccessor = js_util.jsify({ 'get': allowInterop(() { if (domDocument.currentScript == canvasKitScript) { - return js.JsObject(objectConstructor); + return objectConstructor; } else { - return js.context['_flutterWebCachedExports']; + return _flutterWebCachedExports; } }), 'set': allowInterop((dynamic value) { - js.context['_flutterWebCachedExports'] = value; + _flutterWebCachedExports = value; }), 'configurable': true, }); - objectConstructor.callMethod( - 'defineProperty', [js.context, 'exports', exportsAccessor]); + js_util.callMethod(objectConstructor, + 'defineProperty', [domWindow, 'exports', exportsAccessor]); } - if (js.context['module'] == null) { - final js.JsObject moduleAccessor = js.JsObject.jsify({ + if (module == null) { + final Object? moduleAccessor = js_util.jsify({ 'get': allowInterop(() { if (domDocument.currentScript == canvasKitScript) { - return js.JsObject(objectConstructor); + return objectConstructor; } else { - return js.context['_flutterWebCachedModule']; + return _flutterWebCachedModule; } }), 'set': allowInterop((dynamic value) { - js.context['_flutterWebCachedModule'] = value; + _flutterWebCachedModule = value; }), 'configurable': true, }); - objectConstructor.callMethod( - 'defineProperty', [js.context, 'module', moduleAccessor]); + js_util.callMethod(objectConstructor, + 'defineProperty', [domWindow, 'module', moduleAccessor]); } domDocument.head!.appendChild(canvasKitScript); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/navigation/url_strategy.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/navigation/url_strategy.dart index 780c5a1c6a..c57bc39512 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/navigation/url_strategy.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/navigation/url_strategy.dart @@ -4,7 +4,6 @@ import 'dart:async'; -import 'package:js/js.dart' as js; import 'package:ui/ui.dart' as ui; import '../dom.dart'; @@ -159,7 +158,7 @@ class CustomUrlStrategy extends UrlStrategy { @override ui.VoidCallback addPopStateListener(DomEventListener fn) => - delegate.addPopStateListener(js.allowInterop(fn)); + delegate.addPopStateListener(allowInterop(fn)); @override String getPath() => delegate.getPath(); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/safe_browser_api.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/safe_browser_api.dart index 3b918a1731..78614cfde2 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/safe_browser_api.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/safe_browser_api.dart @@ -12,7 +12,6 @@ library browser_api; import 'dart:async'; -import 'dart:js' as js; import 'dart:js_util' as js_util; import 'dart:math' as math; import 'dart:typed_data'; @@ -25,12 +24,14 @@ import 'dom.dart'; import 'platform_dispatcher.dart'; import 'vector_math.dart'; +export 'package:js/js.dart' show allowInterop; + /// Creates JavaScript object populated with [properties]. /// /// This is equivalent to writing `{}` in plain JavaScript. Object createPlainJsObject([Map? properties]) { if (properties != null) { - return js.JsObject.jsify(properties); + return js_util.jsify(properties); } else { return js_util.newObject(); } @@ -70,11 +71,6 @@ T setJsProperty(Object object, String name, T value) { return js_util.setProperty(object, name, value); } -/// Wraps function [f] to be callable from JavaScript. -F allowInterop(F f) { - return js.allowInterop(f); -} - /// Converts a JavaScript `Promise` into Dart [Future]. Future promiseToFuture(Object jsPromise) { return js_util.promiseToFuture(jsPromise); diff --git a/engine/src/flutter/lib/web_ui/test/engine/profiler_test.dart b/engine/src/flutter/lib/web_ui/test/engine/profiler_test.dart index 9d96f54b9b..ec53fb9399 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/profiler_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/profiler_test.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:js' as js; import 'dart:js_util' as js_util; import 'package:test/bootstrap/browser.dart'; @@ -162,7 +161,7 @@ void jsOnBenchmark(dynamic listener) { domWindow, '_flutter_internal_on_benchmark', listener is Function - ? js.allowInterop(listener) + ? allowInterop(listener) : listener, ); } diff --git a/engine/src/flutter/lib/web_ui/test/initialization_test.dart b/engine/src/flutter/lib/web_ui/test/initialization_test.dart index 93806b77ed..37cada5529 100644 --- a/engine/src/flutter/lib/web_ui/test/initialization_test.dart +++ b/engine/src/flutter/lib/web_ui/test/initialization_test.dart @@ -2,18 +2,24 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:js' as js; - +import 'package:js/js.dart'; +import 'package:js/js_util.dart' as js_util; import 'package:test/bootstrap/browser.dart'; import 'package:test/test.dart'; import 'package:ui/src/engine.dart' as engine; import 'package:ui/ui.dart' as ui; +@JS('_flutter') +external set loader(Object? loader); + +@JS('_flutter.loader.didCreateEngineInitializer') +external set didCreateEngineInitializer(Object? callback); + void main() { // Prepare _flutter.loader.didCreateEngineInitializer, so it's ready in the page ASAP. - js.context['_flutter'] = js.JsObject.jsify({ + loader = js_util.jsify({ 'loader': { - 'didCreateEngineInitializer': js.allowInterop(() { print('not mocked'); }), + 'didCreateEngineInitializer': allowInterop(() { print('not mocked'); }), }, }); internalBootstrapBrowserTest(() => testMain); @@ -21,14 +27,14 @@ void main() { void testMain() { test('webOnlyWarmupEngine calls _flutter.loader.didCreateEngineInitializer callback', () async { - js.JsObject? engineInitializer; + Object? engineInitializer; - void didCreateEngineInitializerMock (js.JsObject obj) { + void didCreateEngineInitializerMock(Object? obj) { engineInitializer = obj; } // Prepare the DOM for: _flutter.loader.didCreateEngineInitializer - js.context['_flutter']['loader']['didCreateEngineInitializer'] = js.allowInterop(didCreateEngineInitializerMock); + didCreateEngineInitializer = allowInterop(didCreateEngineInitializerMock); // Reset the engine engine.debugResetEngineInitializationState(); @@ -40,12 +46,12 @@ void testMain() { // Check that the object we captured is actually a loader expect(engineInitializer, isNotNull); - expect(engineInitializer!.hasProperty('initializeEngine'), isTrue, reason: 'Missing FlutterEngineInitializer method: initializeEngine.'); - expect(engineInitializer!.hasProperty('autoStart'), isTrue, reason: 'Missing FlutterEngineInitializer method: autoStart.'); + expect(js_util.hasProperty(engineInitializer!, 'initializeEngine'), isTrue, reason: 'Missing FlutterEngineInitializer method: initializeEngine.'); + expect(js_util.hasProperty(engineInitializer!, 'autoStart'), isTrue, reason: 'Missing FlutterEngineInitializer method: autoStart.'); }); test('webOnlyWarmupEngine does auto-start when _flutter.loader.didCreateEngineInitializer does not exist', () async { - js.context['_flutter']['loader'] = null; + loader = null; bool pluginsRegistered = false; bool appRan = false; diff --git a/engine/src/flutter/web_sdk/sdk_rewriter.dart b/engine/src/flutter/web_sdk/sdk_rewriter.dart index 3ab50d5dfc..39da4b6dfe 100644 --- a/engine/src/flutter/web_sdk/sdk_rewriter.dart +++ b/engine/src/flutter/web_sdk/sdk_rewriter.dart @@ -42,7 +42,6 @@ import 'dart:async'; import 'dart:collection'; import 'dart:convert' hide Codec; import 'dart:developer' as developer; -import 'dart:js' as js; import 'dart:js_util' as js_util; import 'dart:_js_annotations'; import 'dart:math' as math; 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 dc45714f85..ee41a1e83d 100644 --- a/engine/src/flutter/web_sdk/test/js_access_test.dart +++ b/engine/src/flutter/web_sdk/test/js_access_test.dart @@ -16,7 +16,6 @@ import 'package:test/test.dart'; // Libraries that allow making arbitrary calls to JavaScript. const List _jsAccessLibraries = [ - 'dart:js', 'dart:js_util', 'package:js', ]; diff --git a/engine/src/flutter/web_sdk/test/sdk_rewriter_test.dart b/engine/src/flutter/web_sdk/test/sdk_rewriter_test.dart index 383db27eba..4d3cccc8a9 100644 --- a/engine/src/flutter/web_sdk/test/sdk_rewriter_test.dart +++ b/engine/src/flutter/web_sdk/test/sdk_rewriter_test.dart @@ -30,7 +30,6 @@ import 'dart:async'; import 'dart:collection'; import 'dart:convert' hide Codec; import 'dart:developer' as developer; -import 'dart:js' as js; import 'dart:js_util' as js_util; import 'dart:_js_annotations'; import 'dart:math' as math;