1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix: Deno.readSync on stdin (#6126)

Currently sync operations on stdin are failing because tokio::Stdin
cannot be converted to a std::File.

This commit replaces tokio::stdin with a raw file descriptor 
wrapped in a std::fs::File which can be converted to a 
tokio::File and back again making the synchronous version 
of op_read actually work.
This commit is contained in:
Casper Beyer 2020-06-10 00:29:12 +08:00 committed by GitHub
parent 314f666897
commit 1e0808d501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 4 deletions

View file

@ -37,6 +37,17 @@ lazy_static! {
/// resource table is dropped storing reference to that handle, the handle
/// itself won't be closed (so Deno.core.print) will still work.
// TODO(ry) It should be possible to close stdout.
static ref STDIN_HANDLE: std::fs::File = {
#[cfg(not(windows))]
let stdin = unsafe { std::fs::File::from_raw_fd(0) };
#[cfg(windows)]
let stdin = unsafe {
std::fs::File::from_raw_handle(winapi::um::processenv::GetStdHandle(
winapi::um::winbase::STD_INPUT_HANDLE,
))
};
stdin
};
static ref STDOUT_HANDLE: std::fs::File = {
#[cfg(not(windows))]
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
@ -71,10 +82,10 @@ pub fn get_stdio() -> (
StreamResourceHolder,
StreamResourceHolder,
) {
let stdin = StreamResourceHolder::new(StreamResource::Stdin(
tokio::io::stdin(),
TTYMetadata::default(),
));
let stdin = StreamResourceHolder::new(StreamResource::FsFile(Some({
let stdin = STDIN_HANDLE.try_clone().unwrap();
(tokio::fs::File::from_std(stdin), FileMetadata::default())
})));
let stdout = StreamResourceHolder::new(StreamResource::FsFile(Some({
let stdout = STDOUT_HANDLE.try_clone().unwrap();
(tokio::fs::File::from_std(stdout), FileMetadata::default())

View file

@ -0,0 +1,32 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assertEquals } from "./test_util.ts";
unitTest(async function stdioStdinRead() {
const nread = await Deno.stdin.read(new Uint8Array(0));
assertEquals(nread, 0);
});
unitTest(function stdioStdinReadSync() {
const nread = Deno.stdin.readSync(new Uint8Array(0));
assertEquals(nread, 0);
});
unitTest(async function stdioStdoutWrite() {
const nwritten = await Deno.stdout.write(new Uint8Array(0));
assertEquals(nwritten, 0);
});
unitTest(function stdioStdoutWriteSync() {
const nwritten = Deno.stdout.writeSync(new Uint8Array(0));
assertEquals(nwritten, 0);
});
unitTest(async function stdioStderrWrite() {
const nwritten = await Deno.stderr.write(new Uint8Array(0));
assertEquals(nwritten, 0);
});
unitTest(function stdioStderrWriteSync() {
const nwritten = Deno.stderr.writeSync(new Uint8Array(0));
assertEquals(nwritten, 0);
});

View file

@ -52,6 +52,7 @@ import "./request_test.ts";
import "./resources_test.ts";
import "./signal_test.ts";
import "./stat_test.ts";
import "./stdio_test.ts";
import "./streams_internal_test.ts";
import "./streams_piping_test.ts";
import "./streams_transform_test.ts";