diff --git a/.github/workflows/code_generator.yml b/.github/workflows/code_generator.yml index 4a19a96d24..9ef1f715c0 100644 --- a/.github/workflows/code_generator.yml +++ b/.github/workflows/code_generator.yml @@ -91,6 +91,7 @@ jobs: --dart-format-line-length 120 --rust-output ../frb_example/pure_dart_multi/rust/src/generated_api_1.rs ../frb_example/pure_dart_multi/rust/src/generated_api_2.rs --class-name ApiClass1 ApiClass2 + --wasm env: RUST_LOG: debug - name: Run codegen to pure_dart_multi example (windows) @@ -102,6 +103,7 @@ jobs: --dart-format-line-length 120 --rust-output ..\\frb_example\\pure_dart_multi\\rust\\src\\generated_api_1.rs ..\\frb_example\\pure_dart_multi\\rust\\src\\generated_api_2.rs --class-name ApiClass1 ApiClass2 + --wasm env: RUST_LOG: debug diff --git a/book/src/feature/misc.md b/book/src/feature/misc.md index cde23eca6d..5fec37b039 100644 --- a/book/src/feature/misc.md +++ b/book/src/feature/misc.md @@ -4,4 +4,22 @@ The generated `bridge_generated.dart` by default contains definitions of the APIs as well as the implementations. With the flag `--dart-decl-output`, the two can be separated, and the definitions will not contain anything like `dart:ffi`. +A command example as follow: + +```shell +flutter_rust_bridge_codegen .. --dart-decl-output +``` + +where `DECL` is the path to the common class/function declarations file. +For example, if you emit your Dart bridge to `lib/bridge_generated.dart`, +you can put the declarations file at `lib/bridge_definitions.dart` + + +By default this will create new file: + +``` +├── lib +│ ├── bridge_definitions.dart +``` + More information: [#298](https://github.com/fzyzcjy/flutter_rust_bridge/issues/298). \ No newline at end of file diff --git a/book/src/feature/wasm.md b/book/src/feature/wasm.md index ee1de14473..7f56bcebc3 100644 --- a/book/src/feature/wasm.md +++ b/book/src/feature/wasm.md @@ -1,22 +1,17 @@ # WASM `flutter_rust_bridge_codegen` can also generate code to run in browsers using -`wasm_bindgen`. To generate WASM-specifc files, pass in these two options to your -invocation: +`wasm_bindgen`. To generate a WASM-specifc file, pass this option to your invocation: ```shell -flutter_rust_bridge_codegen .. --wasm --dart-decl-output +flutter_rust_bridge_codegen .. --wasm ``` -where `DECL` is the path to the common class/function declarations file. -For example, if you emit your Dart bridge to `lib/bridge_generated.dart`, -you can put the declarations file at `lib/bridge_definitions.dart` - By default this will create several new files: ``` ├── lib -│ ├── bridge_definitions.dart +│ ├── ... │ ├── bridge_generated.io.dart │ └── bridge_generated.web.dart └── native/src @@ -30,3 +25,5 @@ flag as well. Check out [Integrating with Web](../integrate/web.md) for instructions on how to consume the web bridge. + +have a look at [issue 860](https://github.com/fzyzcjy/flutter_rust_bridge/issues/860) \ No newline at end of file diff --git a/frb_codegen/src/config.rs b/frb_codegen/src/config.rs index 770f9f48ca..631a9619a4 100644 --- a/frb_codegen/src/config.rs +++ b/frb_codegen/src/config.rs @@ -64,7 +64,7 @@ pub struct RawOpts { pub verbose: bool, /// Enable WASM module generation. /// Requires: --dart-decl-output - #[clap(long, requires = "dart-decl-output")] + #[clap(long)] pub wasm: bool, /// Inline declaration of Rust bridge modules #[clap(long)] @@ -210,10 +210,7 @@ pub fn parse(raw: RawOpts) -> Vec { .map(|s| canon_path(s.as_str())); let dart_format_line_length = raw.dart_format_line_length; let llvm_paths = get_llvm_paths(&raw.llvm_path); - let llvm_compiler_opts = raw - .llvm_compiler_opts - .clone() - .unwrap_or_else(|| "".to_string()); + let llvm_compiler_opts = raw.llvm_compiler_opts.clone().unwrap_or_default(); let skip_add_mod_to_lib = raw.skip_add_mod_to_lib; let build_runner = !raw.no_build_runner; let wasm = raw.wasm; diff --git a/frb_codegen/src/lib.rs b/frb_codegen/src/lib.rs index 0f50e52df8..faf3542837 100644 --- a/frb_codegen/src/lib.rs +++ b/frb_codegen/src/lib.rs @@ -57,7 +57,7 @@ pub fn frb_codegen(config: &config::Opts, all_symbols: &[String]) -> anyhow::Res let ir_file = transformer::transform(raw_ir_file); info!("Phase: Generate Rust code"); - fs::create_dir_all(&rust_output_dir)?; + fs::create_dir_all(rust_output_dir)?; let generated_rust = ir_file.generate_rust(config); write_rust_modules(config, &generated_rust)?; @@ -115,12 +115,12 @@ pub fn frb_codegen(config: &config::Opts, all_symbols: &[String]) -> anyhow::Res for output in &config.c_output_path { fs::create_dir_all(Path::new(output).parent().unwrap())?; fs::write( - &output, + output, fs::read_to_string(&temp_bindgen_c_output_file)? + "\n" + &c_dummy_code, )?; } - fs::create_dir_all(&dart_output_dir)?; + fs::create_dir_all(dart_output_dir)?; let generated_dart_wire_code_raw = fs::read_to_string(temp_dart_wire_file)?; let generated_dart_wire = extract_dart_wire_content(&modify_dart_wire_content( &generated_dart_wire_code_raw, @@ -140,6 +140,22 @@ pub fn frb_codegen(config: &config::Opts, all_symbols: &[String]) -> anyhow::Res generated_dart_decl_all, &generated_dart_impl_io_wire, )?; + } else if config.wasm_enabled { + fs::write( + &config.dart_output_path, + (&generated_dart.file_prelude + + generated_dart_decl_all + + &generated_dart.impl_code.common) + .to_text(), + )?; + fs::write( + &config.dart_io_output_path(), + (&generated_dart.file_prelude + &generated_dart_impl_io_wire).to_text(), + )?; + fs::write( + config.dart_wasm_output_path(), + (&generated_dart.file_prelude + &generated_dart.impl_code.wasm).to_text(), + )?; } else { let mut out = generated_dart.file_prelude + generated_dart_decl_all @@ -222,7 +238,7 @@ fn write_dart_decls( }; fs::write( - &dart_decl_output_path, + dart_decl_output_path, (&generated_dart.file_prelude + &common_import + generated_dart_decl_all).to_text(), )?; if config.wasm_enabled { diff --git a/frb_codegen/src/source_graph.rs b/frb_codegen/src/source_graph.rs index 0dce3e4904..fb9adf8438 100644 --- a/frb_codegen/src/source_graph.rs +++ b/frb_codegen/src/source_graph.rs @@ -33,7 +33,7 @@ pub struct Crate { impl Crate { pub fn new(manifest_path: &str) -> Self { let mut cmd = MetadataCommand::new(); - cmd.manifest_path(&manifest_path); + cmd.manifest_path(manifest_path); let metadata = cmd.exec().unwrap(); diff --git a/frb_codegen/src/tools.rs b/frb_codegen/src/tools.rs index 2edc11b6aa..efc4567701 100644 --- a/frb_codegen/src/tools.rs +++ b/frb_codegen/src/tools.rs @@ -454,7 +454,7 @@ mod tests { that_package: 1.0.1 other_package: "; - let pubspec: Pubspec = serde_yaml::from_str(&yaml).expect("Failed to parse pubspec.yaml"); + let pubspec: Pubspec = serde_yaml::from_str(yaml).expect("Failed to parse pubspec.yaml"); let mut expected = HashMap::new(); expected.insert( "this_package".to_string(), diff --git a/frb_codegen/src/utils.rs b/frb_codegen/src/utils.rs index 1ad354f141..088a2a03b3 100644 --- a/frb_codegen/src/utils.rs +++ b/frb_codegen/src/utils.rs @@ -23,12 +23,12 @@ pub fn with_changed_file anyhow::Result<()>>( append_content: &str, f: F, ) -> anyhow::Result<()> { - let content_original = fs::read_to_string(&path)?; - fs::write(&path, content_original.clone() + append_content)?; + let content_original = fs::read_to_string(path)?; + fs::write(path, content_original.clone() + append_content)?; f()?; - Ok(fs::write(&path, content_original)?) + Ok(fs::write(path, content_original)?) } pub fn find_all_duplicates(iter: &[T]) -> Vec diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.dart index fc2bce0bc2..552ce1af2c 100644 --- a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.dart +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.dart @@ -6,9 +6,11 @@ import 'dart:convert'; import 'dart:async'; import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_1.io.dart' if (dart.library.html) 'bridge_generated_api_1.web.dart'; import 'package:meta/meta.dart'; -import 'package:meta/meta.dart'; -import 'dart:ffi' as ffi; abstract class ApiClass1 { /// Documentation on a simple adder function. @@ -55,76 +57,3 @@ int api2wire_i32(int raw) { return raw; } // Section: finalizer - -class ApiClass1Platform extends FlutterRustBridgeBase { - ApiClass1Platform(ffi.DynamicLibrary dylib) : super(ApiClass1Wire(dylib)); -// Section: api2wire - -// Section: finalizer - -// Section: api_fill_to_wire - -} - -// ignore_for_file: camel_case_types, non_constant_identifier_names, avoid_positional_boolean_parameters, annotate_overrides, constant_identifier_names - -// AUTO GENERATED FILE, DO NOT EDIT. -// -// Generated by `package:ffigen`. - -/// generated by flutter_rust_bridge -class ApiClass1Wire implements FlutterRustBridgeWireBase { - /// Holds the symbol lookup function. - final ffi.Pointer Function(String symbolName) _lookup; - - /// The symbols are looked up in [dynamicLibrary]. - ApiClass1Wire(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; - - /// The symbols are looked up with [lookup]. - ApiClass1Wire.fromLookup(ffi.Pointer Function(String symbolName) lookup) - : _lookup = lookup; - - void store_dart_post_cobject( - DartPostCObjectFnType ptr, - ) { - return _store_dart_post_cobject( - ptr, - ); - } - - late final _store_dart_post_cobjectPtr = - _lookup>('store_dart_post_cobject'); - late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction(); - - void wire_simple_adder_1( - int port_, - int a, - int b, - ) { - return _wire_simple_adder_1( - port_, - a, - b, - ); - } - - late final _wire_simple_adder_1Ptr = - _lookup>('wire_simple_adder_1'); - late final _wire_simple_adder_1 = _wire_simple_adder_1Ptr.asFunction(); - - void free_WireSyncReturnStruct( - WireSyncReturnStruct val, - ) { - return _free_WireSyncReturnStruct( - val, - ); - } - - late final _free_WireSyncReturnStructPtr = - _lookup>('free_WireSyncReturnStruct'); - late final _free_WireSyncReturnStruct = - _free_WireSyncReturnStructPtr.asFunction(); -} - -typedef DartPostCObjectFnType = ffi.Pointer)>>; -typedef DartPort = ffi.Int64; diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.io.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.io.dart new file mode 100644 index 0000000000..3a67779a49 --- /dev/null +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.io.dart @@ -0,0 +1,84 @@ +// AUTO GENERATED FILE, DO NOT EDIT. +// Generated by `flutter_rust_bridge`@ 1.50.0. +// ignore_for_file: non_constant_identifier_names, unused_element, duplicate_ignore, directives_ordering, curly_braces_in_flow_control_structures, unnecessary_lambdas, slash_for_doc_comments, prefer_const_literals_to_create_immutables, implicit_dynamic_list_literal, duplicate_import, unused_import, prefer_single_quotes, prefer_const_constructors, use_super_parameters, always_use_package_imports, annotate_overrides, invalid_use_of_protected_member, constant_identifier_names, invalid_use_of_internal_member + +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_1.dart'; +export 'bridge_generated_api_1.dart'; +import 'package:meta/meta.dart'; +import 'dart:ffi' as ffi; + +class ApiClass1Platform extends FlutterRustBridgeBase { + ApiClass1Platform(ffi.DynamicLibrary dylib) : super(ApiClass1Wire(dylib)); +// Section: api2wire + +// Section: finalizer + +// Section: api_fill_to_wire + +} + +// ignore_for_file: camel_case_types, non_constant_identifier_names, avoid_positional_boolean_parameters, annotate_overrides, constant_identifier_names + +// AUTO GENERATED FILE, DO NOT EDIT. +// +// Generated by `package:ffigen`. + +/// generated by flutter_rust_bridge +class ApiClass1Wire implements FlutterRustBridgeWireBase { + /// Holds the symbol lookup function. + final ffi.Pointer Function(String symbolName) _lookup; + + /// The symbols are looked up in [dynamicLibrary]. + ApiClass1Wire(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; + + /// The symbols are looked up with [lookup]. + ApiClass1Wire.fromLookup(ffi.Pointer Function(String symbolName) lookup) + : _lookup = lookup; + + void store_dart_post_cobject( + DartPostCObjectFnType ptr, + ) { + return _store_dart_post_cobject( + ptr, + ); + } + + late final _store_dart_post_cobjectPtr = + _lookup>('store_dart_post_cobject'); + late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction(); + + void wire_simple_adder_1( + int port_, + int a, + int b, + ) { + return _wire_simple_adder_1( + port_, + a, + b, + ); + } + + late final _wire_simple_adder_1Ptr = + _lookup>('wire_simple_adder_1'); + late final _wire_simple_adder_1 = _wire_simple_adder_1Ptr.asFunction(); + + void free_WireSyncReturnStruct( + WireSyncReturnStruct val, + ) { + return _free_WireSyncReturnStruct( + val, + ); + } + + late final _free_WireSyncReturnStructPtr = + _lookup>('free_WireSyncReturnStruct'); + late final _free_WireSyncReturnStruct = + _free_WireSyncReturnStructPtr.asFunction(); +} + +typedef DartPostCObjectFnType = ffi.Pointer)>>; +typedef DartPort = ffi.Int64; diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.web.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.web.dart new file mode 100644 index 0000000000..a1d493469b --- /dev/null +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.web.dart @@ -0,0 +1,42 @@ +// AUTO GENERATED FILE, DO NOT EDIT. +// Generated by `flutter_rust_bridge`@ 1.50.0. +// ignore_for_file: non_constant_identifier_names, unused_element, duplicate_ignore, directives_ordering, curly_braces_in_flow_control_structures, unnecessary_lambdas, slash_for_doc_comments, prefer_const_literals_to_create_immutables, implicit_dynamic_list_literal, duplicate_import, unused_import, prefer_single_quotes, prefer_const_constructors, use_super_parameters, always_use_package_imports, annotate_overrides, invalid_use_of_protected_member, constant_identifier_names, invalid_use_of_internal_member + +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_1.dart'; +export 'bridge_generated_api_1.dart'; +import 'package:meta/meta.dart'; + +class ApiClass1Platform extends FlutterRustBridgeBase with FlutterRustBridgeSetupMixin { + ApiClass1Platform(FutureOr dylib) : super(ApiClass1Wire(dylib)) { + setupMixinConstructor(); + } + Future setup() => inner.init; +// Section: api2wire + +// Section: finalizer + +} + +// Section: WASM wire module + +@JS('wasm_bindgen') +external ApiClass1WasmModule get wasmModule; + +@JS() +@anonymous +class ApiClass1WasmModule implements WasmModule { + external Object /* Promise */ call([String? moduleName]); + external ApiClass1WasmModule bind(dynamic thisArg, String moduleName); + external void wire_simple_adder_1(NativePortType port_, int a, int b); +} + +// Section: WASM wire connector + +class ApiClass1Wire extends FlutterRustBridgeWasmWireBase { + ApiClass1Wire(FutureOr module) : super(WasmModule.cast(module)); + + void wire_simple_adder_1(NativePortType port_, int a, int b) => wasmModule.wire_simple_adder_1(port_, a, b); +} diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.dart index cdecf57fd9..3c95ded04f 100644 --- a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.dart +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.dart @@ -6,9 +6,11 @@ import 'dart:convert'; import 'dart:async'; import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_2.io.dart' if (dart.library.html) 'bridge_generated_api_2.web.dart'; import 'package:meta/meta.dart'; -import 'package:meta/meta.dart'; -import 'dart:ffi' as ffi; abstract class ApiClass2 { /// Documentation on a simple adder function. @@ -55,76 +57,3 @@ int api2wire_i32(int raw) { return raw; } // Section: finalizer - -class ApiClass2Platform extends FlutterRustBridgeBase { - ApiClass2Platform(ffi.DynamicLibrary dylib) : super(ApiClass2Wire(dylib)); -// Section: api2wire - -// Section: finalizer - -// Section: api_fill_to_wire - -} - -// ignore_for_file: camel_case_types, non_constant_identifier_names, avoid_positional_boolean_parameters, annotate_overrides, constant_identifier_names - -// AUTO GENERATED FILE, DO NOT EDIT. -// -// Generated by `package:ffigen`. - -/// generated by flutter_rust_bridge -class ApiClass2Wire implements FlutterRustBridgeWireBase { - /// Holds the symbol lookup function. - final ffi.Pointer Function(String symbolName) _lookup; - - /// The symbols are looked up in [dynamicLibrary]. - ApiClass2Wire(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; - - /// The symbols are looked up with [lookup]. - ApiClass2Wire.fromLookup(ffi.Pointer Function(String symbolName) lookup) - : _lookup = lookup; - - void free_WireSyncReturnStruct( - WireSyncReturnStruct val, - ) { - return _free_WireSyncReturnStruct( - val, - ); - } - - late final _free_WireSyncReturnStructPtr = - _lookup>('free_WireSyncReturnStruct'); - late final _free_WireSyncReturnStruct = - _free_WireSyncReturnStructPtr.asFunction(); - - void store_dart_post_cobject( - DartPostCObjectFnType ptr, - ) { - return _store_dart_post_cobject( - ptr, - ); - } - - late final _store_dart_post_cobjectPtr = - _lookup>('store_dart_post_cobject'); - late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction(); - - void wire_simple_adder_2( - int port_, - int a, - int b, - ) { - return _wire_simple_adder_2( - port_, - a, - b, - ); - } - - late final _wire_simple_adder_2Ptr = - _lookup>('wire_simple_adder_2'); - late final _wire_simple_adder_2 = _wire_simple_adder_2Ptr.asFunction(); -} - -typedef DartPostCObjectFnType = ffi.Pointer)>>; -typedef DartPort = ffi.Int64; diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.io.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.io.dart new file mode 100644 index 0000000000..fa37ba4711 --- /dev/null +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.io.dart @@ -0,0 +1,84 @@ +// AUTO GENERATED FILE, DO NOT EDIT. +// Generated by `flutter_rust_bridge`@ 1.50.0. +// ignore_for_file: non_constant_identifier_names, unused_element, duplicate_ignore, directives_ordering, curly_braces_in_flow_control_structures, unnecessary_lambdas, slash_for_doc_comments, prefer_const_literals_to_create_immutables, implicit_dynamic_list_literal, duplicate_import, unused_import, prefer_single_quotes, prefer_const_constructors, use_super_parameters, always_use_package_imports, annotate_overrides, invalid_use_of_protected_member, constant_identifier_names, invalid_use_of_internal_member + +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_2.dart'; +export 'bridge_generated_api_2.dart'; +import 'package:meta/meta.dart'; +import 'dart:ffi' as ffi; + +class ApiClass2Platform extends FlutterRustBridgeBase { + ApiClass2Platform(ffi.DynamicLibrary dylib) : super(ApiClass2Wire(dylib)); +// Section: api2wire + +// Section: finalizer + +// Section: api_fill_to_wire + +} + +// ignore_for_file: camel_case_types, non_constant_identifier_names, avoid_positional_boolean_parameters, annotate_overrides, constant_identifier_names + +// AUTO GENERATED FILE, DO NOT EDIT. +// +// Generated by `package:ffigen`. + +/// generated by flutter_rust_bridge +class ApiClass2Wire implements FlutterRustBridgeWireBase { + /// Holds the symbol lookup function. + final ffi.Pointer Function(String symbolName) _lookup; + + /// The symbols are looked up in [dynamicLibrary]. + ApiClass2Wire(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; + + /// The symbols are looked up with [lookup]. + ApiClass2Wire.fromLookup(ffi.Pointer Function(String symbolName) lookup) + : _lookup = lookup; + + void free_WireSyncReturnStruct( + WireSyncReturnStruct val, + ) { + return _free_WireSyncReturnStruct( + val, + ); + } + + late final _free_WireSyncReturnStructPtr = + _lookup>('free_WireSyncReturnStruct'); + late final _free_WireSyncReturnStruct = + _free_WireSyncReturnStructPtr.asFunction(); + + void store_dart_post_cobject( + DartPostCObjectFnType ptr, + ) { + return _store_dart_post_cobject( + ptr, + ); + } + + late final _store_dart_post_cobjectPtr = + _lookup>('store_dart_post_cobject'); + late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction(); + + void wire_simple_adder_2( + int port_, + int a, + int b, + ) { + return _wire_simple_adder_2( + port_, + a, + b, + ); + } + + late final _wire_simple_adder_2Ptr = + _lookup>('wire_simple_adder_2'); + late final _wire_simple_adder_2 = _wire_simple_adder_2Ptr.asFunction(); +} + +typedef DartPostCObjectFnType = ffi.Pointer)>>; +typedef DartPort = ffi.Int64; diff --git a/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.web.dart b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.web.dart new file mode 100644 index 0000000000..8589d601a2 --- /dev/null +++ b/frb_example/pure_dart_multi/dart/lib/bridge_generated_api_2.web.dart @@ -0,0 +1,42 @@ +// AUTO GENERATED FILE, DO NOT EDIT. +// Generated by `flutter_rust_bridge`@ 1.50.0. +// ignore_for_file: non_constant_identifier_names, unused_element, duplicate_ignore, directives_ordering, curly_braces_in_flow_control_structures, unnecessary_lambdas, slash_for_doc_comments, prefer_const_literals_to_create_immutables, implicit_dynamic_list_literal, duplicate_import, unused_import, prefer_single_quotes, prefer_const_constructors, use_super_parameters, always_use_package_imports, annotate_overrides, invalid_use_of_protected_member, constant_identifier_names, invalid_use_of_internal_member + +import 'dart:convert'; +import 'dart:async'; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; +import 'bridge_generated_api_2.dart'; +export 'bridge_generated_api_2.dart'; +import 'package:meta/meta.dart'; + +class ApiClass2Platform extends FlutterRustBridgeBase with FlutterRustBridgeSetupMixin { + ApiClass2Platform(FutureOr dylib) : super(ApiClass2Wire(dylib)) { + setupMixinConstructor(); + } + Future setup() => inner.init; +// Section: api2wire + +// Section: finalizer + +} + +// Section: WASM wire module + +@JS('wasm_bindgen') +external ApiClass2WasmModule get wasmModule; + +@JS() +@anonymous +class ApiClass2WasmModule implements WasmModule { + external Object /* Promise */ call([String? moduleName]); + external ApiClass2WasmModule bind(dynamic thisArg, String moduleName); + external void wire_simple_adder_2(NativePortType port_, int a, int b); +} + +// Section: WASM wire connector + +class ApiClass2Wire extends FlutterRustBridgeWasmWireBase { + ApiClass2Wire(FutureOr module) : super(WasmModule.cast(module)); + + void wire_simple_adder_2(NativePortType port_, int a, int b) => wasmModule.wire_simple_adder_2(port_, a, b); +} diff --git a/frb_example/pure_dart_multi/rust/build.rs b/frb_example/pure_dart_multi/rust/build.rs index 983adf0304..7479a133a5 100644 --- a/frb_example/pure_dart_multi/rust/build.rs +++ b/frb_example/pure_dart_multi/rust/build.rs @@ -27,6 +27,7 @@ fn main() { dart_output: vec![DART_OUTPUT_1.to_string(), DART_OUTPUT_2.to_string()], // Path of output Rust code rust_output: Some(vec![RUST_OUTPUT_1.to_string(), RUST_OUTPUT_2.to_string()]), + wasm: true, // Class name of each Rust block of api class_name: Some(vec![CLASS_NAME_1.to_string(), CLASS_NAME_2.to_string()]), dart_format_line_length: 120, diff --git a/frb_example/pure_dart_multi/rust/src/generated_api_1.rs b/frb_example/pure_dart_multi/rust/src/generated_api_1.rs index eb5517b601..eeb64925f2 100644 --- a/frb_example/pure_dart_multi/rust/src/generated_api_1.rs +++ b/frb_example/pure_dart_multi/rust/src/generated_api_1.rs @@ -74,6 +74,13 @@ support::lazy_static! { pub static ref FLUTTER_RUST_BRIDGE_HANDLER: support::DefaultHandler = Default::default(); } +/// cbindgen:ignore +#[cfg(target_family = "wasm")] +#[path = "generated_api_1.web.rs"] +mod web; +#[cfg(target_family = "wasm")] +pub use web::*; + #[cfg(not(target_family = "wasm"))] #[path = "generated_api_1.io.rs"] mod io; diff --git a/frb_example/pure_dart_multi/rust/src/generated_api_1.web.rs b/frb_example/pure_dart_multi/rust/src/generated_api_1.web.rs new file mode 100644 index 0000000000..1bb98e8f5c --- /dev/null +++ b/frb_example/pure_dart_multi/rust/src/generated_api_1.web.rs @@ -0,0 +1,21 @@ +use super::*; +// Section: wire functions + +#[wasm_bindgen] +pub fn wire_simple_adder_1(port_: MessagePort, a: i32, b: i32) { + wire_simple_adder_1_impl(port_, a, b) +} + +// Section: allocate functions + +// Section: related functions + +// Section: impl Wire2Api + +// Section: impl Wire2Api for JsValue + +impl Wire2Api for JsValue { + fn wire2api(self) -> i32 { + self.unchecked_into_f64() as _ + } +} diff --git a/frb_example/pure_dart_multi/rust/src/generated_api_2.rs b/frb_example/pure_dart_multi/rust/src/generated_api_2.rs index 201bc4e0ad..f052d9a114 100644 --- a/frb_example/pure_dart_multi/rust/src/generated_api_2.rs +++ b/frb_example/pure_dart_multi/rust/src/generated_api_2.rs @@ -74,6 +74,13 @@ support::lazy_static! { pub static ref FLUTTER_RUST_BRIDGE_HANDLER: support::DefaultHandler = Default::default(); } +/// cbindgen:ignore +#[cfg(target_family = "wasm")] +#[path = "generated_api_2.web.rs"] +mod web; +#[cfg(target_family = "wasm")] +pub use web::*; + #[cfg(not(target_family = "wasm"))] #[path = "generated_api_2.io.rs"] mod io; diff --git a/frb_example/pure_dart_multi/rust/src/generated_api_2.web.rs b/frb_example/pure_dart_multi/rust/src/generated_api_2.web.rs new file mode 100644 index 0000000000..249266d726 --- /dev/null +++ b/frb_example/pure_dart_multi/rust/src/generated_api_2.web.rs @@ -0,0 +1,21 @@ +use super::*; +// Section: wire functions + +#[wasm_bindgen] +pub fn wire_simple_adder_2(port_: MessagePort, a: i32, b: i32) { + wire_simple_adder_2_impl(port_, a, b) +} + +// Section: allocate functions + +// Section: related functions + +// Section: impl Wire2Api + +// Section: impl Wire2Api for JsValue + +impl Wire2Api for JsValue { + fn wire2api(self) -> i32 { + self.unchecked_into_f64() as _ + } +} diff --git a/frb_rust/src/support.rs b/frb_rust/src/support.rs index 317c26976f..1e6e843d88 100644 --- a/frb_rust/src/support.rs +++ b/frb_rust/src/support.rs @@ -89,7 +89,7 @@ impl From> for WireSyncReturnData { /// Bool will be converted to u8 where 0 stands for false and 1 stands for true. impl From for WireSyncReturnData { fn from(data: bool) -> Self { - if data { 1_u8 } else { 0_u8 }.into() + u8::from(data).into() } }