mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
79b3bc05d6
* establish basic event loop for workers * make "self.close()" inside worker * remove "runWorkerMessageLoop() - instead manually call global function in Rust when message arrives. This is done in preparation for structured clone * refactor "WorkerChannel" and use distinct structs for internal and external channels; "WorkerChannelsInternal" and "WorkerHandle" * move "State.worker_channels_internal" to "Worker.internal_channels" * add "WorkerEvent" enum for child->host communication; currently "Message(Buf)" and "Error(ErrBox)" variants are supported * add tests for nested workers * add tests for worker throwing error on startup
61 lines
1.8 KiB
Rust
61 lines
1.8 KiB
Rust
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
use crate::ops;
|
|
use crate::state::State;
|
|
use crate::worker::Worker;
|
|
use deno_core;
|
|
use deno_core::StartupData;
|
|
use std::ops::Deref;
|
|
use std::ops::DerefMut;
|
|
|
|
/// 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
|
|
pub struct CompilerWorker(Worker);
|
|
|
|
impl CompilerWorker {
|
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
|
let state_ = state.clone();
|
|
let mut worker = Worker::new(name, startup_data, state_);
|
|
{
|
|
let isolate = &mut worker.isolate;
|
|
ops::runtime::init(isolate, &state);
|
|
ops::compiler::init(isolate, &state);
|
|
ops::web_worker::init(isolate, &state, &worker.internal_channels.sender);
|
|
ops::errors::init(isolate, &state);
|
|
// for compatibility with Worker scope, though unused at
|
|
// the moment
|
|
ops::timers::init(isolate, &state);
|
|
ops::fetch::init(isolate, &state);
|
|
// TODO(bartlomieju): CompilerWorker should not
|
|
// depend on those ops
|
|
ops::os::init(isolate, &state);
|
|
ops::files::init(isolate, &state);
|
|
ops::fs::init(isolate, &state);
|
|
ops::io::init(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
|
|
}
|
|
}
|