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

Fix Dart closures cannot be encoded when using the new Dart JS interop #2191

Merged
merged 12 commits into from
Jul 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 2.1.0

* Please refer to https://fzyzcjy.github.io/flutter_rust_bridge/guides/miscellaneous/whats-new for what's changed in V2.
* Add the ability to generate plugins from the CLI tool #2144 (thanks @mcmah309)
* Fix codegen halt when having boxed trait objects #2180
* Add attribute `#[frb(dart_async)]` #2181
* Fix Dart closures cannot be encoded when using the new Dart JS interop #2191
* Expose Rust executor's async runtime for customization #2151
* Generate methods of Default trait #2150
* Automatically rename function names to avoid keyword conflict #2150
* Improve parsing trait impl in third party crates #2150
* Support more attributes #2140
* Fix generated use statement pointing to self #2140
* Fix linter warning avoid_return_types_on_setters #2140
* Reduce number of generated files #2140
* Add default_dart_async configuration option #2139
* Handle root module scenario in pub use transformer #2124
* Improve hints when fails to parse a struct or enum #2124

## 2.0.0

* Please refer to https://fzyzcjy.github.io/flutter_rust_bridge/guides/miscellaneous/whats-new for what's changed in V2.
Expand Down
6 changes: 4 additions & 2 deletions frb_dart/lib/src/dart_opaque/_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ Object decodeDartOpaque(
return generalizedFrbRustBinding.dartOpaqueRust2DartDecode(raw);
}

/// {@macro flutter_rust_bridge.only_for_generated_code}
PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort,
/// {@macro flutter_rust_bridge.internal}
PlatformPointer encodeDartOpaqueCommon(
Object raw,
NativePortType dartHandlerPort,
GeneralizedFrbRustBinding generalizedFrbRustBinding) {
return generalizedFrbRustBinding.dartOpaqueDart2RustEncode(
raw, dartHandlerPort);
Expand Down
7 changes: 7 additions & 0 deletions frb_dart/lib/src/dart_opaque/_io.dart
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import 'package:flutter_rust_bridge/src/dart_opaque/_common.dart';
import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart';
import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart';

/// {@macro flutter_rust_bridge.only_for_generated_code}
PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort,
GeneralizedFrbRustBinding generalizedFrbRustBinding) =>
encodeDartOpaqueCommon(raw, dartHandlerPort, generalizedFrbRustBinding);
18 changes: 18 additions & 0 deletions frb_dart/lib/src/dart_opaque/_web.dart
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
import 'dart:js_util';

import 'package:flutter_rust_bridge/src/dart_opaque/_common.dart';
import 'package:flutter_rust_bridge/src/generalized_frb_rust_binding/generalized_frb_rust_binding.dart';
import 'package:flutter_rust_bridge/src/platform_types/platform_types.dart';

/// {@macro flutter_rust_bridge.only_for_generated_code}
PlatformPointer encodeDartOpaque(Object raw, NativePortType dartHandlerPort,
GeneralizedFrbRustBinding generalizedFrbRustBinding) =>
encodeDartOpaqueCommon(
_prepareDartOpaque(raw), dartHandlerPort, generalizedFrbRustBinding);

Object _prepareDartOpaque(Object raw) {
// #2183
if (raw is Function) {
return allowInterop(raw);
}
return raw;
}
8 changes: 4 additions & 4 deletions frb_example/pure_dart/test/api/dart_fn_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinNormal', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinNormal(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinNormal', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output =
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinNormal(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinNormal', () async {
Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart/test/api/dart_opaque_sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('loopback', () {
var syncBack = syncLoopbackTwinNormal(opaque: f);
expect(
identical(syncOptionLoopbackTwinNormal(opaque: syncBack), f), isTrue);
expect((syncOptionLoopbackTwinNormal(opaque: syncBack) as dynamic)(),
'Test_String');
expect(syncOptionLoopbackTwinNormal(opaque: null), isNull);
});

Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart/test/api/dart_opaque_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinNormal(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -55,7 +55,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinNormal(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinRustAsyncSse', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinRustAsyncSse(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinRustAsyncSse', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output =
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinRustAsyncSse(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinRustAsyncSse', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinRustAsync', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinRustAsync(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinRustAsync', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output =
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinRustAsync(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinRustAsync', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinSse', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinSse(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinSse', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output = await rustCallDartWithDartOpaqueResultTwinSse(callback: () {
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinSse(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinSse', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('loopback', () {
var syncBack = syncLoopbackTwinSse(opaque: f);
expect(identical(syncOptionLoopbackTwinSse(opaque: syncBack), f), isTrue);
expect((syncOptionLoopbackTwinSse(opaque: syncBack) as dynamic)(),
'Test_String');
expect(syncOptionLoopbackTwinSse(opaque: null), isNull);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
var back2 =
await loopBackTwinRustAsyncSse(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -67,7 +67,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinRustAsyncSse(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinRustAsync(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -62,7 +62,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinRustAsync(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinSse(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -58,7 +58,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinSse(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinSyncSse(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -60,7 +60,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinSyncSse(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinSync(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -59,7 +59,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinSync(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
8 changes: 4 additions & 4 deletions frb_example/pure_dart_pde/test/api/dart_fn_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinNormal', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinNormal(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinNormal', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output =
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinNormal(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinNormal', () async {
Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart_pde/test/api/dart_opaque_sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('loopback', () {
var syncBack = syncLoopbackTwinNormal(opaque: f);
expect(
identical(syncOptionLoopbackTwinNormal(opaque: syncBack), f), isTrue);
expect((syncOptionLoopbackTwinNormal(opaque: syncBack) as dynamic)(),
'Test_String');
expect(syncOptionLoopbackTwinNormal(opaque: null), isNull);
});

Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart_pde/test/api/dart_opaque_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
expect(back1(), 'Test_String');
var back2 = await loopBackTwinNormal(opaque: back1) as String Function();
expect(back2(), 'Test_String');
expect(identical(back2, f), isTrue);
if (!kIsWeb) expect(identical(back2, f), isTrue);
});

test('drop', () async {
Expand Down Expand Up @@ -57,7 +57,7 @@ Future<void> main({bool skipRustLibInit = false}) async {
final output = await cloneDartOpaqueTwinNormal(opaque: opaque);
expect(output.length, 10);
for (final x in output) {
expect(identical(x, opaque), true);
if (!kIsWeb) expect(identical(x, opaque), true);
expect((x as Function)(42), 42 + 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ Future<void> main({bool skipRustLibInit = false}) async {

test('rustCallDartWithDartOpaqueArgTwinRustAsync', () async {
final opaque = (String whatever) => 42;
var callbackCalls = <Object>[];
var callbackCalls = <dynamic>[];
await rustCallDartWithDartOpaqueArgTwinRustAsync(
input: opaque, callback: (arg) => callbackCalls.add(arg));
expect(callbackCalls, [opaque]);
expect(callbackCalls[0]('hello'), 42);
});

test('rustCallDartWithDartOpaqueResultTwinRustAsync', () async {
final opaque = (String whatever) => 42;
var callCount = 0;
final output =
final dynamic output =
await rustCallDartWithDartOpaqueResultTwinRustAsync(callback: () {
callCount++;
return opaque;
});
expect(callCount, 1);
expect(output, opaque);
expect(output('hello'), 42);
});

test('rustCallDartMultiTimesTwinRustAsync', () async {
Expand Down
Loading
Loading