Skip to content

Commit

Permalink
Merge pull request #2139 from fzyzcjy/feat/12385
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Jun 22, 2024
2 parents 5269678 + bdc92d8 commit b68eb1d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 6 deletions.
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 @@ 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,
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

0 comments on commit b68eb1d

Please sign in to comment.