summaryrefslogtreecommitdiff
path: root/cli/compilers/compiler_worker.rs
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/compilers/compiler_worker.rs
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/compilers/compiler_worker.rs')
-rw-r--r--cli/compilers/compiler_worker.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/cli/compilers/compiler_worker.rs b/cli/compilers/compiler_worker.rs
new file mode 100644
index 000000000..461194c37
--- /dev/null
+++ b/cli/compilers/compiler_worker.rs
@@ -0,0 +1,78 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use crate::ops;
+use crate::state::ThreadSafeState;
+use crate::worker::Worker;
+use crate::worker::WorkerChannels;
+use deno_core;
+use deno_core::ErrBox;
+use deno_core::StartupData;
+use futures::future::FutureExt;
+use std::future::Future;
+use std::ops::Deref;
+use std::ops::DerefMut;
+use std::pin::Pin;
+use std::task::Context;
+use std::task::Poll;
+
+/// This worker is used to host TypeScript and WASM compilers.
+///
+/// It provides minimal set of ops that are necessary to facilitate
+/// compilation.
+///
+/// NOTE: This worker is considered priveleged, because it may
+/// access file system without permission check.
+///
+/// At the moment this worker is meant to be single-use - after
+/// performing single compilation/bundling it should be destroyed.
+///
+/// TODO(bartlomieju): add support to reuse the worker - or in other
+/// words support stateful TS compiler
+#[derive(Clone)]
+pub struct CompilerWorker(Worker);
+
+impl CompilerWorker {
+ pub fn new(
+ name: String,
+ startup_data: StartupData,
+ state: ThreadSafeState,
+ external_channels: WorkerChannels,
+ ) -> Self {
+ let state_ = state.clone();
+ let worker = Worker::new(name, startup_data, state_, external_channels);
+ {
+ let mut isolate = worker.isolate.try_lock().unwrap();
+ ops::compiler::init(&mut isolate, &state);
+ ops::web_worker::init(&mut isolate, &state);
+ // TODO(bartlomieju): CompilerWorker should not
+ // depend on those ops
+ ops::os::init(&mut isolate, &state);
+ ops::files::init(&mut isolate, &state);
+ ops::fs::init(&mut isolate, &state);
+ ops::io::init(&mut isolate, &state);
+ }
+
+ Self(worker)
+ }
+}
+
+impl Deref for CompilerWorker {
+ type Target = Worker;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl DerefMut for CompilerWorker {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl Future for CompilerWorker {
+ type Output = Result<(), ErrBox>;
+
+ fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
+ let inner = self.get_mut();
+ inner.0.poll_unpin(cx)
+ }
+}