Implement Debug for JsValue

This commit is contained in:
Alex Crichton
2018-07-05 20:14:38 -07:00
parent bc2eab1434
commit d930a5a97a
2 changed files with 31 additions and 0 deletions

View File

@ -17,6 +17,7 @@ extern crate serde_json;
extern crate wasm_bindgen_macro;
use core::cell::UnsafeCell;
use core::fmt;
use core::ops::Deref;
use core::ptr;
@ -363,6 +364,33 @@ impl Clone for JsValue {
}
}
impl fmt::Debug for JsValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(n) = self.as_f64() {
return n.fmt(f)
}
#[cfg(feature = "std")]
{
if let Some(n) = self.as_string() {
return n.fmt(f)
}
}
if let Some(n) = self.as_bool() {
return n.fmt(f)
}
if self.is_null() {
return fmt::Display::fmt("null", f)
}
if self.is_undefined() {
return fmt::Display::fmt("undefined", f)
}
if self.is_symbol() {
return fmt::Display::fmt("Symbol(..)", f)
}
fmt::Display::fmt("[object]", f)
}
}
impl Drop for JsValue {
fn drop(&mut self) {
unsafe {