Skip to content

Commit

Permalink
Added initial implementation of console.table() with minimal error ha…
Browse files Browse the repository at this point in the history
…ndling that supports inputting primitive arrays.
  • Loading branch information
switchpiggy committed Apr 30, 2023
1 parent a8b024e commit ef35292
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 40 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ opt-level = 3
# debug = true
# lto = false

[profile.dev]
incremental = true
opt-level = 1


[patch.crates-io]
# If you need to temporarily test Servo with a local fork of some upstream
# crate, add that here. Use the form:
Expand Down
97 changes: 62 additions & 35 deletions components/script/dom/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::conversions::{
is_array_like, FromJSValConvertible, StringificationBehavior,
};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::script_runtime::JSContext as SafeJSContext;
use core::ops::Deref;
use devtools_traits::{ConsoleMessage, LogLevel, ScriptToDevtoolsControlMsg};
use js::jsapi::JSContext;
use js::rust::describe_scripted_caller;
use std::{io};
use std::any::Any;
use js::rust::HandleValue;
use std::io;
use std::option::Option;

// https://developer.mozilla.org/en-US/docs/Web/API/Console
Expand Down Expand Up @@ -67,42 +73,52 @@ fn console_message(global: &GlobalScope, message: DOMString, level: LogLevel) {
})
}

enum TabularData {
PrimitiveArray(Vec<DOMString>)
}

//handle case where tabular data is an array of DOMStrings
fn generate_table_from_primitive_array(tabularData: &Vec<DOMString>) -> Vec<Vec<DOMString>> {
let mut tableMatrix: Vec<Vec<DOMString>> = vec![vec![DOMString::from_string(String::from("(index)")),
DOMString::from_string(String::from("Value"))]];
//generate matrix of table elements
#[allow(unsafe_code)]
unsafe fn generate_table(
cx: &SafeJSContext,
tabular_data: &HandleValue,
_properties: &Option<Vec<DOMString>>,
) -> Option<Vec<Vec<DOMString>>> {
let cx_ptr = *(cx.deref()) as *mut JSContext;

if !tabular_data.get().is_object() {
return None;
}

if is_array_like(cx_ptr, *tabular_data) {
let vec: Vec<DOMString> =
Vec::<DOMString>::from_jsval(cx_ptr, *tabular_data, StringificationBehavior::Empty)
.expect("Error when converting Array object to Vec<DOMString>")
.get_success_value()
.unwrap_or(&Vec::new())
.to_vec();

let mut res: Vec<Vec<DOMString>> = Vec::new();
res.push(vec!["(index)".into(), "Values".into()]);
for (index, word) in vec.into_iter().enumerate() {
res.push(vec![index.to_string().into(), word]);
}

for (index, value) in tabularData.iter().enumerate() {
tableMatrix.push(vec![DOMString::from_string(index.to_string()), value.clone()]);
return Some(res);
}

tableMatrix
None
}

//generate 2D vector from tabular data
fn generate_table(tabularData: &Option<TabularData>) -> Vec<Vec<DOMString>> {
let table: Vec<Vec<DOMString>> = match tabularData {
Some(TabularData::PrimitiveArray(arr)) => generate_table_from_primitive_array(arr),
None => Vec::new()
};

table
}

//squash table 2d array into a printable string
fn generate_table_display_string(tableMatrix: &Vec<Vec<DOMString>>) -> DOMString {
let mut displayString: DOMString = DOMString::new();
for row in tableMatrix.iter() {
//transform matrix of table elements into DOMString for logging
fn generate_table_as_string(table: &[Vec<DOMString>]) -> DOMString {
let mut res: DOMString = DOMString::new();
for row in table.iter() {
for col in row.iter() {
displayString.extend(col.chars());
res.push_str(col as &str);
res.push_str("\t\t");
}

res.push_str("\n");
}

displayString
res
}

#[allow(non_snake_case)]
Expand All @@ -112,12 +128,23 @@ impl Console {
console_messages(global, &messages, LogLevel::Log)
}

//https://developer.mozilla.org/en-US/docs/Web/API/Console/table
pub fn Table(global: &GlobalScope, tabularData: Box<dyn Any>, properties: Option<Vec<DOMString>>) {
// console_messages(global, &messages, LogLevel::Log)
// let tableMatrix = generate_table(&tabularData);
let displayString: DOMString = "hello".into();
console_messages(global, &[displayString], LogLevel::Log);
//https://console.spec.whatwg.org/#table
#[allow(unsafe_code)]
pub fn Table(
cx: SafeJSContext,
global: &GlobalScope,
tabular_data: HandleValue,
properties: Option<Vec<DOMString>>,
) {
unsafe {
match generate_table(&cx, &tabular_data, &properties) {
Some(TableMatrix) => {
let output = generate_table_as_string(&TableMatrix);
console_messages(global, &[output], LogLevel::Log)
},
None => console_messages(global, &[], LogLevel::Log),
}
}
}

// https://developer.mozilla.org/en-US/docs/Web/API/Console/clear
Expand Down
25 changes: 25 additions & 0 deletions tests/html/console-table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Console.table() better work!</title>
<script>
let obj = {
a: 1,
b: 2,
c: 3
};

//Array of string primitives
console.table(["Hello", "world"]);

//Array of int primitives
console.table([1, 2, 3, 4, 5]);

//Array of objects
console.table([obj, obj, obj]);
</script>
</head>
<body>
<p>There will be 3 console.table() calls.</p>
</body>
</html>

0 comments on commit ef35292

Please sign in to comment.