wasm-bindgen/tests/wasm/imports.rs
Alex Crichton 8e56cdacc5
Rewrite wasm-bindgen with updated interface types proposal (#1882)
This commit is a pretty large scale rewrite of the internals of wasm-bindgen. No user-facing changes are expected as a result of this PR, but due to the scale of changes here it's likely inevitable that at least something will break. I'm hoping to get more testing in though before landing!

The purpose of this PR is to update wasm-bindgen to the current state of the interface types proposal. The wasm-bindgen tool was last updated when it was still called "WebIDL bindings" so it's been awhile! All support is now based on https://github.com/bytecodealliance/wasm-interface-types which defines parsers/binary format/writers/etc for wasm-interface types.

This is a pretty massive PR and unfortunately can't really be split up any more afaik. I don't really expect realistic review of all the code here (or commits), but some high-level changes are:

* Interface types now consists of a set of "adapter functions". The IR in wasm-bindgen is modeled the same way not.
* Each adapter function has a list of instructions, and these instructions work at a higher level than wasm itself, for example with strings.
* The wasm-bindgen tool has a suite of instructions which are specific to it and not present in the standard. (like before with webidl bindings)
* The anyref/multi-value transformations are now greatly simplified. They're simply "optimization passes" over adapter functions, removing instructions that are otherwise present. This way we don't have to juggle so much all over the place, and instructions always have the same meaning.
2019-12-03 11:16:44 -06:00

277 lines
6.2 KiB
Rust

use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = "tests/wasm/imports.js")]
extern "C" {
fn test_simple();
fn simple_foo(s: &str);
fn simple_another(a: u32) -> i32;
fn simple_take_and_return_bool(a: bool) -> bool;
fn simple_return_object() -> JsValue;
#[allow(dead_code)]
fn missing_symbol(s: &str);
fn return_string() -> String;
fn take_and_ret_string(s: String) -> String;
#[wasm_bindgen(js_name = take_and_ret_string)]
fn take_and_ret_string2(s: &str) -> String;
fn exceptions_throw();
#[wasm_bindgen(catch)]
fn exceptions_throw2() -> Result<(), JsValue>;
fn test_exception_propagates();
fn assert_valid_error(val: JsValue);
static IMPORT: JsValue;
#[wasm_bindgen(js_name = return_three)]
fn rust_name_for_return_three() -> u32;
fn underscore(_: u8);
#[wasm_bindgen(js_name = pub)]
fn js_function_named_rust_keyword() -> u32;
type bar;
#[wasm_bindgen(js_namespace = bar, js_name = foo)]
static FOO: JsValue;
fn take_custom_type(f: CustomType) -> CustomType;
fn touch_custom_type();
fn custom_type_return_2() -> CustomType;
#[wasm_bindgen(js_name = interpret_2_as_custom_type)]
fn js_interpret_2_as_custom_type();
#[wasm_bindgen(js_name = "baz$")]
fn renamed_with_dollar_sign();
#[wasm_bindgen(js_name = "$foo")]
static RENAMED: JsValue;
fn unused_import();
fn assert_dead_import_not_generated();
fn should_call_undefined_functions() -> bool;
type StaticMethodCheck;
#[wasm_bindgen(static_method_of = StaticMethodCheck)]
fn static_method_of_right_this();
static STATIC_STRING: String;
#[derive(Clone)]
type PassOutOptionUndefined;
fn get_some_val() -> PassOutOptionUndefined;
#[wasm_bindgen(js_name = "receive_undefined")]
fn receive_undefined_ref(arg: Option<&PassOutOptionUndefined>);
#[wasm_bindgen(js_name = "receive_undefined")]
fn receive_undefined_owned(arg: Option<PassOutOptionUndefined>);
#[wasm_bindgen(js_name = "receive_some")]
fn receive_some_ref(arg: Option<&PassOutOptionUndefined>);
#[wasm_bindgen(js_name = "receive_some")]
fn receive_some_owned(arg: Option<PassOutOptionUndefined>);
}
#[wasm_bindgen]
extern "C" {
fn parseInt(a: &str) -> u32;
}
#[wasm_bindgen_test]
fn simple() {
test_simple();
}
#[wasm_bindgen]
pub fn simple_take_str(s: &str) {
simple_foo(s);
}
#[wasm_bindgen]
pub fn simple_another_thunk(a: u32) -> i32 {
simple_another(a)
}
#[wasm_bindgen]
pub fn simple_bool_thunk(a: bool) -> bool {
simple_take_and_return_bool(a)
}
#[wasm_bindgen]
pub fn simple_get_the_object() -> JsValue {
simple_return_object()
}
#[wasm_bindgen_test]
fn string_ret() {
assert_eq!(return_string(), "bar");
}
#[wasm_bindgen_test]
fn strings() {
assert_eq!(take_and_ret_string(String::from("a")), "ab");
assert_eq!(take_and_ret_string2("b"), "bb");
}
#[wasm_bindgen_test]
fn exceptions() {
test_exception_propagates();
assert!(exceptions_throw2().is_err());
}
#[wasm_bindgen]
pub fn exceptions_propagate() {
exceptions_throw();
}
#[wasm_bindgen_test]
fn exn_caught() {
assert_valid_error(exceptions_throw2().unwrap_err());
}
#[wasm_bindgen_test]
fn free_imports() {
assert_eq!(parseInt("3"), 3);
}
#[wasm_bindgen_test]
fn import_a_field() {
assert_eq!(IMPORT.as_f64(), Some(1.0));
}
#[wasm_bindgen_test]
fn rename() {
assert_eq!(rust_name_for_return_three(), 3);
}
#[wasm_bindgen_test]
fn underscore_pattern() {
underscore(2);
}
#[wasm_bindgen_test]
fn rust_keyword() {
assert_eq!(js_function_named_rust_keyword(), 2);
}
#[wasm_bindgen_test]
fn rust_keyword2() {
assert_eq!(FOO.as_f64(), Some(3.0));
}
#[wasm_bindgen_test]
fn custom_type() {
take_custom_type(CustomType(()));
touch_custom_type();
js_interpret_2_as_custom_type();
}
#[wasm_bindgen]
pub fn interpret_2_as_custom_type() {
custom_type_return_2();
}
#[wasm_bindgen]
pub struct CustomType(());
#[wasm_bindgen]
impl CustomType {
pub fn touch(&self) {
panic!()
}
}
#[wasm_bindgen_test]
fn rename_with_string() {
renamed_with_dollar_sign();
}
#[wasm_bindgen_test]
fn rename_static_with_string() {
assert_eq!(RENAMED.as_f64(), Some(1.0));
}
#[wasm_bindgen_test]
fn dead_imports_not_generated() {
assert_dead_import_not_generated();
}
#[wasm_bindgen_test]
#[cfg(feature = "nightly")]
fn import_inside_function_works() {
#[wasm_bindgen(module = "tests/wasm/imports.js")]
extern "C" {
fn import_inside_function_works();
}
import_inside_function_works();
}
#[wasm_bindgen_test]
#[cfg(feature = "nightly")]
fn private_module_imports_work() {
private::foo();
}
#[cfg(feature = "nightly")]
mod private {
use wasm_bindgen::prelude::*;
pub fn foo() {
#[wasm_bindgen(module = "tests/wasm/imports.js")]
extern "C" {
fn import_inside_private_module();
}
import_inside_private_module();
}
}
#[wasm_bindgen]
extern "C" {
fn something_not_defined_in_the_environment();
type TypeThatIsNotDefined;
#[wasm_bindgen(constructor)]
fn new() -> TypeThatIsNotDefined;
#[wasm_bindgen(method)]
fn method(this: &TypeThatIsNotDefined);
#[wasm_bindgen(method, getter)]
fn property(this: &TypeThatIsNotDefined) -> u32;
#[wasm_bindgen(method, setter)]
fn set_property(this: &TypeThatIsNotDefined, val: u32);
}
#[wasm_bindgen_test]
fn undefined_function_is_ok() {
if !should_call_undefined_functions() {
return;
}
something_not_defined_in_the_environment();
let x = TypeThatIsNotDefined::new();
x.method();
x.set_property(x.property());
}
#[wasm_bindgen_test]
fn static_string_ok() {
assert_eq!(*STATIC_STRING, "x");
}
#[wasm_bindgen_test]
fn static_method_of_has_right_this() {
StaticMethodCheck::static_method_of_right_this();
}
#[wasm_bindgen_test]
fn pass_out_options_as_undefined() {
receive_undefined_ref(None);
receive_undefined_ref(None);
receive_undefined_owned(None);
receive_undefined_owned(None);
let v = get_some_val();
receive_some_ref(Some(&v));
receive_some_ref(Some(&v));
receive_some_owned(Some(v.clone()));
receive_some_owned(Some(v));
}