Skip to content

Commit

Permalink
print pretty json representation
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 29, 2024
1 parent 49be74f commit 3af4f68
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/js/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,33 @@ struct Console {}
#[rquickjs::methods]
impl Console {
fn log(&self, value: rquickjs::Value<'_>) -> Result<(), rquickjs::Error> {
let msg = match value.try_into_string() {
Ok(s) => s.to_string()?,
Err(v) => format!("[{}] {:?}", v.type_name(), v),
};

println!("[console.log] {}", msg);
let ty = value.type_name();
println!("[log] ({ty}) {}", string_repr(value)?);
Ok(())
}

fn error(&self, value: rquickjs::Value<'_>) -> Result<(), rquickjs::Error> {
let msg = match value.try_into_string() {
Ok(s) => s.to_string()?,
Err(v) => format!("[{}] {:?}", v.type_name(), v),
};

eprintln!("[console.error] {}", msg);
let ty = value.type_name();
println!("[error] ({ty}) {}", string_repr(value)?);
Ok(())
}
}

fn string_repr(value: rquickjs::Value<'_>) -> Result<String, rquickjs::Error> {
let ctx = value.ctx();
if let Some(json) =
ctx.json_stringify_replacer_space(value.clone(), rquickjs::Undefined, 4)?
{
return Ok(json.to_string().unwrap());
}

if let Some(string) = value.into_string() {
return Ok(string.to_string().unwrap());
}

Ok("unknown value".to_owned())
}

#[derive(Trace)]
#[rquickjs::class]
struct Util {}
Expand Down

0 comments on commit 3af4f68

Please sign in to comment.