Skip to content

Commit

Permalink
fix wasm config statements
Browse files Browse the repository at this point in the history
  • Loading branch information
vhdirk committed Sep 15, 2024
1 parent 577fe8f commit 30a78f2
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 71 deletions.
4 changes: 3 additions & 1 deletion frb_codegen/src/library/codegen/parser/mir/parser/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
type_path: &TypePath,
path: &Path,
) -> anyhow::Result<MirType> {
// TODO: previously, extract_path data would only return those segments with `Type` generic args
// now we return everything.
let segments = extract_path_data(path)?;
let splayed_segments = splay_segments(&segments);

if let Some(last_segment) = splayed_segments.last() {
if let Some(last_segment) = splayed_segments.iter().filter(|s| s.args.len() > 0).last() {
if let Some(ans) = self.parse_type_path_data_custom_ser_des(last_segment)? {
return Ok(ans);
}
Expand Down
14 changes: 12 additions & 2 deletions frb_codegen/src/library/codegen/parser/mir/parser/ty/path_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::codegen::ir::mir::ty::rust_opaque::NameComponent;
use anyhow::{bail, Result};
use syn::{Path, PathArguments, PathSegment};
use syn::{AngleBracketedGenericArguments, GenericArgument, Path, PathArguments, PathSegment};

pub(crate) fn extract_path_data(path: &Path) -> Result<Vec<NameComponent>> {
path.segments.iter().map(parse_path_segment).collect()
Expand All @@ -11,7 +11,7 @@ fn parse_path_segment(segment: &PathSegment) -> Result<NameComponent> {
let args = match &segment.arguments {
PathArguments::None => vec![],
PathArguments::AngleBracketed(args) => {
args.args.iter().map(|arg| arg.to_owned()).collect()
parse_angle_bracketed_generic_arguments(args)
// .with_context(|| {
// // This will stop the whole generator and tell the users, so we do not care about testing it
// // frb-coverage:ignore-start
Expand All @@ -35,6 +35,16 @@ fn parse_path_segment(segment: &PathSegment) -> Result<NameComponent> {
Ok(NameComponent { ident, args })
}

fn parse_angle_bracketed_generic_arguments(args: &AngleBracketedGenericArguments) -> Vec<GenericArgument> {
args.args
.iter()
.filter_map(|arg| match &arg {
GenericArgument::Type(_) | GenericArgument::AssocType(_) => Some(arg.to_owned()),
_ => None,
})
.collect()
}

// not used yet
// fn parse_parenthesized_generic_arguments(
// &mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ mod tests {

body("Something", vec!["Something"]);
body("One<Two>", vec!["One", "Two"]);
body("One<Out = Two>", vec!["One", "Two"]);
body("a::b::One<c::d::Two>", vec!["One", "Two"]);
body("&One", vec!["One"]);
body("&mut One", vec!["One"]);
Expand Down
10 changes: 5 additions & 5 deletions frb_rust/src/codec/dco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ impl Rust2DartMessageTrait for Rust2DartMessageDco {
}

unsafe fn from_raw_wire_sync(raw: Self::WireSyncRust2DartType) -> Self {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
return Self(*crate::for_generated::box_from_leak_ptr(raw));

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
return Self(raw);
}

fn into_raw_wire_sync(self) -> Self::WireSyncRust2DartType {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
return crate::for_generated::new_leak_box_ptr(self.0);

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
return self.0;
}
}
Expand All @@ -78,7 +78,7 @@ where
#[cfg(test)]
mod tests {

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[test]
fn test_simplest() {
use crate::codec::Rust2DartMessageTrait;
Expand Down
12 changes: 6 additions & 6 deletions frb_rust/src/codec/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ impl Rust2DartMessageTrait for Rust2DartMessageSse {
}

unsafe fn from_raw_wire_sync(raw: Self::WireSyncRust2DartType) -> Self {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
{
let WireSyncRust2DartSse { ptr, len } = raw;
Self(crate::for_generated::vec_from_leak_ptr(ptr, len))
}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
Self(js_sys::Uint8Array::new(&raw).to_vec())
}

fn into_raw_wire_sync(self) -> Self::WireSyncRust2DartType {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
{
let (ptr, len) = crate::for_generated::into_leak_vec_ptr(self.0);
WireSyncRust2DartSse { ptr, len }
}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
return <js_sys::Uint8Array>::from(self.0.as_slice()).into();
}
}
Expand All @@ -101,9 +101,9 @@ impl Dart2RustMessageSse {
rust_vec_len: i32,
data_len: i32,
) -> Self {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
let vec = crate::for_generated::vec_from_leak_ptr(ptr, rust_vec_len);
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
let vec = js_sys::Uint8Array::new(&ptr).to_vec();

Self { vec, data_len }
Expand Down
2 changes: 1 addition & 1 deletion frb_rust/src/dart_opaque/boxes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub(crate) mod dart_isolate_box;
pub(crate) mod guarded_box;
pub(crate) mod thread_box;
2 changes: 1 addition & 1 deletion frb_rust/src/dart_opaque/boxes/thread_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl GuardedBoxContext for GuardedBoxContextThread {
}
}

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[cfg(test)]
mod tests {
use crate::dart_opaque::boxes::thread_box::ThreadBox;
Expand Down
10 changes: 5 additions & 5 deletions frb_rust/src/dart_opaque/dart2rust.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{DartOpaque, GeneralizedDartHandle};
use crate::platform_types::{message_port_to_handle, MessagePort};
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;

/// # Safety
///
/// This should never be called manually.
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub unsafe fn cst_decode_dart_opaque(raw: wasm_bindgen::JsValue) -> DartOpaque {
#[cfg(target_pointer_width = "64")]
{
Expand All @@ -19,7 +19,7 @@ pub unsafe fn cst_decode_dart_opaque(raw: wasm_bindgen::JsValue) -> DartOpaque {
/// # Safety
///
/// This should never be called manually.
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub unsafe fn cst_decode_dart_opaque(raw: usize) -> DartOpaque {
DartOpaque::from_raw(raw as _)
}
Expand All @@ -34,7 +34,7 @@ pub unsafe fn sse_decode_dart_opaque(raw: usize) -> DartOpaque {
/// # Safety
///
/// This should never be called manually.
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
#[wasm_bindgen]
pub unsafe fn dart_opaque_dart2rust_encode(
handle: GeneralizedDartHandle,
Expand All @@ -46,7 +46,7 @@ pub unsafe fn dart_opaque_dart2rust_encode(
/// # Safety
///
/// This should never be called manually.
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[no_mangle]
pub unsafe extern "C" fn dart_opaque_dart2rust_encode(
handle: GeneralizedDartHandle,
Expand Down
10 changes: 5 additions & 5 deletions frb_rust/src/dart_opaque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use crate::platform_types::SendableMessagePortHandle;
use std::sync::Arc;

/// cbindgen:ignore
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
mod web;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use web::*;

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
mod io;

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub use io::*;

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
mod auto_drop_dart_persistent_handle;

pub(crate) mod action;
Expand Down
6 changes: 3 additions & 3 deletions frb_rust/src/dart_opaque/non_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::generalized_isolate::Channel;
use crate::generalized_isolate::IntoDart;
use crate::misc::logs::log_warn_or_println;
use crate::platform_types::{handle_to_message_port, SendableMessagePortHandle};
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;

#[derive(Debug)]
Expand Down Expand Up @@ -91,13 +91,13 @@ fn drop_thread_box_persistent_handle_via_port(
};
}

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[no_mangle]
pub unsafe extern "C" fn dart_opaque_drop_thread_box_persistent_handle(ptr: usize) {
dart_opaque_drop_thread_box_persistent_handle_inner(ptr)
}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
#[wasm_bindgen]
pub unsafe extern "C" fn dart_opaque_drop_thread_box_persistent_handle(ptr: usize) {
dart_opaque_drop_thread_box_persistent_handle_inner(ptr)
Expand Down
6 changes: 3 additions & 3 deletions frb_rust/src/dart_opaque/rust2dart.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{DartOpaque, GeneralizedDartHandle};
use crate::generalized_isolate::IntoDart;
use crate::platform_types::DartAbi;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;

impl From<DartOpaque> for DartAbi {
Expand All @@ -16,13 +16,13 @@ impl DartOpaque {
}
}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
#[wasm_bindgen]
pub unsafe fn dart_opaque_rust2dart_decode(ptr: usize) -> GeneralizedDartHandle {
dart_opaque_rust2dart_decode_inner(ptr)
}

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[no_mangle]
pub unsafe extern "C" fn dart_opaque_rust2dart_decode(ptr: usize) -> GeneralizedDartHandle {
dart_opaque_rust2dart_decode_inner(ptr)
Expand Down
8 changes: 4 additions & 4 deletions frb_rust/src/ffi_binding/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::for_generated::{into_leak_vec_ptr, new_leak_vec_ptr, vec_from_leak_ptr};

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
mod io;
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
#[allow(unused)]
pub use io::*;

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
mod web;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
#[allow(unused)]
pub use web::*;

Expand Down
2 changes: 1 addition & 1 deletion frb_rust/src/for_generated/cast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Cast a byte buffer into a boxed slice of the target type without making any copies.
/// Panics if the cast fails.
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub fn slice_from_byte_buffer<T: bytemuck::Pod>(buffer: Vec<u8>) -> Box<[T]> {
let buf = Box::leak(buffer.into_boxed_slice());
match bytemuck::try_cast_slice_mut(buf) {
Expand Down
12 changes: 6 additions & 6 deletions frb_rust/src/for_generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use crate::lockable::{
#[allow(unused)]
pub use crate::misc::manual_impl::*;
pub use crate::misc::version::FLUTTER_RUST_BRIDGE_RUNTIME_VERSION;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use crate::misc::web_utils;
pub use crate::platform_types::{
DartAbi, MessagePort, PlatformGeneralizedUint8ListPtr, WireSyncRust2DartDco,
Expand All @@ -63,23 +63,23 @@ pub use crate::rust_auto_opaque::{inner::RustAutoOpaqueInner, RustAutoOpaqueBase
pub use crate::rust_opaque::{dart2rust::decode_rust_opaque_nom, RustOpaqueBase};
pub use crate::stream::stream_sink::StreamSinkBase;
pub use crate::thread_pool::{BaseThreadPool, SimpleThreadPool};
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use crate::web_transfer::transfer_closure::TransferClosure;
#[cfg(feature = "anyhow")]
pub use anyhow;
pub use byteorder;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use cast::slice_from_byte_buffer;
#[cfg(feature = "dart-opaque")]
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub use dart_sys_fork as dart_sys;
#[cfg(feature = "rust-async")]
pub use futures;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use js_sys;
pub use lazy_static::lazy_static;
#[cfg(feature = "rust-async")]
pub use misc_rust_async::*;
pub use pointer::*;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use wasm_bindgen;
8 changes: 4 additions & 4 deletions frb_rust/src/generalized_isolate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// cbindgen:ignore
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
mod web;
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub use web::*;

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
mod io;
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub use io::*;
8 changes: 4 additions & 4 deletions frb_rust/src/handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ pub enum FfiCallMode {
Sync,
}

#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
pub trait TaskRetFutTrait: Send {}
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
impl<T: Send> TaskRetFutTrait for T {}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
pub trait TaskRetFutTrait {}
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
impl<T> TaskRetFutTrait for T {}

// Originally there were things for StreamSink, but it was moved, so now it is empty
Expand Down
8 changes: 4 additions & 4 deletions frb_rust/src/misc/manual_impl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(feature = "chrono")]
#[inline]
pub fn decode_timestamp(ts: i64) -> Timestamp {
#[cfg(wasm)]
#[cfg(target_family = "wasm")]
const PRECISION: i64 = 1_000;
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
const PRECISION: i64 = 1_000_000;

let s = ts / PRECISION;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn decode_uuid(id: Vec<u8>) -> uuid::Uuid {
mod tests {
#[test]
fn test_decode_timestamp() {
#[cfg(not(wasm))]
#[cfg(not(target_family = "wasm"))]
{
// input in microseconds
let input: i64 = 3_496_567_123;
Expand All @@ -62,7 +62,7 @@ mod tests {
assert_eq!(ns, 567_123_000);
}

#[cfg(wasm)]
#[cfg(target_family = "wasm")]
{
// input in milliseconds
let input: i64 = 3_496_567;
Expand Down
Loading

0 comments on commit 30a78f2

Please sign in to comment.