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-08-14 11:03:02 -04:00
|
|
|
use crate::repl;
|
|
|
|
use crate::resources;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
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
|
|
|
|
|
|
|
pub fn op_repl_start(
|
|
|
|
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);
|
|
|
|
let history_path = repl::history_path(&state.dir, &args.history_file);
|
2019-08-14 11:03:02 -04:00
|
|
|
let repl = repl::Repl::new(history_path);
|
|
|
|
let resource = resources::add_repl(repl);
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!(resource.rid)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ReplReadlineArgs {
|
|
|
|
rid: i32,
|
|
|
|
prompt: String,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_repl_readline(
|
|
|
|
_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)?;
|
|
|
|
let rid = args.rid;
|
|
|
|
let prompt = args.prompt;
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("op_repl_readline {} {}", rid, prompt);
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
blocking_json(false, move || {
|
|
|
|
let repl = resources::get_repl(rid as u32)?;
|
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
|
|
|
})
|
|
|
|
}
|