mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
4e1abb4f3a
To better reflect changes in error types in JS from #3662 this PR changes default error type used in ops from "ErrBox" to "OpError". "OpError" is a type that can be sent over to JSON; it has all information needed to construct error in JavaScript. That made "GetErrorKind" trait useless and so it was removed altogether. To provide compatibility with previous use of "ErrBox" an implementation of "From<ErrBox> for OpError" was added, however, it is an escape hatch and ops implementors should strive to use "OpError" directly.
74 lines
1.9 KiB
Rust
74 lines
1.9 KiB
Rust
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
use super::dispatch_json::{blocking_json, Deserialize, JsonOp, Value};
|
|
use crate::op_error::OpError;
|
|
use crate::ops::json_op;
|
|
use crate::repl;
|
|
use crate::repl::Repl;
|
|
use crate::state::State;
|
|
use deno_core::*;
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
|
|
pub fn init(i: &mut Isolate, s: &State) {
|
|
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))),
|
|
);
|
|
}
|
|
|
|
struct ReplResource(Arc<Mutex<Repl>>);
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct ReplStartArgs {
|
|
history_file: String,
|
|
}
|
|
|
|
fn op_repl_start(
|
|
state: &State,
|
|
args: Value,
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
|
) -> Result<JsonOp, OpError> {
|
|
let args: ReplStartArgs = serde_json::from_value(args)?;
|
|
|
|
debug!("op_repl_start {}", args.history_file);
|
|
let history_path =
|
|
repl::history_path(&state.borrow().global_state.dir, &args.history_file);
|
|
let repl = repl::Repl::new(history_path);
|
|
let mut state = state.borrow_mut();
|
|
let resource = ReplResource(Arc::new(Mutex::new(repl)));
|
|
let rid = state.resource_table.add("repl", Box::new(resource));
|
|
Ok(JsonOp::Sync(json!(rid)))
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct ReplReadlineArgs {
|
|
rid: i32,
|
|
prompt: String,
|
|
}
|
|
|
|
fn op_repl_readline(
|
|
state: &State,
|
|
args: Value,
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
|
) -> Result<JsonOp, OpError> {
|
|
let args: ReplReadlineArgs = serde_json::from_value(args)?;
|
|
let rid = args.rid as u32;
|
|
let prompt = args.prompt;
|
|
debug!("op_repl_readline {} {}", rid, prompt);
|
|
let state = state.borrow();
|
|
let resource = state
|
|
.resource_table
|
|
.get::<ReplResource>(rid)
|
|
.ok_or_else(OpError::bad_resource)?;
|
|
let repl = resource.0.clone();
|
|
|
|
blocking_json(false, move || {
|
|
let line = repl.lock().unwrap().readline(&prompt)?;
|
|
Ok(json!(line))
|
|
})
|
|
}
|