Implement console.count/countReset (#31635)

* Implement console.count/countReset

* Address review comment

Signed-off-by: syvb <me@iter.ca>

---------

Signed-off-by: syvb <me@iter.ca>
This commit is contained in:
Smitty 2024-03-17 05:50:40 -04:00 committed by GitHub
parent f98975bbbe
commit d2dcb20bea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 46 additions and 72 deletions

View file

@ -296,4 +296,21 @@ impl Console {
pub fn GroupEnd(global: &GlobalScope) {
global.pop_console_group();
}
/// <https://console.spec.whatwg.org/#count>
pub fn Count(global: &GlobalScope, label: DOMString) {
let count = global.increment_console_count(&label);
let message = DOMString::from(format!("{label}: {count}"));
console_message(global, message, LogLevel::Log);
}
/// <https://console.spec.whatwg.org/#countreset>
pub fn CountReset(global: &GlobalScope, label: DOMString) {
if global.reset_console_count(&label).is_err() {
Self::internal_warn(
global,
DOMString::from(format!("Counter “{label}” doesnt exist.")),
)
}
}
}

View file

@ -329,6 +329,11 @@ pub struct GlobalScope {
/// The stack of active group labels for the Console APIs.
console_group_stack: DomRefCell<Vec<DOMString>>,
/// The count map for the Console APIs.
///
/// <https://console.spec.whatwg.org/#count>
console_count_map: DomRefCell<HashMap<DOMString, usize>>,
/// List of ongoing dynamic module imports.
dynamic_modules: DomRefCell<DynamicModuleList>,
@ -790,6 +795,7 @@ impl GlobalScope {
frozen_supported_performance_entry_types: DomRefCell::new(Default::default()),
https_state: Cell::new(HttpsState::None),
console_group_stack: DomRefCell::new(Vec::new()),
console_count_map: Default::default(),
dynamic_modules: DomRefCell::new(DynamicModuleList::new()),
inherited_secure_context,
}
@ -3255,6 +3261,25 @@ impl GlobalScope {
let _ = self.console_group_stack.borrow_mut().pop();
}
pub(crate) fn increment_console_count(&self, label: &DOMString) -> usize {
*self
.console_count_map
.borrow_mut()
.entry(label.clone())
.and_modify(|e| *e += 1)
.or_insert(1)
}
pub(crate) fn reset_console_count(&self, label: &DOMString) -> Result<(), ()> {
match self.console_count_map.borrow_mut().get_mut(label) {
Some(value) => {
*value = 0;
Ok(())
},
None => Err(()),
}
}
pub(crate) fn dynamic_module_list(&self) -> RefMut<DynamicModuleList> {
self.dynamic_modules.borrow_mut()
}

View file

@ -17,6 +17,10 @@ namespace console {
undefined assert(boolean condition, optional any message);
undefined clear();
// Counting
undefined count(optional DOMString label = "default");
undefined countReset(optional DOMString label = "default");
// Grouping
undefined group(any... data);
undefined groupCollapsed(any... data);

View file

@ -1,16 +1,4 @@
[console-label-conversion.any.html]
[console.count()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.count() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.countReset()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.countReset() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.timeLog()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
@ -19,18 +7,6 @@
[console-label-conversion.any.worker.html]
[console.count()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.count() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.countReset()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.countReset() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.timeLog()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL

View file

@ -17,12 +17,6 @@
[console namespace: operation dirxml(any...)]
expected: FAIL
[console namespace: operation count(optional DOMString)]
expected: FAIL
[console namespace: operation countReset(optional DOMString)]
expected: FAIL
[console namespace: operation time(optional DOMString)]
expected: FAIL
@ -52,12 +46,6 @@
[console namespace: operation dirxml(any...)]
expected: FAIL
[console namespace: operation count(optional DOMString)]
expected: FAIL
[console namespace: operation countReset(optional DOMString)]
expected: FAIL
[console namespace: operation time(optional DOMString)]
expected: FAIL

View file

@ -1,16 +1,4 @@
[console-label-conversion.any.html]
[console.count()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.count() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.countReset()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.countReset() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.timeLog()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
@ -19,18 +7,6 @@
[console-label-conversion.any.worker.html]
[console.count()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.count() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.countReset()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL
[console.countReset() throws exceptions generated by erroneous label.toString() conversion]
expected: FAIL
[console.timeLog()'s label gets converted to string via label.toString() when label is an object]
expected: FAIL

View file

@ -17,12 +17,6 @@
[console namespace: operation dirxml(any...)]
expected: FAIL
[console namespace: operation count(optional DOMString)]
expected: FAIL
[console namespace: operation countReset(optional DOMString)]
expected: FAIL
[console namespace: operation time(optional DOMString)]
expected: FAIL
@ -52,12 +46,6 @@
[console namespace: operation dirxml(any...)]
expected: FAIL
[console namespace: operation count(optional DOMString)]
expected: FAIL
[console namespace: operation countReset(optional DOMString)]
expected: FAIL
[console namespace: operation time(optional DOMString)]
expected: FAIL