2020-01-21 11:50:06 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::ops;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-02-18 14:47:11 -05:00
|
|
|
use crate::web_worker::WebWorker;
|
|
|
|
use core::task::Context;
|
2020-01-21 11:50:06 -05:00
|
|
|
use deno_core;
|
2020-02-18 14:47:11 -05:00
|
|
|
use deno_core::ErrBox;
|
2020-01-21 11:50:06 -05:00
|
|
|
use deno_core::StartupData;
|
2020-02-18 14:47:11 -05:00
|
|
|
use futures::future::Future;
|
|
|
|
use futures::future::FutureExt;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2020-02-18 14:47:11 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Poll;
|
2020-01-21 11:50:06 -05:00
|
|
|
|
|
|
|
/// 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
|
2020-02-18 14:47:11 -05:00
|
|
|
pub struct CompilerWorker(WebWorker);
|
2020-01-21 11:50:06 -05:00
|
|
|
|
|
|
|
impl CompilerWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
2020-01-21 11:50:06 -05:00
|
|
|
let state_ = state.clone();
|
2020-02-18 14:47:11 -05:00
|
|
|
let mut worker = WebWorker::new(name, startup_data, state_);
|
2020-01-21 11:50:06 -05:00
|
|
|
{
|
2020-02-03 18:08:44 -05:00
|
|
|
let isolate = &mut worker.isolate;
|
|
|
|
ops::compiler::init(isolate, &state);
|
2020-01-21 11:50:06 -05:00
|
|
|
// TODO(bartlomieju): CompilerWorker should not
|
|
|
|
// depend on those ops
|
2020-02-03 18:08:44 -05:00
|
|
|
ops::os::init(isolate, &state);
|
|
|
|
ops::fs::init(isolate, &state);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
Self(worker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for CompilerWorker {
|
2020-02-18 14:47:11 -05:00
|
|
|
type Target = WebWorker;
|
2020-01-21 11:50:06 -05:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for CompilerWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 14:47:11 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|