wasm-bindgen/tests/wasm/simple.rs

225 lines
4.8 KiB
Rust
Raw Normal View History

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = "tests/wasm/simple.js")]
extern "C" {
fn test_add();
fn test_string_arguments();
fn test_return_a_string();
fn test_wrong_types();
fn test_other_exports_still_available();
fn test_jsvalue_typeof();
fn optional_str_none(a: Option<&str>);
fn optional_str_some(a: Option<&str>);
fn optional_slice_none(a: Option<&[u8]>);
fn optional_slice_some(a: Option<&[u8]>);
fn optional_string_none(a: Option<String>);
fn optional_string_some(a: Option<String>);
fn optional_string_some_empty(a: Option<String>);
fn return_string_none() -> Option<String>;
fn return_string_some() -> Option<String>;
fn test_rust_optional();
#[wasm_bindgen(js_name = import_export_same_name)]
fn js_import_export_same_name();
#[wasm_bindgen(js_name = RenamedInRust)]
type Renamed;
fn new_renamed() -> Renamed;
2019-05-13 18:12:32 +03:00
fn test_string_roundtrip();
}
#[wasm_bindgen_test]
fn add() {
test_add();
}
#[wasm_bindgen]
pub fn simple_add(a: u32, b: u32) -> u32 {
a + b
}
#[wasm_bindgen]
pub fn simple_add3(a: u32) -> u32 {
a + 3
}
#[wasm_bindgen]
pub fn simple_get2(_b: bool) -> u32 {
2
}
#[wasm_bindgen]
pub fn simple_return_and_take_bool(a: bool, b: bool) -> bool {
a && b
}
#[wasm_bindgen]
pub fn simple_raw_pointers_work(a: *mut u32, b: *const u8) -> *const u32 {
unsafe {
(*a) = (*b) as u32;
return a;
}
}
#[wasm_bindgen_test]
fn string_arguments() {
test_string_arguments();
}
#[wasm_bindgen]
pub fn simple_assert_foo_and_bar(a: &str, b: &str) {
assert_eq!(a, "foo2");
assert_eq!(b, "bar");
}
#[wasm_bindgen]
pub fn simple_assert_foo(a: &str) {
assert_eq!(a, "foo");
}
#[wasm_bindgen_test]
fn return_a_string() {
test_return_a_string();
}
#[wasm_bindgen]
pub fn simple_clone(a: &str) -> String {
a.to_string()
}
#[wasm_bindgen]
pub fn simple_concat(a: &str, b: &str, c: i8) -> String {
format!("{} {} {}", a, b, c)
}
#[wasm_bindgen_test]
fn wrong_types() {
test_wrong_types();
}
#[wasm_bindgen]
pub fn simple_int(_a: u32) {}
#[wasm_bindgen]
pub fn simple_str(_a: &str) {}
#[wasm_bindgen_test]
fn other_exports() {
test_other_exports_still_available();
}
#[no_mangle]
pub extern "C" fn foo(_a: u32) {}
#[wasm_bindgen_test]
fn jsvalue_typeof() {
test_jsvalue_typeof();
}
#[wasm_bindgen]
pub fn is_object(val: &JsValue) -> bool {
val.is_object()
}
#[wasm_bindgen]
pub fn is_function(val: &JsValue) -> bool {
val.is_function()
}
#[wasm_bindgen]
pub fn is_string(val: &JsValue) -> bool {
val.is_string()
}
#[wasm_bindgen]
extern "C" {
#[derive(Clone)]
type Array;
#[wasm_bindgen(constructor)]
fn new() -> Array;
#[wasm_bindgen(method, catch)]
fn standardized_method_this_js_runtime_doesnt_implement_yet(
this: &Array,
) -> Result<(), JsValue>;
}
#[wasm_bindgen_test]
fn binding_to_unimplemented_apis_doesnt_break_everything() {
let array = Array::new();
let res = array.standardized_method_this_js_runtime_doesnt_implement_yet();
assert!(res.is_err());
}
#[wasm_bindgen_test]
fn optional_slices() {
optional_str_none(None);
optional_str_some(Some("x"));
optional_slice_none(None);
optional_slice_some(Some(&[1, 2, 3]));
optional_string_none(None);
optional_string_some_empty(Some(String::new()));
optional_string_some(Some("abcd".to_string()));
assert_eq!(return_string_none(), None);
assert_eq!(return_string_some(), Some("foo".to_string()));
test_rust_optional();
}
#[wasm_bindgen]
pub fn take_optional_str_none(x: Option<String>) {
assert!(x.is_none())
}
#[wasm_bindgen]
pub fn take_optional_str_some(x: Option<String>) {
assert_eq!(x, Some(String::from("hello")));
}
#[wasm_bindgen]
pub fn return_optional_str_none() -> Option<String> {
None
}
#[wasm_bindgen]
pub fn return_optional_str_some() -> Option<String> {
Some("world".to_string())
}
#[wasm_bindgen_test]
fn renaming_imports_and_instanceof() {
let null = JsValue::NULL;
assert!(!null.is_instance_of::<Renamed>());
let arr: JsValue = Array::new().into();
assert!(!arr.is_instance_of::<Renamed>());
let renamed: JsValue = new_renamed().into();
assert!(renamed.is_instance_of::<Renamed>());
}
#[wasm_bindgen]
pub fn import_export_same_name() {
js_import_export_same_name();
}
2019-05-13 18:12:32 +03:00
#[wasm_bindgen_test]
fn string_roundtrip() {
test_string_roundtrip();
}
#[wasm_bindgen]
pub fn do_string_roundtrip(s: String) -> String {
s
}
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
#[wasm_bindgen_test]
fn anyref_heap_live_count() {
let x = wasm_bindgen::anyref_heap_live_count();
let y = JsValue::null().clone();
assert!(wasm_bindgen::anyref_heap_live_count() > x);
drop(y);
assert_eq!(x, wasm_bindgen::anyref_heap_live_count());
}