2018-10-01 19:37:18 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// Think of Resources as File Descriptors. They are integers that are allocated
|
2018-10-24 11:54:34 -04:00
|
|
|
// by the privileged side of Deno to refer to various resources. The simplest
|
2018-10-01 19:37:18 -04:00
|
|
|
// example are standard file system files and stdio - but there will be other
|
|
|
|
// resources added in the future that might not correspond to operating system
|
|
|
|
// level File Descriptors. To avoid confusion we call them "resources" not "file
|
|
|
|
// descriptors". This module implements a global resource table. Ops (AKA
|
|
|
|
// handlers) look up resources by their integer id here.
|
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use eager_unix as eager;
|
2018-11-05 12:55:59 -05:00
|
|
|
use errors::bad_resource;
|
2018-10-05 12:16:24 -04:00
|
|
|
use errors::DenoError;
|
2018-11-05 12:55:59 -05:00
|
|
|
use errors::DenoResult;
|
2018-10-26 12:14:06 -04:00
|
|
|
use http_body::HttpBody;
|
2018-11-05 12:55:59 -05:00
|
|
|
use repl::Repl;
|
2018-10-19 18:38:27 -04:00
|
|
|
use tokio_util;
|
2018-10-19 18:17:48 -04:00
|
|
|
use tokio_write;
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-10-01 19:37:18 -04:00
|
|
|
use futures;
|
2018-10-23 21:12:21 -04:00
|
|
|
use futures::future::{Either, FutureResult};
|
2018-10-01 19:37:18 -04:00
|
|
|
use futures::Poll;
|
2018-10-26 12:14:06 -04:00
|
|
|
use hyper;
|
2018-10-01 19:37:18 -04:00
|
|
|
use std;
|
|
|
|
use std::collections::HashMap;
|
2018-10-23 21:12:21 -04:00
|
|
|
use std::io::{Error, Read, Write};
|
2018-10-05 12:16:24 -04:00
|
|
|
use std::net::{Shutdown, SocketAddr};
|
2018-11-05 01:21:21 -05:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2018-10-01 19:37:18 -04:00
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
use tokio;
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
2018-10-03 23:58:29 -04:00
|
|
|
use tokio::net::TcpStream;
|
2018-10-19 11:40:54 -04:00
|
|
|
use tokio_io;
|
2018-10-01 19:37:18 -04:00
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
pub type ResourceId = u32; // Sometimes referred to RID.
|
2018-10-01 19:37:18 -04:00
|
|
|
|
|
|
|
// These store Deno's file descriptors. These are not necessarily the operating
|
|
|
|
// system ones.
|
|
|
|
type ResourceTable = HashMap<ResourceId, Repr>;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
// Starts at 3 because stdio is [0-2].
|
2018-11-05 01:21:21 -05:00
|
|
|
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
|
2018-10-01 19:37:18 -04:00
|
|
|
static ref RESOURCE_TABLE: Mutex<ResourceTable> = Mutex::new({
|
|
|
|
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(2, Repr::Stderr(tokio::io::stderr()));
|
|
|
|
m
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal representation of Resource.
|
|
|
|
enum Repr {
|
|
|
|
Stdin(tokio::io::Stdin),
|
|
|
|
Stdout(tokio::io::Stdout),
|
|
|
|
Stderr(tokio::io::Stderr),
|
|
|
|
FsFile(tokio::fs::File),
|
2018-10-03 23:58:29 -04:00
|
|
|
TcpListener(tokio::net::TcpListener),
|
|
|
|
TcpStream(tokio::net::TcpStream),
|
2018-10-26 12:14:06 -04:00
|
|
|
HttpBody(HttpBody),
|
2018-11-05 12:55:59 -05:00
|
|
|
Repl(Repl),
|
2018-10-01 19:37:18 -04:00
|
|
|
}
|
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
pub fn table_entries() -> Vec<(u32, String)> {
|
2018-10-30 15:58:55 -04:00
|
|
|
let table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
|
2018-11-04 09:04:24 -05:00
|
|
|
table
|
2018-10-30 15:58:55 -04:00
|
|
|
.iter()
|
2018-11-04 09:04:24 -05:00
|
|
|
.map(|(key, value)| (*key, inspect_repr(&value)))
|
|
|
|
.collect()
|
2018-10-30 15:58:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_table_entries() {
|
|
|
|
let mut entries = table_entries();
|
|
|
|
entries.sort();
|
|
|
|
assert_eq!(entries.len(), 3);
|
|
|
|
assert_eq!(entries[0], (0, String::from("stdin")));
|
|
|
|
assert_eq!(entries[1], (1, String::from("stdout")));
|
|
|
|
assert_eq!(entries[2], (2, String::from("stderr")));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inspect_repr(repr: &Repr) -> String {
|
|
|
|
let h_repr = match repr {
|
|
|
|
Repr::Stdin(_) => "stdin",
|
|
|
|
Repr::Stdout(_) => "stdout",
|
|
|
|
Repr::Stderr(_) => "stderr",
|
|
|
|
Repr::FsFile(_) => "fsFile",
|
|
|
|
Repr::TcpListener(_) => "tcpListener",
|
|
|
|
Repr::TcpStream(_) => "tcpStream",
|
2018-10-26 12:14:06 -04:00
|
|
|
Repr::HttpBody(_) => "httpBody",
|
2018-11-05 12:55:59 -05:00
|
|
|
Repr::Repl(_) => "repl",
|
2018-10-30 15:58:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
String::from(h_repr)
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:37:18 -04:00
|
|
|
// Abstract async file interface.
|
|
|
|
// Ideally in unix, if Resource represents an OS rid, it will be the same.
|
2018-10-03 23:58:29 -04:00
|
|
|
#[derive(Debug)]
|
2018-10-01 19:37:18 -04:00
|
|
|
pub struct Resource {
|
|
|
|
pub rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:58:29 -04:00
|
|
|
impl Resource {
|
|
|
|
// TODO Should it return a Resource instead of net::TcpStream?
|
|
|
|
pub fn poll_accept(&mut self) -> Poll<(TcpStream, SocketAddr), Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::TcpListener(ref mut s) => s.poll_accept(),
|
|
|
|
_ => panic!("Cannot accept"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close(2) is done by dropping the value. Therefore we just need to remove
|
|
|
|
// the resource from the RESOURCE_TABLE.
|
|
|
|
pub fn close(&mut self) {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = table.remove(&self.rid);
|
|
|
|
assert!(r.is_some());
|
|
|
|
}
|
2018-10-05 12:16:24 -04:00
|
|
|
|
|
|
|
pub fn shutdown(&mut self, how: Shutdown) -> Result<(), DenoError> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::TcpStream(ref mut f) => {
|
2018-11-04 09:04:24 -05:00
|
|
|
TcpStream::shutdown(f, how).map_err(DenoError::from)
|
2018-10-05 12:16:24 -04:00
|
|
|
}
|
|
|
|
_ => panic!("Cannot shutdown"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
2018-10-01 19:37:18 -04:00
|
|
|
impl Read for Resource {
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for Resource {
|
|
|
|
fn poll_read(&mut self, buf: &mut [u8]) -> Poll<usize, Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::FsFile(ref mut f) => f.poll_read(buf),
|
|
|
|
Repr::Stdin(ref mut f) => f.poll_read(buf),
|
2018-10-03 23:58:29 -04:00
|
|
|
Repr::TcpStream(ref mut f) => f.poll_read(buf),
|
2018-10-26 12:14:06 -04:00
|
|
|
Repr::HttpBody(ref mut f) => f.poll_read(buf),
|
2018-11-05 12:55:59 -05:00
|
|
|
_ => panic!("Cannot read"),
|
2018-10-01 19:37:18 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Write for Resource {
|
|
|
|
fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncWrite for Resource {
|
|
|
|
fn poll_write(&mut self, buf: &[u8]) -> Poll<usize, Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::FsFile(ref mut f) => f.poll_write(buf),
|
|
|
|
Repr::Stdout(ref mut f) => f.poll_write(buf),
|
|
|
|
Repr::Stderr(ref mut f) => f.poll_write(buf),
|
2018-10-03 23:58:29 -04:00
|
|
|
Repr::TcpStream(ref mut f) => f.poll_write(buf),
|
2018-11-05 12:55:59 -05:00
|
|
|
_ => panic!("Cannot write"),
|
2018-10-01 19:37:18 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shutdown(&mut self) -> futures::Poll<(), std::io::Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_rid() -> ResourceId {
|
|
|
|
let next_rid = NEXT_RID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
next_rid as ResourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_fs_file(fs_file: tokio::fs::File) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
match tg.insert(rid, Repr::FsFile(fs_file)) {
|
|
|
|
Some(_) => panic!("There is already a file with that rid"),
|
|
|
|
None => Resource { rid },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:58:29 -04:00
|
|
|
pub fn add_tcp_listener(listener: tokio::net::TcpListener) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = tg.insert(rid, Repr::TcpListener(listener));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_tcp_stream(stream: tokio::net::TcpStream) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = tg.insert(rid, Repr::TcpStream(stream));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:14:06 -04:00
|
|
|
pub fn add_hyper_body(body: hyper::Body) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let body = HttpBody::from(body);
|
|
|
|
let r = tg.insert(rid, Repr::HttpBody(body));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
pub fn add_repl(repl: Repl) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = tg.insert(rid, Repr::Repl(repl));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readline(rid: ResourceId, prompt: &str) -> DenoResult<String> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&rid);
|
|
|
|
match maybe_repr {
|
|
|
|
Some(Repr::Repl(ref mut r)) => {
|
|
|
|
let line = r.readline(&prompt)?;
|
|
|
|
Ok(line)
|
|
|
|
}
|
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:37:18 -04:00
|
|
|
pub fn lookup(rid: ResourceId) -> Option<Resource> {
|
2018-10-26 12:14:06 -04:00
|
|
|
debug!("resource lookup {}", rid);
|
2018-10-01 19:37:18 -04:00
|
|
|
let table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
table.get(&rid).map(|_| Resource { rid })
|
|
|
|
}
|
2018-10-19 11:40:54 -04:00
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
pub type EagerRead<R, T> =
|
2018-10-19 11:40:54 -04:00
|
|
|
Either<tokio_io::io::Read<R, T>, FutureResult<(R, T, usize), std::io::Error>>;
|
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
pub type EagerWrite<R, T> =
|
|
|
|
Either<tokio_write::Write<R, T>, FutureResult<(R, T, usize), std::io::Error>>;
|
2018-10-19 11:40:54 -04:00
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
pub type EagerAccept = Either<
|
|
|
|
tokio_util::Accept,
|
|
|
|
FutureResult<(tokio::net::TcpStream, std::net::SocketAddr), std::io::Error>,
|
|
|
|
>;
|
2018-10-23 19:32:55 -04:00
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
pub fn eager_read<T: AsMut<[u8]>>(
|
2018-10-23 19:32:55 -04:00
|
|
|
resource: Resource,
|
|
|
|
mut buf: T,
|
|
|
|
) -> EagerRead<Resource, T> {
|
2018-10-23 21:12:21 -04:00
|
|
|
Either::A(tokio_io::io::read(resource, buf)).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
pub fn eager_write<T: AsRef<[u8]>>(
|
|
|
|
resource: Resource,
|
|
|
|
buf: T,
|
|
|
|
) -> EagerWrite<Resource, T> {
|
|
|
|
Either::A(tokio_write::write(resource, buf)).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
pub fn eager_accept(resource: Resource) -> EagerAccept {
|
|
|
|
Either::A(tokio_util::accept(resource)).into()
|
2018-10-23 19:32:55 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:40:54 -04:00
|
|
|
// This is an optimization that Tokio should do.
|
|
|
|
// Attempt to call read() on the main thread.
|
2018-10-23 21:12:21 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn eager_read<T: AsMut<[u8]>>(
|
|
|
|
resource: Resource,
|
|
|
|
buf: T,
|
|
|
|
) -> EagerRead<Resource, T> {
|
2018-10-19 11:40:54 -04:00
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&resource.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::TcpStream(ref mut tcp_stream) => {
|
2018-10-23 21:12:21 -04:00
|
|
|
eager::tcp_read(tcp_stream, resource, buf)
|
2018-10-19 11:40:54 -04:00
|
|
|
}
|
|
|
|
_ => Either::A(tokio_io::io::read(resource, buf)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 18:17:48 -04:00
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
// This is an optimization that Tokio should do.
|
|
|
|
// Attempt to call write() on the main thread.
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn eager_write<T: AsRef<[u8]>>(
|
2018-10-23 19:32:55 -04:00
|
|
|
resource: Resource,
|
|
|
|
buf: T,
|
|
|
|
) -> EagerWrite<Resource, T> {
|
2018-10-19 18:17:48 -04:00
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&resource.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::TcpStream(ref mut tcp_stream) => {
|
2018-10-23 21:12:21 -04:00
|
|
|
eager::tcp_write(tcp_stream, resource, buf)
|
2018-10-19 18:17:48 -04:00
|
|
|
}
|
|
|
|
_ => Either::A(tokio_write::write(resource, buf)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 18:38:27 -04:00
|
|
|
|
2018-10-23 21:12:21 -04:00
|
|
|
#[cfg(unix)]
|
2018-10-19 18:38:27 -04:00
|
|
|
pub fn eager_accept(resource: Resource) -> EagerAccept {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&resource.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
2018-10-23 19:32:55 -04:00
|
|
|
Repr::TcpListener(ref mut tcp_listener) => {
|
2018-10-23 21:12:21 -04:00
|
|
|
eager::tcp_accept(tcp_listener, resource)
|
2018-10-19 18:38:27 -04:00
|
|
|
}
|
|
|
|
_ => Either::A(tokio_util::accept(resource)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|