mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-17 02:30:50 +00:00
Use std::fs
read/write conveniences
In addition to being more ergonomic these are much more efficient at reading large files as they preallocate internally. This provides a nice speed boost locally, reducing the overhead of `wasm-bindgen-test-runner` from 0.23s to 0.19s, yay!
This commit is contained in:
parent
f3942229fe
commit
0992e45e7f
@ -13,8 +13,7 @@ extern crate failure;
|
||||
use std::any::Any;
|
||||
use std::collections::BTreeSet;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::fs;
|
||||
use std::mem;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@ -158,9 +157,7 @@ impl Bindgen {
|
||||
(module, &name[..])
|
||||
}
|
||||
Input::Path(ref path) => {
|
||||
let mut contents = Vec::new();
|
||||
File::open(&path)
|
||||
.and_then(|mut f| f.read_to_end(&mut contents))
|
||||
let contents = fs::read(&path)
|
||||
.with_context(|_| format!("failed to read `{}`", path.display()))?;
|
||||
let module = parity_wasm::deserialize_buffer::<Module>(&contents)
|
||||
.context("failed to parse input file as wasm")?;
|
||||
@ -232,14 +229,12 @@ impl Bindgen {
|
||||
|
||||
let extension = if self.nodejs_experimental_modules { "mjs" } else { "js" };
|
||||
let js_path = out_dir.join(stem).with_extension(extension);
|
||||
File::create(&js_path)
|
||||
.and_then(|mut f| f.write_all(reset_indentation(&js).as_bytes()))
|
||||
fs::write(&js_path, reset_indentation(&js))
|
||||
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
|
||||
|
||||
if self.typescript {
|
||||
let ts_path = out_dir.join(stem).with_extension("d.ts");
|
||||
File::create(&ts_path)
|
||||
.and_then(|mut f| f.write_all(ts.as_bytes()))
|
||||
fs::write(&ts_path, ts)
|
||||
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
|
||||
}
|
||||
|
||||
@ -248,14 +243,12 @@ impl Bindgen {
|
||||
if self.nodejs {
|
||||
let js_path = wasm_path.with_extension(extension);
|
||||
let shim = self.generate_node_wasm_import(&module, &wasm_path);
|
||||
File::create(&js_path)
|
||||
.and_then(|mut f| f.write_all(shim.as_bytes()))
|
||||
fs::write(&js_path, shim)
|
||||
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
|
||||
}
|
||||
|
||||
let wasm_bytes = parity_wasm::serialize(module)?;
|
||||
File::create(&wasm_path)
|
||||
.and_then(|mut f| f.write_all(&wasm_bytes))
|
||||
fs::write(&wasm_path, wasm_bytes)
|
||||
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ extern crate base64;
|
||||
extern crate tempfile;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::process::Command;
|
||||
|
||||
use failure::{Error, ResultExt};
|
||||
@ -375,8 +375,7 @@ impl Output {
|
||||
let td = tempfile::tempdir()?;
|
||||
let wasm = serialize(self.module)?;
|
||||
let wasm_file = td.as_ref().join("foo.wasm");
|
||||
File::create(&wasm_file)
|
||||
.and_then(|mut f| f.write_all(&wasm))
|
||||
fs::write(&wasm_file, wasm)
|
||||
.with_context(|_| format!("failed to write wasm to `{}`", wasm_file.display()))?;
|
||||
|
||||
let wast_file = td.as_ref().join("foo.wast");
|
||||
@ -396,9 +395,7 @@ impl Output {
|
||||
"wasm2asm",
|
||||
)?;
|
||||
|
||||
let mut asm_func = String::new();
|
||||
File::open(&js_file)
|
||||
.and_then(|mut f| f.read_to_string(&mut asm_func))
|
||||
let asm_func = fs::read_to_string(&js_file)
|
||||
.with_context(|_| format!("failed to read `{}`", js_file.display()))?;
|
||||
|
||||
let mut make_imports = String::from(
|
||||
|
@ -4,8 +4,7 @@ extern crate wasm_bindgen_cli_support;
|
||||
extern crate parity_wasm;
|
||||
|
||||
use std::env;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Write, Read};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{self, Command};
|
||||
|
||||
@ -107,9 +106,7 @@ fn rmain() -> Result<(), Error> {
|
||||
// Note that we're collecting *JS objects* that represent the functions to
|
||||
// execute, and then those objects are passed into wasm for it to execute
|
||||
// when it sees fit.
|
||||
let mut wasm = Vec::new();
|
||||
File::open(&wasm_file_to_test)
|
||||
.and_then(|mut f| f.read_to_end(&mut wasm))
|
||||
let wasm = fs::read(&wasm_file_to_test)
|
||||
.context("failed to read wasm file")?;
|
||||
let wasm = Module::deserialize(&mut &wasm[..])
|
||||
.context("failed to deserialize wasm module")?;
|
||||
@ -136,8 +133,7 @@ fn rmain() -> Result<(), Error> {
|
||||
.context("executing `wasm-bindgen` over the wasm file")?;
|
||||
|
||||
let js_path = tmpdir.join("run.js");
|
||||
File::create(&js_path)
|
||||
.and_then(|mut f| f.write_all(js_to_execute.as_bytes()))
|
||||
fs::write(&js_path, js_to_execute)
|
||||
.context("failed to write JS file")?;
|
||||
|
||||
// Last but not least, execute `node`! Add an entry to `NODE_PATH` for the
|
||||
|
@ -4,8 +4,7 @@ extern crate docopt;
|
||||
extern crate failure;
|
||||
extern crate wasm_bindgen_cli_support;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
@ -58,9 +57,7 @@ fn main() {
|
||||
}
|
||||
|
||||
fn rmain(args: &Args) -> Result<(), Error> {
|
||||
let mut wasm = Vec::new();
|
||||
File::open(&args.arg_input)
|
||||
.and_then(|mut f| f.read_to_end(&mut wasm))
|
||||
let wasm = fs::read(&args.arg_input)
|
||||
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
|
||||
|
||||
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
|
||||
@ -73,8 +70,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
|
||||
if let Some(ref p) = args.flag_output {
|
||||
let dst = p.with_extension("d.ts");
|
||||
let ts = object.typescript();
|
||||
File::create(&dst)
|
||||
.and_then(|mut f| f.write_all(ts.as_bytes()))
|
||||
fs::write(&dst, ts)
|
||||
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
|
||||
}
|
||||
}
|
||||
@ -83,8 +79,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
|
||||
|
||||
match args.flag_output {
|
||||
Some(ref p) => {
|
||||
File::create(p)
|
||||
.and_then(|mut f| f.write_all(js.as_bytes()))
|
||||
fs::write(p, js)
|
||||
.with_context(|_| format!("failed to write `{}`", p.display()))?;
|
||||
}
|
||||
None => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user