mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-17 02:30:50 +00:00
* Shard the `convert.rs` module into sub-modules Hopefully this'll make the organization a little nicer over time! * Start adding support for optional types This commit starts adding support for optional types to wasm-bindgen as arguments/return values to functions. The strategy here is to add two new traits, `OptionIntoWasmAbi` and `OptionFromWasmAbi`. These two traits are used as a blanket impl to implement `IntoWasmAbi` and `FromWasmAbi` for `Option<T>`. Some consequences of this design: * It should be possible to ensure `Option<SomeForeignType>` implements to/from wasm traits. This is because the option-based traits can be implemented for foreign types. * A specialized implementation is possible for all types, so there's no need for `Option<T>` to introduce unnecessary overhead. * Two new traits is a bit unforutnate but I can't currently think of an alternative design that works for the above two constraints, although it doesn't mean one doesn't exist! * The error messages for "can't use this type here" is actually halfway decent because it says these new traits need to be implemented, which provides a good place to document and talk about what's going on here! * Nested references like `Option<&T>` can't implement `FromWasmAbi`. This means that you can't define a function in Rust which takes `Option<&str>`. It may be possible to do this one day but it'll likely require more trait trickery than I'm capable of right now. * Add support for optional slices This commit adds support for optional slice types, things like strings and arrays. The null representation of these has a pointer value of 0, which should never happen in normal Rust. Otherwise the various plumbing is done throughout the tooling to enable these types in all locations. * Fix `takeObject` on global sentinels These don't have a reference count as they're always expected to work, so avoid actually dropping a reference on them. * Remove some no longer needed bindings * Add support for optional anyref types This commit adds support for optional imported class types. Each type imported with `#[wasm_bindgen]` automatically implements the relevant traits and now supports `Option<Foo>` in various argument/return positions. * Fix building without the `std` feature * Actually fix the build... * Add support for optional types to WebIDL Closes #502
234 lines
7.0 KiB
Rust
234 lines
7.0 KiB
Rust
use super::project;
|
|
|
|
#[test]
|
|
fn works() {
|
|
project()
|
|
.file(
|
|
"src/lib.rs",
|
|
r#"
|
|
#![feature(use_extern_macros)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
|
pub fn foo() -> JsValue {
|
|
JsValue::from("foo")
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn bar(s: &str) -> JsValue {
|
|
JsValue::from(s)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn baz() -> JsValue {
|
|
JsValue::from(1.0)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn baz2(a: &JsValue, b: &JsValue) {
|
|
assert_eq!(a.as_f64(), Some(2.0));
|
|
assert_eq!(b.as_f64(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn js_null() -> JsValue {
|
|
JsValue::null()
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn js_undefined() -> JsValue {
|
|
JsValue::undefined()
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn test_is_null_undefined(
|
|
a: &JsValue,
|
|
b: &JsValue,
|
|
c: &JsValue,
|
|
) {
|
|
assert!(a.is_null());
|
|
assert!(!a.is_undefined());
|
|
|
|
assert!(!b.is_null());
|
|
assert!(b.is_undefined());
|
|
|
|
assert!(!c.is_null());
|
|
assert!(!c.is_undefined());
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn get_true() -> JsValue {
|
|
JsValue::from(true)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn get_false() -> JsValue {
|
|
JsValue::from(false)
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn test_bool(
|
|
a: &JsValue,
|
|
b: &JsValue,
|
|
c: &JsValue,
|
|
) {
|
|
assert_eq!(a.as_bool(), Some(true));
|
|
assert_eq!(format!("{:?}", a), "true");
|
|
assert_eq!(b.as_bool(), Some(false));
|
|
assert_eq!(c.as_bool(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn mk_symbol() -> JsValue {
|
|
let a = JsValue::symbol(None);
|
|
assert!(a.is_symbol());
|
|
assert_eq!(format!("{:?}", a), "Symbol(..)");
|
|
return a
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn mk_symbol2(s: &str) -> JsValue {
|
|
let a = JsValue::symbol(Some(s));
|
|
assert!(a.is_symbol());
|
|
return a
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn assert_symbols(a: &JsValue, b: &JsValue) {
|
|
assert!(a.is_symbol());
|
|
assert!(!b.is_symbol());
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn acquire_string(a: &JsValue, b: &JsValue) {
|
|
assert_eq!(a.as_string().unwrap(), "foo");
|
|
assert_eq!(format!("{:?}", a), "\"foo\"");
|
|
assert_eq!(b.as_string(), None);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn acquire_string2(a: &JsValue) -> String {
|
|
a.as_string().unwrap_or("wrong".to_string())
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.js",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
assert.strictEqual(wasm.foo(), 'foo');
|
|
assert.strictEqual(wasm.bar('a'), 'a');
|
|
assert.strictEqual(wasm.baz(), 1);
|
|
wasm.baz2(2, 'a');
|
|
|
|
assert.strictEqual(wasm.js_null(), null);
|
|
assert.strictEqual(wasm.js_undefined(), undefined);
|
|
|
|
wasm.test_is_null_undefined(null, undefined, 1.0);
|
|
|
|
assert.strictEqual(wasm.get_true(), true);
|
|
assert.strictEqual(wasm.get_false(), false);
|
|
wasm.test_bool(true, false, 1.0);
|
|
|
|
assert.strictEqual(typeof(wasm.mk_symbol()), 'symbol');
|
|
assert.strictEqual(typeof(wasm.mk_symbol2('a')), 'symbol');
|
|
assert.strictEqual(Symbol.keyFor(wasm.mk_symbol()), undefined);
|
|
assert.strictEqual(Symbol.keyFor(wasm.mk_symbol2('b')), undefined);
|
|
|
|
wasm.assert_symbols(Symbol(), 'a');
|
|
wasm.acquire_string('foo', null)
|
|
assert.strictEqual(wasm.acquire_string2(''), '');
|
|
assert.strictEqual(wasm.acquire_string2('a'), 'a');
|
|
}
|
|
"#,
|
|
)
|
|
.test();
|
|
}
|
|
|
|
#[test]
|
|
fn eq_works() {
|
|
project()
|
|
.file(
|
|
"src/lib.rs",
|
|
r#"
|
|
#![feature(use_extern_macros)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
|
pub fn test(a: &JsValue, b: &JsValue) -> bool {
|
|
a == b
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn test1(a: &JsValue) -> bool {
|
|
a == a
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.js",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
assert.strictEqual(wasm.test('a', 'a'), true);
|
|
assert.strictEqual(wasm.test('a', 'b'), false);
|
|
assert.strictEqual(wasm.test(NaN, NaN), false);
|
|
assert.strictEqual(wasm.test({a: 'a'}, {a: 'a'}), false);
|
|
assert.strictEqual(wasm.test1(NaN), false);
|
|
let x = {a: 'a'};
|
|
assert.strictEqual(wasm.test(x, x), true);
|
|
assert.strictEqual(wasm.test1(x), true);
|
|
}
|
|
"#,
|
|
)
|
|
.test();
|
|
}
|
|
|
|
#[test]
|
|
fn null_keeps_working() {
|
|
project()
|
|
.file(
|
|
"src/lib.rs",
|
|
r#"
|
|
#![feature(use_extern_macros, wasm_import_module)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen(module = "./foo")]
|
|
extern {
|
|
fn take_null(val: JsValue);
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub fn test() {
|
|
take_null(JsValue::null());
|
|
take_null(JsValue::null());
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"foo.js",
|
|
r#"
|
|
import { strictEqual } from "assert";
|
|
|
|
export function take_null(n) {
|
|
strictEqual(n, null);
|
|
}
|
|
"#,
|
|
)
|
|
.test();
|
|
}
|