2018-04-10 01:32:06 +03:00
|
|
|
use super::project;
|
2017-12-19 08:43:16 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2017-12-19 08:43:16 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[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]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bar(s: &str) {
|
2018-02-08 03:41:33 +03:00
|
|
|
foo(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn another_thunk(a: u32) -> i32 {
|
2018-02-08 03:41:33 +03:00
|
|
|
another(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bool_thunk(a: bool) -> bool {
|
2018-02-08 03:41:33 +03:00
|
|
|
take_and_return_bool(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn get_the_object() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
return_object()
|
2017-12-19 08:43:16 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-01-30 08:20:38 +03:00
|
|
|
import * as wasm from "./out";
|
2017-12-19 08:43:16 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
let ARG: string | null = null;
|
|
|
|
let ANOTHER_ARG: number | null = null;
|
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() {
|
2017-12-19 08:43:16 +03:00
|
|
|
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);
|
2017-12-20 21:22:18 +03:00
|
|
|
|
|
|
|
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);
|
2017-12-19 08:43:16 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2017-12-19 08:43:16 +03:00
|
|
|
.test();
|
|
|
|
}
|
2017-12-20 22:34:53 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unused() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-04-19 23:16:59 +03:00
|
|
|
#![allow(dead_code)]
|
2017-12-20 22:34:53 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
fn debug_print(s: &str);
|
2017-12-20 22:34:53 +03:00
|
|
|
}
|
2018-02-08 03:41:33 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bar() {}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-01-30 08:20:38 +03:00
|
|
|
import * as wasm from "./out";
|
2017-12-20 22:34:53 +03:00
|
|
|
|
2018-01-30 08:20:38 +03:00
|
|
|
export function debug_print() {}
|
2017-12-20 22:34:53 +03:00
|
|
|
|
2018-01-30 08:20:38 +03:00
|
|
|
export function test() {
|
2017-12-20 22:34:53 +03:00
|
|
|
wasm.bar();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2017-12-20 22:34:53 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-02-06 19:39:49 +03:00
|
|
|
|
2018-05-03 07:03:50 +03:00
|
|
|
#[test]
|
|
|
|
fn string_ret() {
|
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-05-03 07:03:50 +03:00
|
|
|
#![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");
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-05-03 07:03:50 +03:00
|
|
|
import * as wasm from "./out";
|
|
|
|
|
|
|
|
export function foo(): string {
|
|
|
|
return 'bar';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
wasm.run();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-05-03 07:03:50 +03:00
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:39:49 +03:00
|
|
|
#[test]
|
|
|
|
fn strings() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![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::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
fn foo(a: String) -> String;
|
|
|
|
}
|
2018-02-06 19:39:49 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bar(a: &str) -> String {
|
2018-02-08 03:41:33 +03:00
|
|
|
foo(a.to_string())
|
|
|
|
}
|
2018-02-06 19:39:49 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn bar2(a: String) -> String {
|
2018-02-08 03:41:33 +03:00
|
|
|
foo(a)
|
2018-02-06 19:39:49 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-02-06 19:39:49 +03:00
|
|
|
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');
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-02-06 19:39:49 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-02-07 06:04:12 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exceptions() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-02-07 06:04:12 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
fn foo();
|
|
|
|
fn bar();
|
|
|
|
#[wasm_bindgen(catch)]
|
|
|
|
fn baz() -> Result<(), JsValue>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() {
|
2018-02-08 03:41:33 +03:00
|
|
|
foo();
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run2() {
|
2018-02-08 03:41:33 +03:00
|
|
|
assert!(baz().is_err());
|
|
|
|
bar();
|
2018-02-07 06:04:12 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-02-07 06:04:12 +03:00
|
|
|
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);
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-02-07 06:04:12 +03:00
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exn_caught() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-02-07 06:04:12 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
#[wasm_bindgen(catch)]
|
|
|
|
fn foo() -> Result<(), JsValue>;
|
|
|
|
}
|
2018-02-07 06:04:12 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() -> JsValue {
|
2018-02-08 03:41:33 +03:00
|
|
|
foo().unwrap_err()
|
2018-02-07 06:04:12 +03:00
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-02-07 06:04:12 +03:00
|
|
|
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!');
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-02-07 06:04:12 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-02-08 03:41:33 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn free_imports() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-02-08 03:41:33 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
extern {
|
|
|
|
fn parseInt(a: &str) -> u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() {
|
2018-02-08 03:41:33 +03:00
|
|
|
assert_eq!(parseInt("3"), 3);
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-02-08 03:41:33 +03:00
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-02-08 03:41:33 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-03-21 18:09:59 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_a_field() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-04-26 08:14:15 +03:00
|
|
|
.debug(false)
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-03-21 18:09:59 +03:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-03-21 18:09:59 +03:00
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export const IMPORT = 1.0;
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-03-21 18:09:59 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-03-23 05:05:14 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rename() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-03-23 05:05:14 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-03-23 05:05:14 +03:00
|
|
|
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);
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-03-23 05:05:14 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-04-25 17:26:33 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn versions() {
|
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-04-25 17:26:33 +03:00
|
|
|
#![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();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.js",
|
|
|
|
r#"
|
2018-04-25 17:26:33 +03:00
|
|
|
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']
|
|
|
|
]
|
|
|
|
});
|
|
|
|
};
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-04-25 17:26:33 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-05-09 18:01:57 +03:00
|
|
|
|
2018-06-29 04:06:35 +03:00
|
|
|
#[test]
|
|
|
|
fn underscore_pattern() {
|
|
|
|
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 {
|
|
|
|
fn foo(_: u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
foo(1);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export function foo(_a: number) {
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:01:57 +03:00
|
|
|
#[test]
|
|
|
|
fn rust_keyword() {
|
|
|
|
project()
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-05-09 18:01:57 +03:00
|
|
|
#![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);
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-05-09 18:01:57 +03:00
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export function self() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-05-09 18:01:57 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-05-11 18:28:09 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rust_keyword2() {
|
|
|
|
project()
|
|
|
|
.debug(false)
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-05-11 18:28:09 +03:00
|
|
|
#![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));
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.ts",
|
|
|
|
r#"
|
2018-05-11 18:28:09 +03:00
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export const bar = {
|
|
|
|
foo: 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
2018-06-28 08:42:34 +03:00
|
|
|
"#,
|
|
|
|
)
|
2018-05-11 18:28:09 +03:00
|
|
|
.test();
|
|
|
|
}
|
2018-06-29 04:08:02 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_type() {
|
|
|
|
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 {
|
|
|
|
fn foo(f: Foo) -> Foo;
|
|
|
|
fn bad2() -> Foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Foo(());
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Foo {
|
|
|
|
pub fn touch(&self) {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
foo(Foo(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn bad() {
|
|
|
|
bad2();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import * as assert from "assert";
|
|
|
|
import { run, Foo, bad } from "./out";
|
|
|
|
|
|
|
|
let VAL: any = null;
|
|
|
|
|
|
|
|
export function foo(f: Foo): Foo {
|
|
|
|
VAL = f;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bad2(): number {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
assert.throws(() => VAL.touch(), /Attempt to use a moved value/);
|
|
|
|
assert.throws(bad, /expected value of type Foo/);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2018-06-30 01:52:31 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unused_imports_not_generated() {
|
2018-06-30 01:52:31 +03:00
|
|
|
let mut project = project();
|
|
|
|
|
|
|
|
project
|
2018-06-30 01:52:31 +03:00
|
|
|
.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]
|
|
|
|
extern {
|
|
|
|
pub fn foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
|
2018-06-30 01:52:31 +03:00
|
|
|
let contents = project.read_js();
|
2018-06-30 01:52:31 +03:00
|
|
|
assert!(contents.contains("run"), "didn't find `run` in {}", contents);
|
|
|
|
assert!(!contents.contains("foo"), "found `foo` in {}", contents);
|
|
|
|
}
|