From de02693bc4f57a805fdb1aa706870a0d61e34d4c Mon Sep 17 00:00:00 2001 From: Sumit Parakh Date: Mon, 20 Jan 2025 01:42:33 +0530 Subject: [PATCH] fallback to default logger if unable to parse --- core/runtime/src/console/mod.rs | 9 +- core/runtime/src/console/table.rs | 173 ++++++++++++++++++++++++++++++ core/runtime/src/console/tests.rs | 3 +- examples/scripts/console.js | 16 +++ 4 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 core/runtime/src/console/table.rs create mode 100644 examples/scripts/console.js diff --git a/core/runtime/src/console/mod.rs b/core/runtime/src/console/mod.rs index 70e55440b49..e1f915552af 100644 --- a/core/runtime/src/console/mod.rs +++ b/core/runtime/src/console/mod.rs @@ -11,6 +11,7 @@ //! [spec]: https://console.spec.whatwg.org/ //! [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Console +mod table; #[cfg(test)] mod tests; @@ -348,7 +349,7 @@ impl Console { .function( console_method(Self::table, state.clone(), logger.clone()), js_string!("table"), - 0 + 0, ) .function( console_method_mut(Self::count, state.clone(), logger.clone()), @@ -560,7 +561,11 @@ impl Console { logger: &impl Logger, context: &mut Context, ) -> JsResult { - logger.log(formatter(args, context)?, &console.state, context)?; + // logger.log(formatter(args, context)?, &console.state, context)?; + + + table::table_formatter(args, context, console, logger); + // table::table(args, logger, context); Ok(JsValue::undefined()) } diff --git a/core/runtime/src/console/table.rs b/core/runtime/src/console/table.rs new file mode 100644 index 00000000000..05bf62e3ada --- /dev/null +++ b/core/runtime/src/console/table.rs @@ -0,0 +1,173 @@ +use boa_engine::{Context, JsResult, JsValue, JsVariant}; +use crate::console::formatter; +use super::{Console, Logger}; + + +struct TableChars { + middle_middle: char, + row_middle: char, + left_middle: char, + top_middle: char, + right_middle: char, + bottom_middle: char, + + top_right: char, + top_left: char, + bottom_right: char, + bottom_left: char, + + left: &'static str, + right: &'static str, + middle: &'static str, +} + +const table_chars: TableChars = TableChars { + middle_middle: '─', + row_middle: '┼', + left_middle: '├', + top_middle: '┬', + right_middle: '┤', + bottom_middle: '┴', + top_right: '┐', + top_left: '┌', + bottom_right: '┘', + bottom_left: '└', + + left: "│ ", + right: " │", + middle: " │ ", +}; + +pub(super) fn table_formatter( + data: &[JsValue], + context: &mut Context, + console: &Console, + logger: &impl Logger, +) -> JsResult { + + println!("Data: {:?}", data); + + + fn render_row(value_vec: Vec) -> String { + return String::from("value"); + } + + fn print_table(value_vec: &Vec) { + println!("Printing table"); + let max_value_width = value_vec.iter().map(|value| value.len()).max().unwrap_or(0); + let column_widths = value_vec + .iter() + .map(|value| value.len()) + .collect::>(); + println!("Column widths: {:?}", column_widths); + println!("Max value width: {:?}", max_value_width); + println!("Vector length: {}", value_vec.len()); + + let top_divider = column_widths + .iter() + .map(|width| table_chars.middle_middle.to_string().repeat(width + 2)) + .collect::>(); + + let top_left = table_chars.top_left.to_string(); + let top_middle = table_chars.top_middle.to_string(); + let top_right = table_chars.top_right.to_string(); + let left_middle = table_chars.left_middle.to_string(); + let row_middle = table_chars.row_middle.to_string(); + let right_middle = table_chars.right_middle.to_string(); + let bottom_left = table_chars.bottom_left.to_string(); + let bottom_middle = table_chars.bottom_middle.to_string(); + let bottom_right = table_chars.bottom_right.to_string(); + + let mut result = format!( + "{}{}{}\n", + top_left, + top_divider.join(&top_middle).to_string(), + top_right + ); + + result.push_str(&format!("{}\n", render_row(value_vec.clone()))); + result.push_str(&format!( + "{}{}{}\n", + left_middle, + top_divider.join(&row_middle).to_string(), + right_middle + )); + result.push_str(&format!( + "{}{}{}", + bottom_left, + top_divider.join(&bottom_middle).to_string(), + bottom_right + )); + println!("{}", result); + } + + let mut value_vec: Vec = Vec::new(); + + for arg in data { + match arg.as_object() { + // arg if let Some(obj) = arg.as_object() + Some(arg) if arg.is_array() => { + println!("This is array ------------"); + let array = arg.borrow(); + + let key_value_array = array.properties().index_properties(); + for key_value in key_value_array { + match key_value.1.value().unwrap().variant() { + JsVariant::Integer32(integer) => { + value_vec.push(integer.to_string()); + } + JsVariant::BigInt(bigint) => { + value_vec.push(bigint.to_string()); + } + JsVariant::Boolean(boolean) => { + value_vec.push(boolean.to_string()); + } + JsVariant::Float64(float64) => { + value_vec.push(float64.to_string()); + } + JsVariant::String(string) => { + value_vec.push(string.to_std_string_escaped()); + } + JsVariant::Symbol(symbol) => { + value_vec.push(symbol.to_string()); + } + JsVariant::Null => { + value_vec.push(String::from("null")); + } + JsVariant::Undefined => { + value_vec.push(String::from("undefined")); + } + JsVariant::Object(obj) => { + // TODO: Implement object formatter. eg. {name: 'Xyz', age: 20} and so on. + value_vec.push(String::from("Object")); + // obj.borrow().properties() + // value_vec.push(JsObject::is_ordinary(&self) obj.prim) + // obj.fmt(format_args!("{:?}", obj)); + } + _ => { + value_vec.push(String::from("undefined")); + } + } + } + print_table(&value_vec); + } + + Some(arg) if arg.is_ordinary() => { + println!("This is object ------------"); + let ordinary_object = arg.borrow(); + let key_value_array = ordinary_object.properties().index_properties(); + for key_value in key_value_array { + let key = key_value.0; + // let value = key_value.1.value().unwrap().to_string(context).unwrap(); + println!("Obj: {key}"); + } + } + + _ => { + logger.log(formatter(data, context)?, &console.state, context); + } + } + } + + Ok(JsValue::undefined()) +} diff --git a/core/runtime/src/console/tests.rs b/core/runtime/src/console/tests.rs index 5645a233061..f3f56a3c99e 100644 --- a/core/runtime/src/console/tests.rs +++ b/core/runtime/src/console/tests.rs @@ -107,8 +107,7 @@ fn console_log_cyclic() { let a = [1]; a[1] = a; console.log(a); - let obj = {a: 1, b: 2}; - console.table(obj); + console.table(['a','b','c']); "#})], &mut context, ); diff --git a/examples/scripts/console.js b/examples/scripts/console.js new file mode 100644 index 00000000000..246494c5a67 --- /dev/null +++ b/examples/scripts/console.js @@ -0,0 +1,16 @@ +console.log("Hello world"); +console.table(JSON.stringify({ a: 1, b: 2 })); +console.warn("This is a warning"); +console.error("This is an error"); +console.info("This is an info message"); +console.assert(1 === 2, "This is an assertion message"); + + +function foo() { + function bar() { + console.trace(); + } + bar(); +} + +foo();