2017-12-15 06:31:01 +03:00
|
|
|
extern crate wasm_bindgen_cli_support as cli;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2017-12-15 06:36:41 +03:00
|
|
|
use std::io::{Write, Read};
|
2017-12-15 06:31:01 +03:00
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
use std::process::Command;
|
|
|
|
use std::sync::atomic::*;
|
2017-12-15 06:36:41 +03:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
|
|
|
use std::time::Instant;
|
2017-12-15 06:31:01 +03:00
|
|
|
|
2017-12-19 01:31:01 +03:00
|
|
|
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
thread_local!(static IDX: usize = CNT.fetch_add(1, Ordering::SeqCst));
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
pub struct Project {
|
|
|
|
files: Vec<(String, String)>,
|
2017-12-21 00:24:18 +03:00
|
|
|
debug: bool,
|
2017-12-15 06:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn project() -> Project {
|
|
|
|
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let dir = dir.parent().unwrap() // chop off `test-support`
|
|
|
|
.parent().unwrap(); // chop off `crates`
|
2017-12-15 06:36:41 +03:00
|
|
|
|
|
|
|
let mut lockfile = String::new();
|
|
|
|
fs::File::open(&dir.join("Cargo.lock")).unwrap()
|
|
|
|
.read_to_string(&mut lockfile).unwrap();
|
2017-12-15 06:31:01 +03:00
|
|
|
Project {
|
2017-12-21 00:24:18 +03:00
|
|
|
debug: true,
|
2017-12-15 06:31:01 +03:00
|
|
|
files: vec![
|
|
|
|
("Cargo.toml".to_string(), format!(r#"
|
|
|
|
[package]
|
2017-12-19 01:31:01 +03:00
|
|
|
name = "test{}"
|
2017-12-15 06:31:01 +03:00
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
2017-12-19 01:31:01 +03:00
|
|
|
[workspace]
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
[lib]
|
|
|
|
crate-type = ["cdylib"]
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
wasm-bindgen = {{ path = '{}' }}
|
|
|
|
|
|
|
|
[profile.dev]
|
|
|
|
opt-level = 2 # TODO: decrease when upstream is not buggy
|
2017-12-19 01:31:01 +03:00
|
|
|
"#, IDX.with(|x| *x), dir.display())),
|
2017-12-15 06:31:01 +03:00
|
|
|
|
2017-12-15 06:36:41 +03:00
|
|
|
("Cargo.lock".to_string(), lockfile),
|
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
("run.ts".to_string(), r#"
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as process from "process";
|
|
|
|
|
|
|
|
import { instantiate } from "./out";
|
|
|
|
import * as test from "./test";
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
var wasm = fs.readFileSync("out.wasm");
|
2017-12-20 06:06:48 +03:00
|
|
|
|
|
|
|
instantiate(wasm, test.imports).then(m => {
|
|
|
|
test.test(m);
|
2017-12-21 00:24:18 +03:00
|
|
|
if ((m as any).assertHeapAndStackEmpty)
|
|
|
|
(m as any).assertHeapAndStackEmpty();
|
2017-12-20 06:06:48 +03:00
|
|
|
}).catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
2017-12-15 06:31:01 +03:00
|
|
|
});
|
|
|
|
"#.to_string()),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root() -> PathBuf {
|
|
|
|
let idx = IDX.with(|x| *x);
|
|
|
|
|
|
|
|
let mut me = env::current_exe().unwrap();
|
|
|
|
me.pop(); // chop off exe name
|
|
|
|
me.pop(); // chop off `deps`
|
|
|
|
me.pop(); // chop off `debug` / `release`
|
|
|
|
me.push("generated-tests");
|
|
|
|
me.push(&format!("test{}", idx));
|
|
|
|
return me
|
|
|
|
}
|
|
|
|
|
2017-12-20 06:06:48 +03:00
|
|
|
fn typescript() -> PathBuf {
|
2017-12-15 06:31:01 +03:00
|
|
|
static INIT: Once = ONCE_INIT;
|
|
|
|
|
|
|
|
let mut me = env::current_exe().unwrap();
|
|
|
|
me.pop(); // chop off exe name
|
|
|
|
me.pop(); // chop off `deps`
|
|
|
|
me.pop(); // chop off `debug` / `release`
|
|
|
|
let install_dir = me.clone();
|
2017-12-20 06:06:48 +03:00
|
|
|
me.push("node_modules/typescript/bin/tsc");
|
2017-12-15 06:31:01 +03:00
|
|
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
if !me.exists() {
|
2017-12-19 03:35:04 +03:00
|
|
|
let mut npm = if cfg!(windows) {
|
|
|
|
let mut n = Command::new("cmd");
|
|
|
|
n.arg("/c").arg("npm");
|
|
|
|
n
|
|
|
|
} else {
|
|
|
|
Command::new("npm")
|
|
|
|
};
|
|
|
|
run(npm
|
2017-12-15 06:31:01 +03:00
|
|
|
.arg("install")
|
2017-12-20 06:06:48 +03:00
|
|
|
.arg("typescript")
|
|
|
|
.arg("@types/node")
|
|
|
|
.arg("@types/webassembly-js-api")
|
2017-12-15 06:31:01 +03:00
|
|
|
.current_dir(&install_dir), "npm");
|
|
|
|
assert!(me.exists());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return me
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Project {
|
|
|
|
pub fn file(&mut self, name: &str, contents: &str) -> &mut Project {
|
|
|
|
self.files.push((name.to_string(), contents.to_string()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-21 00:24:18 +03:00
|
|
|
pub fn debug(&mut self, debug: bool) -> &mut Project {
|
|
|
|
self.debug = debug;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
pub fn test(&mut self) {
|
|
|
|
let root = root();
|
|
|
|
drop(fs::remove_dir_all(&root));
|
|
|
|
for &(ref file, ref contents) in self.files.iter() {
|
|
|
|
let dst = root.join(file);
|
|
|
|
fs::create_dir_all(dst.parent().unwrap()).unwrap();
|
|
|
|
fs::File::create(&dst).unwrap().write_all(contents.as_ref()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let target_dir = root.parent().unwrap() // chop off test name
|
|
|
|
.parent().unwrap(); // chop off `generated-tests`
|
|
|
|
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.arg("build")
|
|
|
|
.arg("--target")
|
|
|
|
.arg("wasm32-unknown-unknown")
|
|
|
|
.current_dir(&root)
|
|
|
|
.env("CARGO_TARGET_DIR", &target_dir);
|
|
|
|
run(&mut cmd, "cargo");
|
|
|
|
|
2017-12-19 01:31:01 +03:00
|
|
|
let idx = IDX.with(|x| *x);
|
|
|
|
let mut out = target_dir.join(&format!("wasm32-unknown-unknown/debug/test{}.wasm", idx));
|
2017-12-15 06:31:01 +03:00
|
|
|
if Command::new("wasm-gc").output().is_ok() {
|
|
|
|
let tmp = out;
|
|
|
|
out = tmp.with_extension("gc.wasm");
|
|
|
|
let mut cmd = Command::new("wasm-gc");
|
|
|
|
cmd.arg(&tmp).arg(&out);
|
|
|
|
run(&mut cmd, "wasm-gc");
|
|
|
|
}
|
|
|
|
|
|
|
|
let obj = cli::Bindgen::new()
|
|
|
|
.input_path(&out)
|
2017-12-15 08:55:21 +03:00
|
|
|
.nodejs(true)
|
2017-12-21 00:24:18 +03:00
|
|
|
.debug(self.debug)
|
2017-12-15 06:31:01 +03:00
|
|
|
.generate()
|
|
|
|
.expect("failed to run bindgen");
|
2017-12-20 06:06:48 +03:00
|
|
|
obj.write_ts_to(root.join("out.ts")).expect("failed to write ts");
|
2017-12-15 06:31:01 +03:00
|
|
|
obj.write_wasm_to(root.join("out.wasm")).expect("failed to write wasm");
|
|
|
|
|
|
|
|
let mut cmd = Command::new("node");
|
2017-12-20 06:06:48 +03:00
|
|
|
cmd.arg(typescript())
|
|
|
|
.current_dir(&target_dir)
|
|
|
|
.arg(root.join("run.ts"))
|
|
|
|
.arg("--strict")
|
|
|
|
.arg("--noImplicitAny")
|
|
|
|
.arg("--strictNullChecks")
|
|
|
|
.arg("--strictFunctionTypes")
|
|
|
|
.arg("--noUnusedLocals")
|
2017-12-20 19:17:20 +03:00
|
|
|
.arg("--noUnusedParameters")
|
2017-12-20 06:06:48 +03:00
|
|
|
.arg("--noImplicitReturns")
|
2017-12-20 19:25:45 +03:00
|
|
|
.arg("--declaration")
|
2017-12-20 06:06:48 +03:00
|
|
|
.arg("--lib")
|
|
|
|
.arg("es6");
|
2017-12-15 06:31:01 +03:00
|
|
|
run(&mut cmd, "node");
|
|
|
|
|
|
|
|
let mut cmd = Command::new("node");
|
|
|
|
cmd.arg("run.js")
|
|
|
|
.current_dir(&root);
|
|
|
|
run(&mut cmd, "node");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(cmd: &mut Command, program: &str) {
|
2017-12-15 06:36:41 +03:00
|
|
|
println!("···················································");
|
2017-12-15 06:31:01 +03:00
|
|
|
println!("running {:?}", cmd);
|
2017-12-15 06:36:41 +03:00
|
|
|
let start = Instant::now();
|
2017-12-15 06:31:01 +03:00
|
|
|
let output = match cmd.output() {
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(err) => panic!("failed to spawn `{}`: {}", program, err),
|
|
|
|
};
|
|
|
|
println!("exit: {}", output.status);
|
2017-12-15 06:36:41 +03:00
|
|
|
let dur = start.elapsed();
|
|
|
|
println!("dur: {}.{:03}ms", dur.as_secs(), dur.subsec_nanos() / 1_000_000);
|
2017-12-15 06:31:01 +03:00
|
|
|
if output.stdout.len() > 0 {
|
|
|
|
println!("stdout ---\n{}", String::from_utf8_lossy(&output.stdout));
|
|
|
|
}
|
|
|
|
if output.stderr.len() > 0 {
|
|
|
|
println!("stderr ---\n{}", String::from_utf8_lossy(&output.stderr));
|
|
|
|
}
|
|
|
|
assert!(output.status.success());
|
|
|
|
}
|