From 1bf0d700d186eae427748c376c303b2c8b63addc Mon Sep 17 00:00:00 2001 From: Andrew Bovbel Date: Sun, 14 Jul 2024 19:49:47 -0400 Subject: [PATCH 1/6] feat: added simple table reading for arrays --- core/runtime/src/console/mod.rs | 102 ++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 5 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index 8953e322287..71465e6cb32 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -15,14 +15,11 @@ mod tests; use boa_engine::{ - js_str, js_string, - native_function::NativeFunction, - object::{JsObject, ObjectInitializer}, - value::{JsValue, Numeric}, - Context, JsArgs, JsData, JsResult, JsStr, JsString, + ast::statement::Throw, js_str, js_string, native_function::NativeFunction, object::{JsObject, ObjectInitializer}, value::{JsValue, Numeric}, Context, JsArgs, JsData, JsResult, JsStr, JsString }; use boa_gc::{Finalize, Trace}; use rustc_hash::FxHashMap; +use core::borrow; use std::{cell::RefCell, collections::hash_map::Entry, rc::Rc, time::SystemTime}; /// This represents the different types of log messages. @@ -192,6 +189,11 @@ impl Console { js_string!("log"), 0, ) + .function( + console_method(Self::table, state.clone()), + js_string!("table"), + 0, + ) .function( console_method(Self::trace, state.clone()), js_string!("trace"), @@ -385,6 +387,96 @@ impl Console { Ok(JsValue::undefined()) } + /// `console.table(...data)` + /// + /// Prints a JavaScript values to a tabular form with "log" logLevel. + /// + /// More information: + /// - [MDN documentation][mdn] + /// - [WHATWG `console` specification][spec] + /// + /// [spec]: https://console.spec.whatwg.org/#log + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/log + fn table( + _: &JsValue, + args: &[JsValue], + console: &Self, + context: &mut Context, + ) -> JsResult { + let mut separator: usize = 0; + + let mut get_value = |value: String| -> String { + if value.len() > 6 { + separator = value.len(); + } + value + }; + + for arg in args { + match arg { + JsValue::Object(obj) => { + if obj.is_array() { + let borrowed_object = obj.borrow(); + + let key_value_array = borrowed_object.properties().index_properties(); + for key_value in key_value_array { + let key = key_value.0; + // let value = key_value.1.value().unwrap().as_number().unwrap(); + let value: String; + print!("{key}"); + match key_value.1.value().unwrap() { + JsValue::Integer(integer) => { + value = get_value(integer.to_string()) + + }, + JsValue::BigInt(big_int) => { + value = get_value(big_int.to_string()) + } + JsValue::Rational(rational) => { + value = get_value(rational.to_string()); + } + JsValue::Symbol(symbol) => { + value = get_value(symbol.to_string()); + } + JsValue::String(s) => { + value = get_value(s.to_std_string_escaped()); + + }, + JsValue::Boolean(b) => { + value = get_value(b.to_string()); + }, + JsValue::Null => { + value = String::from("null"); + }, + JsValue::Undefined => { + value = String::from("undefined"); + }, + _ => { + value = String::from("unknown"); + }, + } + println!("{value}") + + } + } + if obj.is_ordinary() { + //when doing new Class() + println!("ordinary") + } + } + + _ => { + // Handle other JsValue types + logger(LogMessage::Log(formatter(args, context)?), console); + } + + } + } + + println!("{separator}"); + Ok(JsValue::undefined()) + } + /// `console.trace(...data)` /// /// Prints a stack trace with "trace" logLevel, optionally labelled by data. From 2e98b7fa567d8f70520b4a9e587511d03e04a37a Mon Sep 17 00:00:00 2001 From: Andrew Bovbel Date: Sun, 14 Jul 2024 20:32:46 -0400 Subject: [PATCH 2/6] feat: formatting for print table --- core/runtime/src/console/mod.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index 71465e6cb32..3975cf6dd58 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -404,6 +404,7 @@ impl Console { context: &mut Context, ) -> JsResult { let mut separator: usize = 0; + let mut value_vec: Vec = Vec::new(); let mut get_value = |value: String| -> String { if value.len() > 6 { @@ -412,6 +413,25 @@ impl Console { value }; + fn print_table(value_vec: &Vec) { + let max_value_width = value_vec.iter().map(|value| value.len()).max().unwrap_or(0); + + println!("{max_value_width}"); + + // Print the header + println!("┌─────────┬─{:─width$} │", "", width = if max_value_width < 5 { 0 } else { max_value_width - 4}); + // println!("│ (index) │ Values│"); + println!("├─────────┼{:─ { @@ -420,10 +440,9 @@ impl Console { let key_value_array = borrowed_object.properties().index_properties(); for key_value in key_value_array { - let key = key_value.0; - // let value = key_value.1.value().unwrap().as_number().unwrap(); + // let key = key_value.0; let value: String; - print!("{key}"); + // print!("{key}"); match key_value.1.value().unwrap() { JsValue::Integer(integer) => { value = get_value(integer.to_string()) @@ -455,9 +474,10 @@ impl Console { value = String::from("unknown"); }, } - println!("{value}") + value_vec.push(value) - } + } + print_table(&value_vec); } if obj.is_ordinary() { //when doing new Class() From ef9d650ce6198c4a7e03e38853fcdb7fef9a88f9 Mon Sep 17 00:00:00 2001 From: Andrew Bovbel Date: Sun, 14 Jul 2024 20:36:57 -0400 Subject: [PATCH 3/6] clean up --- core/runtime/src/console/mod.rs | 42 ++++++++------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index 3975cf6dd58..b52d4f3dea6 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -403,35 +403,21 @@ impl Console { console: &Self, context: &mut Context, ) -> JsResult { - let mut separator: usize = 0; let mut value_vec: Vec = Vec::new(); - let mut get_value = |value: String| -> String { - if value.len() > 6 { - separator = value.len(); - } - value - }; - fn print_table(value_vec: &Vec) { let max_value_width = value_vec.iter().map(|value| value.len()).max().unwrap_or(0); - - println!("{max_value_width}"); - // Print the header println!("┌─────────┬─{:─width$} │", "", width = if max_value_width < 5 { 0 } else { max_value_width - 4}); - // println!("│ (index) │ Values│"); println!("├─────────┼{:─ { @@ -440,42 +426,37 @@ impl Console { let key_value_array = borrowed_object.properties().index_properties(); for key_value in key_value_array { - // let key = key_value.0; - let value: String; - // print!("{key}"); + match key_value.1.value().unwrap() { JsValue::Integer(integer) => { - value = get_value(integer.to_string()) - + value_vec.push(integer.to_string()) }, JsValue::BigInt(big_int) => { - value = get_value(big_int.to_string()) + value_vec.push(big_int.to_string()) } JsValue::Rational(rational) => { - value = get_value(rational.to_string()); + value_vec.push(rational.to_string()); } JsValue::Symbol(symbol) => { - value = get_value(symbol.to_string()); + value_vec.push(symbol.to_string()); } JsValue::String(s) => { - value = get_value(s.to_std_string_escaped()); + value_vec.push(s.to_std_string_escaped()); }, JsValue::Boolean(b) => { - value = get_value(b.to_string()); + value_vec.push(b.to_string()); }, JsValue::Null => { - value = String::from("null"); + value_vec.push(String::from("null")); }, JsValue::Undefined => { - value = String::from("undefined"); + value_vec.push(String::from("undefined")); }, _ => { - value = String::from("unknown"); + value_vec.push(String::from("unknown")); }, } - value_vec.push(value) - } print_table(&value_vec); } @@ -489,11 +470,8 @@ impl Console { // Handle other JsValue types logger(LogMessage::Log(formatter(args, context)?), console); } - } } - - println!("{separator}"); Ok(JsValue::undefined()) } From 49b738153a0200f890faba9d85cdf83d97684338 Mon Sep 17 00:00:00 2001 From: Andrew Bovbel Date: Sun, 14 Jul 2024 23:01:56 -0400 Subject: [PATCH 4/6] fix: imports --- core/runtime/src/console/mod.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index b52d4f3dea6..e81fa99018a 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -15,11 +15,10 @@ mod tests; use boa_engine::{ - ast::statement::Throw, js_str, js_string, native_function::NativeFunction, object::{JsObject, ObjectInitializer}, value::{JsValue, Numeric}, Context, JsArgs, JsData, JsResult, JsStr, JsString + js_str, js_string, native_function::NativeFunction, object::{JsObject, ObjectInitializer}, value::{JsValue, Numeric}, Context, JsArgs, JsData, JsResult, JsStr, JsString }; use boa_gc::{Finalize, Trace}; use rustc_hash::FxHashMap; -use core::borrow; use std::{cell::RefCell, collections::hash_map::Entry, rc::Rc, time::SystemTime}; /// This represents the different types of log messages. @@ -387,7 +386,7 @@ impl Console { Ok(JsValue::undefined()) } - /// `console.table(...data)` + /// `console.table(...data, columns)` /// /// Prints a JavaScript values to a tabular form with "log" logLevel. /// @@ -395,8 +394,8 @@ impl Console { /// - [MDN documentation][mdn] /// - [WHATWG `console` specification][spec] /// - /// [spec]: https://console.spec.whatwg.org/#log - /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/log + /// [spec]: https://console.spec.whatwg.org/#table + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/table fn table( _: &JsValue, args: &[JsValue], From 7472056d27961476a6bd90d618b5eb9d2604c983 Mon Sep 17 00:00:00 2001 From: Andrew Bovbel Date: Sun, 14 Jul 2024 23:13:50 -0400 Subject: [PATCH 5/6] fix: spacing issue --- core/runtime/src/console/mod.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index e81fa99018a..60913b100d1 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -407,14 +407,14 @@ impl Console { fn print_table(value_vec: &Vec) { let max_value_width = value_vec.iter().map(|value| value.len()).max().unwrap_or(0); - println!("┌─────────┬─{:─width$} │", "", width = if max_value_width < 5 { 0 } else { max_value_width - 4}); - println!("├─────────┼{:─width$} │", "", width = if max_value_width < 1 { 0 } else { max_value_width - 1}); + println!("├─────────┼{:─ Date: Sun, 14 Jul 2024 23:17:01 -0400 Subject: [PATCH 6/6] fix: cleaner --- core/runtime/src/console/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index 60913b100d1..9ca636e7ade 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -407,14 +407,14 @@ impl Console { fn print_table(value_vec: &Vec) { let max_value_width = value_vec.iter().map(|value| value.len()).max().unwrap_or(0); - println!("┌─────────┬─{:─width$} │", "", width = if max_value_width < 1 { 0 } else { max_value_width - 1}); - println!("├─────────┼{:─width$}│", "", width = max_value_width); + println!("├─────────┼{:─