mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
make stdout unbuffered (#1355)
This commit is contained in:
parent
cbee2895b3
commit
bee55fcd20
10 changed files with 44 additions and 2 deletions
2
BUILD.gn
2
BUILD.gn
|
@ -42,6 +42,8 @@ main_extern = [
|
||||||
"$rust_build:tokio_process",
|
"$rust_build:tokio_process",
|
||||||
"$rust_build:tokio_threadpool",
|
"$rust_build:tokio_threadpool",
|
||||||
"$rust_build:url",
|
"$rust_build:url",
|
||||||
|
"$rust_build:kernel32",
|
||||||
|
"$rust_build:winapi",
|
||||||
"//build_extra/flatbuffers/rust:flatbuffers",
|
"//build_extra/flatbuffers/rust:flatbuffers",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -161,6 +161,7 @@ dependencies = [
|
||||||
"http 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
"http 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"hyper 0.12.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
"hyper 0.12.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"hyper-rustls 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"hyper-rustls 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -178,6 +179,7 @@ dependencies = [
|
||||||
"tokio-process 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tokio-process 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tokio-threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tokio-threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -34,3 +34,5 @@ tokio-io = "=0.1.10"
|
||||||
tokio-process = "=0.2.3"
|
tokio-process = "=0.2.3"
|
||||||
tokio-threadpool = "=0.1.9"
|
tokio-threadpool = "=0.1.9"
|
||||||
url = "=1.7.1"
|
url = "=1.7.1"
|
||||||
|
kernel32-sys = "=0.2.2"
|
||||||
|
winapi = "=0.3.6"
|
||||||
|
|
|
@ -43,6 +43,18 @@ pub type ResourceId = u32; // Sometimes referred to RID.
|
||||||
// system ones.
|
// system ones.
|
||||||
type ResourceTable = HashMap<ResourceId, Repr>;
|
type ResourceTable = HashMap<ResourceId, Repr>;
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
use std::os::unix::io::FromRawFd;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
use std::os::windows::io::FromRawHandle;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
extern crate kernel32;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
extern crate winapi;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
// Starts at 3 because stdio is [0-2].
|
// Starts at 3 because stdio is [0-2].
|
||||||
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
|
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
|
||||||
|
@ -50,7 +62,18 @@ lazy_static! {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
// TODO Load these lazily during lookup?
|
// TODO Load these lazily during lookup?
|
||||||
m.insert(0, Repr::Stdin(tokio::io::stdin()));
|
m.insert(0, Repr::Stdin(tokio::io::stdin()));
|
||||||
m.insert(1, Repr::Stdout(tokio::io::stdout()));
|
|
||||||
|
m.insert(1, Repr::Stdout({
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
|
||||||
|
#[cfg(windows)]
|
||||||
|
let stdout = unsafe {
|
||||||
|
std::fs::File::from_raw_handle(kernel32::GetStdHandle(
|
||||||
|
winapi::um::winbase::STD_OUTPUT_HANDLE))
|
||||||
|
};
|
||||||
|
tokio::fs::File::from_std(stdout)
|
||||||
|
}));
|
||||||
|
|
||||||
m.insert(2, Repr::Stderr(tokio::io::stderr()));
|
m.insert(2, Repr::Stderr(tokio::io::stderr()));
|
||||||
m
|
m
|
||||||
});
|
});
|
||||||
|
@ -59,7 +82,7 @@ lazy_static! {
|
||||||
// Internal representation of Resource.
|
// Internal representation of Resource.
|
||||||
enum Repr {
|
enum Repr {
|
||||||
Stdin(tokio::io::Stdin),
|
Stdin(tokio::io::Stdin),
|
||||||
Stdout(tokio::io::Stdout),
|
Stdout(tokio::fs::File),
|
||||||
Stderr(tokio::io::Stderr),
|
Stderr(tokio::io::Stderr),
|
||||||
FsFile(tokio::fs::File),
|
FsFile(tokio::fs::File),
|
||||||
TcpListener(tokio::net::TcpListener),
|
TcpListener(tokio::net::TcpListener),
|
||||||
|
|
3
tests/unbuffered_stderr.test
Normal file
3
tests/unbuffered_stderr.test
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
args: tests/unbuffered_stderr.ts --reload
|
||||||
|
check_stderr: true
|
||||||
|
output: tests/unbuffered_stderr.ts.out
|
3
tests/unbuffered_stderr.ts
Normal file
3
tests/unbuffered_stderr.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { stderr } from "deno";
|
||||||
|
|
||||||
|
stderr.write(new TextEncoder().encode("x"));
|
1
tests/unbuffered_stderr.ts.out
Normal file
1
tests/unbuffered_stderr.ts.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[WILDCARD]x
|
2
tests/unbuffered_stdout.test
Normal file
2
tests/unbuffered_stdout.test
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
args: tests/unbuffered_stdout.ts --reload
|
||||||
|
output: tests/unbuffered_stdout.ts.out
|
3
tests/unbuffered_stdout.ts
Normal file
3
tests/unbuffered_stdout.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { stdout } from "deno";
|
||||||
|
|
||||||
|
stdout.write(new TextEncoder().encode("a"));
|
1
tests/unbuffered_stdout.ts.out
Normal file
1
tests/unbuffered_stdout.ts.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
a
|
Loading…
Add table
Add a link
Reference in a new issue