mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -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:
parent
314f666897
commit
1e0808d501
3 changed files with 48 additions and 4 deletions
|
@ -37,6 +37,17 @@ lazy_static! {
|
||||||
/// resource table is dropped storing reference to that handle, the handle
|
/// resource table is dropped storing reference to that handle, the handle
|
||||||
/// itself won't be closed (so Deno.core.print) will still work.
|
/// itself won't be closed (so Deno.core.print) will still work.
|
||||||
// TODO(ry) It should be possible to close stdout.
|
// 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 = {
|
static ref STDOUT_HANDLE: std::fs::File = {
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
|
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
|
||||||
|
@ -71,10 +82,10 @@ pub fn get_stdio() -> (
|
||||||
StreamResourceHolder,
|
StreamResourceHolder,
|
||||||
StreamResourceHolder,
|
StreamResourceHolder,
|
||||||
) {
|
) {
|
||||||
let stdin = StreamResourceHolder::new(StreamResource::Stdin(
|
let stdin = StreamResourceHolder::new(StreamResource::FsFile(Some({
|
||||||
tokio::io::stdin(),
|
let stdin = STDIN_HANDLE.try_clone().unwrap();
|
||||||
TTYMetadata::default(),
|
(tokio::fs::File::from_std(stdin), FileMetadata::default())
|
||||||
));
|
})));
|
||||||
let stdout = StreamResourceHolder::new(StreamResource::FsFile(Some({
|
let stdout = StreamResourceHolder::new(StreamResource::FsFile(Some({
|
||||||
let stdout = STDOUT_HANDLE.try_clone().unwrap();
|
let stdout = STDOUT_HANDLE.try_clone().unwrap();
|
||||||
(tokio::fs::File::from_std(stdout), FileMetadata::default())
|
(tokio::fs::File::from_std(stdout), FileMetadata::default())
|
||||||
|
|
32
cli/tests/unit/stdio_test.ts
Normal file
32
cli/tests/unit/stdio_test.ts
Normal 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);
|
||||||
|
});
|
|
@ -52,6 +52,7 @@ import "./request_test.ts";
|
||||||
import "./resources_test.ts";
|
import "./resources_test.ts";
|
||||||
import "./signal_test.ts";
|
import "./signal_test.ts";
|
||||||
import "./stat_test.ts";
|
import "./stat_test.ts";
|
||||||
|
import "./stdio_test.ts";
|
||||||
import "./streams_internal_test.ts";
|
import "./streams_internal_test.ts";
|
||||||
import "./streams_piping_test.ts";
|
import "./streams_piping_test.ts";
|
||||||
import "./streams_transform_test.ts";
|
import "./streams_transform_test.ts";
|
||||||
|
|
Loading…
Reference in a new issue