summaryrefslogtreecommitdiff
path: root/cli/ops
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-01-21 17:50:06 +0100
committerGitHub <noreply@github.com>2020-01-21 17:50:06 +0100
commitecd1d3abb0cae9c7cbc1330cbaa035a5786e94d7 (patch)
tree73d4b12a06f5f6a6bc0e7168e0021efc3a2eda7c /cli/ops
parent229eb292f83dedbc32dc24f912841caf79a53e9c (diff)
refactor: split cli::Worker (#3735)
* cli::Worker is base struct to create specialized workers * add MainWorker * add CompilerWorker * refactor WebWorker to use Worker
Diffstat (limited to 'cli/ops')
-rw-r--r--cli/ops/compiler.rs48
-rw-r--r--cli/ops/mod.rs1
-rw-r--r--cli/ops/runtime_compiler.rs56
-rw-r--r--cli/ops/web_worker.rs30
-rw-r--r--cli/ops/worker_host.rs28
5 files changed, 69 insertions, 94 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs
index 5d6875fb0..e515081df 100644
--- a/cli/ops/compiler.rs
+++ b/cli/ops/compiler.rs
@@ -1,14 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
-use crate::compilers::runtime_compile_async;
-use crate::compilers::runtime_transpile_async;
use crate::futures::future::try_join_all;
use crate::msg;
use crate::ops::json_op;
use crate::state::ThreadSafeState;
use deno_core::Loader;
use deno_core::*;
-use std::collections::HashMap;
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
i.register_op("cache", s.core_op(json_op(s.stateful_op(op_cache))));
@@ -20,8 +17,6 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
"fetch_source_files",
s.core_op(json_op(s.stateful_op(op_fetch_source_files))),
);
- i.register_op("compile", s.core_op(json_op(s.stateful_op(op_compile))));
- i.register_op("transpile", s.core_op(json_op(s.stateful_op(op_transpile))));
}
#[derive(Deserialize)]
@@ -150,46 +145,3 @@ fn op_fetch_source_files(
Ok(JsonOp::Async(future))
}
-
-#[derive(Deserialize, Debug)]
-#[serde(rename_all = "camelCase")]
-struct CompileArgs {
- root_name: String,
- sources: Option<HashMap<String, String>>,
- bundle: bool,
- options: Option<String>,
-}
-
-fn op_compile(
- state: &ThreadSafeState,
- args: Value,
- _zero_copy: Option<PinnedBuf>,
-) -> Result<JsonOp, ErrBox> {
- let args: CompileArgs = serde_json::from_value(args)?;
- Ok(JsonOp::Async(runtime_compile_async(
- state.global_state.clone(),
- &args.root_name,
- &args.sources,
- args.bundle,
- &args.options,
- )))
-}
-
-#[derive(Deserialize, Debug)]
-struct TranspileArgs {
- sources: HashMap<String, String>,
- options: Option<String>,
-}
-
-fn op_transpile(
- state: &ThreadSafeState,
- args: Value,
- _zero_copy: Option<PinnedBuf>,
-) -> Result<JsonOp, ErrBox> {
- let args: TranspileArgs = serde_json::from_value(args)?;
- Ok(JsonOp::Async(runtime_transpile_async(
- state.global_state.clone(),
- &args.sources,
- &args.options,
- )))
-}
diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs
index 203d1e17e..81f95ffb9 100644
--- a/cli/ops/mod.rs
+++ b/cli/ops/mod.rs
@@ -21,6 +21,7 @@ pub mod process;
pub mod random;
pub mod repl;
pub mod resources;
+pub mod runtime_compiler;
pub mod timers;
pub mod tls;
pub mod web_worker;
diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs
new file mode 100644
index 000000000..4202f6b3c
--- /dev/null
+++ b/cli/ops/runtime_compiler.rs
@@ -0,0 +1,56 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use super::dispatch_json::{Deserialize, JsonOp, Value};
+use crate::compilers::runtime_compile_async;
+use crate::compilers::runtime_transpile_async;
+use crate::ops::json_op;
+use crate::state::ThreadSafeState;
+use deno_core::*;
+use std::collections::HashMap;
+
+pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
+ i.register_op("compile", s.core_op(json_op(s.stateful_op(op_compile))));
+ i.register_op("transpile", s.core_op(json_op(s.stateful_op(op_transpile))));
+}
+
+#[derive(Deserialize, Debug)]
+#[serde(rename_all = "camelCase")]
+struct CompileArgs {
+ root_name: String,
+ sources: Option<HashMap<String, String>>,
+ bundle: bool,
+ options: Option<String>,
+}
+
+fn op_compile(
+ state: &ThreadSafeState,
+ args: Value,
+ _zero_copy: Option<PinnedBuf>,
+) -> Result<JsonOp, ErrBox> {
+ let args: CompileArgs = serde_json::from_value(args)?;
+ Ok(JsonOp::Async(runtime_compile_async(
+ state.global_state.clone(),
+ &args.root_name,
+ &args.sources,
+ args.bundle,
+ &args.options,
+ )))
+}
+
+#[derive(Deserialize, Debug)]
+struct TranspileArgs {
+ sources: HashMap<String, String>,
+ options: Option<String>,
+}
+
+fn op_transpile(
+ state: &ThreadSafeState,
+ args: Value,
+ _zero_copy: Option<PinnedBuf>,
+) -> Result<JsonOp, ErrBox> {
+ let args: TranspileArgs = serde_json::from_value(args)?;
+ Ok(JsonOp::Async(runtime_transpile_async(
+ state.global_state.clone(),
+ &args.sources,
+ &args.options,
+ )))
+}
diff --git a/cli/ops/web_worker.rs b/cli/ops/web_worker.rs
index 300a0dfd1..be987c09f 100644
--- a/cli/ops/web_worker.rs
+++ b/cli/ops/web_worker.rs
@@ -11,10 +11,6 @@ use futures::sink::SinkExt;
use futures::stream::StreamExt;
use std;
use std::convert::From;
-use std::future::Future;
-use std::pin::Pin;
-use std::task::Context;
-use std::task::Poll;
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
i.register_op(
@@ -27,33 +23,16 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
);
}
-struct GetMessageFuture {
- state: ThreadSafeState,
-}
-
-impl Future for GetMessageFuture {
- type Output = Option<Buf>;
-
- fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
- let inner = self.get_mut();
- let mut channels = inner.state.worker_channels.lock().unwrap();
- let receiver = &mut channels.receiver;
- receiver.poll_next_unpin(cx)
- }
-}
-
/// Get message from host as guest worker
fn op_worker_get_message(
state: &ThreadSafeState,
_args: Value,
_data: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
- let op = GetMessageFuture {
- state: state.clone(),
- };
-
+ let state_ = state.clone();
let op = async move {
- let maybe_buf = op.await;
+ let mut receiver = state_.worker_channels.receiver.lock().await;
+ let maybe_buf = receiver.next().await;
debug!("op_worker_get_message");
Ok(json!({ "data": maybe_buf }))
};
@@ -68,8 +47,7 @@ fn op_worker_post_message(
data: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice();
- let mut channels = state.worker_channels.lock().unwrap();
- let sender = &mut channels.sender;
+ let mut sender = state.worker_channels.sender.clone();
futures::executor::block_on(sender.send(d))
.map_err(|e| DenoError::new(ErrorKind::Other, e.to_string()))?;
diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs
index 6ac48228d..c64e86c1c 100644
--- a/cli/ops/worker_host.rs
+++ b/cli/ops/worker_host.rs
@@ -57,21 +57,6 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
i.register_op("metrics", s.core_op(json_op(s.stateful_op(op_metrics))));
}
-struct GetMessageFuture {
- state: ThreadSafeState,
-}
-
-impl Future for GetMessageFuture {
- type Output = Option<Buf>;
-
- fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
- let inner = self.get_mut();
- let mut channels = inner.state.worker_channels.lock().unwrap();
- let receiver = &mut channels.receiver;
- receiver.poll_next_unpin(cx)
- }
-}
-
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CreateWorkerArgs {
@@ -250,9 +235,12 @@ fn op_host_close_worker(
let mut workers_table = state_.workers.lock().unwrap();
let maybe_worker = workers_table.remove(&id);
if let Some(worker) = maybe_worker {
- let mut channels = worker.state.worker_channels.lock().unwrap();
- channels.sender.close_channel();
- channels.receiver.close();
+ let channels = worker.state.worker_channels.clone();
+ let mut sender = channels.sender.clone();
+ sender.close_channel();
+
+ let mut receiver = futures::executor::block_on(channels.receiver.lock());
+ receiver.close();
};
Ok(JsonOp::Sync(json!({})))
@@ -285,9 +273,9 @@ fn op_host_get_message(
_data: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: HostGetMessageArgs = serde_json::from_value(args)?;
-
+ let state_ = state.clone();
let id = args.id as u32;
- let mut table = state.workers.lock().unwrap();
+ let mut table = state_.workers.lock().unwrap();
// TODO: don't return bad resource anymore
let worker = table.get_mut(&id).ok_or_else(bad_resource)?;
let fut = worker.get_message();