-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better output from impl Debug for JsValue
.
#1161
Conversation
This should have some tests I think (it doesn't yet). |
c29b7cd
to
8e732f2
Compare
Thanks for this! Reading this over, I wonder if we perhaps want to actually just move the entire |
Good idea! I'll experiment. |
54fb81c
to
7d5d443
Compare
I've done an implementation in JS. See what you think. There's still a TODO around no-std. This patch actually regresses to doing nothing. Can we do something to get the string in the case that we don't have an allocator? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach!
src/lib.rs
Outdated
@@ -298,11 +298,32 @@ impl JsValue { | |||
unsafe { __wbindgen_is_object(self.idx) == 1 } | |||
} | |||
|
|||
/// Tests whether `Array.isArray(self)`. | |||
#[inline] | |||
pub fn is_array(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be unused? If so, then we should remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah if it were to be included it should be a separate PR. I'll remove.
Ok(String::from( | ||
" | ||
function(i, len_ptr) { | ||
const debug_str = val => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to come up with something that supported as rich of debug string output as this but was a little more terse. Not sure I really ended up on an improvement over what you have here, but for posterity:
function debug(val) {
switch(typeof val) {
case "undefined":
case "number":
case "boolean":
case "symbol":
return String(val);
case "function":
return `Function(${val.name || ""})`;
case "object":
if (val === null) {
return "null";
}
const s = Object.prototype.toString.call(val);
if (s === "[object Object]" || s === "[object Array]") {
try {
return JSON.stringify(val);
} catch (_) {}
}
return s;
default:
return Object.prototype.toString.call(val);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool. I did think about doing a big switch. If JSON.stringify can throw, I should catch it and just fall through like you do. If we can capture the function name that would be cool also. I'll try to merge the 2 impls.
Also since I'm assuming there's no stability guarantees on the output here we can always refine later.
- get name of the function if possible
- if JSON.stringify can throw, catch and ignore any exceptions.
- get toString from Object.prototype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is an easy way, unfortunately. |
I think I probably need to restore the old impl then for the no_std case. |
I think it's fine to in general not worry much about the |
Agreed, FWIW. |
d19c580
to
f50e715
Compare
I've added suggestings from @fitzgen, and also rebased. |
I think this is ready for review now. I've tried to cover all the cases in the tests. I found quite a few problems, which I guess is why tests!!!! |
Not sure where the failure's from. |
https://travis-ci.com/rustwasm/wasm-bindgen/jobs/169969977#L894-L906
I believe that node doesn't support Array.prototype.values yet, so maybe that is what is going on here. |
And fix loads of bugs.
02f28c2
to
5f2ba3f
Compare
impl Debug for JsValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a couple nitpicks below about the __wbg_is_array
stuff that still hasn't gotten removed.
crates/cli-support/src/js/mod.rs
Outdated
@@ -299,6 +299,18 @@ impl<'a> Context<'a> { | |||
)) | |||
})?; | |||
|
|||
self.bind("__wbindgen_is_array", &|me| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: remove this
src/lib.rs
Outdated
@@ -474,9 +489,11 @@ externs! { | |||
fn __wbindgen_symbol_new(ptr: *const u8, len: usize) -> u32; | |||
fn __wbindgen_is_symbol(idx: u32) -> u32; | |||
fn __wbindgen_is_object(idx: u32) -> u32; | |||
fn __wbindgen_is_array(idx: u32) -> u32; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep sorry missed this last time.
We can use
JSON.stringify
to get a better debug representation of a value. It's slow, but I think there's an acceptance that Debug (unlike maybe Display) is allowed to be slow to write better strings, given that it's used mostly before deployment.There's also a new method for testing whether something is an array in javascript. I can remove this if you don't think it's useful.