wasm-bindgen/tests/all/api.rs

194 lines
5.5 KiB
Rust
Raw Normal View History

use super::project;
#[test]
fn works() {
project()
2018-06-28 08:42:34 +03:00
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section)]
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!(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());
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!(b.as_string(), None);
}
#[wasm_bindgen]
pub fn acquire_string2(a: &JsValue) -> String {
a.as_string().unwrap_or("wrong".to_string())
}
2018-06-28 08:42:34 +03:00
"#,
)
.file(
"test.ts",
r#"
import * as assert from "assert";
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-30 08:20:38 +03:00
import * as wasm from "./out";
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-30 08:20:38 +03:00
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');
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-30 08:20:38 +03:00
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol()), undefined);
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol2('b')), undefined);
Rewrite wasm-bindgen with ES6 modules in mind This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
2018-01-30 08:20:38 +03:00
wasm.assert_symbols((Symbol as any)(), 'a');
wasm.acquire_string('foo', null)
assert.strictEqual(wasm.acquire_string2(''), '');
assert.strictEqual(wasm.acquire_string2('a'), 'a');
}
2018-06-28 08:42:34 +03:00
"#,
)
.test();
}
#[test]
fn eq_works() {
project()
2018-06-28 08:42:34 +03:00
.file(
"src/lib.rs",
r#"
#![feature(proc_macro, wasm_custom_section)]
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
}
2018-06-28 08:42:34 +03:00
"#,
)
.file(
"test.ts",
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);
}
2018-06-28 08:42:34 +03:00
"#,
)
.test();
}