From a31a3cd71a5ad2a927d4323bd602ea560ed7ebc0 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 22 Jul 2020 03:52:02 +0200 Subject: [PATCH 1/3] Better error formatting --- boa/src/builtins/value/display.rs | 8 ++++++++ 1 file changed, 8 insertions(+) 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, From ea2a3b7a8df1da4810dfdc5f260dbcf8b33009ba Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 22 Jul 2020 04:49:36 +0200 Subject: [PATCH 2/3] Added color for readline and uncaught errors --- Cargo.lock | 12 +++++++++++- boa_cli/Cargo.toml | 1 + boa_cli/src/main.rs | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 545b12ce296..5700b1f0fba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "arrayref" version = "0.3.6" @@ -97,6 +106,7 @@ name = "boa_cli" version = "0.9.0" dependencies = [ "Boa", + "ansi_term 0.12.1", "jemallocator", "rustyline", "serde_json", @@ -162,7 +172,7 @@ version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" dependencies = [ - "ansi_term", + "ansi_term 0.11.0", "atty", "bitflags", "strsim", diff --git a/boa_cli/Cargo.toml b/boa_cli/Cargo.toml index bf9dd0db125..321baa49fb4 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" +ansi_term = "0.12.1" [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..0a8d8420f6d 100644 --- a/boa_cli/src/main.rs +++ b/boa_cli/src/main.rs @@ -25,6 +25,7 @@ clippy::as_conversions )] +use ansi_term::{Colour as Color, Style}; use boa::{ exec::Interpreter, forward_val, @@ -208,8 +209,14 @@ pub fn main() -> Result<(), std::io::Error> { let mut editor = Editor::<()>::with_config(config); let _ = editor.load_history(CLI_HISTORY); + let readline = Style::default() + .bold() + .fg(Color::Cyan) + .paint("> ") + .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 +230,11 @@ 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!( + "{}: {}", + Color::Red.paint("Uncaught"), + Color::Red.paint(&v.to_string()) + ), } } } From f7e231cc6f0750519cefdff70df01cfcdc57382a Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 22 Jul 2020 16:56:44 +0200 Subject: [PATCH 3/3] Changed from ansi_term to colored --- Cargo.lock | 24 +++++++++++++----------- boa_cli/Cargo.toml | 2 +- boa_cli/src/main.rs | 14 +++----------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5700b1f0fba..0cf66f99ee7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,15 +40,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "arrayref" version = "0.3.6" @@ -106,7 +97,7 @@ name = "boa_cli" version = "0.9.0" dependencies = [ "Boa", - "ansi_term 0.12.1", + "colored", "jemallocator", "rustyline", "serde_json", @@ -172,7 +163,7 @@ version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" dependencies = [ - "ansi_term 0.11.0", + "ansi_term", "atty", "bitflags", "strsim", @@ -190,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_cli/Cargo.toml b/boa_cli/Cargo.toml index 321baa49fb4..58e0e59b5a1 100644 --- a/boa_cli/Cargo.toml +++ b/boa_cli/Cargo.toml @@ -15,7 +15,7 @@ Boa = { path = "../boa", features = ["serde"] } rustyline = "6.2.0" structopt = "0.3.15" serde_json = "1.0.56" -ansi_term = "0.12.1" +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 0a8d8420f6d..8e778c2c878 100644 --- a/boa_cli/src/main.rs +++ b/boa_cli/src/main.rs @@ -25,13 +25,13 @@ clippy::as_conversions )] -use ansi_term::{Colour as Color, Style}; use boa::{ exec::Interpreter, forward_val, 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}; @@ -209,11 +209,7 @@ pub fn main() -> Result<(), std::io::Error> { let mut editor = Editor::<()>::with_config(config); let _ = editor.load_history(CLI_HISTORY); - let readline = Style::default() - .bold() - .fg(Color::Cyan) - .paint("> ") - .to_string(); + let readline = "> ".cyan().bold().to_string(); loop { match editor.readline(&readline) { @@ -230,11 +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!( - "{}: {}", - Color::Red.paint("Uncaught"), - Color::Red.paint(&v.to_string()) - ), + Err(v) => eprintln!("{}: {}", "Uncaught".red(), v.to_string().red()), } } }