2017-12-15 06:31:01 +03:00
|
|
|
extern crate wasm_bindgen_cli_support as cli;
|
|
|
|
|
|
|
|
use std::env;
|
2018-01-30 08:20:38 +03:00
|
|
|
use std::fs::{self, File};
|
2018-03-03 06:20:14 +03:00
|
|
|
use std::io::{self, Write, Read};
|
2017-12-15 06:31:01 +03:00
|
|
|
use std::path::{PathBuf, Path};
|
2018-05-06 00:10:25 +03:00
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
use std::sync::{Once, ONCE_INIT};
|
2017-12-15 06:31:01 +03:00
|
|
|
use std::sync::atomic::*;
|
2017-12-15 06:36:41 +03:00
|
|
|
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));
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
struct Project {
|
2017-12-15 06:31:01 +03:00
|
|
|
files: Vec<(String, String)>,
|
2017-12-21 00:24:18 +03:00
|
|
|
debug: bool,
|
2018-03-29 11:47:44 +03:00
|
|
|
node: bool,
|
2018-04-19 23:08:54 +03:00
|
|
|
no_std: bool,
|
2018-04-27 05:03:46 +03:00
|
|
|
serde: bool,
|
2018-04-27 23:43:47 +03:00
|
|
|
rlib: bool,
|
2018-05-06 00:10:25 +03:00
|
|
|
node_args: Vec<String>,
|
2018-04-27 23:43:47 +03:00
|
|
|
deps: Vec<String>,
|
2017-12-15 06:31:01 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
fn project() -> Project {
|
2017-12-15 06:31:01 +03:00
|
|
|
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
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,
|
2018-03-29 11:47:44 +03:00
|
|
|
node: false,
|
2018-04-19 23:08:54 +03:00
|
|
|
no_std: false,
|
2018-04-27 05:03:46 +03:00
|
|
|
serde: false,
|
2018-04-27 23:43:47 +03:00
|
|
|
rlib: false,
|
|
|
|
deps: Vec::new(),
|
2018-05-06 00:10:25 +03:00
|
|
|
node_args: Vec::new(),
|
2017-12-15 06:31:01 +03:00
|
|
|
files: vec![
|
2017-12-15 06:36:41 +03:00
|
|
|
("Cargo.lock".to_string(), lockfile),
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
("run.js".to_string(), r#"
|
2017-12-20 06:06:48 +03:00
|
|
|
import * as process from "process";
|
2018-04-26 08:14:15 +03:00
|
|
|
let wasm = import('./out');
|
2017-12-20 06:06:48 +03:00
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
const test = import("./test");
|
2017-12-20 06:06:48 +03:00
|
|
|
|
2018-04-26 08:14:15 +03:00
|
|
|
Promise.all([test, wasm]).then(results => {
|
|
|
|
let [test, wasm] = results;
|
2018-01-30 08:20:38 +03:00
|
|
|
test.test();
|
2018-04-26 08:14:15 +03:00
|
|
|
|
|
|
|
if (wasm.assertStackEmpty)
|
|
|
|
wasm.assertStackEmpty();
|
|
|
|
if (wasm.assertSlabEmpty)
|
|
|
|
wasm.assertSlabEmpty();
|
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()),
|
2018-01-30 08:20:38 +03:00
|
|
|
|
2018-03-30 00:49:36 +03:00
|
|
|
("run-node.js".to_string(), r#"
|
|
|
|
require("./test").test();
|
|
|
|
"#.to_string()),
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
("webpack.config.js".to_string(), r#"
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
entry: './run.js',
|
|
|
|
mode: "development",
|
|
|
|
devtool: "source-map",
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.ts$/,
|
|
|
|
use: 'ts-loader',
|
|
|
|
exclude: /node_modules/
|
|
|
|
}
|
2018-03-03 01:20:05 +03:00
|
|
|
]
|
2018-03-03 06:20:14 +03:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
extensions: [ '.ts', '.js', '.wasm' ]
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
filename: 'bundle.js',
|
|
|
|
path: path.resolve(__dirname, '.')
|
|
|
|
},
|
|
|
|
target: 'node'
|
|
|
|
};
|
|
|
|
"#.to_string()),
|
|
|
|
("tsconfig.json".to_string(), r#"
|
|
|
|
{
|
|
|
|
"compilerOptions": {
|
|
|
|
"noEmitOnError": true,
|
|
|
|
"noImplicitAny": true,
|
|
|
|
"noImplicitThis": true,
|
|
|
|
"noUnusedParameters": true,
|
|
|
|
"noUnusedLocals": true,
|
|
|
|
"noImplicitReturns": true,
|
|
|
|
"strictFunctionTypes": true,
|
|
|
|
"strictNullChecks": true,
|
|
|
|
"alwaysStrict": true,
|
|
|
|
"strict": true,
|
|
|
|
"target": "es5",
|
|
|
|
"lib": ["es2015"]
|
|
|
|
}
|
2018-01-30 08:20:38 +03:00
|
|
|
}
|
|
|
|
"#.to_string()),
|
2017-12-15 06:31:01 +03:00
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
fn root() -> PathBuf {
|
2017-12-15 06:31:01 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:10:25 +03:00
|
|
|
fn assert_bigint_support() -> Option<&'static str> {
|
|
|
|
static BIGINT_SUPPORED: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
static INIT: Once = ONCE_INIT;
|
|
|
|
|
|
|
|
INIT.call_once(|| {
|
|
|
|
let mut cmd = Command::new("node");
|
|
|
|
cmd.arg("-e").arg("BigInt");
|
|
|
|
cmd.stdin(Stdio::null())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped());
|
|
|
|
if cmd.status().unwrap().success() {
|
|
|
|
BIGINT_SUPPORED.store(1, Ordering::SeqCst);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.arg("--harmony-bigint");
|
|
|
|
if cmd.status().unwrap().success() {
|
|
|
|
BIGINT_SUPPORED.store(2, Ordering::SeqCst);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
match BIGINT_SUPPORED.load(Ordering::SeqCst) {
|
|
|
|
1 => return None,
|
|
|
|
2 => return Some("--harmony-bigint"),
|
|
|
|
_ => {
|
|
|
|
panic!("the version of node.js that is installed for these tests \
|
|
|
|
does not support `BigInt`, you may wish to try installing \
|
|
|
|
node 10 to fix this")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
impl Project {
|
2018-04-10 01:32:06 +03:00
|
|
|
fn file(&mut self, name: &str, contents: &str) -> &mut Project {
|
2017-12-15 06:31:01 +03:00
|
|
|
self.files.push((name.to_string(), contents.to_string()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
fn debug(&mut self, debug: bool) -> &mut Project {
|
2017-12-21 00:24:18 +03:00
|
|
|
self.debug = debug;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
fn node(&mut self, node: bool) -> &mut Project {
|
2018-03-29 11:47:44 +03:00
|
|
|
self.node = node;
|
2018-03-07 19:50:56 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-19 23:08:54 +03:00
|
|
|
fn no_std(&mut self, no_std: bool) -> &mut Project {
|
|
|
|
self.no_std = no_std;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-27 05:03:46 +03:00
|
|
|
fn serde(&mut self, serde: bool) -> &mut Project {
|
|
|
|
self.serde = serde;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:43:47 +03:00
|
|
|
fn rlib(&mut self, rlib: bool) -> &mut Project {
|
|
|
|
self.rlib = rlib;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-27 05:03:46 +03:00
|
|
|
fn depend(&mut self, dep: &str) -> &mut Project {
|
2018-04-27 23:43:47 +03:00
|
|
|
self.deps.push(dep.to_string());
|
2018-04-27 05:03:46 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:32:06 +03:00
|
|
|
fn add_local_dependency(&mut self, name: &str, path: &str) -> &mut Project {
|
2018-04-27 23:43:47 +03:00
|
|
|
self.deps.push(format!("{} = {{ path = '{}' }}", name, path));
|
2018-03-07 20:49:22 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:43:47 +03:00
|
|
|
fn crate_name(&self) -> String {
|
|
|
|
format!("test{}", IDX.with(|x| *x))
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:10:25 +03:00
|
|
|
fn requires_bigint(&mut self) -> &mut Project {
|
|
|
|
if let Some(arg) = assert_bigint_support() {
|
|
|
|
self.node_args.push(arg.to_string());
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:43:47 +03:00
|
|
|
fn build(&mut self) -> (PathBuf, PathBuf) {
|
|
|
|
let mut manifest = format!(r#"
|
|
|
|
[package]
|
|
|
|
name = "test{}"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[workspace]
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
"#, IDX.with(|x| *x));
|
|
|
|
|
|
|
|
if !self.rlib {
|
|
|
|
manifest.push_str("crate-type = [\"cdylib\"]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest.push_str("[dependencies]\n");
|
|
|
|
for dep in self.deps.iter() {
|
|
|
|
manifest.push_str(dep);
|
|
|
|
manifest.push_str("\n");
|
|
|
|
}
|
|
|
|
manifest.push_str("wasm-bindgen = { path = '");
|
|
|
|
manifest.push_str(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
manifest.push_str("'");
|
|
|
|
if self.no_std {
|
|
|
|
manifest.push_str(", default-features = false");
|
2018-04-19 23:08:54 +03:00
|
|
|
}
|
2018-04-27 23:43:47 +03:00
|
|
|
if self.serde {
|
|
|
|
manifest.push_str(", features = ['serde-serialize']");
|
|
|
|
}
|
|
|
|
manifest.push_str(" }\n");
|
|
|
|
self.files.push(("Cargo.toml".to_string(), manifest));
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
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`
|
2018-04-27 23:43:47 +03:00
|
|
|
(root.clone(), target_dir.to_path_buf())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test(&mut self) {
|
|
|
|
let (root, target_dir) = self.build();
|
2017-12-15 06:31:01 +03:00
|
|
|
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.arg("build")
|
|
|
|
.arg("--target")
|
|
|
|
.arg("wasm32-unknown-unknown")
|
|
|
|
.current_dir(&root)
|
2018-04-19 23:16:59 +03:00
|
|
|
.env("CARGO_TARGET_DIR", &target_dir)
|
|
|
|
// Catch any warnings in generated code because we don't want any
|
|
|
|
.env("RUSTFLAGS", "-Dwarnings");
|
2017-12-15 06:31:01 +03:00
|
|
|
run(&mut cmd, "cargo");
|
|
|
|
|
2017-12-19 01:31:01 +03:00
|
|
|
let idx = IDX.with(|x| *x);
|
2018-03-31 12:35:09 +03:00
|
|
|
let out = target_dir.join(&format!("wasm32-unknown-unknown/debug/test{}.wasm", idx));
|
2017-12-15 06:31:01 +03:00
|
|
|
|
2018-01-30 08:20:38 +03:00
|
|
|
let as_a_module = root.join("out.wasm");
|
|
|
|
fs::copy(&out, &as_a_module).unwrap();
|
|
|
|
|
2018-04-27 05:03:46 +03:00
|
|
|
let res = cli::Bindgen::new()
|
2018-01-30 08:20:38 +03:00
|
|
|
.input_path(&as_a_module)
|
|
|
|
.typescript(true)
|
2018-03-29 11:47:44 +03:00
|
|
|
.nodejs(self.node)
|
2017-12-21 00:24:18 +03:00
|
|
|
.debug(self.debug)
|
2018-04-27 05:03:46 +03:00
|
|
|
.generate(&root);
|
|
|
|
if let Err(e) = res {
|
|
|
|
for e in e.causes() {
|
|
|
|
println!("- {}", e);
|
|
|
|
}
|
|
|
|
panic!("failed");
|
|
|
|
}
|
2018-01-30 08:20:38 +03:00
|
|
|
|
|
|
|
let mut wasm = Vec::new();
|
2018-03-06 00:25:14 +03:00
|
|
|
File::open(root.join("out_bg.wasm")).unwrap()
|
2018-01-30 08:20:38 +03:00
|
|
|
.read_to_end(&mut wasm).unwrap();
|
|
|
|
let obj = cli::wasm2es6js::Config::new()
|
|
|
|
.base64(true)
|
|
|
|
.generate(&wasm)
|
|
|
|
.expect("failed to convert wasm to js");
|
2018-03-06 00:25:14 +03:00
|
|
|
File::create(root.join("out_bg.d.ts")).unwrap()
|
2018-01-30 08:20:38 +03:00
|
|
|
.write_all(obj.typescript().as_bytes()).unwrap();
|
2018-03-03 06:20:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
// move files from the root into each test, it looks like this may be
|
|
|
|
// needed for webpack to work well when invoked concurrently.
|
|
|
|
fs::hard_link("package.json", root.join("package.json")).unwrap();
|
2018-06-01 21:42:59 +03:00
|
|
|
if !Path::new("node_modules").exists() {
|
|
|
|
panic!("\n\nfailed to find `node_modules`, have you run `npm install` yet?\n\n");
|
2018-04-03 17:07:14 +03:00
|
|
|
}
|
2018-06-01 21:42:59 +03:00
|
|
|
fs::hard_link("package-lock.json", root.join("package-lock.json")).unwrap();
|
2018-03-03 06:20:14 +03:00
|
|
|
let cwd = env::current_dir().unwrap();
|
|
|
|
symlink_dir(&cwd.join("node_modules"), &root.join("node_modules")).unwrap();
|
2017-12-15 06:31:01 +03:00
|
|
|
|
2018-03-29 11:47:44 +03:00
|
|
|
if self.node {
|
|
|
|
let mut cmd = Command::new("node");
|
2018-05-06 00:10:25 +03:00
|
|
|
cmd.args(&self.node_args);
|
2018-03-30 00:49:36 +03:00
|
|
|
cmd.arg(root.join("run-node.js"))
|
2018-03-29 11:47:44 +03:00
|
|
|
.current_dir(&root);
|
|
|
|
run(&mut cmd, "node");
|
2018-03-03 01:20:05 +03:00
|
|
|
} else {
|
2018-03-29 11:47:44 +03:00
|
|
|
let mut cmd = if cfg!(windows) {
|
|
|
|
let mut c = Command::new("cmd");
|
|
|
|
c.arg("/c");
|
2018-06-01 21:42:59 +03:00
|
|
|
c.arg("npm");
|
2018-03-29 11:47:44 +03:00
|
|
|
c
|
|
|
|
} else {
|
2018-06-01 21:42:59 +03:00
|
|
|
Command::new("npm")
|
2018-03-29 11:47:44 +03:00
|
|
|
};
|
2018-06-01 21:42:59 +03:00
|
|
|
cmd.arg("run").arg("run-webpack").current_dir(&root);
|
|
|
|
run(&mut cmd, "npm");
|
2018-03-29 11:47:44 +03:00
|
|
|
|
|
|
|
let mut cmd = Command::new("node");
|
2018-05-06 00:10:25 +03:00
|
|
|
cmd.args(&self.node_args);
|
2018-03-29 11:47:44 +03:00
|
|
|
cmd.arg(root.join("bundle.js"))
|
|
|
|
.current_dir(&root);
|
|
|
|
run(&mut cmd, "node");
|
|
|
|
}
|
2017-12-15 06:31:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:20:14 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn symlink_dir(a: &Path, b: &Path) -> io::Result<()> {
|
|
|
|
use std::os::unix::fs::symlink;
|
|
|
|
symlink(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn symlink_dir(a: &Path, b: &Path) -> io::Result<()> {
|
|
|
|
use std::os::windows::fs::symlink_dir;
|
|
|
|
symlink_dir(a, b)
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:31:01 +03:00
|
|
|
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());
|
|
|
|
}
|
2018-04-10 01:32:06 +03:00
|
|
|
|
|
|
|
mod api;
|
|
|
|
mod classes;
|
|
|
|
mod closures;
|
|
|
|
mod dependencies;
|
|
|
|
mod enums;
|
|
|
|
mod import_class;
|
|
|
|
mod imports;
|
|
|
|
mod jsobjects;
|
|
|
|
mod math;
|
|
|
|
mod node;
|
|
|
|
mod non_debug;
|
|
|
|
mod simple;
|
|
|
|
mod slice;
|
|
|
|
mod structural;
|
2018-04-27 23:43:47 +03:00
|
|
|
mod non_wasm;
|
2018-05-06 00:10:25 +03:00
|
|
|
mod u64;
|
2018-05-26 01:47:40 +03:00
|
|
|
mod char;
|
|
|
|
mod webidl;
|