-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Unable to view properties of exported Rust structs #1857
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
Comments
Thanks for the report! I'd be somewhat wary to develop a node.js-specific solution here rather than a JS-general solution. Do you know if it's possible to fix this on the web as well? If not then this may fall within the category of "not fixable at this time" for now |
@alexcrichton I do like this idea, but I'm a little concerned about code size: this will add a significant amount of code to every single class, and that code cannot be dead code eliminated. So I'd prefer if this was behind an attribute or a |
I do like the idea of placing these behind an attribute, my only reasoning to include them by default is that the JS objects would match the Rust structs when you log and inspect them. If the attribute is documented, thought I think it's fair that anyone picking up wasm-bindgen would see that caveat and be able to decide if they need it in their case I'll try and make time to implement this behind a |
Instrumenting this as part of |
I've been able to get this working, but I'm not happy with the implementation. The I also had to add some bookkeeping I'm not happy about. The cli code has to keep a vector of methods on the struct to check for |
I suspect it's probably fine to ignore the case where |
* Add support for #[wasm_bindgen(inspectable)] This annotation generates a `toJSON` and `toString` implementation for generated JavaScript classes which display all readable properties available via the class or its getters This is useful because wasm-bindgen classes currently serialize to display one value named `ptr`, which does not model the properties of the struct in Rust This annotation addresses #1857 * Support console.log for inspectable attr in Nodejs `#[wasm_bindgen(inspectable)]` now generates an implementation of `[util.inspect.custom]` for the Node.js target only. This implementation causes `console.log` and friends to yield the same class-style output, but with all readable fields of the Rust struct displayed * Reduce duplication in generated methods Generated `toString` and `[util.inspect.custom]` methods now call `toJSON` to reduce duplication * Store module name in variable
Describe the Bug
When exporting a struct from Rust to JavaScript, calling
console.log
orJSON.stringify
on an object of the class shows only aptr
property and none of the exported struct's propertiesSteps to Reproduce
I've created an example project at codehearts/whoops to reproduce the issue
Consider the following exported Rust struct:
In the output from
wasm-pack build
, theSeries
object has the following properties:However, when logging the object we see neither of these properties:
Expected Behavior
I would expect to see the
name
property exported from Rust, like how a native JavaScript class would lookActual Behavior
The object shows only a
ptr
property, which is not useful for debugging or logging stateAdditional Context
I found a solution that works for Node.js at least. The output of
console.log
can be customized by defining a[util.inspect.custom]
method within the class, and theJSON.stringify
result is based on an existingtoJSON
method if one is definedwasm-bindgen would have to define the
[util.inspect.custom]
method becauseutil
is not imported by default and the brackets create an invalid symbol during compilation, buttoJSON
can be exported from the RustI'm wondering if wasm-bindgen should provide a default
[util.inspect.custom]
,toJSON
, andtoString
which provides the exported properties. Would these be appropriate methods for wasm-bindgen to include when targeting Node.js (and possible just JS for the JSON and string representations?)The text was updated successfully, but these errors were encountered: