From 5403510594c9d0e9fa4e801f6f7620769ddedde1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Thu, 14 Mar 2019 08:17:24 +0000 Subject: [PATCH 1/7] Update @wasm-tool/wasm-pack-plugin requirement from 0.2.5 to 0.2.7 Updates the requirements on [@wasm-tool/wasm-pack-plugin](https://github.com/wasm-tool/wasm-pack-plugin) to permit the latest version. - [Release notes](https://github.com/wasm-tool/wasm-pack-plugin/releases) - [Commits](https://github.com/wasm-tool/wasm-pack-plugin/commits) Signed-off-by: dependabot[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 22697755585..febc40258ea 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "serve": "webpack-dev-server -p" }, "devDependencies": { - "@wasm-tool/wasm-pack-plugin": "0.2.5", + "@wasm-tool/wasm-pack-plugin": "0.2.7", "html-webpack-plugin": "^3.2.0", "text-encoding": "^0.7.0", "webpack": "^4.29.4", From 9178231b60a92e8554e7ef8cb1377067be0ef4c0 Mon Sep 17 00:00:00 2001 From: alexlapa Date: Thu, 14 Mar 2019 12:15:02 -0600 Subject: [PATCH 2/7] 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 89a69e02187..7bfbb2fb245 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 5956f1689bb..931bb2b5d9c 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 d4c01b8d107..f27d6af71f5 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 00000000000..632e0c233e1 --- /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 3/7] 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 632e0c233e1..29bf80dc936 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 4/7] 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 29bf80dc936..15dbe7aa11a 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 0d592fffb04b361023bbfbfd5bdcc83b30e3777f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 15 Mar 2019 08:04:25 -0700 Subject: [PATCH 5/7] Add a `raw_module` attribute to `#[wasm_bindgen]` This allows subverting the checks and resolution performed by the `module` attribute added as part of [RFC 6] and has been discussed in #1343. Closes #1343 [RFC 6]: https://github.com/rustwasm/rfcs/pull/6 --- crates/backend/src/ast.rs | 5 +++ crates/backend/src/encode.rs | 1 + crates/cli-support/src/js/mod.rs | 13 ++++--- crates/macro-support/src/parser.rs | 37 +++++++++++-------- crates/shared/src/lib.rs | 1 + guide/src/SUMMARY.md | 1 + .../attributes/on-js-imports/module.md | 5 +++ .../attributes/on-js-imports/raw_module.md | 19 ++++++++++ 8 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 guide/src/reference/attributes/on-js-imports/raw_module.md diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index 4c317cea44f..75c73860275 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -79,6 +79,7 @@ pub struct Import { pub enum ImportModule { None, Named(String, Span), + RawNamed(String, Span), Inline(usize, Span), } @@ -96,6 +97,10 @@ impl Hash for ImportModule { 2u8.hash(h); idx.hash(h); } + ImportModule::RawNamed(name, _) => { + 3u8.hash(h); + name.hash(h); + } } } } diff --git a/crates/backend/src/encode.rs b/crates/backend/src/encode.rs index abc8c613762..2e327d20253 100644 --- a/crates/backend/src/encode.rs +++ b/crates/backend/src/encode.rs @@ -208,6 +208,7 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result { 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, }, diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 57e90368eb8..cdd2ea57af4 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -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, }; @@ -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 diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index 574af3c27ed..d94a468070c 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -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)), (setter, Setter(Span, Option)), @@ -1085,24 +1086,28 @@ impl MacroParse 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()) { diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index 9b9356f2498..5bd0072cd23 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -28,6 +28,7 @@ macro_rules! shared_api { enum ImportModule<'a> { None, Named(&'a str), + RawNamed(&'a str), Inline(u32), } diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index 077c50cbe3b..530b1286b7e 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -66,6 +66,7 @@ - [`js_namespace`](./reference/attributes/on-js-imports/js_namespace.md) - [`method`](./reference/attributes/on-js-imports/method.md) - [`module = "blah"`](./reference/attributes/on-js-imports/module.md) + - [`raw_module = "blah"`](./reference/attributes/on-js-imports/raw_module.md) - [`static_method_of = Blah`](./reference/attributes/on-js-imports/static_method_of.md) - [`structural`](./reference/attributes/on-js-imports/structural.md) - [`variadic`](./reference/attributes/on-js-imports/variadic.md) diff --git a/guide/src/reference/attributes/on-js-imports/module.md b/guide/src/reference/attributes/on-js-imports/module.md index 4f6b4408e7c..24643e42c73 100644 --- a/guide/src/reference/attributes/on-js-imports/module.md +++ b/guide/src/reference/attributes/on-js-imports/module.md @@ -31,3 +31,8 @@ generates JavaScript import glue like: ```js let illmatic = this.illmatic; ``` + +Note that if the string specified with `module` starts with `./`, `../`, or `/` +then it's interpreted as a path to a [local JS snippet](../../js-snippets.html). +If this doesn't work for your use case you might be interested in the +[`raw_module` attribute](raw_module.html) diff --git a/guide/src/reference/attributes/on-js-imports/raw_module.md b/guide/src/reference/attributes/on-js-imports/raw_module.md new file mode 100644 index 00000000000..1f8da2f3dfb --- /dev/null +++ b/guide/src/reference/attributes/on-js-imports/raw_module.md @@ -0,0 +1,19 @@ +# `raw_module = "blah"` + +This attribute performs exactly the same purpose as the [`module` +attribute](module.html) on JS imports, but it does not attempt to interpret +paths starting with `./`, `../`, or `/` as JS snippets. For example: + +```rust +#[wasm_bindgen(raw_module = "./some/js/file.js")] +extern "C" { + fn the_function(); +} +``` + +Note that if you use this attribute with a relative or absolute path, it's +likely up to the final bundler or project to assign meaning to that path. This +typically means that the JS file or module will be resolved relative to the +final location of the wasm file itself. That means that `raw_module` is likely +unsuitable for libraries on crates.io, but may be usable within end-user +applications. From 4e32b5e430d52a3d30d0a0975bb3280e3044794e Mon Sep 17 00:00:00 2001 From: alexlapa Date: Fri, 15 Mar 2019 14:27:18 -0600 Subject: [PATCH 6/7] 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 fa23ec818e1..1750f69e2e4 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 b5e0949e785..23f258c22b4 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 acd043b319e..b200888d3de 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; }; From dc50a5ab582e0d5a59304a5b107852e44caa4bfe Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 15 Mar 2019 22:15:04 -0300 Subject: [PATCH 7/7] Improve TS tests --- crates/typescript-tests/index.ts | 11 --------- crates/typescript-tests/src/custom_section.rs | 11 +++++++++ crates/typescript-tests/src/custom_section.ts | 3 +++ crates/typescript-tests/src/lib.rs | 24 ++++--------------- crates/typescript-tests/src/memory.ts | 3 +++ .../typescript-tests/src/opt_args_and_ret.rs | 6 +++++ .../typescript-tests/src/opt_args_and_ret.ts | 3 +++ crates/typescript-tests/src/simple_fn.rs | 4 ++++ crates/typescript-tests/src/simple_fn.ts | 5 ++++ crates/typescript-tests/src/simple_struct.rs | 17 +++++++++++++ crates/typescript-tests/src/simple_struct.ts | 6 +++++ crates/typescript-tests/tsconfig.json | 2 +- 12 files changed, 63 insertions(+), 32 deletions(-) delete mode 100644 crates/typescript-tests/index.ts create mode 100644 crates/typescript-tests/src/custom_section.rs create mode 100644 crates/typescript-tests/src/custom_section.ts create mode 100644 crates/typescript-tests/src/memory.ts create mode 100644 crates/typescript-tests/src/opt_args_and_ret.rs create mode 100644 crates/typescript-tests/src/opt_args_and_ret.ts create mode 100644 crates/typescript-tests/src/simple_fn.rs create mode 100644 crates/typescript-tests/src/simple_fn.ts create mode 100644 crates/typescript-tests/src/simple_struct.rs create mode 100644 crates/typescript-tests/src/simple_struct.ts diff --git a/crates/typescript-tests/index.ts b/crates/typescript-tests/index.ts deleted file mode 100644 index f36c5192936..00000000000 --- a/crates/typescript-tests/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import * as wbg from './pkg/typescript_tests'; -import * as wasm from './pkg/typescript_tests_bg'; - -const a1: (a: string) => void = wbg.greet; -const a2: (a: number, b: number) => void = wasm.greet; -const a3: WebAssembly.Memory = wasm.memory; - -const c = new wbg.A(); -wbg.A.other(); -c.foo(); -c.free(); diff --git a/crates/typescript-tests/src/custom_section.rs b/crates/typescript-tests/src/custom_section.rs new file mode 100644 index 00000000000..e1f5c1fc790 --- /dev/null +++ b/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, +} \ No newline at end of file diff --git a/crates/typescript-tests/src/custom_section.ts b/crates/typescript-tests/src/custom_section.ts new file mode 100644 index 00000000000..6420ea6dd59 --- /dev/null +++ b/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(); \ No newline at end of file diff --git a/crates/typescript-tests/src/lib.rs b/crates/typescript-tests/src/lib.rs index ea10b7b2a7a..5e927e5ae72 100644 --- a/crates/typescript-tests/src/lib.rs +++ b/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; \ No newline at end of file diff --git a/crates/typescript-tests/src/memory.ts b/crates/typescript-tests/src/memory.ts new file mode 100644 index 00000000000..b1095b006a6 --- /dev/null +++ b/crates/typescript-tests/src/memory.ts @@ -0,0 +1,3 @@ +import * as wasm from '../pkg/typescript_tests_bg'; + +const memory: WebAssembly.Memory = wasm.memory; diff --git a/crates/typescript-tests/src/opt_args_and_ret.rs b/crates/typescript-tests/src/opt_args_and_ret.rs new file mode 100644 index 00000000000..a84f2bfef27 --- /dev/null +++ b/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) -> Option { + None +} \ No newline at end of file diff --git a/crates/typescript-tests/src/opt_args_and_ret.ts b/crates/typescript-tests/src/opt_args_and_ret.ts new file mode 100644 index 00000000000..d0bd20a4f2e --- /dev/null +++ b/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; \ No newline at end of file diff --git a/crates/typescript-tests/src/simple_fn.rs b/crates/typescript-tests/src/simple_fn.rs new file mode 100644 index 00000000000..29a7d4aa604 --- /dev/null +++ b/crates/typescript-tests/src/simple_fn.rs @@ -0,0 +1,4 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn greet(_: &str) {} \ No newline at end of file diff --git a/crates/typescript-tests/src/simple_fn.ts b/crates/typescript-tests/src/simple_fn.ts new file mode 100644 index 00000000000..f8b91d08f7c --- /dev/null +++ b/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; \ No newline at end of file diff --git a/crates/typescript-tests/src/simple_struct.rs b/crates/typescript-tests/src/simple_struct.rs new file mode 100644 index 00000000000..d15e5031ff2 --- /dev/null +++ b/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) {} +} \ No newline at end of file diff --git a/crates/typescript-tests/src/simple_struct.ts b/crates/typescript-tests/src/simple_struct.ts new file mode 100644 index 00000000000..9583039950f --- /dev/null +++ b/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(); diff --git a/crates/typescript-tests/tsconfig.json b/crates/typescript-tests/tsconfig.json index bdd0690736c..9089297990a 100644 --- a/crates/typescript-tests/tsconfig.json +++ b/crates/typescript-tests/tsconfig.json @@ -9,6 +9,6 @@ "baseUrl": "." }, "include": [ - "index.ts" + "src/*.ts" ] }