Skip to content

Commit

Permalink
Merge branch 'master' into reexporting_in_2018
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Mar 18, 2019
2 parents 69bbf59 + 2177dee commit 42c1cdd
Show file tree
Hide file tree
Showing 29 changed files with 260 additions and 54 deletions.
5 changes: 5 additions & 0 deletions crates/backend/src/ast.rs
Expand Up @@ -79,6 +79,7 @@ pub struct Import {
pub enum ImportModule {
None,
Named(String, Span),
RawNamed(String, Span),
Inline(usize, Span),
}

Expand All @@ -96,6 +97,10 @@ impl Hash for ImportModule {
2u8.hash(h);
idx.hash(h);
}
ImportModule::RawNamed(name, _) => {
3u8.hash(h);
name.hash(h);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/backend/src/codegen.rs
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/backend/src/encode.rs
Expand Up @@ -208,6 +208,7 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result<Import<
ast::ImportModule::Named(m, span) => {
ImportModule::Named(intern.resolve_import_module(m, *span)?)
}
ast::ImportModule::RawNamed(m, _span) => ImportModule::RawNamed(intern.intern_str(m)),
ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32),
ast::ImportModule::None => ImportModule::None,
},
Expand Down
13 changes: 8 additions & 5 deletions crates/cli-support/src/js/mod.rs
Expand Up @@ -2836,6 +2836,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
// not sure how to import them.
let is_local_snippet = match import.module {
decode::ImportModule::Named(s) => self.cx.local_modules.contains_key(s),
decode::ImportModule::RawNamed(_) => false,
decode::ImportModule::Inline(_) => true,
decode::ImportModule::None => false,
};
Expand Down Expand Up @@ -2921,11 +2922,13 @@ impl<'a, 'b> SubContext<'a, 'b> {
name,
field,
},
decode::ImportModule::Named(module) => Import::Module {
module,
name,
field,
},
decode::ImportModule::Named(module) | decode::ImportModule::RawNamed(module) => {
Import::Module {
module,
name,
field,
}
}
decode::ImportModule::Inline(idx) => {
let offset = *self
.cx
Expand Down
37 changes: 21 additions & 16 deletions crates/macro-support/src/parser.rs
Expand Up @@ -33,6 +33,7 @@ macro_rules! attrgen {
(static_method_of, StaticMethodOf(Span, Ident)),
(js_namespace, JsNamespace(Span, Ident)),
(module, Module(Span, String, Span)),
(raw_module, RawModule(Span, String, Span)),
(inline_js, InlineJs(Span, String, Span)),
(getter, Getter(Span, Option<Ident>)),
(setter, Setter(Span, Option<Ident>)),
Expand Down Expand Up @@ -1085,24 +1086,28 @@ impl MacroParse<BindgenAttrs> for syn::ItemForeignMod {
));
}
}
let module = match opts.module() {
Some((name, span)) => {
if opts.inline_js().is_some() {
let msg = "cannot specify both `module` and `inline_js`";
errors.push(Diagnostic::span_error(span, msg));
}
ast::ImportModule::Named(name.to_string(), span)
let module = if let Some((name, span)) = opts.module() {
if opts.inline_js().is_some() {
let msg = "cannot specify both `module` and `inline_js`";
errors.push(Diagnostic::span_error(span, msg));
}
None => {
match opts.inline_js() {
Some((js, span)) => {
let i = program.inline_js.len();
program.inline_js.push(js.to_string());
ast::ImportModule::Inline(i, span)
}
None => ast::ImportModule::None
}
if opts.raw_module().is_some() {
let msg = "cannot specify both `module` and `raw_module`";
errors.push(Diagnostic::span_error(span, msg));
}
ast::ImportModule::Named(name.to_string(), span)
} else if let Some((name, span)) = opts.raw_module() {
if opts.inline_js().is_some() {
let msg = "cannot specify both `raw_module` and `inline_js`";
errors.push(Diagnostic::span_error(span, msg));
}
ast::ImportModule::RawNamed(name.to_string(), span)
} else if let Some((js, span)) = opts.inline_js() {
let i = program.inline_js.len();
program.inline_js.push(js.to_string());
ast::ImportModule::Inline(i, span)
} else {
ast::ImportModule::None
};
for item in self.items.into_iter() {
if let Err(e) = item.macro_parse(program, module.clone()) {
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Expand Up @@ -28,6 +28,7 @@ macro_rules! shared_api {
enum ImportModule<'a> {
None,
Named(&'a str),
RawNamed(&'a str),
Inline(u32),
}

Expand Down
11 changes: 0 additions & 11 deletions crates/typescript-tests/index.ts

This file was deleted.

11 changes: 11 additions & 0 deletions crates/typescript-tests/src/custom_section.rs
@@ -0,0 +1,11 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_INTERFACE_EXPORT: &'static str = r"
interface Height { height: number; }
";

#[wasm_bindgen]
pub struct Person {
pub height: u32,
}
3 changes: 3 additions & 0 deletions crates/typescript-tests/src/custom_section.ts
@@ -0,0 +1,3 @@
import * as wbg from '../pkg/typescript_tests';

const height: wbg.Height = new wbg.Person();
24 changes: 4 additions & 20 deletions crates/typescript-tests/src/lib.rs
@@ -1,20 +1,4 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(_: &str) {}

#[wasm_bindgen]
struct A {
}

#[wasm_bindgen]
impl A {
#[wasm_bindgen(constructor)]
pub fn new() -> A {
A {}
}

pub fn other() {}

pub fn foo(&self) {}
}
mod custom_section;
mod opt_args_and_ret;
mod simple_fn;
mod simple_struct;
3 changes: 3 additions & 0 deletions crates/typescript-tests/src/memory.ts
@@ -0,0 +1,3 @@
import * as wasm from '../pkg/typescript_tests_bg';

const memory: WebAssembly.Memory = wasm.memory;
6 changes: 6 additions & 0 deletions crates/typescript-tests/src/opt_args_and_ret.rs
@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn opt_fn(_a: Option<i32>) -> Option<i32> {
None
}
3 changes: 3 additions & 0 deletions crates/typescript-tests/src/opt_args_and_ret.ts
@@ -0,0 +1,3 @@
import * as wbg from '../pkg/typescript_tests';

const opt_fn: (a: number | undefined) => number | undefined = wbg.opt_fn;
4 changes: 4 additions & 0 deletions crates/typescript-tests/src/simple_fn.rs
@@ -0,0 +1,4 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(_: &str) {}
5 changes: 5 additions & 0 deletions crates/typescript-tests/src/simple_fn.ts
@@ -0,0 +1,5 @@
import * as wbg from '../pkg/typescript_tests';
import * as wasm from '../pkg/typescript_tests_bg';

const wbg_greet: (a: string) => void = wbg.greet;
const wasm_greet: (a: number, b: number) => void = wasm.greet;
17 changes: 17 additions & 0 deletions crates/typescript-tests/src/simple_struct.rs
@@ -0,0 +1,17 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct A {
}

#[wasm_bindgen]
impl A {
#[wasm_bindgen(constructor)]
pub fn new() -> A {
A {}
}

pub fn other() {}

pub fn foo(&self) {}
}
6 changes: 6 additions & 0 deletions crates/typescript-tests/src/simple_struct.ts
@@ -0,0 +1,6 @@
import * as wbg from '../pkg/typescript_tests';

const a = new wbg.A();
wbg.A.other();
a.foo();
a.free();
2 changes: 1 addition & 1 deletion crates/typescript-tests/tsconfig.json
Expand Up @@ -9,6 +9,6 @@
"baseUrl": "."
},
"include": [
"index.ts"
"src/*.ts"
]
}
4 changes: 4 additions & 0 deletions crates/web-sys/Cargo.toml
Expand Up @@ -814,6 +814,9 @@ RtcRtpSender = []
RtcRtpSourceEntry = []
RtcRtpSourceEntryType = []
RtcRtpSynchronizationSource = []
RtcRtpTransceiver = []
RtcRtpTransceiverDirection = []
RtcRtpTransceiverInit = []
RtcRtxParameters = []
RtcSdpType = []
RtcSessionDescription = []
Expand All @@ -826,6 +829,7 @@ RtcStatsReport = []
RtcStatsReportInternal = []
RtcStatsType = []
RtcTrackEvent = []
RtcTrackEventInit = []
RtcTransportStats = []
RtcdtmfSender = []
RtcdtmfToneChangeEvent = []
Expand Down
1 change: 1 addition & 0 deletions crates/web-sys/tests/wasm/main.rs
Expand Up @@ -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;
Expand Down
92 changes: 92 additions & 0 deletions crates/web-sys/tests/wasm/rtc_rtp_transceiver_direction.rs
@@ -0,0 +1,92 @@
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;

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; }"
)]
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() -> Box<dyn Future<Item = (), Error = JsValue>> {
if !is_unified_avail() {
return Box::new(Ok(()).into_future());
}

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();

let r = 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(())
});

Box::new(r)
}

fn exchange_sdps(
p1: RtcPeerConnection,
p2: RtcPeerConnection,
) -> impl Future<Item = (RtcPeerConnection, RtcPeerConnection), Error = JsValue> {
JsFuture::from(p1.create_offer())
.and_then(move |offer| {
let offer = offer.unchecked_into::<RtcSessionDescriptionInit>();
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::<RtcSessionDescriptionInit>();
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)))
}
8 changes: 8 additions & 0 deletions crates/webidl-tests/enums.js
Expand Up @@ -18,4 +18,12 @@ global.Shape = class Shape {
getShape() {
return this.kind;
}

get shapeTypeNone() {
return null;
}

get shapeTypeSome() {
return this.kind;
}
};

0 comments on commit 42c1cdd

Please sign in to comment.