refactor: split cli::Worker (#3735)

* cli::Worker is base struct to create specialized workers
* add MainWorker
* add CompilerWorker
* refactor WebWorker to use Worker
This commit is contained in:
Bartek Iwańczuk 2020-01-21 17:50:06 +01:00 committed by GitHub
parent 229eb292f8
commit ecd1d3abb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 299 additions and 280 deletions

View file

@ -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)
}
}

View file

@ -3,6 +3,7 @@ use deno_core::ErrBox;
use futures::Future;
use serde_json::Value;
mod compiler_worker;
mod js;
mod json;
mod ts;

View file

@ -1,4 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::compiler_worker::CompilerWorker;
use crate::compilers::CompilationResultFuture;
use crate::compilers::CompiledModule;
use crate::compilers::CompiledModuleFuture;
@ -13,7 +14,6 @@ use crate::source_maps::SourceMapGetter;
use crate::startup_data;
use crate::state::*;
use crate::version;
use crate::worker::Worker;
use deno_core::Buf;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
@ -228,7 +228,7 @@ impl TsCompiler {
}
/// Create a new V8 worker with snapshot of TS compiler and setup compiler's runtime.
fn setup_worker(global_state: ThreadSafeGlobalState) -> Worker {
fn setup_worker(global_state: ThreadSafeGlobalState) -> CompilerWorker {
let (int, ext) = ThreadSafeState::create_channels();
let worker_state =
ThreadSafeState::new(global_state.clone(), None, None, int)
@ -240,7 +240,7 @@ impl TsCompiler {
.compiler_starts
.fetch_add(1, Ordering::SeqCst);
let mut worker = Worker::new(
let mut worker = CompilerWorker::new(
"TS".to_string(),
startup_data::compiler_isolate_init(),
worker_state,
@ -279,7 +279,7 @@ impl TsCompiler {
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let maybe_msg = worker_.get_message().await?;
let maybe_msg = worker_.get_message().await;
debug!("Received message from worker");
if let Some(msg) = maybe_msg {
let json_str = std::str::from_utf8(&msg).unwrap();
@ -378,7 +378,7 @@ impl TsCompiler {
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let maybe_msg = worker_.get_message().await?;
let maybe_msg = worker_.get_message().await;
if let Some(msg) = maybe_msg {
let json_str = std::str::from_utf8(&msg).unwrap();
debug!("Message: {}", json_str);
@ -633,7 +633,7 @@ pub fn runtime_compile_async<S: BuildHasher>(
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let msg = (worker_.get_message().await?).unwrap();
let msg = (worker_.get_message().await).unwrap();
let json_str = std::str::from_utf8(&msg).unwrap();
Ok(json!(json_str))
}
@ -661,7 +661,7 @@ pub fn runtime_transpile_async<S: BuildHasher>(
worker.post_message(req_msg).await?;
worker.await?;
debug!("Sent message to worker");
let msg = (worker_.get_message().await?).unwrap();
let msg = (worker_.get_message().await).unwrap();
let json_str = std::str::from_utf8(&msg).unwrap();
Ok(json!(json_str))
}

View file

@ -1,11 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::compiler_worker::CompilerWorker;
use crate::compilers::CompiledModule;
use crate::compilers::CompiledModuleFuture;
use crate::file_fetcher::SourceFile;
use crate::global_state::ThreadSafeGlobalState;
use crate::startup_data;
use crate::state::*;
use crate::worker::Worker;
use futures::FutureExt;
use serde_derive::Deserialize;
use serde_json;
@ -42,7 +42,7 @@ pub struct WasmCompiler {
impl WasmCompiler {
/// Create a new V8 worker with snapshot of WASM compiler and setup compiler's runtime.
fn setup_worker(global_state: ThreadSafeGlobalState) -> Worker {
fn setup_worker(global_state: ThreadSafeGlobalState) -> CompilerWorker {
let (int, ext) = ThreadSafeState::create_channels();
let worker_state =
ThreadSafeState::new(global_state.clone(), None, None, int)
@ -54,7 +54,7 @@ impl WasmCompiler {
.compiler_starts
.fetch_add(1, Ordering::SeqCst);
let mut worker = Worker::new(
let mut worker = CompilerWorker::new(
"WASM".to_string(),
startup_data::compiler_isolate_init(),
worker_state,
@ -100,10 +100,9 @@ impl WasmCompiler {
std::process::exit(1);
}
debug!("Sent message to worker");
let maybe_msg = worker_.get_message().await.expect("not handled");
let json_msg = worker_.get_message().await.expect("not handled");
debug!("Received message from worker");
let json_msg = maybe_msg.unwrap();
let module_info: WasmModuleInfo =
serde_json::from_slice(&json_msg).unwrap();
debug!("WASM module info: {:#?}", &module_info);