2018-02-06 01:24:25 +03:00
|
|
|
extern crate test_support;
|
|
|
|
|
2018-02-06 19:23:51 +03:00
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
test_support::project()
|
|
|
|
.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:23:51 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn get_random() -> f64 {
|
2018-03-23 02:59:48 +03:00
|
|
|
random()
|
2018-02-08 03:41:33 +03:00
|
|
|
}
|
2018-02-06 19:23:51 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn do_log(a: f64) -> f64 {
|
2018-03-23 02:59:48 +03:00
|
|
|
log(a)
|
2018-02-08 03:41:33 +03:00
|
|
|
}
|
2018-02-06 19:23:51 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
extern {
|
2018-03-23 02:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Math)]
|
2018-02-08 03:41:33 +03:00
|
|
|
fn random() -> f64;
|
2018-03-23 02:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Math)]
|
2018-02-08 03:41:33 +03:00
|
|
|
fn log(a: f64) -> f64;
|
2018-02-06 19:23:51 +03:00
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import * as wasm from "./out";
|
|
|
|
import * as assert from "assert";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
wasm.get_random();
|
|
|
|
assert.strictEqual(wasm.do_log(1.0), Math.log(1.0));
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_class() {
|
|
|
|
test_support::project()
|
|
|
|
.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:23:51 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[wasm_bindgen(module = "./another")]
|
2018-02-08 03:41:33 +03:00
|
|
|
extern {
|
2018-03-23 02:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Foo)]
|
2018-02-08 03:41:33 +03:00
|
|
|
fn bar();
|
|
|
|
}
|
2018-02-06 19:23:51 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-23 02:59:48 +03:00
|
|
|
pub fn baz() {
|
|
|
|
bar();
|
2018-02-06 19:23:51 +03:00
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
2018-03-23 02:59:48 +03:00
|
|
|
import { baz } from "./out";
|
2018-03-03 06:20:14 +03:00
|
|
|
import { called } from "./another";
|
2018-02-06 19:23:51 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
export function test() {
|
2018-03-23 02:59:48 +03:00
|
|
|
baz();
|
2018-03-03 06:20:14 +03:00
|
|
|
assert.strictEqual(called, true);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
|
|
|
export let called = false;
|
2018-02-06 19:23:51 +03:00
|
|
|
|
|
|
|
export class Foo {
|
|
|
|
static bar() {
|
|
|
|
called = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn construct() {
|
|
|
|
test_support::project()
|
|
|
|
.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:23:51 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[wasm_bindgen(module = "./another")]
|
2018-02-08 03:41:33 +03:00
|
|
|
extern {
|
|
|
|
type Foo;
|
2018-03-23 02:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Foo)]
|
2018-02-08 03:41:33 +03:00
|
|
|
fn create() -> Foo;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn get_internal_string(this: &Foo) -> String;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn append_to_internal_string(this: &Foo, s: &str);
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn assert_internal_string(this: &Foo, s: &str);
|
|
|
|
}
|
2018-02-06 19:23:51 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() {
|
2018-02-08 03:41:33 +03:00
|
|
|
let f = Foo::create();
|
|
|
|
assert_eq!(f.get_internal_string(), "this");
|
|
|
|
f.append_to_internal_string(" foo");
|
|
|
|
f.assert_internal_string("this foo");
|
2018-02-06 19:23:51 +03:00
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
2018-03-03 06:20:14 +03:00
|
|
|
import { run } from "./out";
|
|
|
|
import { called } from "./another";
|
|
|
|
import * as assert from "assert";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
assert.strictEqual(called, true);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
2018-02-06 19:23:51 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
export let called = false;
|
2018-02-06 19:23:51 +03:00
|
|
|
|
|
|
|
export class Foo {
|
2018-02-06 19:58:15 +03:00
|
|
|
private internal_string: string = '';
|
2018-02-06 19:23:51 +03:00
|
|
|
|
|
|
|
static create() {
|
|
|
|
const ret = new Foo();
|
2018-02-06 19:58:15 +03:00
|
|
|
ret.internal_string = 'this';
|
2018-02-06 19:23:51 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-06 19:58:15 +03:00
|
|
|
get_internal_string() {
|
|
|
|
return this.internal_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
append_to_internal_string(s: string) {
|
|
|
|
this.internal_string += s;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_internal_string(s: string) {
|
|
|
|
assert.strictEqual(this.internal_string, s);
|
2018-02-06 19:23:51 +03:00
|
|
|
called = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2018-02-07 02:19:47 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_constructors() {
|
|
|
|
test_support::project()
|
|
|
|
.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 02:19:47 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[wasm_bindgen(module = "./another")]
|
2018-02-08 03:41:33 +03:00
|
|
|
extern {
|
|
|
|
type Foo;
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new(arg: i32) -> Foo;
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn get(this: &Foo) -> i32;
|
|
|
|
}
|
2018-02-07 02:19:47 +03:00
|
|
|
|
2018-02-08 03:41:33 +03:00
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() {
|
2018-02-08 03:41:33 +03:00
|
|
|
let f = Foo::new(1);
|
|
|
|
assert_eq!(f.get(), 2);
|
2018-02-07 02:19:47 +03:00
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { run } from "./out";
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
2018-02-07 02:19:47 +03:00
|
|
|
export class Foo {
|
|
|
|
constructor(private field: number) {
|
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.field + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2018-02-14 23:51:58 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn switch_methods() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-02-14 23:51:58 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[wasm_bindgen(module = "./another")]
|
2018-02-14 23:51:58 +03:00
|
|
|
extern {
|
|
|
|
type Foo;
|
|
|
|
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> Foo;
|
|
|
|
|
2018-03-23 02:59:48 +03:00
|
|
|
#[wasm_bindgen(js_namespace = Foo)]
|
2018-02-14 23:51:58 +03:00
|
|
|
fn a();
|
|
|
|
|
|
|
|
#[wasm_bindgen(method)]
|
|
|
|
fn b(this: &Foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn a() {
|
2018-02-14 23:51:58 +03:00
|
|
|
Foo::a();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn b() {
|
2018-02-14 23:51:58 +03:00
|
|
|
Foo::new().b();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { a, b } from "./out";
|
2018-03-03 06:20:14 +03:00
|
|
|
import { Foo, called } from "./another";
|
2018-02-14 23:51:58 +03:00
|
|
|
import * as assert from "assert";
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
export function test() {
|
|
|
|
assert.strictEqual(called.a, false);
|
|
|
|
a();
|
|
|
|
assert.strictEqual(called.a, true);
|
|
|
|
called.a = false;
|
|
|
|
Foo.a = function() {};
|
|
|
|
assert.strictEqual(called.a, false);
|
|
|
|
a();
|
|
|
|
assert.strictEqual(called.a, true);
|
|
|
|
|
|
|
|
called.a = false;
|
|
|
|
assert.strictEqual(called.a, false);
|
|
|
|
b();
|
|
|
|
assert.strictEqual(called.a, true);
|
|
|
|
called.a = false;
|
|
|
|
Foo.prototype.b = function() {};
|
|
|
|
assert.strictEqual(called.a, false);
|
|
|
|
b();
|
|
|
|
assert.strictEqual(called.a, true);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
|
|
|
export let called = { a: false };
|
2018-02-14 23:51:58 +03:00
|
|
|
|
|
|
|
export class Foo {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
static a() {
|
2018-03-03 06:20:14 +03:00
|
|
|
called.a = true;
|
2018-02-14 23:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b() {
|
2018-03-03 06:20:14 +03:00
|
|
|
called.a = true;
|
2018-02-14 23:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2018-02-15 00:16:02 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn properties() {
|
|
|
|
test_support::project()
|
|
|
|
.file("src/lib.rs", r#"
|
2018-03-15 00:33:53 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-02-15 00:16:02 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[wasm_bindgen(module = "./another")]
|
2018-02-15 00:16:02 +03:00
|
|
|
extern {
|
|
|
|
type Foo;
|
|
|
|
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> Foo;
|
|
|
|
|
|
|
|
#[wasm_bindgen(getter, method)]
|
|
|
|
fn a(this: &Foo) -> i32;
|
|
|
|
|
|
|
|
#[wasm_bindgen(setter, method)]
|
|
|
|
fn set_a(this: &Foo, a: i32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2018-03-06 01:25:15 +03:00
|
|
|
pub fn run() {
|
2018-02-15 00:16:02 +03:00
|
|
|
let a = Foo::new();
|
|
|
|
assert_eq!(a.a(), 1);
|
|
|
|
a.set_a(2);
|
|
|
|
assert_eq!(a.a(), 2);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { run } from "./out";
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
2018-02-15 00:16:02 +03:00
|
|
|
export class Foo {
|
|
|
|
constructor(private num: number) {
|
|
|
|
this.num = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get a() {
|
|
|
|
return this.num;
|
|
|
|
}
|
|
|
|
|
|
|
|
set a(val) {
|
|
|
|
this.num = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|
2018-03-23 04:21:41 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rename_setter_getter() {
|
|
|
|
test_support::project()
|
|
|
|
.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 04:21:41 +03:00
|
|
|
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen(module = "./another")]
|
|
|
|
extern {
|
|
|
|
type Foo;
|
|
|
|
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
fn new() -> Foo;
|
|
|
|
|
2018-03-23 05:05:14 +03:00
|
|
|
#[wasm_bindgen(getter = a, method)]
|
2018-03-23 04:21:41 +03:00
|
|
|
fn test(this: &Foo) -> i32;
|
|
|
|
|
2018-03-23 05:05:14 +03:00
|
|
|
#[wasm_bindgen(setter = a, method)]
|
2018-03-23 04:21:41 +03:00
|
|
|
fn another(this: &Foo, a: i32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
let a = Foo::new();
|
|
|
|
assert_eq!(a.test(), 1);
|
|
|
|
a.another(2);
|
|
|
|
assert_eq!(a.test(), 2);
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("test.ts", r#"
|
|
|
|
import { run } from "./out";
|
|
|
|
|
|
|
|
export function test() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.file("another.ts", r#"
|
|
|
|
export class Foo {
|
|
|
|
constructor(private num: number) {
|
|
|
|
this.num = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get a() {
|
|
|
|
return this.num;
|
|
|
|
}
|
|
|
|
|
|
|
|
set a(val) {
|
|
|
|
this.num = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#)
|
|
|
|
.test();
|
|
|
|
}
|