summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorRy Dahl <ry@tinyclouds.org>2019-11-06 12:17:28 -0500
committerGitHub <noreply@github.com>2019-11-06 12:17:28 -0500
commit5c1deac0cfe66ef27020aa0e863c16f3bc2afb50 (patch)
treed3de45e89924de58a5ae573dd5086af04e7a19c6 /cli
parent92b8674162aff30a9552b1a07855b685d305830a (diff)
Remove CoreResource::inspect_repr method (#3274)
Towards simplifying (or better removing entirely) the CoreResource trait. Resources should be any bit of privileged heap allocated memory that needs to be referenced from JS, not very specific trait implementations. Therefore CoreResource should be pushed towards being as general as possible.
Diffstat (limited to 'cli')
-rw-r--r--cli/ops/repl.rs8
-rw-r--r--cli/resources.rs72
-rw-r--r--cli/state.rs2
-rw-r--r--cli/worker.rs6
4 files changed, 40 insertions, 48 deletions
diff --git a/cli/ops/repl.rs b/cli/ops/repl.rs
index 4a3ba68d4..ba63c5109 100644
--- a/cli/ops/repl.rs
+++ b/cli/ops/repl.rs
@@ -24,11 +24,7 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
struct ReplResource(Arc<Mutex<Repl>>);
-impl CoreResource for ReplResource {
- fn inspect_repr(&self) -> &str {
- "repl"
- }
-}
+impl CoreResource for ReplResource {}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -49,7 +45,7 @@ fn op_repl_start(
let repl = repl::Repl::new(history_path);
let resource = ReplResource(Arc::new(Mutex::new(repl)));
let mut table = resources::lock_resource_table();
- let rid = table.add(Box::new(resource));
+ let rid = table.add("repl", Box::new(resource));
Ok(JsonOp::Sync(json!(rid)))
}
diff --git a/cli/resources.rs b/cli/resources.rs
index ba7795f5d..2910d16b6 100644
--- a/cli/resources.rs
+++ b/cli/resources.rs
@@ -49,9 +49,9 @@ lazy_static! {
let mut table = ResourceTable::default();
// TODO Load these lazily during lookup?
- table.add(Box::new(CliResource::Stdin(tokio::io::stdin())));
+ table.add("stdin", Box::new(CliResource::Stdin(tokio::io::stdin())));
- table.add(Box::new(CliResource::Stdout({
+ table.add("stdout", Box::new(CliResource::Stdout({
#[cfg(not(windows))]
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
#[cfg(windows)]
@@ -62,7 +62,7 @@ lazy_static! {
tokio::fs::File::from_std(stdout)
})));
- table.add(Box::new(CliResource::Stderr(tokio::io::stderr())));
+ table.add("stderr", Box::new(CliResource::Stderr(tokio::io::stderr())));
table
});
}
@@ -98,6 +98,9 @@ enum CliResource {
}
impl CoreResource for CliResource {
+ // TODO(ry) These task notifications are hacks to workaround various behaviors
+ // in Tokio. They should not influence the overall design of Deno. The
+ // CoreResource::close should be removed in favor of the drop trait.
fn close(&self) {
match self {
CliResource::TcpListener(_, Some(t)) => {
@@ -109,25 +112,6 @@ impl CoreResource for CliResource {
_ => {}
}
}
-
- fn inspect_repr(&self) -> &str {
- match self {
- CliResource::Stdin(_) => "stdin",
- CliResource::Stdout(_) => "stdout",
- CliResource::Stderr(_) => "stderr",
- CliResource::FsFile(_) => "fsFile",
- CliResource::TcpListener(_, _) => "tcpListener",
- CliResource::TlsListener(_, _, _) => "tlsListener",
- CliResource::TcpStream(_) => "tcpStream",
- CliResource::ClientTlsStream(_) => "clientTlsStream",
- CliResource::ServerTlsStream(_) => "serverTlsStream",
- CliResource::HttpBody(_) => "httpBody",
- CliResource::Child(_) => "child",
- CliResource::ChildStdin(_) => "childStdin",
- CliResource::ChildStdout(_) => "childStdout",
- CliResource::ChildStderr(_) => "childStderr",
- }
- }
}
pub fn lock_resource_table<'a>() -> MutexGuard<'a, ResourceTable> {
@@ -319,13 +303,16 @@ impl DenoAsyncWrite for Resource {
pub fn add_fs_file(fs_file: tokio::fs::File) -> Resource {
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::FsFile(fs_file)));
+ let rid = table.add("fsFile", Box::new(CliResource::FsFile(fs_file)));
Resource { rid }
}
pub fn add_tcp_listener(listener: tokio::net::TcpListener) -> Resource {
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::TcpListener(listener, None)));
+ let rid = table.add(
+ "tcpListener",
+ Box::new(CliResource::TcpListener(listener, None)),
+ );
Resource { rid }
}
@@ -334,33 +321,41 @@ pub fn add_tls_listener(
acceptor: TlsAcceptor,
) -> Resource {
let mut table = lock_resource_table();
- let rid =
- table.add(Box::new(CliResource::TlsListener(listener, acceptor, None)));
+ let rid = table.add(
+ "tlsListener",
+ Box::new(CliResource::TlsListener(listener, acceptor, None)),
+ );
Resource { rid }
}
pub fn add_tcp_stream(stream: tokio::net::TcpStream) -> Resource {
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::TcpStream(stream)));
+ let rid = table.add("tcpStream", Box::new(CliResource::TcpStream(stream)));
Resource { rid }
}
pub fn add_tls_stream(stream: ClientTlsStream<TcpStream>) -> Resource {
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::ClientTlsStream(Box::new(stream))));
+ let rid = table.add(
+ "clientTlsStream",
+ Box::new(CliResource::ClientTlsStream(Box::new(stream))),
+ );
Resource { rid }
}
pub fn add_server_tls_stream(stream: ServerTlsStream<TcpStream>) -> Resource {
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::ServerTlsStream(Box::new(stream))));
+ let rid = table.add(
+ "serverTlsStream",
+ Box::new(CliResource::ServerTlsStream(Box::new(stream))),
+ );
Resource { rid }
}
pub fn add_reqwest_body(body: ReqwestDecoder) -> Resource {
let body = HttpBody::from(body);
let mut table = lock_resource_table();
- let rid = table.add(Box::new(CliResource::HttpBody(body)));
+ let rid = table.add("httpBody", Box::new(CliResource::HttpBody(body)));
Resource { rid }
}
@@ -383,21 +378,23 @@ pub fn add_child(mut child: tokio_process::Child) -> ChildResources {
if child.stdin().is_some() {
let stdin = child.stdin().take().unwrap();
- let rid = table.add(Box::new(CliResource::ChildStdin(stdin)));
+ let rid = table.add("childStdin", Box::new(CliResource::ChildStdin(stdin)));
resources.stdin_rid = Some(rid);
}
if child.stdout().is_some() {
let stdout = child.stdout().take().unwrap();
- let rid = table.add(Box::new(CliResource::ChildStdout(stdout)));
+ let rid =
+ table.add("childStdout", Box::new(CliResource::ChildStdout(stdout)));
resources.stdout_rid = Some(rid);
}
if child.stderr().is_some() {
let stderr = child.stderr().take().unwrap();
- let rid = table.add(Box::new(CliResource::ChildStderr(stderr)));
+ let rid =
+ table.add("childStderr", Box::new(CliResource::ChildStderr(stderr)));
resources.stderr_rid = Some(rid);
}
- let rid = table.add(Box::new(CliResource::Child(Box::new(child))));
+ let rid = table.add("child", Box::new(CliResource::Child(Box::new(child))));
resources.child_rid = Some(rid);
resources
@@ -440,7 +437,7 @@ pub fn get_file(rid: ResourceId) -> Result<std::fs::File, ErrBox> {
let mut table = lock_resource_table();
// We take ownership of File here.
// It is put back below while still holding the lock.
- let repr = table.map.remove(&rid).ok_or_else(bad_resource)?;
+ let (_name, repr) = table.map.remove(&rid).ok_or_else(bad_resource)?;
let repr = repr
.downcast::<CliResource>()
.or_else(|_| Err(bad_resource()))?;
@@ -460,7 +457,10 @@ pub fn get_file(rid: ResourceId) -> Result<std::fs::File, ErrBox> {
// Insert the entry back with the same rid.
table.map.insert(
rid,
- Box::new(CliResource::FsFile(tokio_fs::File::from_std(std_file))),
+ (
+ "fsFile".to_string(),
+ Box::new(CliResource::FsFile(tokio_fs::File::from_std(std_file))),
+ ),
);
maybe_std_file_copy.map_err(ErrBox::from)
diff --git a/cli/state.rs b/cli/state.rs
index 544c199b8..8d14042aa 100644
--- a/cli/state.rs
+++ b/cli/state.rs
@@ -196,7 +196,7 @@ impl ThreadSafeState {
};
let mut table = resources::lock_resource_table();
- let rid = table.add(Box::new(external_channels));
+ let rid = table.add("worker", Box::new(external_channels));
let import_map: Option<ImportMap> =
match global_state.flags.import_map_path.as_ref() {
diff --git a/cli/worker.rs b/cli/worker.rs
index 90fb95af7..6b4970015 100644
--- a/cli/worker.rs
+++ b/cli/worker.rs
@@ -31,11 +31,7 @@ pub struct WorkerChannels {
pub receiver: mpsc::Receiver<Buf>,
}
-impl CoreResource for WorkerChannels {
- fn inspect_repr(&self) -> &str {
- "worker"
- }
-}
+impl CoreResource for WorkerChannels {}
/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
/// high-level module loading.