wasm-bindgen/tests/all/imports.rs

504 lines
12 KiB
Rust
Raw Normal View History

use super::project;
#[test]
fn simple() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn foo(s: &str);
fn another(a: u32) -> i32;
fn take_and_return_bool(a: bool) -> bool;
fn return_object() -> JsValue;
}
#[wasm_bindgen]
pub fn bar(s: &str) {
foo(s);
}
#[wasm_bindgen]
pub fn another_thunk(a: u32) -> i32 {
another(a)
}
#[wasm_bindgen]
pub fn bool_thunk(a: bool) -> bool {
take_and_return_bool(a)
}
#[wasm_bindgen]
pub fn get_the_object() -> JsValue {
return_object()
}
"#)
.file("test.ts", r#"
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";
import * as assert from "assert";
let ARG: string | null = null;
let ANOTHER_ARG: number | null = null;
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
let SYM = (Symbol as any)('a');
export function foo(s: string): void {
assert.strictEqual(ARG, null);
assert.strictEqual(s, "foo");
ARG = s;
}
export function another(s: number): number {
assert.strictEqual(ANOTHER_ARG, null);
assert.strictEqual(s, 21);
ANOTHER_ARG = s;
return 35;
}
export function take_and_return_bool(s: boolean): boolean {
return s;
}
export function return_object(): any {
return SYM;
}
export function test() {
assert.strictEqual(ARG, null);
wasm.bar("foo");
assert.strictEqual(ARG, "foo");
assert.strictEqual(ANOTHER_ARG, null);
assert.strictEqual(wasm.another_thunk(21), 35);
assert.strictEqual(ANOTHER_ARG, 21);
assert.strictEqual(wasm.bool_thunk(true), true);
assert.strictEqual(wasm.bool_thunk(false), false);
2017-12-20 21:44:08 +03:00
assert.strictEqual(wasm.get_the_object(), SYM);
}
"#)
.test();
}
2017-12-20 22:34:53 +03:00
#[test]
fn unused() {
project()
2017-12-20 22:34:53 +03:00
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
#![allow(dead_code)]
2017-12-20 22:34:53 +03:00
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn debug_print(s: &str);
2017-12-20 22:34:53 +03:00
}
#[wasm_bindgen]
pub fn bar() {}
2017-12-20 22:34:53 +03:00
"#)
.file("test.ts", r#"
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";
2017-12-20 22:34:53 +03:00
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 debug_print() {}
2017-12-20 22:34:53 +03:00
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() {
2017-12-20 22:34:53 +03:00
wasm.bar();
}
"#)
.test();
}
2018-02-06 19:39:49 +03:00
#[test]
fn string_ret() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn foo() -> String;
}
#[wasm_bindgen]
pub fn run() {
assert_eq!(foo(), "bar");
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
export function foo(): string {
return 'bar';
}
export function test() {
wasm.run();
}
"#)
.test();
}
2018-02-06 19:39:49 +03:00
#[test]
fn strings() {
project()
2018-02-06 19:39:49 +03:00
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
2018-02-06 19:39:49 +03:00
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn foo(a: String) -> String;
}
2018-02-06 19:39:49 +03:00
#[wasm_bindgen]
pub fn bar(a: &str) -> String {
foo(a.to_string())
}
2018-02-06 19:39:49 +03:00
#[wasm_bindgen]
pub fn bar2(a: String) -> String {
foo(a)
2018-02-06 19:39:49 +03:00
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
import * as assert from "assert";
export function foo(a: string): string {
return a + 'b';
}
export function test() {
assert.strictEqual(wasm.bar('a'), 'ab');
assert.strictEqual(wasm.bar2('a'), 'ab');
}
"#)
.test();
}
#[test]
fn exceptions() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
fn foo();
fn bar();
#[wasm_bindgen(catch)]
fn baz() -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() {
foo();
bar();
}
#[wasm_bindgen]
pub fn run2() {
assert!(baz().is_err());
bar();
}
"#)
.file("test.ts", r#"
import { run, run2 } from "./out";
import * as assert from "assert";
let called = false;
export function foo() {
throw new Error('error!');
}
export function baz() {
throw new Error('error2');
}
export function bar() {
called = true;
}
export function test() {
assert.throws(run, /error!/);
assert.strictEqual(called, false);
run2();
assert.strictEqual(called, true);
}
"#)
.test();
}
#[test]
fn exn_caught() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
#[wasm_bindgen(catch)]
fn foo() -> Result<(), JsValue>;
}
#[wasm_bindgen]
pub fn run() -> JsValue {
foo().unwrap_err()
}
"#)
.file("test.ts", r#"
import { run } from "./out";
import * as assert from "assert";
export function foo() {
throw new Error('error!');
}
export function test() {
const obj = run();
assert.strictEqual(obj instanceof Error, true);
assert.strictEqual(obj.message, 'error!');
}
"#)
.test();
}
#[test]
fn free_imports() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn parseInt(a: &str) -> u32;
}
#[wasm_bindgen]
pub fn run() {
assert_eq!(parseInt("3"), 3);
}
"#)
.file("test.ts", r#"
import { run } from "./out";
export function test() {
run();
}
"#)
.test();
}
#[test]
fn import_a_field() {
project()
.debug(false)
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
static IMPORT: JsValue;
}
#[wasm_bindgen]
pub fn run() {
assert_eq!(IMPORT.as_f64(), Some(1.0));
}
"#)
.file("test.ts", r#"
import { run } from "./out";
export const IMPORT = 1.0;
export function test() {
run();
}
"#)
.test();
}
#[test]
fn rename() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
#[wasm_bindgen(js_name = baz)]
fn foo();
}
#[wasm_bindgen]
pub fn run() {
foo();
}
"#)
.file("test.ts", r#"
import * as wasm from "./out";
import * as assert from "assert";
let called = false;
export function baz() {
called = true;
}
export function test() {
wasm.run();
assert.strictEqual(called, true);
}
"#)
.test();
}
#[test]
fn versions() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "webpack", version = "^0.2.0")]
extern {
fn foo();
}
#[wasm_bindgen]
pub fn run() {
foo();
}
"#)
.file("test.js", r#"
const fs = require("fs");
const assert = require("assert");
exports.test = function() {
const bytes = fs.readFileSync('out_bg.wasm');
const m = new WebAssembly.Module(bytes);
const name = '__wasm_pack_unstable';
const sections = WebAssembly.Module.customSections(m, name);
assert.strictEqual(sections.length, 1);
const b = new Uint8Array(sections[0]);
const buf = new Buffer(b);
const map = JSON.parse(buf.toString());
assert.deepStrictEqual(map, {
version: '0.0.1',
modules: [
['webpack', '^0.2.0']
]
});
};
"#)
.test();
}
#[test]
fn rust_keyword() {
project()
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
#[wasm_bindgen(js_name = self)]
fn foo() -> u32;
}
#[wasm_bindgen]
pub fn run() {
assert_eq!(foo(), 2);
}
"#)
.file("test.ts", r#"
import { run } from "./out";
export function self() {
return 2;
}
export function test() {
run();
}
"#)
.test();
}
#[test]
fn rust_keyword2() {
project()
.debug(false)
.file("src/lib.rs", r#"
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./test")]
extern {
pub type bar;
#[wasm_bindgen(js_namespace = bar, js_name = foo)]
static FOO: JsValue;
}
#[wasm_bindgen]
pub fn run() {
assert_eq!(FOO.as_f64(), Some(3.0));
}
"#)
.file("test.ts", r#"
import { run } from "./out";
export const bar = {
foo: 3,
};
export function test() {
run();
}
"#)
.test();
}