From 8a3ae23f2563b67e299cd53ed6961e4366657e89 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 09:37:03 +0800 Subject: [PATCH 1/6] feat: config --- frb_codegen/src/binary/commands.rs | 4 ++++ frb_codegen/src/binary/commands_parser.rs | 1 + frb_codegen/src/library/codegen/config/config.rs | 2 ++ 3 files changed, 7 insertions(+) diff --git a/frb_codegen/src/binary/commands.rs b/frb_codegen/src/binary/commands.rs index 962981ba1e..897b9b24cc 100644 --- a/frb_codegen/src/binary/commands.rs +++ b/frb_codegen/src/binary/commands.rs @@ -156,6 +156,10 @@ pub(crate) struct GenerateCommandArgsPrimary { #[arg(long)] pub type_64bit_int: bool, + /// Whether default Dart code is asynchronous or synchronous + #[arg(long)] + pub no_default_dart_async: bool, + /// If having error when, for example, parsing a function, directly stop instead of continue and skip it #[arg(long)] pub stop_on_error: bool, diff --git a/frb_codegen/src/binary/commands_parser.rs b/frb_codegen/src/binary/commands_parser.rs index b7ec02e479..2efe32dcb9 100644 --- a/frb_codegen/src/binary/commands_parser.rs +++ b/frb_codegen/src/binary/commands_parser.rs @@ -56,6 +56,7 @@ fn compute_codegen_config_from_naive_command_args(args: GenerateCommandArgsPrima dart_type_rename: None, // complex type, not supported on command line yet enable_lifetime: positive_bool_arg(args.enable_lifetime), type_64bit_int: positive_bool_arg(args.type_64bit_int), + default_dart_async: negative_bool_arg(args.no_default_dart_async), stop_on_error: positive_bool_arg(args.stop_on_error), dump: args.dump, dump_all: positive_bool_arg(args.dump_all), diff --git a/frb_codegen/src/library/codegen/config/config.rs b/frb_codegen/src/library/codegen/config/config.rs index d18b321066..72b4b90923 100644 --- a/frb_codegen/src/library/codegen/config/config.rs +++ b/frb_codegen/src/library/codegen/config/config.rs @@ -34,6 +34,7 @@ pub struct Config { pub dart_type_rename: Option>, pub enable_lifetime: Option, pub type_64bit_int: Option, + pub default_dart_async: Option, pub stop_on_error: Option, pub dump: Option>, pub dump_all: Option, @@ -88,6 +89,7 @@ generate_merge!( dart_type_rename, enable_lifetime, type_64bit_int, + default_dart_async, stop_on_error, dump, dump_all, From 3da728d9d66764b8c11f142aca23c78afb4cc453 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 09:38:49 +0800 Subject: [PATCH 2/6] refactor: extract --- .../codegen/parser/mir/parser/function/real/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs index b9671710d0..40ff8ea5e4 100644 --- a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs +++ b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs @@ -219,8 +219,9 @@ impl<'a, 'b> FunctionParser<'a, 'b> { info = self.transform_fn_info(info); let codec_mode_pack = compute_codec_mode_pack(&attributes, force_codec_mode_pack); - let mode = compute_func_mode(&attributes, &info); - let stream_dart_await = attributes.stream_dart_await() && !attributes.sync(); + let dart_async = !attributes.sync(); // TODO + let mode = compute_func_mode(dart_async, &info); + let stream_dart_await = attributes.stream_dart_await() && dart_async; let namespace_refined = refine_namespace(&owner).unwrap_or(func.namespace.clone()); let accessor = attributes.accessor(); @@ -278,11 +279,11 @@ fn create_output_skip(func: &HirFlatFunction, reason: IrSkipReason) -> MirFuncOr }) } -fn compute_func_mode(attributes: &FrbAttributes, info: &FunctionPartialInfo) -> MirFuncMode { - info.mode.unwrap_or(if attributes.sync() { - MirFuncMode::Sync - } else { +fn compute_func_mode(dart_async: bool, info: &FunctionPartialInfo) -> MirFuncMode { + info.mode.unwrap_or(if dart_async { MirFuncMode::Normal + } else { + MirFuncMode::Sync }) } From 5c99d7e8117af0d2edbbc1b3b6cdf3267d714491 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 09:39:12 +0800 Subject: [PATCH 3/6] refactor: extract --- .../library/codegen/parser/mir/parser/function/real/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs index 40ff8ea5e4..3d07439fe2 100644 --- a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs +++ b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs @@ -219,7 +219,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> { info = self.transform_fn_info(info); let codec_mode_pack = compute_codec_mode_pack(&attributes, force_codec_mode_pack); - let dart_async = !attributes.sync(); // TODO + let dart_async = compute_dart_async(&attributes); let mode = compute_func_mode(dart_async, &info); let stream_dart_await = attributes.stream_dart_await() && dart_async; let namespace_refined = refine_namespace(&owner).unwrap_or(func.namespace.clone()); @@ -262,6 +262,10 @@ impl<'a, 'b> FunctionParser<'a, 'b> { } } +fn compute_dart_async(attributes: &FrbAttributes) -> bool { + !attributes.sync() +} + fn should_forbid_type_self_for_inputs(owner: &MirFuncOwnerInfo) -> bool { if let MirFuncOwnerInfo::Method(method) = owner { if matches!(method.owner_ty, MirType::TraitDef(_)) { From d2bce11295ffe136a7c87bd1eeac1543035dab7a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 09:40:07 +0800 Subject: [PATCH 4/6] feat: more --- .../codegen/parser/mir/parser/function/real/mod.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs index 3d07439fe2..1ccf85d634 100644 --- a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs +++ b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs @@ -84,6 +84,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> { default_rust_opaque_codec: RustOpaqueCodecMode, enable_lifetime: bool, type_64bit_int: bool, + default_dart_async: bool, parse_mode: ParseMode, stop_on_error: bool, ) -> anyhow::Result { @@ -95,6 +96,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> { default_rust_opaque_codec, enable_lifetime, type_64bit_int, + default_dart_async, parse_mode, ) { Ok(output) => Ok(output), @@ -129,6 +131,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> { default_rust_opaque_codec: RustOpaqueCodecMode, enable_lifetime: bool, type_64bit_int: bool, + default_dart_async: bool, parse_mode: ParseMode, ) -> anyhow::Result { debug!("parse_function function name: {:?}", func.item_fn.name()); @@ -219,7 +222,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> { info = self.transform_fn_info(info); let codec_mode_pack = compute_codec_mode_pack(&attributes, force_codec_mode_pack); - let dart_async = compute_dart_async(&attributes); + let dart_async = compute_dart_async(&attributes, default_dart_async); let mode = compute_func_mode(dart_async, &info); let stream_dart_await = attributes.stream_dart_await() && dart_async; let namespace_refined = refine_namespace(&owner).unwrap_or(func.namespace.clone()); @@ -262,8 +265,12 @@ impl<'a, 'b> FunctionParser<'a, 'b> { } } -fn compute_dart_async(attributes: &FrbAttributes) -> bool { - !attributes.sync() +fn compute_dart_async(attributes: &FrbAttributes, default_dart_async: bool) -> bool { + if attributes.sync() { + false + } else { + default_dart_async + } } fn should_forbid_type_self_for_inputs(owner: &MirFuncOwnerInfo) -> bool { From 8d9aa3bb3dc5eee09e0397fd8e4b94566811cc35 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 09:40:35 +0800 Subject: [PATCH 5/6] feat: pass --- .../src/library/codegen/config/internal_config_parser/mod.rs | 1 + frb_codegen/src/library/codegen/parser/mir/internal_config.rs | 1 + .../src/library/codegen/parser/mir/parser/function/real/mod.rs | 1 + frb_codegen/src/library/codegen/parser/mod.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/frb_codegen/src/library/codegen/config/internal_config_parser/mod.rs b/frb_codegen/src/library/codegen/config/internal_config_parser/mod.rs index 0f5e52d8f6..1e682f5775 100644 --- a/frb_codegen/src/library/codegen/config/internal_config_parser/mod.rs +++ b/frb_codegen/src/library/codegen/config/internal_config_parser/mod.rs @@ -117,6 +117,7 @@ impl InternalConfig { stop_on_error, enable_lifetime: config.enable_lifetime.unwrap_or_default(), type_64bit_int: config.type_64bit_int.unwrap_or_default(), + default_dart_async: config.default_dart_async.unwrap_or(true), }, }, generator, diff --git a/frb_codegen/src/library/codegen/parser/mir/internal_config.rs b/frb_codegen/src/library/codegen/parser/mir/internal_config.rs index 5e1ae2a08f..cbe8d1e637 100644 --- a/frb_codegen/src/library/codegen/parser/mir/internal_config.rs +++ b/frb_codegen/src/library/codegen/parser/mir/internal_config.rs @@ -12,6 +12,7 @@ pub(crate) struct ParserMirInternalConfig { pub stop_on_error: bool, pub enable_lifetime: bool, pub type_64bit_int: bool, + pub default_dart_async: bool, } // TODO rename - this is no longer an "input-namespace"-only pack diff --git a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs index 1ccf85d634..f31b820bbf 100644 --- a/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs +++ b/frb_codegen/src/library/codegen/parser/mir/parser/function/real/mod.rs @@ -58,6 +58,7 @@ pub(crate) fn parse( config.default_rust_opaque_codec, config.enable_lifetime, config.type_64bit_int, + config.default_dart_async, parse_mode, config.stop_on_error, ) diff --git a/frb_codegen/src/library/codegen/parser/mod.rs b/frb_codegen/src/library/codegen/parser/mod.rs index 37b8f3b0ef..e0aeada3a6 100644 --- a/frb_codegen/src/library/codegen/parser/mod.rs +++ b/frb_codegen/src/library/codegen/parser/mod.rs @@ -183,6 +183,7 @@ mod tests { stop_on_error: true, enable_lifetime: false, type_64bit_int: false, + default_dart_async: true, }, }; From bdc92d82a0bfb93d6658b9af45f67c348c24d670 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Sat, 22 Jun 2024 11:36:30 +0800 Subject: [PATCH 6/6] chore: goldens --- .../single_rust_input/expect_output.json | 1 + .../wildcard_rust_input/expect_output.json | 1 + website/docs/generated/_frb-codegen-command-generate.mdx | 3 +++ 3 files changed, 5 insertions(+) diff --git a/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/single_rust_input/expect_output.json b/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/single_rust_input/expect_output.json index 42fe32d0e5..0129a5c1f7 100644 --- a/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/single_rust_input/expect_output.json +++ b/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/single_rust_input/expect_output.json @@ -109,6 +109,7 @@ "third_party_crate_names": [] }, "mir": { + "default_dart_async": true, "default_rust_opaque_codec": "Moi", "default_stream_sink_codec": "Sse", "enable_lifetime": false, diff --git a/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/wildcard_rust_input/expect_output.json b/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/wildcard_rust_input/expect_output.json index 42fe32d0e5..0129a5c1f7 100644 --- a/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/wildcard_rust_input/expect_output.json +++ b/frb_codegen/test_fixtures/library/codegen/config/internal_config_parser/wildcard_rust_input/expect_output.json @@ -109,6 +109,7 @@ "third_party_crate_names": [] }, "mir": { + "default_dart_async": true, "default_rust_opaque_codec": "Moi", "default_stream_sink_codec": "Sse", "enable_lifetime": false, diff --git a/website/docs/generated/_frb-codegen-command-generate.mdx b/website/docs/generated/_frb-codegen-command-generate.mdx index 78a3d4677f..b3c3cd693b 100644 --- a/website/docs/generated/_frb-codegen-command-generate.mdx +++ b/website/docs/generated/_frb-codegen-command-generate.mdx @@ -84,6 +84,9 @@ Options: --type-64bit-int Let 64 bit types be translated to `int`s instead of types like `BigInt`s + --no-default-dart-async + Whether default Dart code is asynchronous or synchronous + --stop-on-error If having error when, for example, parsing a function, directly stop instead of continue and skip it