0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

make stdout unbuffered (#1355)

This commit is contained in:
Yoshiya Hinosawa 2018-12-23 13:53:29 +09:00 committed by Ryan Dahl
parent cbee2895b3
commit bee55fcd20
10 changed files with 44 additions and 2 deletions

View file

@ -42,6 +42,8 @@ main_extern = [
"$rust_build:tokio_process",
"$rust_build:tokio_threadpool",
"$rust_build:url",
"$rust_build:kernel32",
"$rust_build:winapi",
"//build_extra/flatbuffers/rust:flatbuffers",
]

2
Cargo.lock generated
View file

@ -161,6 +161,7 @@ dependencies = [
"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-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)",
"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)",
@ -178,6 +179,7 @@ dependencies = [
"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)",
"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]]

View file

@ -34,3 +34,5 @@ tokio-io = "=0.1.10"
tokio-process = "=0.2.3"
tokio-threadpool = "=0.1.9"
url = "=1.7.1"
kernel32-sys = "=0.2.2"
winapi = "=0.3.6"

View file

@ -43,6 +43,18 @@ pub type ResourceId = u32; // Sometimes referred to RID.
// system ones.
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! {
// Starts at 3 because stdio is [0-2].
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
@ -50,7 +62,18 @@ lazy_static! {
let mut m = HashMap::new();
// TODO Load these lazily during lookup?
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
});
@ -59,7 +82,7 @@ lazy_static! {
// Internal representation of Resource.
enum Repr {
Stdin(tokio::io::Stdin),
Stdout(tokio::io::Stdout),
Stdout(tokio::fs::File),
Stderr(tokio::io::Stderr),
FsFile(tokio::fs::File),
TcpListener(tokio::net::TcpListener),

View file

@ -0,0 +1,3 @@
args: tests/unbuffered_stderr.ts --reload
check_stderr: true
output: tests/unbuffered_stderr.ts.out

View file

@ -0,0 +1,3 @@
import { stderr } from "deno";
stderr.write(new TextEncoder().encode("x"));

View file

@ -0,0 +1 @@
[WILDCARD]x

View file

@ -0,0 +1,2 @@
args: tests/unbuffered_stdout.ts --reload
output: tests/unbuffered_stdout.ts.out

View file

@ -0,0 +1,3 @@
import { stdout } from "deno";
stdout.write(new TextEncoder().encode("a"));

View file

@ -0,0 +1 @@
a