Browse Source

Better error formatting and cli color (#586)

pull/590/head
HalidOdat 4 years ago committed by GitHub
parent
commit
795adc519a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      Cargo.lock
  2. 8
      boa/src/builtins/value/display.rs
  3. 1
      boa_cli/Cargo.toml
  4. 7
      boa_cli/src/main.rs

12
Cargo.lock generated

@ -97,6 +97,7 @@ name = "boa_cli"
version = "0.9.0"
dependencies = [
"Boa",
"colored",
"jemallocator",
"rustyline",
"serde_json",
@ -180,6 +181,17 @@ dependencies = [
"bitflags",
]
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"

8
boa/src/builtins/value/display.rs

@ -175,6 +175,14 @@ pub(crate) fn display_obj(v: &Value, print_internals: bool) -> String {
// in-memory address in this set
let mut encounters = HashSet::new();
if let Value::Object(object) = v {
if object.borrow().is_error() {
let name = v.get_field("name");
let message = v.get_field("message");
return format!("{}: {}", name, message);
}
}
fn display_obj_internal(
data: &Value,
encounters: &mut HashSet<usize>,

1
boa_cli/Cargo.toml

@ -15,6 +15,7 @@ Boa = { path = "../boa", features = ["serde"] }
rustyline = "6.2.0"
structopt = "0.3.15"
serde_json = "1.0.56"
colored = "2.0.0"
[target.x86_64-unknown-linux-gnu.dependencies]
jemallocator = "0.3.2"

7
boa_cli/src/main.rs

@ -31,6 +31,7 @@ use boa::{
realm::Realm,
syntax::ast::{node::StatementList, token::Token},
};
use colored::*;
use rustyline::{config::Config, error::ReadlineError, EditMode, Editor};
use std::{fs::read_to_string, path::PathBuf};
use structopt::{clap::arg_enum, StructOpt};
@ -208,8 +209,10 @@ pub fn main() -> Result<(), std::io::Error> {
let mut editor = Editor::<()>::with_config(config);
let _ = editor.load_history(CLI_HISTORY);
let readline = "> ".cyan().bold().to_string();
loop {
match editor.readline("> ") {
match editor.readline(&readline) {
Ok(line) if line == ".exit" => break,
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
@ -223,7 +226,7 @@ pub fn main() -> Result<(), std::io::Error> {
} else {
match forward_val(&mut engine, line.trim_end()) {
Ok(v) => println!("{}", v),
Err(v) => eprintln!("{}", v),
Err(v) => eprintln!("{}: {}", "Uncaught".red(), v.to_string().red()),
}
}
}

Loading…
Cancel
Save