Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic console.table() implementation #29689

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

switchpiggy
Copy link
Contributor

@switchpiggy switchpiggy commented Apr 30, 2023

Implemented a basic version of console.table() that supports arrays of primitives as input. The full functionality as described in the spec as mentioned in #27212 will also be implemented later.


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes do not require tests because this is only an initial implementation, and tests will be added later if needed.

@servo-wpt-sync
Copy link
Collaborator

🛠 These changes could not be applied onto the latest upstream WPT. Servo's copy of the Web Platform Tests may be out of sync.

Copy link
Member

@mrobinson mrobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. This is a nice start. It needs a bit of work before it's ready to merge.

tabular_data: HandleValue,
properties: Option<Vec<DOMString>>,
) {
unsafe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unsafe block is very wide. Try to move it down to exactly the problematic code and then let's see if we can eliminate it entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problematic part is calling the generate_table() function, which is an unsafe function because it calls Vec::from_jsval() and is_array_like(). I'm not very experienced with the servo documentation, so there could be a way to implement generate_table() safely which I'm not aware of.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least here, I think you should reduce the unsafe block to enclose only the unsafe operations instead of marking the entire function as unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sure! I've removed the unsafe block around generate_table() and just restricted the unsafe blocks to the unsafe operations inside the function.

components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
components/script/dom/console.rs Outdated Show resolved Hide resolved
for row in table.iter() {
for col in row.iter() {
res.push_str(col as &str);
res.push_str("\t\t");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we implement this, it deserves an implementation that knows how to properly align the columns of the table. Two tabs are pretty brittle, so maybe investigate if there is a more resilient way to do this based on the actual strings that we are processing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. I'll see if there's a good way to align the columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented a simple way to align the columns by setting each column's width to be the maximum length of all of its rows, plus some extra space.

…ndling that supports inputting primitive arrays.
…rror handling that supports inputting primitive arrays.
@@ -65,13 +73,76 @@ fn console_message(global: &GlobalScope, message: DOMString, level: LogLevel) {
})
}

///Generates matrix of table elements, and then converts matrix into a properly formatted DOMString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When making a comment in rust you should put a space after the slashes. In this case, I don't think the rustdoc will work without the space.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok sure! Thanks for the suggestion, I'm not really familiar with rustdoc so that really helps. I'll make sure to fix the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants