Skip to content

Commit

Permalink
Add logging of promises (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison authored Dec 17, 2024
1 parent 1ce8d66 commit aae8d9c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
37 changes: 35 additions & 2 deletions llrt_core/src/modules/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use llrt_json::{escape::escape_json, stringify::json_stringify};
use llrt_numbers::float_to_string;
use llrt_utils::{
class::get_class_name,
error::ErrorExtensions,
primordials::{BasePrimordials, Primordial},
};
use rquickjs::{
Expand All @@ -20,7 +21,8 @@ use rquickjs::{
module::{Declarations, Exports, ModuleDef},
object::{Accessor, Filter},
prelude::{Func, Rest},
Array, Class, Coerced, Ctx, Function, Object, Result, Symbol, Type, Value,
promise::PromiseState,
Array, Class, Coerced, Ctx, Error, Function, Object, Result, Symbol, Type, Value,
};

use crate::modules::module::export_default;
Expand Down Expand Up @@ -346,7 +348,36 @@ fn format_raw_inner<'js>(
result.push(']');
},
Type::Promise => {
result.push_str("Promise {}");
let promise = unsafe { value.as_promise().unwrap_unchecked() };
let state = promise.state();
result.push_str("Promise {");
let is_pending = matches!(state, PromiseState::Pending);
let apply_indentation = bitmask(depth < 2 && !is_pending);
write_sep(result, false, apply_indentation > 0, options.newline);
push_indentation(result, apply_indentation & (depth + 1));
match state {
PromiseState::Pending => {
Color::CYAN.push(result, color_enabled_mask);
result.push_str("<pending>");
Color::reset(result, color_enabled_mask);
},
PromiseState::Resolved => {
let value: Value = unsafe { promise.result().unwrap_unchecked() }?;
format_raw_inner(result, value, options, visited, depth + 1)?;
},
PromiseState::Rejected => {
let value: Error =
unsafe { promise.result::<Value>().unwrap_unchecked() }.unwrap_err();
let value = value.into_value(promise.ctx())?;
Color::RED.push(result, color_enabled_mask);
result.push_str("<rejected> ");
Color::reset(result, color_enabled_mask);
format_raw_inner(result, value, options, visited, depth + 1)?;
},
}
write_sep(result, false, apply_indentation > 0, options.newline);
push_indentation(result, apply_indentation & (depth));
result.push('}');
return Ok(());
},
Type::Array | Type::Object | Type::Exception => {
Expand Down Expand Up @@ -417,6 +448,8 @@ fn format_raw_inner<'js>(
if let Some(class_name) = class_name {
result.push_str(&class_name);
result.push(SPACING);

//TODO fix when quickjs-ng exposes these types
is_typed_array = matches!(
class_name.as_str(),
"Int8Array"
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ it("should log complex object", () => {
3: {},
[3.14]: 1,
4: [1, 2, 3],
5: Promise.reject(1),
6: Promise.resolve(1),
abc: 123,
};

Expand All @@ -138,9 +140,15 @@ it("should log complex object", () => {
`
{
'1': Symbol(foo),
'2': Promise {},
'2': Promise { <pending> },
'3': {},
'4': [ 1, 2, 3 ],
'5': Promise {
<rejected> 1
},
'6': Promise {
1
},
a: 1,
b: \'foo\',
c: {
Expand Down

0 comments on commit aae8d9c

Please sign in to comment.