From 9178231b60a92e8554e7ef8cb1377067be0ef4c0 Mon Sep 17 00:00:00 2001 From: alexlapa Date: Thu, 14 Mar 2019 12:15:02 -0600 Subject: [PATCH 1/4] impl OptionFromWasmAbi and OptionIntoWasmAbi for ImportEnum, enable RTCRtpTransceiver.webidl, add add rtc_rtp_transceiver_direction test --- crates/backend/src/codegen.rs | 12 +++ crates/web-sys/Cargo.toml | 4 + crates/web-sys/tests/wasm/main.rs | 1 + .../wasm/rtc_rtp_transceiver_direction.rs | 87 +++++++++++++++++++ .../RTCRtpTransceiver.webidl | 0 5 files changed, 104 insertions(+) create mode 100644 crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs rename crates/web-sys/webidls/{unavailable_option_primitive => enabled}/RTCRtpTransceiver.webidl (100%) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 89a69e02..7bfbb2fb 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -844,6 +844,18 @@ impl ToTokens for ast::ImportEnum { } } + #[allow(clippy::all)] + impl ::wasm_bindgen::convert::OptionIntoWasmAbi for #name { + #[inline] + fn none() -> Self::Abi { Object::none() } + } + + #[allow(clippy::all)] + impl ::wasm_bindgen::convert::OptionFromWasmAbi for #name { + #[inline] + fn is_none(abi: &Self::Abi) -> bool { Object::is_none(abi) } + } + #[allow(clippy::all)] impl From<#name> for ::wasm_bindgen::JsValue { fn from(obj: #name) -> ::wasm_bindgen::JsValue { diff --git a/crates/web-sys/Cargo.toml b/crates/web-sys/Cargo.toml index 5956f168..931bb2b5 100644 --- a/crates/web-sys/Cargo.toml +++ b/crates/web-sys/Cargo.toml @@ -814,6 +814,9 @@ RtcRtpSender = [] RtcRtpSourceEntry = [] RtcRtpSourceEntryType = [] RtcRtpSynchronizationSource = [] +RtcRtpTransceiver = [] +RtcRtpTransceiverDirection = [] +RtcRtpTransceiverInit = [] RtcRtxParameters = [] RtcSdpType = [] RtcSessionDescription = [] @@ -826,6 +829,7 @@ RtcStatsReport = [] RtcStatsReportInternal = [] RtcStatsType = [] RtcTrackEvent = [] +RtcTrackEventInit = [] RtcTransportStats = [] RtcdtmfSender = [] RtcdtmfToneChangeEvent = [] diff --git a/crates/web-sys/tests/wasm/main.rs b/crates/web-sys/tests/wasm/main.rs index d4c01b8d..f27d6af7 100644 --- a/crates/web-sys/tests/wasm/main.rs +++ b/crates/web-sys/tests/wasm/main.rs @@ -48,6 +48,7 @@ pub mod pre_element; pub mod progress_element; pub mod quote_element; pub mod response; +pub mod rtc_rtp_transceiver_direction; pub mod script_element; pub mod select_element; pub mod slot_element; diff --git a/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs new file mode 100644 index 00000000..632e0c23 --- /dev/null +++ b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs @@ -0,0 +1,87 @@ +use wasm_bindgen::{prelude::*, JsCast}; +use wasm_bindgen_futures::JsFuture; +use wasm_bindgen_test::*; + +use futures::Future; + +use web_sys::{ + RtcPeerConnection, RtcRtpTransceiver, RtcRtpTransceiverDirection, RtcRtpTransceiverInit, + RtcSessionDescriptionInit, +}; + + +#[wasm_bindgen(inline_js = "export function is_unified_avail() { return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection')>-1; }")] +extern "C" { + /// Available in FF since forever, in Chrome since 72, in Safari since 12.1 + fn is_unified_avail() -> bool; +} + +#[wasm_bindgen_test(async)] +fn rtc_rtp_transceiver_direction() -> impl Future { + + if !is_unified_avail(){ + Ok(()) + } + + let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new(); + + let pc1: RtcPeerConnection = RtcPeerConnection::new().unwrap(); + + let tr1: RtcRtpTransceiver = pc1.add_transceiver_with_str_and_init( + "audio", + tr_init.direction(RtcRtpTransceiverDirection::Sendonly), + ); + assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly); + assert_eq!(tr1.current_direction(), None); + + let pc2: RtcPeerConnection = RtcPeerConnection::new().unwrap(); + + exchange_sdps(pc1, pc2).and_then(move |(_, p2)| { + assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly); + assert_eq!( + tr1.current_direction(), + Some(RtcRtpTransceiverDirection::Sendonly) + ); + + let tr2: RtcRtpTransceiver = js_sys::try_iter(&p2.get_transceivers()) + .unwrap() + .unwrap() + .next() + .unwrap() + .unwrap() + .unchecked_into(); + + assert_eq!(tr2.direction(), RtcRtpTransceiverDirection::Recvonly); + assert_eq!( + tr2.current_direction(), + Some(RtcRtpTransceiverDirection::Recvonly) + ); + + Ok(()) + }) +} + +fn exchange_sdps( + p1: RtcPeerConnection, + p2: RtcPeerConnection, +) -> impl Future { + JsFuture::from(p1.create_offer()) + .and_then(move |offer| { + let offer = offer.unchecked_into::(); + JsFuture::from(p1.set_local_description(&offer)).join4( + JsFuture::from(p2.set_remote_description(&offer)), + Ok(p1), + Ok(p2), + ) + }) + .and_then(|(_, _, p1, p2)| JsFuture::from(p2.create_answer()).join3(Ok(p1), Ok(p2))) + .and_then(|(answer, p1, p2)| { + let answer = answer.unchecked_into::(); + JsFuture::from(p2.set_local_description(&answer)).join4( + JsFuture::from(p1.set_remote_description(&answer)), + Ok(p1), + Ok(p2), + ) + }) + .and_then(|(_, _, p1, p2)| Ok((p1, p2))) +} diff --git a/crates/web-sys/webidls/unavailable_option_primitive/RTCRtpTransceiver.webidl b/crates/web-sys/webidls/enabled/RTCRtpTransceiver.webidl similarity index 100% rename from crates/web-sys/webidls/unavailable_option_primitive/RTCRtpTransceiver.webidl rename to crates/web-sys/webidls/enabled/RTCRtpTransceiver.webidl From bf273a2035c69f609a1a43c578b4d802fd022c30 Mon Sep 17 00:00:00 2001 From: alexlapa Date: Thu, 14 Mar 2019 12:30:08 -0600 Subject: [PATCH 2/4] fix --- .../tests/wasm/rtc_rtp_transceiver_direction.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs index 632e0c23..29bf80dc 100644 --- a/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs +++ b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs @@ -2,15 +2,19 @@ use wasm_bindgen::{prelude::*, JsCast}; use wasm_bindgen_futures::JsFuture; use wasm_bindgen_test::*; -use futures::Future; +use futures::{ + future::{ok, IntoFuture}, + Future, +}; use web_sys::{ RtcPeerConnection, RtcRtpTransceiver, RtcRtpTransceiverDirection, RtcRtpTransceiverInit, RtcSessionDescriptionInit, }; - -#[wasm_bindgen(inline_js = "export function is_unified_avail() { return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection')>-1; }")] +#[wasm_bindgen( + inline_js = "export function is_unified_avail() { return Object.keys(RTCRtpTransceiver.prototype).indexOf('currentDirection')>-1; }" +)] extern "C" { /// Available in FF since forever, in Chrome since 72, in Safari since 12.1 fn is_unified_avail() -> bool; @@ -18,9 +22,8 @@ extern "C" { #[wasm_bindgen_test(async)] fn rtc_rtp_transceiver_direction() -> impl Future { - - if !is_unified_avail(){ - Ok(()) + if !is_unified_avail() { + ok::<(), JsValue>(()); } let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new(); From e6c42d4155648e438eba0af40caa41d9ca96be6f Mon Sep 17 00:00:00 2001 From: alexlapa Date: Thu, 14 Mar 2019 12:50:50 -0600 Subject: [PATCH 3/4] fix --- .../tests/wasm/rtc_rtp_transceiver_direction.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs index 29bf80dc..15dbe7aa 100644 --- a/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs +++ b/crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs @@ -21,9 +21,9 @@ extern "C" { } #[wasm_bindgen_test(async)] -fn rtc_rtp_transceiver_direction() -> impl Future { +fn rtc_rtp_transceiver_direction() -> Box> { if !is_unified_avail() { - ok::<(), JsValue>(()); + return Box::new(Ok(()).into_future()); } let mut tr_init: RtcRtpTransceiverInit = RtcRtpTransceiverInit::new(); @@ -39,7 +39,7 @@ fn rtc_rtp_transceiver_direction() -> impl Future { let pc2: RtcPeerConnection = RtcPeerConnection::new().unwrap(); - exchange_sdps(pc1, pc2).and_then(move |(_, p2)| { + let r = exchange_sdps(pc1, pc2).and_then(move |(_, p2)| { assert_eq!(tr1.direction(), RtcRtpTransceiverDirection::Sendonly); assert_eq!( tr1.current_direction(), @@ -61,7 +61,9 @@ fn rtc_rtp_transceiver_direction() -> impl Future { ); Ok(()) - }) + }); + + Box::new(r) } fn exchange_sdps( From 4e32b5e430d52a3d30d0a0975bb3280e3044794e Mon Sep 17 00:00:00 2001 From: alexlapa Date: Fri, 15 Mar 2019 14:27:18 -0600 Subject: [PATCH 4/4] add read_optional_enum_attribute to webidl-tests/enums --- crates/webidl-tests/enums.js | 8 ++++++++ crates/webidl-tests/enums.rs | 14 ++++++++++++++ crates/webidl-tests/enums.webidl | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/crates/webidl-tests/enums.js b/crates/webidl-tests/enums.js index fa23ec81..1750f69e 100644 --- a/crates/webidl-tests/enums.js +++ b/crates/webidl-tests/enums.js @@ -18,4 +18,12 @@ global.Shape = class Shape { getShape() { return this.kind; } + + get shapeTypeNone() { + return null; + } + + get shapeTypeSome() { + return this.kind; + } }; diff --git a/crates/webidl-tests/enums.rs b/crates/webidl-tests/enums.rs index b5e0949e..23f258c2 100644 --- a/crates/webidl-tests/enums.rs +++ b/crates/webidl-tests/enums.rs @@ -35,3 +35,17 @@ fn invalid_enum_return() { _ => {} // Success }; } + +#[wasm_bindgen_test] +fn read_optional_enum_attribute_none() { + let shape = Shape::new(ShapeType::Circle).unwrap(); + let shape_type: Option = shape.shape_type_none(); + assert_eq!(shape_type, None); +} + +#[wasm_bindgen_test] +fn read_optional_enum_attribute_some() { + let shape = Shape::new(ShapeType::Circle).unwrap(); + let shape_type: Option = shape.shape_type_some(); + assert_eq!(shape_type, Some(ShapeType::Circle)); +} diff --git a/crates/webidl-tests/enums.webidl b/crates/webidl-tests/enums.webidl index acd043b3..b200888d 100644 --- a/crates/webidl-tests/enums.webidl +++ b/crates/webidl-tests/enums.webidl @@ -12,4 +12,8 @@ interface Shape { [Pure] ShapeType getShape(); + + readonly attribute ShapeType? shapeTypeNone; + + readonly attribute ShapeType? shapeTypeSome; };