2018-04-10 01:32:06 +03:00
|
|
|
use super::project;
|
2018-03-29 11:47:44 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works() {
|
2018-04-10 01:32:06 +03:00
|
|
|
project()
|
2018-07-05 06:37:09 +03:00
|
|
|
.debug(false)
|
|
|
|
.nodejs_experimental_modules(false)
|
2018-06-28 08:42:34 +03:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2018-07-05 06:37:09 +03:00
|
|
|
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
extern crate wasm_bindgen;
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
use wasm_bindgen::prelude::*;
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen(module = "./test")]
|
|
|
|
extern {
|
|
|
|
static FOO: JsValue;
|
|
|
|
fn hit();
|
|
|
|
}
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run() {
|
|
|
|
hit();
|
|
|
|
assert_eq!(FOO.as_f64(), Some(1.0));
|
|
|
|
}
|
2018-03-29 13:12:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct Foo {
|
|
|
|
contents: u32,
|
|
|
|
}
|
2018-03-30 00:49:36 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Foo {
|
|
|
|
pub fn new() -> Foo {
|
|
|
|
Foo::with_contents(0)
|
|
|
|
}
|
|
|
|
pub fn with_contents(a: u32) -> Foo {
|
|
|
|
Foo { contents: a }
|
|
|
|
}
|
|
|
|
pub fn add(&mut self, amt: u32) -> u32 {
|
|
|
|
self.contents += amt;
|
|
|
|
self.contents
|
|
|
|
}
|
2018-03-29 13:12:54 +03:00
|
|
|
}
|
2018-07-05 06:37:09 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub enum Color {
|
|
|
|
Green,
|
|
|
|
Yellow,
|
|
|
|
Red,
|
2018-03-29 13:12:54 +03:00
|
|
|
}
|
2018-07-05 06:37:09 +03:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn cycle(color: Color) -> Color {
|
|
|
|
match color {
|
|
|
|
Color::Green => Color::Yellow,
|
|
|
|
Color::Yellow => Color::Red,
|
|
|
|
Color::Red => Color::Green,
|
|
|
|
}
|
2018-03-29 13:12:54 +03:00
|
|
|
}
|
2018-07-05 06:37:09 +03:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn math(a: f32, b: f64) -> f64 {
|
|
|
|
b.acos() +
|
|
|
|
b.asin() +
|
|
|
|
b.atan() +
|
|
|
|
b.atan2(b) +
|
|
|
|
b.cbrt() +
|
|
|
|
b.cosh() +
|
|
|
|
b.exp_m1() +
|
|
|
|
b.ln_1p() +
|
|
|
|
b.sinh() +
|
|
|
|
b.tan() +
|
|
|
|
b.tanh() +
|
|
|
|
b.hypot(b) +
|
|
|
|
b.cos() +
|
|
|
|
b.exp() +
|
|
|
|
b.exp2() +
|
|
|
|
b.mul_add(b, b) +
|
|
|
|
b.ln() +
|
|
|
|
b.log(b) +
|
|
|
|
b.log10() +
|
|
|
|
b.log2() +
|
|
|
|
b.powi(8) +
|
|
|
|
b.powf(b) +
|
|
|
|
b.round() +
|
|
|
|
b.sin() +
|
|
|
|
b.abs() +
|
|
|
|
b.signum() +
|
|
|
|
b.floor() +
|
|
|
|
b.ceil() +
|
|
|
|
b.trunc() +
|
|
|
|
b.sqrt() +
|
|
|
|
(b % (a as f64)) +
|
|
|
|
((a.cos() +
|
|
|
|
a.exp() +
|
|
|
|
a.exp2() +
|
|
|
|
a.mul_add(a, a) +
|
|
|
|
a.ln() +
|
|
|
|
a.log(a) +
|
|
|
|
a.log10() +
|
|
|
|
a.log2() +
|
|
|
|
a.powi(8) +
|
|
|
|
a.powf(a) +
|
|
|
|
a.round() +
|
|
|
|
a.sin() +
|
|
|
|
a.abs() +
|
|
|
|
a.signum() +
|
|
|
|
a.floor() +
|
|
|
|
a.ceil() +
|
|
|
|
a.trunc() +
|
|
|
|
a.sqrt() +
|
|
|
|
(a % (b as f32))) as f64) +
|
|
|
|
(b + 2.0f64.powf(a as f64))
|
2018-03-29 18:11:28 +03:00
|
|
|
}
|
2018-07-05 06:37:09 +03:00
|
|
|
"#,
|
2018-06-28 08:42:34 +03:00
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"test.js",
|
|
|
|
r#"
|
2018-07-05 06:37:09 +03:00
|
|
|
const assert = require('assert');
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
var called = false;
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
module.exports.hit = function() {
|
|
|
|
called = true;
|
|
|
|
};
|
2018-03-29 11:47:44 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
module.exports.FOO = 1.0;
|
2018-03-30 00:49:36 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
const { math, run, Foo, Color, cycle } = require('./out');
|
2018-03-29 13:12:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
module.exports.test = function() {
|
|
|
|
run();
|
|
|
|
assert.strictEqual(called, true);
|
2018-03-29 13:12:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
var r = Foo.new();
|
|
|
|
assert.strictEqual(r.add(0), 0);
|
|
|
|
assert.strictEqual(r.add(1), 1);
|
|
|
|
assert.strictEqual(r.add(2), 3);
|
|
|
|
r.free();
|
2018-03-29 18:11:28 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
var r2 = Foo.with_contents(10);
|
|
|
|
assert.strictEqual(r2.add(0), 10);
|
|
|
|
assert.strictEqual(r2.add(1), 11);
|
|
|
|
assert.strictEqual(r2.add(2), 13);
|
|
|
|
r2.free();
|
2018-03-29 23:29:39 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
assert.strictEqual(Color.Green, 0);
|
|
|
|
assert.strictEqual(Color.Yellow, 1);
|
|
|
|
assert.strictEqual(Color.Red, 2);
|
|
|
|
assert.strictEqual(Object.keys(Color).length, 3);
|
|
|
|
assert.strictEqual(cycle(Color.Green), Color.Yellow);
|
2018-03-29 13:12:54 +03:00
|
|
|
|
2018-07-05 06:37:09 +03:00
|
|
|
math(1.0, 2.0);
|
|
|
|
};
|
|
|
|
"#,
|
2018-06-28 08:42:34 +03:00
|
|
|
)
|
2018-03-29 11:47:44 +03:00
|
|
|
.test();
|
|
|
|
}
|