mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 02:48:24 +00:00
Make Rc/Arc wrapper around State/GlobalState visible (#7104)
This commit is contained in:
parent
de1007fc6a
commit
27f4aeb924
33 changed files with 368 additions and 430 deletions
|
@ -92,19 +92,23 @@ pub struct Worker {
|
|||
pub name: String,
|
||||
pub isolate: deno_core::EsIsolate,
|
||||
pub inspector: Option<Box<DenoInspector>>,
|
||||
pub state: State,
|
||||
pub state: Rc<State>,
|
||||
pub waker: AtomicWaker,
|
||||
pub(crate) internal_channels: WorkerChannelsInternal,
|
||||
external_channels: WorkerHandle,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
||||
let loader = Rc::new(state.clone());
|
||||
let mut isolate = deno_core::EsIsolate::new(loader, startup_data, false);
|
||||
pub fn new(
|
||||
name: String,
|
||||
startup_data: StartupData,
|
||||
state: &Rc<State>,
|
||||
) -> Self {
|
||||
let mut isolate =
|
||||
deno_core::EsIsolate::new(state.clone(), startup_data, false);
|
||||
|
||||
{
|
||||
let global_state = state.borrow().global_state.clone();
|
||||
let global_state = state.global_state.clone();
|
||||
let core_state_rc = CoreIsolate::state(&isolate);
|
||||
let mut core_state = core_state_rc.borrow_mut();
|
||||
core_state.set_js_error_create_fn(move |core_js_error| {
|
||||
|
@ -113,7 +117,6 @@ impl Worker {
|
|||
}
|
||||
|
||||
let inspector = {
|
||||
let state = state.borrow();
|
||||
let global_state = &state.global_state;
|
||||
global_state
|
||||
.flags
|
||||
|
@ -129,7 +132,7 @@ impl Worker {
|
|||
name,
|
||||
isolate,
|
||||
inspector,
|
||||
state,
|
||||
state: state.clone(),
|
||||
waker: AtomicWaker::new(),
|
||||
internal_channels,
|
||||
external_channels,
|
||||
|
@ -193,8 +196,7 @@ impl Worker {
|
|||
|
||||
fn wait_for_inspector_session(&mut self) {
|
||||
let should_break_on_first_statement = self.inspector.is_some() && {
|
||||
let state = self.state.borrow();
|
||||
state.is_main && state.global_state.flags.inspect_brk.is_some()
|
||||
self.state.is_main && self.state.global_state.flags.inspect_brk.is_some()
|
||||
};
|
||||
if should_break_on_first_statement {
|
||||
self
|
||||
|
@ -250,9 +252,8 @@ pub struct MainWorker(Worker);
|
|||
|
||||
impl MainWorker {
|
||||
// TODO(ry) combine MainWorker::new and MainWorker::create.
|
||||
fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
||||
let state_ = state.clone();
|
||||
let mut worker = Worker::new(name, startup_data, state_);
|
||||
fn new(name: String, startup_data: StartupData, state: &Rc<State>) -> Self {
|
||||
let mut worker = Worker::new(name, startup_data, state);
|
||||
{
|
||||
let isolate = &mut worker.isolate;
|
||||
ops::runtime::init(isolate, &state);
|
||||
|
@ -281,11 +282,11 @@ impl MainWorker {
|
|||
}
|
||||
|
||||
pub fn create(
|
||||
global_state: GlobalState,
|
||||
global_state: &Arc<GlobalState>,
|
||||
main_module: ModuleSpecifier,
|
||||
) -> Result<MainWorker, ErrBox> {
|
||||
let state = State::new(
|
||||
global_state.clone(),
|
||||
&global_state,
|
||||
None,
|
||||
main_module,
|
||||
global_state.maybe_import_map.clone(),
|
||||
|
@ -294,7 +295,7 @@ impl MainWorker {
|
|||
let mut worker = MainWorker::new(
|
||||
"main".to_string(),
|
||||
startup_data::deno_isolate_init(),
|
||||
state,
|
||||
&state,
|
||||
);
|
||||
{
|
||||
let (stdin, stdout, stderr) = get_stdio();
|
||||
|
@ -335,7 +336,6 @@ mod tests {
|
|||
use crate::flags;
|
||||
use crate::global_state::GlobalState;
|
||||
use crate::startup_data;
|
||||
use crate::state::State;
|
||||
use crate::tokio_util;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
|
@ -349,12 +349,11 @@ mod tests {
|
|||
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
||||
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
||||
let state =
|
||||
State::new(global_state, None, module_specifier.clone(), None, false)
|
||||
State::new(&global_state, None, module_specifier.clone(), None, false)
|
||||
.unwrap();
|
||||
let state_ = state.clone();
|
||||
tokio_util::run_basic(async move {
|
||||
tokio_util::run_basic(async {
|
||||
let mut worker =
|
||||
MainWorker::new("TEST".to_string(), StartupData::None, state);
|
||||
MainWorker::new("TEST".to_string(), StartupData::None, &state);
|
||||
let result = worker.execute_module(&module_specifier).await;
|
||||
if let Err(err) = result {
|
||||
eprintln!("execute_mod err {:?}", err);
|
||||
|
@ -363,8 +362,7 @@ mod tests {
|
|||
panic!("Future got unexpected error: {:?}", e);
|
||||
}
|
||||
});
|
||||
let state = state_.borrow();
|
||||
assert_eq!(state.metrics.resolve_count, 2);
|
||||
assert_eq!(state.metrics.borrow().resolve_count, 2);
|
||||
// Check that we didn't start the compiler.
|
||||
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
@ -379,12 +377,11 @@ mod tests {
|
|||
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
||||
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
||||
let state =
|
||||
State::new(global_state, None, module_specifier.clone(), None, false)
|
||||
State::new(&global_state, None, module_specifier.clone(), None, false)
|
||||
.unwrap();
|
||||
let state_ = state.clone();
|
||||
tokio_util::run_basic(async move {
|
||||
tokio_util::run_basic(async {
|
||||
let mut worker =
|
||||
MainWorker::new("TEST".to_string(), StartupData::None, state);
|
||||
MainWorker::new("TEST".to_string(), StartupData::None, &state);
|
||||
let result = worker.execute_module(&module_specifier).await;
|
||||
if let Err(err) = result {
|
||||
eprintln!("execute_mod err {:?}", err);
|
||||
|
@ -394,7 +391,6 @@ mod tests {
|
|||
}
|
||||
});
|
||||
|
||||
let state = state_.borrow();
|
||||
// Check that we didn't start the compiler.
|
||||
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
@ -416,18 +412,13 @@ mod tests {
|
|||
..flags::Flags::default()
|
||||
};
|
||||
let global_state = GlobalState::new(flags).unwrap();
|
||||
let state = State::new(
|
||||
global_state.clone(),
|
||||
None,
|
||||
module_specifier.clone(),
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
let state =
|
||||
State::new(&global_state, None, module_specifier.clone(), None, false)
|
||||
.unwrap();
|
||||
let mut worker = MainWorker::new(
|
||||
"TEST".to_string(),
|
||||
startup_data::deno_isolate_init(),
|
||||
state.clone(),
|
||||
&state,
|
||||
);
|
||||
worker.execute("bootstrap.mainRuntime()").unwrap();
|
||||
let result = worker.execute_module(&module_specifier).await;
|
||||
|
@ -437,8 +428,7 @@ mod tests {
|
|||
if let Err(e) = (&mut *worker).await {
|
||||
panic!("Future got unexpected error: {:?}", e);
|
||||
}
|
||||
let state = state.borrow();
|
||||
assert_eq!(state.metrics.resolve_count, 3);
|
||||
assert_eq!(state.metrics.borrow().resolve_count, 3);
|
||||
// Check that we've only invoked the compiler once.
|
||||
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
@ -448,7 +438,7 @@ mod tests {
|
|||
let mut worker = MainWorker::new(
|
||||
"TEST".to_string(),
|
||||
startup_data::deno_isolate_init(),
|
||||
state,
|
||||
&state,
|
||||
);
|
||||
worker.execute("bootstrap.mainRuntime()").unwrap();
|
||||
worker
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue