2018-08-05 19:12:23 +03:00
|
|
|
//! dox
|
|
|
|
|
|
|
|
#![deny(missing_docs)] // test that documenting public bindings is enough
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
2018-09-26 18:26:00 +03:00
|
|
|
use wasm_bindgen_test::*;
|
2018-08-05 19:12:23 +03:00
|
|
|
|
2018-08-06 20:55:04 +03:00
|
|
|
#[wasm_bindgen(module = "tests/wasm/import_class.js")]
|
2018-09-26 18:26:00 +03:00
|
|
|
extern "C" {
|
2018-08-05 19:12:23 +03:00
|
|
|
fn math_log(f: f64) -> f64;
|
|
|
|
|
|
|
|
#[wasm_bindgen(js_namespace = StaticFunction)]
|
|
|
|
fn bar() -> u32;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
type Construct;
|
|
|
|
#[wasm_bindgen(js_namespace = Construct)]
|
|
|
|
fn create() -> Construct;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn get_internal_string(this: &Construct) -> String;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn append_to_internal_string(this: &Construct, s: &str);
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn assert_internal_string(this: &Construct, s: &str);
|
|
|
|
|
|
|
|
type NewConstructors;
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new(arg: i32) -> NewConstructors;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn get(this: &NewConstructors) -> i32;
|
|
|
|
|
2018-12-11 21:42:37 +03:00
|
|
|
#[wasm_bindgen(js_name = default)]
|
|
|
|
type RenamedTypes;
|
|
|
|
#[wasm_bindgen(constructor, js_class = default)]
|
|
|
|
fn new(arg: i32) -> RenamedTypes;
|
|
|
|
#[wasm_bindgen(method, js_class = default)]
|
|
|
|
fn get(this: &RenamedTypes) -> i32;
|
|
|
|
|
2018-08-05 19:12:23 +03:00
|
|
|
fn switch_methods_a();
|
|
|
|
fn switch_methods_b();
|
|
|
|
type SwitchMethods;
|
2018-11-28 02:14:59 +03:00
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
#[wasm_bindgen(final)]
|
2018-08-05 19:12:23 +03:00
|
|
|
fn new() -> SwitchMethods;
|
2018-11-09 18:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = SwitchMethods, final)]
|
2018-08-05 19:12:23 +03:00
|
|
|
fn a();
|
|
|
|
fn switch_methods_called() -> bool;
|
2018-11-09 18:59:48 +03:00
|
|
|
#[wasm_bindgen(method, final)]
|
2018-08-05 19:12:23 +03:00
|
|
|
fn b(this: &SwitchMethods);
|
|
|
|
|
|
|
|
type Properties;
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> Properties;
|
|
|
|
#[wasm_bindgen(getter, method)]
|
|
|
|
fn a(this: &Properties) -> i32;
|
|
|
|
#[wasm_bindgen(setter, method)]
|
|
|
|
fn set_a(this: &Properties, a: i32);
|
|
|
|
|
|
|
|
type RenameProperties;
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> RenameProperties;
|
|
|
|
#[wasm_bindgen(getter = a, method)]
|
|
|
|
fn test(this: &RenameProperties) -> i32;
|
2018-09-22 03:34:41 +03:00
|
|
|
#[wasm_bindgen(getter, method, js_name = a)]
|
|
|
|
fn test2(this: &RenameProperties) -> i32;
|
2018-08-05 19:12:23 +03:00
|
|
|
#[wasm_bindgen(setter = a, method)]
|
|
|
|
fn another(this: &RenameProperties, a: i32);
|
2018-09-22 03:34:41 +03:00
|
|
|
#[wasm_bindgen(setter, method, js_name = a)]
|
|
|
|
fn another2(this: &RenameProperties, a: i32);
|
2018-08-05 19:12:23 +03:00
|
|
|
|
|
|
|
/// dox
|
|
|
|
pub type AssertImportDenyDocsWorks;
|
|
|
|
/// dox
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> AssertImportDenyDocsWorks;
|
|
|
|
/// dox
|
|
|
|
#[wasm_bindgen(getter = a, method)]
|
|
|
|
pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
|
|
|
|
/// dox
|
|
|
|
pub fn foo();
|
|
|
|
|
|
|
|
pub type Options;
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> Options;
|
|
|
|
fn take_none(val: Option<Options>);
|
|
|
|
fn take_some(val: Option<Options>);
|
|
|
|
fn return_null() -> Option<Options>;
|
|
|
|
fn return_undefined() -> Option<Options>;
|
|
|
|
fn return_some() -> Option<Options>;
|
|
|
|
fn run_rust_option_tests();
|
2018-08-20 20:40:54 +03:00
|
|
|
|
|
|
|
type CatchConstructors;
|
|
|
|
#[wasm_bindgen(constructor, catch)]
|
|
|
|
fn new(x: u32) -> Result<CatchConstructors, JsValue>;
|
2018-11-08 22:50:34 +03:00
|
|
|
|
|
|
|
type StaticStructural;
|
|
|
|
#[wasm_bindgen(static_method_of = StaticStructural, structural)]
|
|
|
|
fn static_structural(a: u32) -> u32;
|
2018-08-05 19:12:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-09-26 18:26:00 +03:00
|
|
|
extern "C" {
|
2018-08-05 19:12:23 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Math)]
|
|
|
|
fn random() -> f64;
|
|
|
|
#[wasm_bindgen(js_namespace = Math)]
|
|
|
|
fn log(a: f64) -> f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn simple() {
|
|
|
|
random();
|
|
|
|
assert_eq!(log(1.0), math_log(1.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn import_class() {
|
|
|
|
assert_eq!(bar(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn construct() {
|
|
|
|
let f = Construct::create();
|
|
|
|
assert_eq!(f.get_internal_string(), "this");
|
|
|
|
assert_eq!(f.clone().get_internal_string(), "this");
|
|
|
|
f.append_to_internal_string(" foo");
|
|
|
|
f.assert_internal_string("this foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn new_constructors() {
|
|
|
|
let f = NewConstructors::new(1);
|
|
|
|
assert_eq!(f.get(), 2);
|
|
|
|
}
|
|
|
|
|
2018-12-11 21:42:37 +03:00
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn rename_type() {
|
|
|
|
let f = RenamedTypes::new(1);
|
|
|
|
assert_eq!(f.get(), 2);
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:12:23 +03:00
|
|
|
#[wasm_bindgen_test]
|
2019-05-30 21:00:51 +03:00
|
|
|
#[cfg(ignored)] // TODO: fix this before landing
|
2018-08-05 19:12:23 +03:00
|
|
|
fn switch_methods() {
|
|
|
|
assert!(!switch_methods_called());
|
|
|
|
SwitchMethods::a();
|
|
|
|
assert!(switch_methods_called());
|
|
|
|
|
|
|
|
switch_methods_a();
|
|
|
|
|
|
|
|
assert!(!switch_methods_called());
|
|
|
|
SwitchMethods::a();
|
|
|
|
assert!(switch_methods_called());
|
|
|
|
|
|
|
|
assert!(!switch_methods_called());
|
|
|
|
SwitchMethods::new().b();
|
|
|
|
assert!(switch_methods_called());
|
|
|
|
|
2018-11-08 23:30:40 +03:00
|
|
|
switch_methods_b();
|
2018-08-05 19:12:23 +03:00
|
|
|
|
|
|
|
assert!(!switch_methods_called());
|
|
|
|
SwitchMethods::new().b();
|
First refactor for WebIDL bindings
This commit starts the `wasm-bindgen` CLI tool down the road to being a
true polyfill for WebIDL bindings. This refactor is probably the first
of a few, but is hopefully the largest and most sprawling and everything
will be a bit more targeted from here on out.
The goal of this refactoring is to separate out the massive
`crates/cli-support/src/js/mod.rs` into a number of separate pieces of
functionality. It currently takes care of basically everything
including:
* Binding intrinsics
* Handling anyref transformations
* Generating all JS for imports/exports
* All the logic for how to import and how to name imports
* Execution and management of wasm-bindgen closures
Many of these are separable concerns and most overlap with WebIDL
bindings. The internal refactoring here is intended to make it more
clear who's responsible for what as well as making some existing
operations much more straightforward. At a high-level, the following
changes are done:
1. A `src/webidl.rs` module is introduced. The purpose of this module is
to take all of the raw wasm-bindgen custom sections from the module
and transform them into a WebIDL bindings section.
This module has a placeholder `WebidlCustomSection` which is nowhere
near the actual custom section but if you squint is in theory very
similar. It's hoped that this will eventually become the true WebIDL
custom section, currently being developed in an external crate.
Currently, however, the WebIDL bindings custom section only covers a
subset of the functionality we export to wasm-bindgen users. To avoid
leaving them high and dry this module also contains an auxiliary
custom section named `WasmBindgenAux`. This custom section isn't
intended to have a binary format, but is intended to represent a
theoretical custom section necessary to couple with WebIDL bindings to
achieve all our desired functionality in `wasm-bindgen`. It'll never
be standardized, but it'll also never be serialized :)
2. The `src/webidl.rs` module now takes over quite a bit of
functionality from `src/js/mod.rs`. Namely it handles synthesis of an
`export_map` and an `import_map` mapping export/import IDs to exactly
what's expected to be hooked up there. This does not include type
information (as that's in the bindings section) but rather includes
things like "this is the method of class A" or "this import is from
module `foo`" and things like that. These could arguably be subsumed
by future JS features as well, but that's for another time!
3. All handling of wasm-bindgen "descriptor functions" now happens in a
dedicated `src/descriptors.rs` module. The output of this module is
its own custom section (intended to be immediately consumed by the
WebIDL module) which is in theory what we want to ourselves emit one
day but rustc isn't capable of doing so right now.
4. Invocations and generations of imports are completely overhauled.
Using the `import_map` generated in the WebIDL step all imports are
now handled much more precisely in one location rather than
haphazardly throughout the module. This means we have precise
information about each import of the module and we only modify
exactly what we're looking at. This also vastly simplifies intrinsic
generation since it's all simply a codegen part of the `rust2js.rs`
module now.
5. Handling of direct imports which don't have a JS shim generated is
slightly different from before and is intended to be
future-compatible with WebIDL bindings in its full glory, but we'll
need to update it to handle cases for constructors and method calls
eventually as well.
6. Intrinsic definitions now live in their own file (`src/intrinsic.rs`)
and have a separated definition for their symbol name and signature.
The actual implementation of each intrinsic lives in `rust2js.rs`
There's a number of TODO items to finish before this merges. This
includes reimplementing the anyref pass and actually implementing import
maps for other targets. Those will come soon in follow-up commits, but
the entire `tests/wasm/main.rs` suite is currently passing and this
seems like a good checkpoint.
2019-05-23 19:15:26 +03:00
|
|
|
assert!(!switch_methods_called());
|
2018-08-05 19:12:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn properties() {
|
|
|
|
let a = Properties::new();
|
|
|
|
assert_eq!(a.a(), 1);
|
|
|
|
a.set_a(2);
|
|
|
|
assert_eq!(a.a(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn rename_setter_getter() {
|
|
|
|
let x: fn() -> RenameProperties = RenameProperties::new;
|
|
|
|
let a = x();
|
|
|
|
assert_eq!(a.test(), 1);
|
|
|
|
a.another(2);
|
|
|
|
assert_eq!(a.test(), 2);
|
2018-09-22 03:34:41 +03:00
|
|
|
a.another2(3);
|
|
|
|
assert_eq!(a.test2(), 3);
|
2018-08-05 19:12:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dox
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct AssertDenyDocsWorks {
|
|
|
|
/// dox
|
|
|
|
pub a: u32,
|
|
|
|
_b: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dox
|
|
|
|
#[wasm_bindgen]
|
2018-09-26 18:26:00 +03:00
|
|
|
pub fn assert_deny_docs_works() {}
|
2018-08-05 19:12:23 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn options() {
|
|
|
|
take_none(None);
|
|
|
|
take_some(Some(Options::new()));
|
|
|
|
assert!(return_null().is_none());
|
|
|
|
assert!(return_undefined().is_none());
|
|
|
|
assert!(return_some().is_some());
|
|
|
|
run_rust_option_tests();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doc
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn rust_take_none(a: Option<Options>) {
|
|
|
|
assert!(a.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doc
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn rust_take_some(a: Option<Options>) {
|
|
|
|
assert!(a.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doc
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn rust_return_none() -> Option<Options> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// doc
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn rust_return_some() -> Option<Options> {
|
|
|
|
Some(Options::new())
|
|
|
|
}
|
2018-08-20 20:40:54 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn catch_constructors() {
|
|
|
|
assert!(CatchConstructors::new(0).is_err());
|
|
|
|
assert!(CatchConstructors::new(1).is_ok());
|
|
|
|
}
|
2018-11-08 22:50:34 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn static_structural() {
|
|
|
|
assert_eq!(StaticStructural::static_structural(30), 33);
|
|
|
|
}
|