1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/cli/ops/repl.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

// 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};
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,
}
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-26 08:50:21 -04:00
debug!("op_repl_start {}", args.history_file);
let history_path = repl::history_path(&state.dir, &args.history_file);
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,
}
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;
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)?;
let line = repl.lock().unwrap().readline(&prompt)?;
2019-08-26 08:50:21 -04:00
Ok(json!(line))
})
}