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.
This commit is contained in:
Alex Crichton
2018-01-29 21:20:38 -08:00
parent f27e4a9e94
commit c51a342cb3
22 changed files with 1514 additions and 1946 deletions

View File

@@ -98,11 +98,9 @@ fn works() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
export function test() {
assert.strictEqual(wasm.foo(), 'foo');
assert.strictEqual(wasm.bar('a'), 'a');
assert.strictEqual(wasm.baz(), 1);
@@ -119,10 +117,10 @@ fn works() {
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);
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol()), undefined);
assert.strictEqual((Symbol as any).keyFor(wasm.mk_symbol2('b')), undefined);
wasm.assert_symbols(Symbol(), 'a');
wasm.assert_symbols((Symbol as any)(), 'a');
wasm.acquire_string('foo', null)
assert.strictEqual(wasm.acquire_string2(''), '');
assert.strictEqual(wasm.acquire_string2('a'), 'a');

View File

@@ -33,18 +33,16 @@ fn simple() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import { Foo } from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
const r = wasm.Foo.new();
export function test() {
const r = Foo.new();
assert.strictEqual(r.add(0), 0);
assert.strictEqual(r.add(1), 1);
assert.strictEqual(r.add(1), 2);
r.free();
const r2 = wasm.Foo.with_contents(10);
const r2 = Foo.with_contents(10);
assert.strictEqual(r2.add(1), 11);
assert.strictEqual(r2.add(2), 13);
assert.strictEqual(r2.add(3), 16);
@@ -96,12 +94,10 @@ fn strings() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import { Foo } from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
const r = wasm.Foo.new();
export function test() {
const r = Foo.new();
r.set(3);
let bar = r.bar('baz');
r.free();
@@ -149,32 +145,28 @@ fn exceptions() {
}
"#)
.file("test.js", r#"
var assert = require("assert");
import * as assert from "assert";
import { A, B } from "./out";
exports.imports = {};
exports.test = function(wasm) {
assert.throws(() => new wasm.A(), /cannot invoke `new` directly/);
let a = wasm.A.new();
export function test() {
assert.throws(() => new A(), /cannot invoke `new` directly/);
let a = A.new();
a.free();
assert.throws(() => a.free(), /null pointer passed to rust/);
let b = wasm.A.new();
let b = A.new();
b.foo(b);
assert.throws(() => b.bar(b), /recursive use of an object/);
let c = wasm.A.new();
let d = wasm.B.new();
let c = A.new();
let d = B.new();
assert.throws(() => c.foo(d), /expected instance of A/);
d.free();
c.free();
};
"#)
.file("test.d.ts", r#"
import { Exports, Imports } from "./out";
export const imports: Imports;
export function test(wasm: Exports): void;
export function test(): void;
"#)
.test();
}
@@ -214,13 +206,11 @@ fn pass_one_to_another() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import { A, B } from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
let a = wasm.A.new();
let b = wasm.B.new();
export function test() {
let a = A.new();
let b = B.new();
a.foo(b);
a.bar(b);
a.free();

View File

@@ -11,6 +11,7 @@ fn simple() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn foo(s: &str);
fn another(a: u32) -> i32;
@@ -33,34 +34,32 @@ fn simple() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
import * as assert from "assert";
let ARG: string | null = null;
let ANOTHER_ARG: number | null = null;
let SYM = Symbol('a');
let SYM = (Symbol as any)('a');
export const imports: Imports = {
foo(s) {
assert.strictEqual(ARG, null);
assert.strictEqual(s, "foo");
ARG = s;
},
another(s) {
assert.strictEqual(ANOTHER_ARG, null);
assert.strictEqual(s, 21);
ANOTHER_ARG = s;
return 35;
},
take_and_return_bool(s: boolean): boolean {
return s;
},
return_object(): any {
return SYM;
},
};
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(wasm: Exports) {
export function test() {
assert.strictEqual(ARG, null);
wasm.bar("foo");
assert.strictEqual(ARG, "foo");
@@ -89,6 +88,7 @@ fn unused() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn debug_print(s: &str);
}
@@ -97,11 +97,11 @@ fn unused() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function debug_print() {}
export function test(wasm: Exports) {
export function test() {
wasm.bar();
}
"#)

View File

@@ -1,79 +0,0 @@
extern crate test_support;
const SRC: &str = r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
wasm_bindgen! {
pub struct A {}
impl A {
pub fn new() -> A {
A {}
}
}
pub fn clone(a: &JsObject) -> JsObject {
drop(a.clone());
a.clone()
}
extern "JS" {
fn bar(a: &JsObject, b: JsObject);
}
pub fn foo(
_: &str,
_: bool,
_: i32,
_: &A,
_: A,
a: JsObject,
b: &JsObject,
) -> String {
a.is_symbol();
a.as_f64();
a.as_string();
a.as_bool();
a.is_null();
a.is_undefined();
bar(b, a);
JsObject::from("a");
JsObject::from(3);
String::new()
}
}
"#;
#[test]
fn works() {
test_support::project()
.js(true)
.debug(true)
.file("src/lib.rs", SRC)
.file("test.ts", r#"
export const imports = {};
export function test(_) {
}
"#)
.test();
}
#[test]
fn works_non_debug() {
test_support::project()
.js(true)
.debug(false)
.file("src/lib.rs", SRC)
.file("test.ts", r#"
export const imports = {};
export function test(_) {
}
"#)
.test();
}

View File

@@ -11,6 +11,7 @@ fn simple() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn foo(s: &JsObject);
}
@@ -20,21 +21,19 @@ fn simple() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
import * as assert from "assert";
let ARG: string | null = null;
export const imports: Imports = {
foo(s) {
assert.strictEqual(ARG, null);
ARG = s;
},
};
export function test(wasm: Exports) {
export function foo(s: any): void {
assert.strictEqual(ARG, null);
let sym = Symbol('test');
ARG = s;
}
export function test() {
assert.strictEqual(ARG, null);
let sym = (Symbol as any)('test');
wasm.bar(sym);
assert.strictEqual(ARG, sym);
}
@@ -53,6 +52,7 @@ fn owned() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn foo(s: JsObject);
}
@@ -62,21 +62,19 @@ fn owned() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
import * as assert from "assert";
let ARG: Symbol | null = null;
let ARG: any = null;
export const imports: Imports = {
foo(s) {
assert.strictEqual(ARG, null);
ARG = s;
},
};
export function test(wasm: Exports) {
export function foo(s: any): void {
assert.strictEqual(ARG, null);
let sym = Symbol('test');
ARG = s;
}
export function test() {
assert.strictEqual(ARG, null);
let sym = (Symbol as any)('test');
wasm.bar(sym);
assert.strictEqual(ARG, sym);
}
@@ -95,6 +93,7 @@ fn clone() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn foo1(s: JsObject);
fn foo2(s: &JsObject);
@@ -113,20 +112,18 @@ fn clone() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
import * as assert from "assert";
let ARG = Symbol('test');
let ARG = (Symbol as any)('test');
export const imports: Imports = {
foo1(s) { assert.strictEqual(s, ARG); },
foo2(s) { assert.strictEqual(s, ARG); },
foo3(s) { assert.strictEqual(s, ARG); },
foo4(s) { assert.strictEqual(s, ARG); },
foo5(s) { assert.strictEqual(s, ARG); },
};
export function foo1(s: any): void { assert.strictEqual(s, ARG); }
export function foo2(s: any): void { assert.strictEqual(s, ARG); }
export function foo3(s: any): void { assert.strictEqual(s, ARG); }
export function foo4(s: any): void { assert.strictEqual(s, ARG); }
export function foo5(s: any): void { assert.strictEqual(s, ARG); }
export function test(wasm: Exports) {
export function test() {
wasm.bar(ARG);
}
"#)
@@ -144,6 +141,7 @@ fn promote() {
use wasm_bindgen::prelude::*;
wasm_bindgen! {
#[wasm_module = "./test"]
extern "JS" {
fn foo1(s: &JsObject);
fn foo2(s: JsObject);
@@ -160,19 +158,17 @@ fn promote() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
import * as assert from "assert";
let ARG = Symbol('test');
let ARG = (Symbol as any)('test');
export const imports: Imports = {
foo1(s) { assert.strictEqual(s, ARG); },
foo2(s) { assert.strictEqual(s, ARG); },
foo3(s) { assert.strictEqual(s, ARG); },
foo4(s) { assert.strictEqual(s, ARG); },
};
export function foo1(s: any): void { assert.strictEqual(s, ARG); }
export function foo2(s: any): void { assert.strictEqual(s, ARG); }
export function foo3(s: any): void { assert.strictEqual(s, ARG); }
export function foo4(s: any): void { assert.strictEqual(s, ARG); }
export function test(wasm: Exports) {
export function test() {
wasm.bar(ARG);
}
"#)

View File

@@ -27,12 +27,10 @@ fn works() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
let sym = Symbol('a');
export function test() {
let sym = (Symbol as any)('a');
assert.strictEqual(wasm.clone(sym), sym);
let a = wasm.A.new();
a.free();

View File

@@ -37,11 +37,9 @@ fn add() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
export function test() {
assert.strictEqual(wasm.add(1, 2), 3);
assert.strictEqual(wasm.add(2, 3), 5);
assert.strictEqual(wasm.add3(2), 5);
@@ -74,11 +72,9 @@ fn string_arguments() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
export function test() {
wasm.assert_foo("foo");
wasm.assert_foo_and_bar("foo2", "bar");
}
@@ -110,11 +106,9 @@ fn return_a_string() {
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
import * as wasm from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
export function test() {
assert.strictEqual(wasm.clone("foo"), "foo");
assert.strictEqual(wasm.clone("another"), "another");
assert.strictEqual(wasm.concat("a", "b", 3), "a b 3");
@@ -140,66 +134,53 @@ fn exceptions() {
}
"#)
.file("test.js", r#"
var assert = require("assert");
import * as assert from "assert";
import * as wasm from "./out";
exports.imports = {};
exports.test = function(wasm) {
export function test() {
assert.throws(() => wasm.foo('a'), /expected a number argument/);
assert.throws(() => wasm.bar(3), /expected a string argument/);
};
}
"#)
.file("test.d.ts", r#"
import { Exports, Imports } from "./out";
export const imports: Imports;
export function test(wasm: Exports): void;
export function test(): void;
"#)
.test();
}
#[test]
fn other_imports() {
test_support::project()
.file("src/lib.rs", r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
extern {
fn another_import(a: u32);
}
wasm_bindgen! {
pub fn foo(a: u32) {
unsafe { another_import(a); }
}
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
let ARG: number | null = null;
export const imports: Imports = {
env: {
another_import(a: number) {
assert.strictEqual(ARG, null);
ARG = a;
},
},
};
export function test(wasm: Exports) {
wasm.foo(2);
assert.strictEqual(ARG, 2);
}
"#)
.test();
}
// #[test]
// fn other_imports() {
// test_support::project()
// .file("src/lib.rs", r#"
// #![feature(proc_macro)]
//
// extern crate wasm_bindgen;
//
// use wasm_bindgen::prelude::*;
//
// extern {
// fn another_import(a: u32);
// }
//
// wasm_bindgen! {
// pub fn foo(a: u32) {
// unsafe { another_import(a); }
// }
// }
// "#)
// .file("test.ts", r#"
// import * as assert from "assert";
// import * as wasm from "./out";
//
// let ARG: number | null = null;
//
// export function test() {
// wasm.foo(2);
// assert.strictEqual(ARG, 2);
// }
// "#)
// .test();
// }
#[test]
fn other_exports() {
@@ -210,12 +191,10 @@ fn other_exports() {
}
"#)
.file("test.ts", r#"
import { Exports, Imports } from "./out";
import * as wasm from "./out_wasm";
export const imports: Imports = {};
export function test(wasm: Exports) {
wasm.extra.foo(2);
export function test() {
wasm.foo(2);
}
"#)
.test();

View File

@@ -1,42 +0,0 @@
extern crate test_support;
#[test]
fn works() {
test_support::project()
.uglify(true)
.file("src/lib.rs", r#"
#![feature(proc_macro)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
wasm_bindgen! {
pub struct A {}
impl A {
pub fn new() -> A {
A {}
}
}
pub fn clone(a: &JsObject) -> JsObject {
drop(a.clone());
a.clone()
}
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import { Exports, Imports } from "./out";
export const imports: Imports = {};
export function test(wasm: Exports) {
let sym = Symbol('a');
assert.strictEqual(wasm.clone(sym), sym);
let a = wasm.A.new();
a.free();
}
"#)
.test();
}