Skip to content

Commit 6828898

Browse files
eyebrowsoffireloic-sharma
authored andcommitted
Some fixes for dart2wasm (flutter#38167)
* A few fixes to get things compiling via dart2wasm. * Use `futureToPromise`. * Some more fixes for dart2wasm. * Allow promises to have nullable objects.
1 parent 7dbece4 commit 6828898

File tree

6 files changed

+44
-35
lines changed

6 files changed

+44
-35
lines changed

lib/web_ui/lib/src/engine/app_bootstrap.dart

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,17 @@ class AppBootstrap {
4040
// This is a convenience method that lets the programmer call "autoStart"
4141
// from JavaScript immediately after the main.dart.js has loaded.
4242
// Returns a promise that resolves to the Flutter app that was started.
43-
autoStart: allowInterop(() {
44-
return Promise<FlutterApp>(allowInterop((
45-
PromiseResolver<FlutterApp> resolve,
46-
PromiseRejecter _,
47-
) async {
48-
await autoStart();
49-
// Return the App that was just started
50-
resolve(_prepareFlutterApp());
51-
}));
52-
}),
43+
autoStart: allowInterop(() => futureToPromise(() async {
44+
await autoStart();
45+
// Return the App that was just started
46+
return _prepareFlutterApp();
47+
}())),
5348
// Calls [_initEngine], and returns a JS Promise that resolves to an
5449
// app runner object.
55-
initializeEngine: allowInterop(([JsFlutterConfiguration? configuration]) {
56-
// `params` coming from Javascript may be used to configure the engine intialization.
57-
// The internal `initEngine` function must accept those params.
58-
return Promise<FlutterAppRunner>(allowInterop((
59-
PromiseResolver<FlutterAppRunner> resolve,
60-
PromiseRejecter _,
61-
) async {
62-
await _initializeEngine(configuration);
63-
// Return an app runner object
64-
resolve(_prepareAppRunner());
65-
}));
66-
}),
50+
initializeEngine: allowInterop(([JsFlutterConfiguration? configuration]) => futureToPromise(() async {
51+
await _initializeEngine(configuration);
52+
return _prepareAppRunner();
53+
}()))
6754
);
6855
}
6956

@@ -77,7 +64,7 @@ class AppBootstrap {
7764
) async {
7865
await _runApp();
7966
// Return the App that was just started
80-
resolve(_prepareFlutterApp());
67+
resolve.resolve(_prepareFlutterApp());
8168
}));
8269
}));
8370
}

lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ class ColorSpace {}
176176
@staticInterop
177177
class SkWebGLContextOptions {
178178
external factory SkWebGLContextOptions({
179-
required int antialias,
179+
required double antialias,
180180
// WebGL version: 1 or 2.
181-
required int majorVersion,
181+
required double majorVersion,
182182
});
183183
}
184184

@@ -1362,7 +1362,8 @@ final SkFloat32List _sharedSkColor3 = mallocFloat32List(4);
13621362
@JS('window.flutterCanvasKit.Path')
13631363
@staticInterop
13641364
class SkPath {
1365-
external factory SkPath([SkPath? other]);
1365+
external factory SkPath();
1366+
external factory SkPath.from(SkPath other);
13661367
}
13671368

13681369
extension SkPathExtension on SkPath {

lib/web_ui/lib/src/engine/canvaskit/surface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class Surface {
325325
// Default to no anti-aliasing. Paint commands can be explicitly
326326
// anti-aliased by setting their `Paint` object's `antialias` property.
327327
antialias: _kUsingMSAA ? 1 : 0,
328-
majorVersion: webGLVersion,
328+
majorVersion: webGLVersion.toDouble(),
329329
),
330330
).toInt();
331331

lib/web_ui/lib/src/engine/js_interop/js_loader.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class FlutterEngineInitializer{
5454
/// [JsFlutterConfiguration] comes from `../configuration.dart`. It is the same
5555
/// object that can be used to configure flutter "inline", through the
5656
/// (to be deprecated) `window.flutterConfiguration` object.
57-
typedef InitializeEngineFn = Promise<FlutterAppRunner?> Function([JsFlutterConfiguration?]);
57+
typedef InitializeEngineFn = Promise<FlutterAppRunner> Function([JsFlutterConfiguration?]);
5858

5959
/// Typedef for the `autoStart` function that can be called straight from an engine initializer instance.
6060
/// (Similar to [RunAppFn], but taking no specific "runApp" parameters).

lib/web_ui/lib/src/engine/js_interop/js_promise.dart

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,39 @@
66
library js_promise;
77

88
import 'package:js/js.dart';
9+
import 'package:js/js_util.dart' as js_util;
10+
11+
@JS()
12+
@staticInterop
13+
class PromiseResolver<T extends Object?> {}
14+
15+
extension PromiseResolverExtension<T extends Object?> on PromiseResolver<T> {
16+
void resolve(T result) => js_util.callMethod(this, 'call', <Object>[this, if (result != null) result]);
17+
}
18+
19+
@JS()
20+
@staticInterop
21+
class PromiseRejecter {}
22+
23+
extension PromiseRejecterExtension on PromiseRejecter {
24+
void reject(Object? error) => js_util.callMethod(this, 'call', <Object>[this, if (error != null) error]);
25+
}
926

1027
/// Type-safe JS Promises
1128
@JS('Promise')
1229
@staticInterop
13-
abstract class Promise<T> {
30+
abstract class Promise<T extends Object?> {
1431
/// A constructor for a JS promise
1532
external factory Promise(PromiseExecutor<T> executor);
1633
}
1734

1835
/// The type of function that is used to create a Promise<T>
19-
typedef PromiseExecutor<T> = void Function(PromiseResolver<T> resolve, PromiseRejecter reject);
20-
/// The type of function used to resolve a Promise<T>
21-
typedef PromiseResolver<T> = void Function(T result);
22-
/// The type of function used to reject a Promise (of any <T>)
23-
typedef PromiseRejecter = void Function(Object? error);
36+
typedef PromiseExecutor<T extends Object?> = void Function(PromiseResolver<T> resolve, PromiseRejecter reject);
37+
38+
Promise<T> futureToPromise<T extends Object>(Future<T> future) {
39+
return Promise<T>(allowInterop((PromiseResolver<T> resolver, PromiseRejecter rejecter) {
40+
future.then(
41+
(T value) => resolver.resolve(value),
42+
onError: (Object? error) => rejecter.reject(error));
43+
}));
44+
}

lib/web_ui/lib/src/engine/navigation/history.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class MultiEntriesBrowserHistory extends BrowserHistory {
145145
if (_hasSerialCount(currentState)) {
146146
final Map<dynamic, dynamic> stateMap =
147147
currentState! as Map<dynamic, dynamic>;
148-
return stateMap['serialCount'] as int;
148+
return (stateMap['serialCount'] as double).toInt();
149149
}
150150
return 0;
151151
}

0 commit comments

Comments
 (0)