Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions core/wintertc/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ impl Logger for NullLogger {
}

/// This represents the `console` formatter.
///
/// See Also: https://console.spec.whatwg.org/
fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {
fn to_string(value: &JsValue, _context: &mut Context) -> String {
match value.variant() {
Expand All @@ -203,12 +205,18 @@ fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {
[] => Ok(String::new()),
[val] => Ok(to_string(val, context)),
data => {
// Only strings are format strings. Otherwises do not format it.
let Some(target) = data[0].as_string() else {
return Ok(data
.iter()
.map(|value| to_string(value, context))
.collect::<Vec<_>>()
.join(" "));
};

let mut formatted = String::new();
let mut arg_index = 1;
let target = data
.get_or_undefined(0)
.to_string(context)?
.to_std_string_escaped();
let target = target.to_std_string_escaped();
let mut chars = target.chars();
while let Some(c) = chars.next() {
if c == '%' {
Expand Down
35 changes: 35 additions & 0 deletions core/wintertc/src/console/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,41 @@ fn console_log_date() {
);
}

#[test]
fn console_log_non_string_first_argument() {
let mut context = Context::default();
let logger = RecordingLogger::default();
Console::register_with_logger(logger.clone(), &mut context).unwrap();

run_test_actions_with(
[TestAction::run(indoc! {r#"
console.log([], { hello: 'world' });
console.log({ hello: 'world' }, 'tail');
console.log(Symbol('test'), 'tail');
console.log(['%s'], 'tail');
console.log(null, undefined, false, 42, 1n);
const array = [];
Object.defineProperty(array, 'toString', {
value() { throw new Error('must not call toString'); }
});
console.log(array, 'tail');
"#})],
&mut context,
);

assert_eq!(
*logger.log.borrow(),
indoc! {r#"
[] { hello: "world" }
{ hello: "world" } tail
Symbol(test) tail
[ "%s" ] tail
null undefined false 42 1n
[] tail
"#}
);
}

#[test]
fn trace_with_stack_trace() {
let mut context = Context::default();
Expand Down