mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
refactor: replace Arc<Box<..>> with Rc<..> (#3996)
This commit is contained in:
parent
87c329c45a
commit
a0f015b1a3
6 changed files with 33 additions and 33 deletions
|
@ -7,9 +7,9 @@ use dlopen::symbor::Library;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State, r: Arc<deno_core::OpRegistry>) {
|
pub fn init(i: &mut Isolate, s: &State, r: Rc<deno_core::OpRegistry>) {
|
||||||
let r_ = r;
|
let r_ = r;
|
||||||
i.register_op(
|
i.register_op(
|
||||||
"open_plugin",
|
"open_plugin",
|
||||||
|
@ -51,7 +51,7 @@ struct OpenPluginArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_open_plugin(
|
pub fn op_open_plugin(
|
||||||
registry: &Arc<deno_core::OpRegistry>,
|
registry: &Rc<deno_core::OpRegistry>,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
|
|
@ -18,6 +18,7 @@ use std::future::Future;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
@ -99,8 +100,8 @@ pub struct Worker {
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
||||||
let mut isolate =
|
let loader = Rc::new(state.clone());
|
||||||
deno_core::EsIsolate::new(Box::new(state.clone()), startup_data, false);
|
let mut isolate = deno_core::EsIsolate::new(loader, startup_data, false);
|
||||||
|
|
||||||
let global_state_ = state.borrow().global_state.clone();
|
let global_state_ = state.borrow().global_state.clone();
|
||||||
isolate.set_js_error_create(move |v8_exception| {
|
isolate.set_js_error_create(move |v8_exception| {
|
||||||
|
|
|
@ -21,7 +21,7 @@ use std::collections::HashMap;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ pub struct SourceCodeInfo {
|
||||||
/// loading of modules can be customized by the implementor.
|
/// loading of modules can be customized by the implementor.
|
||||||
pub struct EsIsolate {
|
pub struct EsIsolate {
|
||||||
core_isolate: Box<Isolate>,
|
core_isolate: Box<Isolate>,
|
||||||
loader: Arc<Box<dyn Loader + Unpin>>,
|
loader: Rc<dyn Loader + Unpin>,
|
||||||
pub modules: Modules,
|
pub modules: Modules,
|
||||||
pub(crate) next_dyn_import_id: DynImportId,
|
pub(crate) next_dyn_import_id: DynImportId,
|
||||||
pub(crate) dyn_import_map:
|
pub(crate) dyn_import_map:
|
||||||
|
@ -101,7 +101,7 @@ impl Drop for EsIsolate {
|
||||||
|
|
||||||
impl EsIsolate {
|
impl EsIsolate {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
loader: Box<dyn Loader + Unpin>,
|
loader: Rc<dyn Loader + Unpin>,
|
||||||
startup_data: StartupData,
|
startup_data: StartupData,
|
||||||
will_snapshot: bool,
|
will_snapshot: bool,
|
||||||
) -> Box<Self> {
|
) -> Box<Self> {
|
||||||
|
@ -118,7 +118,7 @@ impl EsIsolate {
|
||||||
|
|
||||||
let es_isolate = Self {
|
let es_isolate = Self {
|
||||||
modules: Modules::new(),
|
modules: Modules::new(),
|
||||||
loader: Arc::new(loader),
|
loader,
|
||||||
core_isolate,
|
core_isolate,
|
||||||
next_dyn_import_id: 0,
|
next_dyn_import_id: 0,
|
||||||
dyn_import_map: HashMap::new(),
|
dyn_import_map: HashMap::new(),
|
||||||
|
@ -606,6 +606,7 @@ pub mod tests {
|
||||||
use crate::ops::*;
|
use crate::ops::*;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mods() {
|
fn test_mods() {
|
||||||
|
@ -638,7 +639,7 @@ pub mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let loader = Box::new(ModsLoader::default());
|
let loader = Rc::new(ModsLoader::default());
|
||||||
let resolve_count = loader.count.clone();
|
let resolve_count = loader.count.clone();
|
||||||
let dispatch_count = Arc::new(AtomicUsize::new(0));
|
let dispatch_count = Arc::new(AtomicUsize::new(0));
|
||||||
let dispatch_count_ = dispatch_count.clone();
|
let dispatch_count_ = dispatch_count.clone();
|
||||||
|
@ -740,7 +741,7 @@ pub mod tests {
|
||||||
|
|
||||||
// Test an erroneous dynamic import where the specified module isn't found.
|
// Test an erroneous dynamic import where the specified module isn't found.
|
||||||
run_in_task(|cx| {
|
run_in_task(|cx| {
|
||||||
let loader = Box::new(DynImportErrLoader::default());
|
let loader = Rc::new(DynImportErrLoader::default());
|
||||||
let count = loader.count.clone();
|
let count = loader.count.clone();
|
||||||
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
||||||
|
|
||||||
|
@ -894,7 +895,7 @@ pub mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
run_in_task(|cx| {
|
run_in_task(|cx| {
|
||||||
let loader = Box::new(DynImportOkLoader::default());
|
let loader = Rc::new(DynImportOkLoader::default());
|
||||||
let resolve_count = loader.resolve_count.clone();
|
let resolve_count = loader.resolve_count.clone();
|
||||||
let load_count = loader.load_count.clone();
|
let load_count = loader.load_count.clone();
|
||||||
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
||||||
|
|
|
@ -29,6 +29,7 @@ use std::fmt;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::rc::Rc;
|
||||||
use std::sync::{Arc, Mutex, Once};
|
use std::sync::{Arc, Mutex, Once};
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
@ -175,7 +176,7 @@ pub struct Isolate {
|
||||||
pending_unref_ops: FuturesUnordered<PendingOpFuture>,
|
pending_unref_ops: FuturesUnordered<PendingOpFuture>,
|
||||||
have_unpolled_ops: bool,
|
have_unpolled_ops: bool,
|
||||||
startup_script: Option<OwnedScript>,
|
startup_script: Option<OwnedScript>,
|
||||||
pub op_registry: Arc<OpRegistry>,
|
pub op_registry: Rc<OpRegistry>,
|
||||||
waker: AtomicWaker,
|
waker: AtomicWaker,
|
||||||
error_handler: Option<Box<IsolateErrorHandleFn>>,
|
error_handler: Option<Box<IsolateErrorHandleFn>>,
|
||||||
}
|
}
|
||||||
|
@ -335,7 +336,7 @@ impl Isolate {
|
||||||
pending_unref_ops: FuturesUnordered::new(),
|
pending_unref_ops: FuturesUnordered::new(),
|
||||||
have_unpolled_ops: false,
|
have_unpolled_ops: false,
|
||||||
startup_script,
|
startup_script,
|
||||||
op_registry: Arc::new(OpRegistry::new()),
|
op_registry: Rc::new(OpRegistry::new()),
|
||||||
waker: AtomicWaker::new(),
|
waker: AtomicWaker::new(),
|
||||||
error_handler: None,
|
error_handler: None,
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,7 +16,7 @@ use std::collections::HashSet;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ pub struct RecursiveModuleLoad {
|
||||||
// Kind::Main
|
// Kind::Main
|
||||||
pub dyn_import_id: Option<DynImportId>,
|
pub dyn_import_id: Option<DynImportId>,
|
||||||
pub state: LoadState,
|
pub state: LoadState,
|
||||||
pub loader: Arc<Box<dyn Loader + Unpin>>,
|
pub loader: Rc<dyn Loader + Unpin>,
|
||||||
pub pending: FuturesUnordered<Pin<Box<SourceCodeInfoFuture>>>,
|
pub pending: FuturesUnordered<Pin<Box<SourceCodeInfoFuture>>>,
|
||||||
pub is_pending: HashSet<ModuleSpecifier>,
|
pub is_pending: HashSet<ModuleSpecifier>,
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ impl RecursiveModuleLoad {
|
||||||
pub fn main(
|
pub fn main(
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
code: Option<String>,
|
code: Option<String>,
|
||||||
loader: Arc<Box<dyn Loader + Unpin>>,
|
loader: Rc<dyn Loader + Unpin>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let kind = Kind::Main;
|
let kind = Kind::Main;
|
||||||
let state = LoadState::ResolveMain(specifier.to_owned(), code);
|
let state = LoadState::ResolveMain(specifier.to_owned(), code);
|
||||||
|
@ -95,7 +95,7 @@ impl RecursiveModuleLoad {
|
||||||
id: DynImportId,
|
id: DynImportId,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
referrer: &str,
|
referrer: &str,
|
||||||
loader: Arc<Box<dyn Loader + Unpin>>,
|
loader: Rc<dyn Loader + Unpin>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let kind = Kind::DynamicImport;
|
let kind = Kind::DynamicImport;
|
||||||
let state =
|
let state =
|
||||||
|
@ -110,7 +110,7 @@ impl RecursiveModuleLoad {
|
||||||
fn new(
|
fn new(
|
||||||
kind: Kind,
|
kind: Kind,
|
||||||
state: LoadState,
|
state: LoadState,
|
||||||
loader: Arc<Box<dyn Loader + Unpin>>,
|
loader: Rc<dyn Loader + Unpin>,
|
||||||
dyn_import_id: Option<DynImportId>,
|
dyn_import_id: Option<DynImportId>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -478,6 +478,7 @@ mod tests {
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
struct MockLoader {
|
struct MockLoader {
|
||||||
|
@ -651,8 +652,7 @@ mod tests {
|
||||||
fn test_recursive_load() {
|
fn test_recursive_load() {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let loads = loader.loads.clone();
|
let loads = loader.loads.clone();
|
||||||
let mut isolate =
|
let mut isolate = EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
||||||
let a_id_fut = isolate.load_module("/a.js", None);
|
let a_id_fut = isolate.load_module("/a.js", None);
|
||||||
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");
|
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");
|
||||||
|
|
||||||
|
@ -711,8 +711,7 @@ mod tests {
|
||||||
fn test_circular_load() {
|
fn test_circular_load() {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let loads = loader.loads.clone();
|
let loads = loader.loads.clone();
|
||||||
let mut isolate =
|
let mut isolate = EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
||||||
|
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let result = isolate.load_module("/circular1.js", None).await;
|
let result = isolate.load_module("/circular1.js", None).await;
|
||||||
|
@ -782,8 +781,7 @@ mod tests {
|
||||||
fn test_redirect_load() {
|
fn test_redirect_load() {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let loads = loader.loads.clone();
|
let loads = loader.loads.clone();
|
||||||
let mut isolate =
|
let mut isolate = EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
||||||
|
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let result = isolate.load_module("/redirect1.js", None).await;
|
let result = isolate.load_module("/redirect1.js", None).await;
|
||||||
|
@ -845,7 +843,7 @@ mod tests {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let loads = loader.loads.clone();
|
let loads = loader.loads.clone();
|
||||||
let mut isolate =
|
let mut isolate =
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
let mut recursive_load =
|
let mut recursive_load =
|
||||||
isolate.load_module("/main.js", None).boxed_local();
|
isolate.load_module("/main.js", None).boxed_local();
|
||||||
|
|
||||||
|
@ -891,7 +889,7 @@ mod tests {
|
||||||
run_in_task(|mut cx| {
|
run_in_task(|mut cx| {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let mut isolate =
|
let mut isolate =
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
let mut load_fut =
|
let mut load_fut =
|
||||||
isolate.load_module("/bad_import.js", None).boxed_local();
|
isolate.load_module("/bad_import.js", None).boxed_local();
|
||||||
let result = load_fut.poll_unpin(&mut cx);
|
let result = load_fut.poll_unpin(&mut cx);
|
||||||
|
@ -919,8 +917,7 @@ mod tests {
|
||||||
fn recursive_load_main_with_code() {
|
fn recursive_load_main_with_code() {
|
||||||
let loader = MockLoader::new();
|
let loader = MockLoader::new();
|
||||||
let loads = loader.loads.clone();
|
let loads = loader.loads.clone();
|
||||||
let mut isolate =
|
let mut isolate = EsIsolate::new(Rc::new(loader), StartupData::None, false);
|
||||||
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
||||||
// In default resolution code should be empty.
|
// In default resolution code should be empty.
|
||||||
// Instead we explicitly pass in our own code.
|
// Instead we explicitly pass in our own code.
|
||||||
// The behavior should be very similar to /a.js.
|
// The behavior should be very similar to /a.js.
|
||||||
|
|
|
@ -3,7 +3,7 @@ use crate::ZeroCopyBuf;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
pub type OpId = u32;
|
pub type OpId = u32;
|
||||||
|
@ -34,7 +34,7 @@ pub type OpDispatcher = dyn Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp + 'static;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct OpRegistry {
|
pub struct OpRegistry {
|
||||||
dispatchers: RwLock<Vec<Arc<Box<OpDispatcher>>>>,
|
dispatchers: RwLock<Vec<Rc<OpDispatcher>>>,
|
||||||
name_to_id: RwLock<HashMap<String, OpId>>,
|
name_to_id: RwLock<HashMap<String, OpId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ impl OpRegistry {
|
||||||
format!("Op already registered: {}", name)
|
format!("Op already registered: {}", name)
|
||||||
);
|
);
|
||||||
|
|
||||||
lock.push(Arc::new(Box::new(op)));
|
lock.push(Rc::new(op));
|
||||||
drop(name_lock);
|
drop(name_lock);
|
||||||
drop(lock);
|
drop(lock);
|
||||||
op_id
|
op_id
|
||||||
|
@ -90,7 +90,7 @@ impl OpRegistry {
|
||||||
}
|
}
|
||||||
let lock = self.dispatchers.read().unwrap();
|
let lock = self.dispatchers.read().unwrap();
|
||||||
if let Some(op) = lock.get(op_id as usize) {
|
if let Some(op) = lock.get(op_id as usize) {
|
||||||
let op_ = Arc::clone(&op);
|
let op_ = Rc::clone(&op);
|
||||||
// This should allow for changes to the dispatcher list during a call.
|
// This should allow for changes to the dispatcher list during a call.
|
||||||
drop(lock);
|
drop(lock);
|
||||||
Some(op_(control, zero_copy_buf))
|
Some(op_(control, zero_copy_buf))
|
||||||
|
|
Loading…
Add table
Reference in a new issue