Skip to content

Commit

Permalink
Merge pull request #861 from huang12zheng/wasm_no_decl_pr
Browse files Browse the repository at this point in the history
fix #860
  • Loading branch information
fzyzcjy authored Nov 29, 2022
2 parents e2d459e + 9b00aef commit 0621602
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 173 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/code_generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
18 changes: 18 additions & 0 deletions book/src/feature/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DECL>
```

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).
13 changes: 5 additions & 8 deletions book/src/feature/wasm.md
Original file line number Diff line number Diff line change
@@ -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 <DECL>
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
Expand All @@ -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)
7 changes: 2 additions & 5 deletions frb_codegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -210,10 +210,7 @@ pub fn parse(raw: RawOpts) -> Vec<Opts> {
.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;
Expand Down
24 changes: 20 additions & 4 deletions frb_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/source_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions frb_codegen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub fn with_changed_file<F: FnOnce() -> 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<T>(iter: &[T]) -> Vec<T>
Expand Down
79 changes: 4 additions & 75 deletions frb_example/pure_dart_multi/dart/lib/bridge_generated_api_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -55,76 +57,3 @@ int api2wire_i32(int raw) {
return raw;
}
// Section: finalizer

class ApiClass1Platform extends FlutterRustBridgeBase<ApiClass1Wire> {
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<T> Function<T extends ffi.NativeType>(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<T> Function<T extends ffi.NativeType>(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<ffi.NativeFunction<ffi.Void Function(DartPostCObjectFnType)>>('store_dart_post_cobject');
late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction<void Function(DartPostCObjectFnType)>();

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<ffi.NativeFunction<ffi.Void Function(ffi.Int64, ffi.Int32, ffi.Int32)>>('wire_simple_adder_1');
late final _wire_simple_adder_1 = _wire_simple_adder_1Ptr.asFunction<void Function(int, int, int)>();

void free_WireSyncReturnStruct(
WireSyncReturnStruct val,
) {
return _free_WireSyncReturnStruct(
val,
);
}

late final _free_WireSyncReturnStructPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(WireSyncReturnStruct)>>('free_WireSyncReturnStruct');
late final _free_WireSyncReturnStruct =
_free_WireSyncReturnStructPtr.asFunction<void Function(WireSyncReturnStruct)>();
}

typedef DartPostCObjectFnType = ffi.Pointer<ffi.NativeFunction<ffi.Bool Function(DartPort, ffi.Pointer<ffi.Void>)>>;
typedef DartPort = ffi.Int64;
Original file line number Diff line number Diff line change
@@ -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<ApiClass1Wire> {
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<T> Function<T extends ffi.NativeType>(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<T> Function<T extends ffi.NativeType>(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<ffi.NativeFunction<ffi.Void Function(DartPostCObjectFnType)>>('store_dart_post_cobject');
late final _store_dart_post_cobject = _store_dart_post_cobjectPtr.asFunction<void Function(DartPostCObjectFnType)>();

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<ffi.NativeFunction<ffi.Void Function(ffi.Int64, ffi.Int32, ffi.Int32)>>('wire_simple_adder_1');
late final _wire_simple_adder_1 = _wire_simple_adder_1Ptr.asFunction<void Function(int, int, int)>();

void free_WireSyncReturnStruct(
WireSyncReturnStruct val,
) {
return _free_WireSyncReturnStruct(
val,
);
}

late final _free_WireSyncReturnStructPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(WireSyncReturnStruct)>>('free_WireSyncReturnStruct');
late final _free_WireSyncReturnStruct =
_free_WireSyncReturnStructPtr.asFunction<void Function(WireSyncReturnStruct)>();
}

typedef DartPostCObjectFnType = ffi.Pointer<ffi.NativeFunction<ffi.Bool Function(DartPort, ffi.Pointer<ffi.Void>)>>;
typedef DartPort = ffi.Int64;
Loading

0 comments on commit 0621602

Please sign in to comment.