From af5ad81ca78f6f7494e85f3c50891e111619471e Mon Sep 17 00:00:00 2001 From: David Iglesias Date: Mon, 26 Jun 2023 17:01:21 -0700 Subject: [PATCH] [web] Removes patchCanvasKitModule. (flutter/engine#42941) **This must land _after_ https://github.com/flutter/flutter/pull/129032** Flutter web uses requireJS in `debug` mode to assemble a DDC-compiled app from a bunch of small files ("modules"). This caused that `canvaskit.js` (then, but probably all other modules that used a browserify-like loading header) didn't work because it attempted to use the `define` function provided by Flutter's instance of `requireJS` (which kept the defined modules private, rather than as globals on the page, as the users of the JS expected). A [fix](https://github.com/flutter/engine/pull/27342) was added to `flutter/engine` to trick loaders into *not* using the `requireJS` module loader, but a recent change in the fix's js-interop layer *subtly* changed its JS output on the page (objects went from `undefined` to `null`), causing this: * https://github.com/flutter/flutter/issues/126131 (and others) After flutter/flutter#129032, the engine fix shouldn't be required anymore, so this PR removes it. ## Issues * Fixes https://github.com/flutter/flutter/issues/126131 (and possibly others) ## Testing * Manually tested with some test apps, and miscellanous JS scripts as reported by users. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../src/engine/canvaskit/canvaskit_api.dart | 93 ------------------- .../lib/web_ui/lib/src/engine/dom.dart | 4 + .../does_not_mock_module_exports_test.dart | 39 ++++++++ ..._ui_test.dart => services_vs_ui_test.dart} | 0 ...nfig_test.dart => stores_config_test.dart} | 0 5 files changed, 43 insertions(+), 93 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart rename engine/src/flutter/lib/web_ui/test/canvaskit/initialization/{initialization_services_vs_ui_test.dart => services_vs_ui_test.dart} (100%) rename engine/src/flutter/lib/web_ui/test/canvaskit/initialization/{initialization_stores_config_test.dart => stores_config_test.dart} (100%) 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 be0e8bc19e..cc2c295cf7 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 @@ -3501,39 +3501,6 @@ extension SkPartialImageInfoExtension on SkPartialImageInfo { double get width => _width.toDartDouble; } -/// Helper interop methods for [patchCanvasKitModule]. -@JS() -external set __flutterWebCachedModule(JSAny? module); -set _flutterWebCachedModule(Object? module) => - __flutterWebCachedModule = module?.toJSAnyShallow; - -@JS() -external JSAny? get __flutterWebCachedModule; -Object? get _flutterWebCachedModule => - __flutterWebCachedModule?.toObjectShallow; - -@JS() -external set __flutterWebCachedExports(JSAny? exports); -set _flutterWebCachedExports(Object? exports) => - __flutterWebCachedExports = exports?.toJSAnyShallow; - -@JS() -external JSAny? get __flutterWebCachedExports; -Object? get _flutterWebCachedExports => - __flutterWebCachedExports?.toObjectShallow; - -@JS('Object') -external JSAny get _objectConstructor; -Object get objectConstructor => _objectConstructor.toObjectShallow; - -@JS('exports') -external JSAny? get _exports; -Object? get exports => _exports?.toObjectShallow; - -@JS('module') -external JSAny? get _module; -Object? get module => _module?.toObjectShallow; - @JS('window.flutterCanvasKit.RuntimeEffect') @anonymous @staticInterop @@ -3558,65 +3525,6 @@ extension SkSkRuntimeEffectExtension on SkRuntimeEffect { children.toJSAnyShallow); } -/// Monkey-patch the top-level `module` and `exports` objects so that -/// CanvasKit doesn't attempt to register itself as an anonymous module. -/// -/// The idea behind making these fake `exports` and `module` objects is -/// that `canvaskit.js` contains the following lines of code: -/// -/// if (typeof exports === 'object' && typeof module === 'object') -/// module.exports = CanvasKitInit; -/// else if (typeof define === 'function' && define['amd']) -/// define([], function() { return CanvasKitInit; }); -/// -/// We need to avoid hitting the case where CanvasKit defines an anonymous -/// module, since this breaks RequireJS, which DDC and some plugins use. -/// Temporarily removing the `define` function won't work because RequireJS -/// could load in between this code running and the CanvasKit code running. -/// Also, we cannot monkey-patch the `define` function because it is -/// non-configurable (it is a top-level 'var'). -// TODO(hterkelsen): Rather than this monkey-patch hack, we should -// build CanvasKit ourselves. See: -// https://github.com/flutter/flutter/issues/52588 -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. - if (exports == null) { - final Object? exportsAccessor = js_util.jsify({ - 'get': js_util.allowInterop(() { - if (domDocument.currentScript == canvasKitScript) { - return js_util.callConstructor(objectConstructor, []); - } else { - return _flutterWebCachedExports; - } - }), - 'set': js_util.allowInterop((dynamic value) { - _flutterWebCachedExports = value; - }), - 'configurable': true, - }); - js_util.callMethod(objectConstructor, - 'defineProperty', [domWindow, 'exports', exportsAccessor]); - } - if (module == null) { - final Object? moduleAccessor = js_util.jsify({ - 'get': js_util.allowInterop(() { - if (domDocument.currentScript == canvasKitScript) { - return js_util.callConstructor(objectConstructor, []); - } else { - return _flutterWebCachedModule; - } - }), - 'set': js_util.allowInterop((dynamic value) { - _flutterWebCachedModule = value; - }), - 'configurable': true, - }); - js_util.callMethod(objectConstructor, - 'defineProperty', [domWindow, 'module', moduleAccessor]); - } -} - const String _kFullCanvasKitJsFileName = 'canvaskit.js'; const String _kChromiumCanvasKitJsFileName = 'chromium/canvaskit.js'; @@ -3711,7 +3619,6 @@ Future _downloadCanvasKitJs(String url) { canvasKitScript.addEventListener('load', loadCallback); canvasKitScript.addEventListener('error', errorCallback); - patchCanvasKitModule(canvasKitScript); domDocument.head!.appendChild(canvasKitScript); return canvasKitLoadCompleter.future; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/dom.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/dom.dart index b8b24a4239..39dd0b355e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/dom.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/dom.dart @@ -60,6 +60,10 @@ extension JSAnyToObjectExtension on JSAny { Object get toObjectDeep => js_util.dartify(this)!; } +@JS('Object') +external JSAny get _objectConstructor; +Object get objectConstructor => _objectConstructor.toObjectShallow; + @JS() @staticInterop class DomWindow extends DomEventTarget {} diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart new file mode 100644 index 0000000000..c06e3877db --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart @@ -0,0 +1,39 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +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'; + +void main() { + internalBootstrapBrowserTest(() => testMain); +} + +void testMain() { + group('initializeEngineServices', () { + test('does not mock module loaders', () async { + // Initialize CanvasKit... + await initializeEngineServices(); + + // CanvasKitInit should be defined... + expect( + js_util.hasProperty(domWindow, 'CanvasKitInit'), + isTrue, + reason: 'CanvasKitInit should be defined on Window', + ); + + // window.exports and window.module should be undefined! + expect( + js_util.hasProperty(domWindow, 'exports'), + isFalse, + reason: '`window.exports` should not be defined.', + ); + expect( + js_util.hasProperty(domWindow, 'module'), + isFalse, + reason: '`window.module` should not be defined.', + ); + }); + }); +} diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/initialization_services_vs_ui_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/services_vs_ui_test.dart similarity index 100% rename from engine/src/flutter/lib/web_ui/test/canvaskit/initialization/initialization_services_vs_ui_test.dart rename to engine/src/flutter/lib/web_ui/test/canvaskit/initialization/services_vs_ui_test.dart diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/initialization_stores_config_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/initialization/stores_config_test.dart similarity index 100% rename from engine/src/flutter/lib/web_ui/test/canvaskit/initialization/initialization_stores_config_test.dart rename to engine/src/flutter/lib/web_ui/test/canvaskit/initialization/stores_config_test.dart