[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
This commit is contained in:
David Iglesias
2023-06-26 17:01:21 -07:00
committed by GitHub
parent 34e09d7ff0
commit af5ad81ca7
5 changed files with 43 additions and 93 deletions

View File

@@ -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(<String, dynamic>{
'get': js_util.allowInterop(() {
if (domDocument.currentScript == canvasKitScript) {
return js_util.callConstructor(objectConstructor, <Object>[]);
} else {
return _flutterWebCachedExports;
}
}),
'set': js_util.allowInterop((dynamic value) {
_flutterWebCachedExports = value;
}),
'configurable': true,
});
js_util.callMethod(objectConstructor,
'defineProperty', <dynamic>[domWindow, 'exports', exportsAccessor]);
}
if (module == null) {
final Object? moduleAccessor = js_util.jsify(<String, dynamic>{
'get': js_util.allowInterop(() {
if (domDocument.currentScript == canvasKitScript) {
return js_util.callConstructor(objectConstructor, <Object>[]);
} else {
return _flutterWebCachedModule;
}
}),
'set': js_util.allowInterop((dynamic value) {
_flutterWebCachedModule = value;
}),
'configurable': true,
});
js_util.callMethod(objectConstructor,
'defineProperty', <dynamic>[domWindow, 'module', moduleAccessor]);
}
}
const String _kFullCanvasKitJsFileName = 'canvaskit.js';
const String _kChromiumCanvasKitJsFileName = 'chromium/canvaskit.js';
@@ -3711,7 +3619,6 @@ Future<bool> _downloadCanvasKitJs(String url) {
canvasKitScript.addEventListener('load', loadCallback);
canvasKitScript.addEventListener('error', errorCallback);
patchCanvasKitModule(canvasKitScript);
domDocument.head!.appendChild(canvasKitScript);
return canvasKitLoadCompleter.future;

View File

@@ -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 {}

View File

@@ -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.',
);
});
});
}