2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::repl;
|
2019-10-28 20:42:44 -04:00
|
|
|
use crate::repl::Repl;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
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-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_repl_start", op_repl_start);
|
|
|
|
super::reg_json_async(rt, "op_repl_readline", 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-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: ReplStartArgs = serde_json::from_value(args)?;
|
|
|
|
debug!("op_repl_start {}", args.history_file);
|
2020-09-10 09:57:45 -04:00
|
|
|
let history_path = {
|
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
repl::history_path(&cli_state.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-09-10 09:57:45 -04:00
|
|
|
let rid = state.resource_table.add("repl", Box::new(resource));
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(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
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_repl_readline(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
|
|
|
) -> Result<Value, ErrBox> {
|
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-09-10 09:57:45 -04:00
|
|
|
let repl = {
|
|
|
|
let state = state.borrow();
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
|
|
|
.get::<ReplResource>(rid)
|
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
|
|
|
resource.0.clone()
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(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
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|