2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 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};
|
2019-10-28 20:42:44 -04:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::repl;
|
2019-10-28 20:42:44 -04:00
|
|
|
use crate::repl::Repl;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-11-14 12:10:25 -05:00
|
|
|
use deno::Resource;
|
2019-08-14 11:03:02 -04:00
|
|
|
use deno::*;
|
2019-10-28 20:42:44 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op(
|
|
|
|
"repl_start",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_repl_start))),
|
|
|
|
);
|
|
|
|
i.register_op(
|
|
|
|
"repl_readline",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_repl_readline))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:42:44 -04:00
|
|
|
struct ReplResource(Arc<Mutex<Repl>>);
|
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
impl Resource for ReplResource {}
|
2019-10-28 20:42:44 -04:00
|
|
|
|
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(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ReplStartArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
debug!("op_repl_start {}", args.history_file);
|
2019-11-04 10:38:52 -05:00
|
|
|
let history_path =
|
|
|
|
repl::history_path(&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)));
|
2019-11-14 12:10:25 -05:00
|
|
|
let mut table = state.lock_resource_table();
|
2019-11-06 12:17:28 -05:00
|
|
|
let rid = 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(
|
2019-11-14 12:10:25 -05:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
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);
|
2019-11-14 12:10:25 -05:00
|
|
|
let state = state.clone();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
blocking_json(false, move || {
|
2019-11-14 12:10:25 -05:00
|
|
|
let table = state.lock_resource_table();
|
2019-10-28 20:42:44 -04:00
|
|
|
let resource = table.get::<ReplResource>(rid).ok_or_else(bad_resource)?;
|
|
|
|
let repl = resource.0.clone();
|
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
|
|
|
})
|
|
|
|
}
|