Editorial review: Improve docs for Wasm mutable globals#43843
Editorial review: Improve docs for Wasm mutable globals#43843chrisdavidmills merged 11 commits intomdn:mainfrom
Conversation
bvisness
left a comment
There was a problem hiding this comment.
Overall this looks solid and long overdue!
This comment was marked as spam.
This comment was marked as spam.
|
I think it all looks good to me now except for the list-of-types thing and the constant-expression thing. |
Superb, thanks @bvisness. I think I've fixed them appropriately, but let me know if you think they need some more work. In the meantime, I think this can be safely passed on to the editorial review stage. @hamishwillee, fancy a bit more Wasm? You know you do ;-) |
|
|
||
| A string indicating the value of the global. | ||
|
|
||
| For the setter to work, the global must be mutable (the [`mutable`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#mutable) option was set to `true` when it was declared), and the global's value must have the same data type as the one specified in the constructor's [`value`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#value) property when the global was first created. |
There was a problem hiding this comment.
FMI what happens if the conditions are not true? I mean it might fail, but what are the side effects, if any, of failure?
There was a problem hiding this comment.
Yeah, sorry, a TypeError is thrown. There are no side effects as such, just the value is not changed because it is immutable.
There was a problem hiding this comment.
I've updated the page to explain in more detail what happens if those conditions are not true. Really oddly, if you set the value to a different data type, like a string, the value is set to 0, and the global becomes mutable. I observed this behavior in both Firefox and Chrome.
@bvisness is this expected behavior? Sounds like a bug to me.
There was a problem hiding this comment.
Wierd. From a docs point of view your fix is good though.
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
hamishwillee
left a comment
There was a problem hiding this comment.
@hamishwillee, fancy a bit more Wasm? You know you do ;-)
I hate you :-)
Looks pretty good. A few nits/points for discussion.
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
hamishwillee
left a comment
There was a problem hiding this comment.
Thanks very much @chrisdavidmills
I've approved, but if it were me I might wait for a response from @bvisness on #43843 (comment) before merging.
Cool, thanks very much for your work on this, @hamishwillee. I'll get the Wasm engineers to comment on this asap. |
| - The global must be mutable (the [`mutable`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#mutable) option was set to `true` when it was declared). If this is not the case, a `TypeError` exception is thrown. | ||
| - The global's value must have the same data type as the one specified in the constructor's [`value`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#value) property when the global was first created. If this is not the case (for example, if you set the global `value` to a string), the `value` is set to `0`, and the global becomes mutable. | ||
| - The global must be mutable (the [`mutable`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#mutable) option was set to `true` when it was declared). | ||
| - The global's value must have the same data type as the one specified in the constructor's [`value`](/en-US/docs/WebAssembly/Reference/JavaScript_interface/Global/Global#value) property when the global was first created. |
There was a problem hiding this comment.
This is straightforwardly contradicted by the content below, no? If you declare it with an i32 but try to set it to a string, you don't get a TypeError, you get a coerced value of some kind.
Personally, I do not think it is worth belaboring the fact that ill-typed values get coerced to something else. This is obvious and pervasive across not just WebAssembly APIs but all web APIs. For example, parseInt takes a string argument, but you can call it with a number, and the first thing the spec does is to run ToString on it. This is not documented because to do so would be exhausting and unhelpful (e.g. what happens with numbers, arrays, objects, null, undefined, BigInt? who cares?)
Specifically, for WebAssembly, any time a JS value is passed to WebAssembly, it is passed through the ToWebAssemblyValue operation, which typically just calls operations like ToInt32 and ToNumber in the JS spec. In this case, because the global has type i32, it is calling ToInt32, which snakes around in a bunch of confusing logic but at the end of the day does a best-effort attempt to convert a string to a number. Your "hello" example converts to zero, but "123" converts to 123, "3.14" converts to 3, and "0xDEADBEEF" converts to -559038737. Therefore your disclaimer is actually incomplete and unhelpful.
In the end, this is just typical JS stuff and shouldn't be documented here.
There was a problem hiding this comment.
OK, sorry, that wasn't very well thought out. I've removed all the stuff about typing and coercion, and just kept it simple:
The
valueproperty of aGlobalobject instance allows you to directly get or set the global's value.For the setter to work, the global must be mutable (the
mutableoption was set totruewhen it was declared). If this is not the case, aTypeErrorexception is thrown.
|
|
||
| If global is mutable and you attempt to set its `value` to an incompatible value type, the actual value set will be an appropriate value chosen by the browser. For example: | ||
|
|
||
| - If you attempt to set it to a string, the value set will be `0`. |
There was a problem hiding this comment.
Per the above, this is wrong because only some strings coerce to 0; others coerce to actual numeric values.
|
The updates look good to me! |
Excellent. Let's get it merged! |
Description
This PR documents Wasm mutable globals. We had a bit of this already, which this PR largely cleans up. Specifically, it:
globalfrom an instruction to a definition, and adds a lot more detail, including using the new template.global.getandglobal.set, including using the new template.WebAssembly.GlobalJS API docs to add more detail and add pages forvalueandvalueOfMotivation
Additional details
Related issues and pull requests