2018-10-03 09:55:07 +03:00
|
|
|
#[allow(unused_imports)] // test for #919
|
|
|
|
use std::borrow::BorrowMut;
|
|
|
|
|
2018-08-04 21:52:21 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-09-26 18:26:00 +03:00
|
|
|
use wasm_bindgen_test::*;
|
2018-08-04 21:52:21 +03:00
|
|
|
|
2018-08-06 20:55:04 +03:00
|
|
|
#[wasm_bindgen(module = "tests/wasm/classes.js")]
|
2018-09-26 18:26:00 +03:00
|
|
|
extern "C" {
|
2018-08-05 05:00:16 +03:00
|
|
|
fn js_simple();
|
|
|
|
fn js_strings();
|
|
|
|
fn js_exceptions();
|
|
|
|
fn js_pass_one_to_another();
|
2018-08-04 21:52:21 +03:00
|
|
|
fn take_class(foo: ClassesIntoJs);
|
|
|
|
#[wasm_bindgen(js_name = take_class)]
|
|
|
|
fn take_class_as_jsvalue(foo: JsValue);
|
2018-08-05 05:00:16 +03:00
|
|
|
fn js_constructors();
|
|
|
|
fn js_empty_structs();
|
|
|
|
fn js_public_fields();
|
|
|
|
fn js_using_self();
|
|
|
|
fn js_readonly_fields();
|
|
|
|
fn js_double_consume();
|
|
|
|
fn js_js_rename();
|
2018-10-10 20:21:19 +03:00
|
|
|
fn js_access_fields();
|
2018-11-05 23:29:14 +03:00
|
|
|
fn js_renamed_export();
|
2019-01-26 01:31:50 +03:00
|
|
|
fn js_conditional_bindings();
|
2019-02-19 20:08:37 +03:00
|
|
|
|
|
|
|
fn js_assert_none(a: Option<OptionClass>);
|
|
|
|
fn js_assert_some(a: Option<OptionClass>);
|
|
|
|
fn js_return_none1() -> Option<OptionClass>;
|
|
|
|
fn js_return_none2() -> Option<OptionClass>;
|
|
|
|
fn js_return_some(a: OptionClass) -> Option<OptionClass>;
|
|
|
|
fn js_test_option_classes();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn simple() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_simple();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesSimple {
|
|
|
|
contents: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesSimple {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> ClassesSimple {
|
|
|
|
ClassesSimple::with_contents(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_contents(a: u32) -> ClassesSimple {
|
|
|
|
ClassesSimple { contents: a }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&mut self, amt: u32) -> u32 {
|
|
|
|
self.contents += amt;
|
|
|
|
self.contents
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn consume(self) -> u32 {
|
|
|
|
self.contents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn strings() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_strings()
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesStrings1 {
|
|
|
|
name: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesStrings2 {
|
|
|
|
contents: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesStrings1 {
|
|
|
|
pub fn new() -> ClassesStrings1 {
|
|
|
|
ClassesStrings1 { name: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, amt: u32) {
|
|
|
|
self.name = amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bar(&self, mix: &str) -> ClassesStrings2 {
|
2018-09-26 18:26:00 +03:00
|
|
|
ClassesStrings2 {
|
|
|
|
contents: format!("foo-{}-{}", mix, self.name),
|
|
|
|
}
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesStrings2 {
|
|
|
|
pub fn name(&self) -> String {
|
|
|
|
self.contents.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn exceptions() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_exceptions();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
2018-08-05 05:00:16 +03:00
|
|
|
|
2018-08-04 21:52:21 +03:00
|
|
|
#[wasm_bindgen]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub struct ClassesExceptions1 {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesExceptions1 {
|
|
|
|
pub fn new() -> ClassesExceptions1 {
|
|
|
|
ClassesExceptions1 {}
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:00:16 +03:00
|
|
|
pub fn foo(&self, _: &ClassesExceptions1) {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
2018-08-05 05:00:16 +03:00
|
|
|
pub fn bar(&mut self, _: &mut ClassesExceptions1) {}
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub struct ClassesExceptions2 {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesExceptions2 {
|
|
|
|
pub fn new() -> ClassesExceptions2 {
|
|
|
|
ClassesExceptions2 {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn pass_one_to_another() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_pass_one_to_another();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesPassA {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesPassA {
|
|
|
|
pub fn new() -> ClassesPassA {
|
|
|
|
ClassesPassA {}
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:00:16 +03:00
|
|
|
pub fn foo(&self, _other: &ClassesPassB) {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
2018-08-05 05:00:16 +03:00
|
|
|
pub fn bar(&self, _other: ClassesPassB) {}
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesPassB {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesPassB {
|
|
|
|
pub fn new() -> ClassesPassB {
|
|
|
|
ClassesPassB {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn pass_into_js() {
|
|
|
|
take_class(ClassesIntoJs(13));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ClassesIntoJs(i32);
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ClassesIntoJs {
|
|
|
|
pub fn inner(&self) -> i32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Issue27Context {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Issue27Context {
|
|
|
|
pub fn parse(&self, _expr: &str) -> Issue27Expr {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
pub fn eval(&self, _expr: &Issue27Expr) -> f64 {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
pub fn set(&mut self, _var: &str, _val: f64) {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Issue27Expr {}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn pass_into_js_as_js_class() {
|
|
|
|
take_class_as_jsvalue(ClassesIntoJs(13).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn constructors() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_constructors();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn cross_item_construction() -> ConstructorsBar {
|
|
|
|
ConstructorsBar::other_name(7, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ConstructorsFoo {
|
|
|
|
number: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ConstructorsFoo {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new(number: u32) -> ConstructorsFoo {
|
|
|
|
ConstructorsFoo { number }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_number(&self) -> u32 {
|
|
|
|
self.number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct ConstructorsBar {
|
|
|
|
number: u32,
|
|
|
|
number2: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl ConstructorsBar {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn other_name(number: u32, number2: u32) -> ConstructorsBar {
|
|
|
|
ConstructorsBar { number, number2 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_sum(&self) -> u32 {
|
|
|
|
self.number + self.number2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn empty_structs() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_empty_structs();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct MissingClass {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct OtherEmpty {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl OtherEmpty {
|
2018-09-26 18:26:00 +03:00
|
|
|
pub fn return_a_value() -> MissingClass {
|
|
|
|
MissingClass {}
|
|
|
|
}
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn public_fields() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_public_fields();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PublicFields {
|
|
|
|
pub a: u32,
|
|
|
|
pub b: f32,
|
|
|
|
pub c: f64,
|
|
|
|
pub d: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl PublicFields {
|
|
|
|
pub fn new() -> PublicFields {
|
|
|
|
PublicFields::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn using_self() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_using_self();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub struct UseSelf {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl UseSelf {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
UseSelf {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn readonly_fields() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_readonly_fields();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Readonly {
|
|
|
|
#[wasm_bindgen(readonly)]
|
|
|
|
pub a: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Readonly {
|
|
|
|
pub fn new() -> Readonly {
|
|
|
|
Readonly::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn double_consume() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_double_consume();
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub struct DoubleConsume {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl DoubleConsume {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> DoubleConsume {
|
|
|
|
DoubleConsume {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn consume(self, other: DoubleConsume) {
|
|
|
|
drop(other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn rename_function_for_js() {
|
2018-08-05 05:00:16 +03:00
|
|
|
js_js_rename();
|
2018-08-04 21:52:21 +03:00
|
|
|
foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub struct JsRename {}
|
2018-08-04 21:52:21 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl JsRename {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> JsRename {
|
|
|
|
let f = JsRename {};
|
|
|
|
f.foo();
|
2018-08-05 05:00:16 +03:00
|
|
|
f
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = bar)]
|
2018-08-05 05:00:16 +03:00
|
|
|
pub fn foo(&self) {}
|
2018-08-04 21:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = classes_foo)]
|
|
|
|
pub fn foo() {}
|
2018-10-10 20:21:19 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct AccessFieldFoo {
|
|
|
|
pub bar: AccessFieldBar,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AccessFieldBar {
|
2018-10-11 02:09:24 +03:00
|
|
|
_value: u32,
|
2018-10-10 20:21:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl AccessFieldFoo {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> AccessFieldFoo {
|
|
|
|
AccessFieldFoo {
|
2018-10-11 02:09:24 +03:00
|
|
|
bar: AccessFieldBar { _value: 2 },
|
2018-10-10 20:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn access_fields() {
|
|
|
|
js_access_fields();
|
|
|
|
}
|
2018-11-05 23:29:14 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = JsRenamedExport)]
|
|
|
|
pub struct RenamedExport {
|
|
|
|
pub x: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(js_class = JsRenamedExport)]
|
|
|
|
impl RenamedExport {
|
|
|
|
#[wasm_bindgen(constructor)]
|
2018-11-27 23:07:59 +03:00
|
|
|
pub fn new() -> RenamedExport {
|
|
|
|
RenamedExport { x: 3 }
|
2018-11-05 23:29:14 +03:00
|
|
|
}
|
2018-11-27 23:07:59 +03:00
|
|
|
pub fn foo(&self) {}
|
2018-11-05 23:29:14 +03:00
|
|
|
|
|
|
|
pub fn bar(&self, other: &RenamedExport) {
|
|
|
|
drop(other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn renamed_export() {
|
|
|
|
js_renamed_export();
|
|
|
|
}
|
2019-01-26 01:31:50 +03:00
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 20:54:23 +03:00
|
|
|
pub struct ConditionalBindings {}
|
2019-01-26 01:31:50 +03:00
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
|
|
|
|
impl ConditionalBindings {
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
|
|
|
|
pub fn new() -> ConditionalBindings {
|
|
|
|
ConditionalBindings {}
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 20:08:37 +03:00
|
|
|
|
2019-01-26 01:31:50 +03:00
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn conditional_bindings() {
|
|
|
|
js_conditional_bindings();
|
|
|
|
}
|
2019-02-19 20:08:37 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct OptionClass(u32);
|
|
|
|
|
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn option_class() {
|
|
|
|
js_assert_none(None);
|
|
|
|
js_assert_some(Some(OptionClass(1)));
|
|
|
|
assert!(js_return_none1().is_none());
|
|
|
|
assert!(js_return_none2().is_none());
|
|
|
|
assert_eq!(js_return_some(OptionClass(2)).unwrap().0, 2);
|
|
|
|
js_test_option_classes();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn option_class_none() -> Option<OptionClass> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn option_class_some() -> Option<OptionClass> {
|
|
|
|
Some(OptionClass(3))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn option_class_assert_none(x: Option<OptionClass>) {
|
|
|
|
assert!(x.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn option_class_assert_some(x: Option<OptionClass>) {
|
|
|
|
assert_eq!(x.unwrap().0, 3);
|
|
|
|
}
|