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

Add default_dart_async configuration option #2139

Merged
merged 6 commits into from
Jun 22, 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
4 changes: 4 additions & 0 deletions frb_codegen/src/binary/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@
#[arg(long)]
pub type_64bit_int: bool,

/// Whether default Dart code is asynchronous or synchronous
#[arg(long)]
pub no_default_dart_async: bool,

Check warning on line 161 in frb_codegen/src/binary/commands.rs

View check run for this annotation

Codecov / codecov/patch

frb_codegen/src/binary/commands.rs#L161

Added line #L161 was not covered by tests

/// If having error when, for example, parsing a function, directly stop instead of continue and skip it
#[arg(long)]
pub stop_on_error: bool,
Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/binary/commands_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/codegen/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Config {
pub dart_type_rename: Option<HashMap<String, String>>,
pub enable_lifetime: Option<bool>,
pub type_64bit_int: Option<bool>,
pub default_dart_async: Option<bool>,
pub stop_on_error: Option<bool>,
pub dump: Option<Vec<ConfigDumpContent>>,
pub dump_all: Option<bool>,
Expand Down Expand Up @@ -88,6 +89,7 @@ generate_merge!(
dart_type_rename,
enable_lifetime,
type_64bit_int,
default_dart_async,
stop_on_error,
dump,
dump_all,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -84,6 +85,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<MirFuncOrSkip> {
Expand All @@ -95,6 +97,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),
Expand Down Expand Up @@ -129,6 +132,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<MirFuncOrSkip> {
debug!("parse_function function name: {:?}", func.item_fn.name());
Expand Down Expand Up @@ -219,8 +223,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 = 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());
let accessor = attributes.accessor();

Expand Down Expand Up @@ -261,6 +266,14 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
}
}

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 {
if let MirFuncOwnerInfo::Method(method) = owner {
if matches!(method.owner_ty, MirType::TraitDef(_)) {
Expand All @@ -278,11 +291,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
})
}

Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod tests {
stop_on_error: true,
enable_lifetime: false,
type_64bit_int: false,
default_dart_async: true,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions website/docs/generated/_frb-codegen-command-generate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading