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

Fix geneartion of async functions when default_dart_async: false ; Update frb_example/integrate_third_party #2190

Merged
merged 9 commits into from
Jul 7, 2024
4 changes: 4 additions & 0 deletions frb_codegen/src/library/codegen/ir/hir/flat/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl HirFlatFunction {
} => None,
}
}

pub(crate) fn is_async(&self) -> bool {
self.item_fn.sig().asyncness.is_some()
}
}

#[derive(Debug, Clone, Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,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.dart_async().unwrap_or(default_dart_async);
let dart_async = compute_dart_async(func, &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());
Expand Down Expand Up @@ -269,6 +269,16 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
}
}

fn compute_dart_async(
func: &HirFlatFunction,
attributes: &FrbAttributes,
default_dart_async: bool,
) -> bool {
attributes
.dart_async()
.unwrap_or(func.is_async() || 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ abstract class AnalyserNodeExt {
{required FutureOr<void> Function(String) callback});
}

abstract class AnalyserNodeMiscExt {
Future<Uint8List> getByteTimeDomainData({required int len});

Future<Float32List> getFloatTimeDomainData({required int len});
}

abstract class AudioBufferSourceNodeExt {
Future<void> connect({required AudioNode dest});

Future<void> setOnProcessorError(
{required FutureOr<void> Function(String) callback});
}

abstract class AudioBufferSourceNodeMiscExt {
Future<void> setBuffer({required AudioBuffer audioBuffer});
}

abstract class AudioBufferSourceNodeScheduledSourceNodeMiscExt {
Future<void> setOnEnded({required FutureOr<void> Function(Event) callback});
}
Expand Down Expand Up @@ -188,6 +198,11 @@ abstract class ScriptProcessorNodeExt {
{required FutureOr<void> Function(String) callback});
}

abstract class ScriptProcessorNodeMiscExt {
Future<void> setOnaudioprocess(
{required FutureOr<void> Function(AudioProcessingEvent) callback});
}

abstract class StereoPannerNodeExt {
Future<void> connect({required AudioNode dest});

Expand Down
1,356 changes: 774 additions & 582 deletions frb_example/integrate_third_party/lib/src/rust/frb_generated.dart

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_CastedPrimitive_usize(dynamic raw);

@protected
FutureOr<void> Function(AudioProcessingEvent)
dco_decode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerAudioProcessingEvent_Output_unit_AnyhowException(
dynamic raw);

@protected
FutureOr<void> Function(Event)
dco_decode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerEvent_Output_unit_AnyhowException(
Expand Down Expand Up @@ -1229,10 +1234,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
AnalyserNodeExt dco_decode_TraitDef_AnalyserNodeExt(dynamic raw);

@protected
AnalyserNodeMiscExt dco_decode_TraitDef_AnalyserNodeMiscExt(dynamic raw);

@protected
AudioBufferSourceNodeExt dco_decode_TraitDef_AudioBufferSourceNodeExt(
dynamic raw);

@protected
AudioBufferSourceNodeMiscExt dco_decode_TraitDef_AudioBufferSourceNodeMiscExt(
dynamic raw);

@protected
AudioBufferSourceNodeScheduledSourceNodeMiscExt
dco_decode_TraitDef_AudioBufferSourceNodeScheduledSourceNodeMiscExt(
Expand Down Expand Up @@ -1335,6 +1347,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
ScriptProcessorNodeExt dco_decode_TraitDef_ScriptProcessorNodeExt(
dynamic raw);

@protected
ScriptProcessorNodeMiscExt dco_decode_TraitDef_ScriptProcessorNodeMiscExt(
dynamic raw);

@protected
StereoPannerNodeExt dco_decode_TraitDef_StereoPannerNodeExt(dynamic raw);

Expand Down Expand Up @@ -3448,6 +3464,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_CastedPrimitive_usize(int self, SseSerializer serializer);

@protected
void
sse_encode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerAudioProcessingEvent_Output_unit_AnyhowException(
FutureOr<void> Function(AudioProcessingEvent) self,
SseSerializer serializer);

@protected
void
sse_encode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerEvent_Output_unit_AnyhowException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_CastedPrimitive_usize(dynamic raw);

@protected
FutureOr<void> Function(AudioProcessingEvent)
dco_decode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerAudioProcessingEvent_Output_unit_AnyhowException(
dynamic raw);

@protected
FutureOr<void> Function(Event)
dco_decode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerEvent_Output_unit_AnyhowException(
Expand Down Expand Up @@ -1231,10 +1236,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
AnalyserNodeExt dco_decode_TraitDef_AnalyserNodeExt(dynamic raw);

@protected
AnalyserNodeMiscExt dco_decode_TraitDef_AnalyserNodeMiscExt(dynamic raw);

@protected
AudioBufferSourceNodeExt dco_decode_TraitDef_AudioBufferSourceNodeExt(
dynamic raw);

@protected
AudioBufferSourceNodeMiscExt dco_decode_TraitDef_AudioBufferSourceNodeMiscExt(
dynamic raw);

@protected
AudioBufferSourceNodeScheduledSourceNodeMiscExt
dco_decode_TraitDef_AudioBufferSourceNodeScheduledSourceNodeMiscExt(
Expand Down Expand Up @@ -1337,6 +1349,10 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
ScriptProcessorNodeExt dco_decode_TraitDef_ScriptProcessorNodeExt(
dynamic raw);

@protected
ScriptProcessorNodeMiscExt dco_decode_TraitDef_ScriptProcessorNodeMiscExt(
dynamic raw);

@protected
StereoPannerNodeExt dco_decode_TraitDef_StereoPannerNodeExt(dynamic raw);

Expand Down Expand Up @@ -3450,6 +3466,12 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_CastedPrimitive_usize(int self, SseSerializer serializer);

@protected
void
sse_encode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerAudioProcessingEvent_Output_unit_AnyhowException(
FutureOr<void> Function(AudioProcessingEvent) self,
SseSerializer serializer);

@protected
void
sse_encode_DartFn_Inputs_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerEvent_Output_unit_AnyhowException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import 'worklet.dart';

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<AnalyserNode>>
abstract class AnalyserNode
implements RustOpaqueInterface, AnalyserNodeExt, AudioNode {
implements
RustOpaqueInterface,
AnalyserNodeExt,
AnalyserNodeMiscExt,
AudioNode {
Future<void> channelConfig();

/// Represents an integer used to determine how many channels are used when up-mixing and
Expand Down Expand Up @@ -56,6 +60,10 @@ abstract class AnalyserNode

Future<void> connect({required AudioNode dest});

Future<Uint8List> getByteTimeDomainData({required int len});

Future<Float32List> getFloatTimeDomainData({required int len});

/// Number of bins in the FFT results, is half the FFT size
///
/// # Panics
Expand Down Expand Up @@ -133,6 +141,7 @@ abstract class AudioBufferSourceNode
implements
RustOpaqueInterface,
AudioBufferSourceNodeExt,
AudioBufferSourceNodeMiscExt,
AudioBufferSourceNodeScheduledSourceNodeMiscExt,
AudioNode,
AudioScheduledSourceNode {
Expand Down Expand Up @@ -175,6 +184,8 @@ abstract class AudioBufferSourceNode

Future<void> connect({required AudioNode dest});

Future<void> setBuffer({required AudioBuffer audioBuffer});

/// Defines if the playback the [`AudioBuffer`] should be looped
Future<bool> loop();

Expand Down Expand Up @@ -206,14 +217,6 @@ abstract class AudioBufferSourceNode

Future<void> registration();

/// Provide an [`AudioBuffer`] as the source of data to be played bask
///
/// # Panics
///
/// Panics if a buffer has already been given to the source (though `new` or through
/// `set_buffer`)
Future<void> setBuffer({required AudioBuffer audioBuffer});

Future<void> setLoop({required bool value});

Future<void> setLoopEnd({required double value});
Expand Down Expand Up @@ -1159,7 +1162,11 @@ abstract class PannerNode

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<ScriptProcessorNode>>
abstract class ScriptProcessorNode
implements RustOpaqueInterface, AudioNode, ScriptProcessorNodeExt {
implements
RustOpaqueInterface,
AudioNode,
ScriptProcessorNodeExt,
ScriptProcessorNodeMiscExt {
Future<int> bufferSize();

Future<void> channelConfig();
Expand Down Expand Up @@ -1195,6 +1202,9 @@ abstract class ScriptProcessorNode

Future<void> connect({required AudioNode dest});

Future<void> setOnaudioprocess(
{required FutureOr<void> Function(AudioProcessingEvent) callback});

Future<int> numberOfInputs();

Future<int> numberOfOutputs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::sync::Arc;
use web_audio_api::context::{AudioContext, BaseAudioContext, OfflineAudioContext};
use web_audio_api::media_streams::{MediaStream, MediaStreamTrack};
use web_audio_api::node::*;
use web_audio_api::{AudioBuffer, AudioParam, Event, OfflineAudioCompletionEvent};
use web_audio_api::{
AudioBuffer, AudioParam, AudioProcessingEvent, Event, OfflineAudioCompletionEvent,
};

#[ext]
pub impl AudioContext {
Expand Down Expand Up @@ -46,6 +48,21 @@ pub impl AudioContext {
}
}

#[ext(name = AnalyserNodeMiscExt)]
pub impl AnalyserNode {
fn frb_override_get_byte_time_domain_data(&mut self, len: usize) -> Vec<u8> {
let mut bins = vec![0; len];
self.get_byte_time_domain_data(&mut bins);
bins
}

fn frb_override_get_float_time_domain_data(&mut self, len: usize) -> Vec<f32> {
let mut bins = vec![0.0; len];
self.get_float_time_domain_data(&mut bins);
bins
}
}

#[ext]
pub impl OfflineAudioContext {
fn set_on_complete(
Expand Down Expand Up @@ -138,6 +155,32 @@ pub impl Event {
}
}

#[ext(name = ScriptProcessorNodeMiscExt)]
pub impl ScriptProcessorNode {
// NOTE: The original name was `set_onaudioprocess` and here the new name has `_`
fn frb_override_set_onaudioprocess(
&self,
callback: impl Fn(AudioProcessingEvent) -> DartFnFuture<()> + Send + 'static + std::marker::Sync,
) {
let callback = Arc::new(callback);
self.set_onaudioprocess(move |event| {
let callback_cloned = callback.clone();
FLUTTER_RUST_BRIDGE_HANDLER
.async_runtime()
.spawn(async move { callback_cloned(event).await });
})
}
}

#[ext(name = AudioBufferSourceNodeMiscExt)]
pub impl AudioBufferSourceNode {
// calls the regular fn `setBuffer()` after cloning the argument.
fn frb_override_set_buffer(&mut self, audio_buffer: &AudioBuffer) {
let clone = audio_buffer.clone();
self.set_buffer(clone)
}
}

#[ext]
pub impl MediaStream {
fn frb_override_get_tracks(&self) -> Vec<MediaStreamTrack> {
Expand Down
Loading
Loading