diff --git a/Cargo.lock b/Cargo.lock index 545b12ce296..0cf66f99ee7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/boa/src/builtins/value/display.rs b/boa/src/builtins/value/display.rs index 1d936e6d906..40c0b8f7204 100644 --- a/boa/src/builtins/value/display.rs +++ b/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, diff --git a/boa_cli/Cargo.toml b/boa_cli/Cargo.toml index bf9dd0db125..58e0e59b5a1 100644 --- a/boa_cli/Cargo.toml +++ b/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" diff --git a/boa_cli/src/main.rs b/boa_cli/src/main.rs index 8dee2832c80..8e778c2c878 100644 --- a/boa_cli/src/main.rs +++ b/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()), } } }