[dart2wasm] Return `null` in `void` typed interop functions
Instead of boxing the return values just return `null`.
Also drop the result type from the imports when the return type is
`void`.
Change-Id: I871717480e516ac4cec260b2f9f4abe3dd5df535
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/424742
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
diff --git a/pkg/dart2wasm/lib/js/interop_specializer.dart b/pkg/dart2wasm/lib/js/interop_specializer.dart
index 2f1a298..b0bdd27 100644
--- a/pkg/dart2wasm/lib/js/interop_specializer.dart
+++ b/pkg/dart2wasm/lib/js/interop_specializer.dart
@@ -100,7 +100,9 @@
'dart2wasm.$jsMethodName',
FunctionNode(null,
positionalParameters: dartPositionalParameters,
- returnType: _util.nullableWasmExternRefType),
+ returnType: function.returnType is VoidType
+ ? VoidType()
+ : _util.nullableWasmExternRefType),
fileUri,
AnnotationType.import,
isExternal: true);
diff --git a/pkg/dart2wasm/lib/js/util.dart b/pkg/dart2wasm/lib/js/util.dart
index 4f8b8ab..c386ebf 100644
--- a/pkg/dart2wasm/lib/js/util.dart
+++ b/pkg/dart2wasm/lib/js/util.dart
@@ -326,14 +326,17 @@
Expression castInvocationForReturn(
Expression invocation, DartType returnType) {
Expression expression;
- if (returnType is VoidType || isJSValueType(returnType)) {
+ if (returnType is VoidType) {
// Technically a `void` return value can still be used, by casting the
// return type to `dynamic` or `Object?`. However this case should be
- // extremely rare, and `dartifyRaw` overhead for return values that will
+ // extremely rare, and `dartifyRaw` overhead for return values that should
// never be used in practice is too much, so we avoid `dartifyRaw` on
- // `void` returns. We still box the `externref` as the value can be passed
- // around as a Dart object.
+ // `void` returns and always return `null`.
+ return BlockExpression(
+ Block([ExpressionStatement(invocation)]), NullLiteral());
+ }
+ if (isJSValueType(returnType)) {
// TODO(joshualitt): Expose boxed `JSNull` and `JSUndefined` to Dart
// code after migrating existing users of js interop on Dart2Wasm.
// expression = _createJSValue(invocation);