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] Removes patchCanvasKitModule. #42941

Merged
merged 3 commits into from
Jun 27, 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
93 changes: 0 additions & 93 deletions lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3501,39 +3501,6 @@ extension SkPartialImageInfoExtension on SkPartialImageInfo {
double get width => _width.toDart;
}

/// 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
Expand All @@ -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';

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.',
);
});
});
}