2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{blocking_json, Deserialize, JsonOp, Value};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::repl;
|
2019-10-28 20:42:44 -04:00
|
|
|
use crate::repl::Repl;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-05-29 17:41:39 -04:00
|
|
|
use deno_core::CoreIsolateState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-10-28 20:42:44 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-04-21 09:48:44 -04:00
|
|
|
i.register_op("op_repl_start", s.stateful_json_op2(op_repl_start));
|
|
|
|
i.register_op("op_repl_readline", s.stateful_json_op2(op_repl_readline));
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:42:44 -04:00
|
|
|
struct ReplResource(Arc<Mutex<Repl>>);
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ReplStartArgs {
|
|
|
|
history_file: String,
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_repl_start(
|
2020-05-29 17:41:39 -04:00
|
|
|
isolate_state: &mut CoreIsolateState,
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: ReplStartArgs = serde_json::from_value(args)?;
|
|
|
|
debug!("op_repl_start {}", args.history_file);
|
2019-11-04 10:38:52 -05:00
|
|
|
let history_path =
|
2020-02-08 14:34:31 -05:00
|
|
|
repl::history_path(&state.borrow().global_state.dir, &args.history_file);
|
2019-08-14 11:03:02 -04:00
|
|
|
let repl = repl::Repl::new(history_path);
|
2019-10-28 20:42:44 -04:00
|
|
|
let resource = ReplResource(Arc::new(Mutex::new(repl)));
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut resource_table = isolate_state.resource_table.borrow_mut();
|
2020-04-21 09:48:44 -04:00
|
|
|
let rid = resource_table.add("repl", Box::new(resource));
|
2019-10-28 20:42:44 -04:00
|
|
|
Ok(JsonOp::Sync(json!(rid)))
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ReplReadlineArgs {
|
|
|
|
rid: i32,
|
|
|
|
prompt: String,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_repl_readline(
|
2020-05-29 17:41:39 -04:00
|
|
|
isolate_state: &mut CoreIsolateState,
|
2020-04-21 09:48:44 -04:00
|
|
|
_state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: ReplReadlineArgs = serde_json::from_value(args)?;
|
2019-10-28 20:42:44 -04:00
|
|
|
let rid = args.rid as u32;
|
2019-08-26 08:50:21 -04:00
|
|
|
let prompt = args.prompt;
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("op_repl_readline {} {}", rid, prompt);
|
2020-05-29 17:41:39 -04:00
|
|
|
let resource_table = isolate_state.resource_table.borrow();
|
2020-04-21 09:48:44 -04:00
|
|
|
let resource = resource_table
|
2020-02-08 14:34:31 -05:00
|
|
|
.get::<ReplResource>(rid)
|
2020-03-05 08:30:41 -05:00
|
|
|
.ok_or_else(OpError::bad_resource_id)?;
|
2020-02-08 14:34:31 -05:00
|
|
|
let repl = resource.0.clone();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
blocking_json(false, move || {
|
2019-08-14 11:03:02 -04:00
|
|
|
let line = repl.lock().unwrap().readline(&prompt)?;
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(json!(line))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|