summaryrefslogtreecommitdiff
path: root/cli/ops/repl.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-02-08 20:34:31 +0100
committerGitHub <noreply@github.com>2020-02-08 20:34:31 +0100
commitcdba5ab6fc633606aaa6f95d0825832c3ac6fe5c (patch)
treee8dee2801e14b65b2da6aca62e39cd3d3ac2a786 /cli/ops/repl.rs
parent619a24390ff15d5ea5e577a4d0391823f94e8592 (diff)
refactor: rename ThreadSafeState, use RefCell for mutable state (#3931)
* rename ThreadSafeState to State * State stores InnerState wrapped in Rc and RefCell
Diffstat (limited to 'cli/ops/repl.rs')
-rw-r--r--cli/ops/repl.rs24
1 files changed, 13 insertions, 11 deletions
diff --git a/cli/ops/repl.rs b/cli/ops/repl.rs
index 9b47f262a..d7c94a56e 100644
--- a/cli/ops/repl.rs
+++ b/cli/ops/repl.rs
@@ -4,12 +4,12 @@ use crate::deno_error::bad_resource;
use crate::ops::json_op;
use crate::repl;
use crate::repl::Repl;
-use crate::state::ThreadSafeState;
+use crate::state::State;
use deno_core::*;
use std::sync::Arc;
use std::sync::Mutex;
-pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
+pub fn init(i: &mut Isolate, s: &State) {
i.register_op(
"repl_start",
s.core_op(json_op(s.stateful_op(op_repl_start))),
@@ -29,7 +29,7 @@ struct ReplStartArgs {
}
fn op_repl_start(
- state: &ThreadSafeState,
+ state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
@@ -37,11 +37,11 @@ fn op_repl_start(
debug!("op_repl_start {}", args.history_file);
let history_path =
- repl::history_path(&state.global_state.dir, &args.history_file);
+ repl::history_path(&state.borrow().global_state.dir, &args.history_file);
let repl = repl::Repl::new(history_path);
+ let mut state = state.borrow_mut();
let resource = ReplResource(Arc::new(Mutex::new(repl)));
- let mut table = state.lock_resource_table();
- let rid = table.add("repl", Box::new(resource));
+ let rid = state.resource_table.add("repl", Box::new(resource));
Ok(JsonOp::Sync(json!(rid)))
}
@@ -52,7 +52,7 @@ struct ReplReadlineArgs {
}
fn op_repl_readline(
- state: &ThreadSafeState,
+ state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
@@ -60,12 +60,14 @@ fn op_repl_readline(
let rid = args.rid as u32;
let prompt = args.prompt;
debug!("op_repl_readline {} {}", rid, prompt);
- let state = state.clone();
+ let state = state.borrow();
+ let resource = state
+ .resource_table
+ .get::<ReplResource>(rid)
+ .ok_or_else(bad_resource)?;
+ let repl = resource.0.clone();
blocking_json(false, move || {
- let table = state.lock_resource_table();
- let resource = table.get::<ReplResource>(rid).ok_or_else(bad_resource)?;
- let repl = resource.0.clone();
let line = repl.lock().unwrap().readline(&prompt)?;
Ok(json!(line))
})