Skip to content

Editorial review: Improve docs for Wasm mutable globals#43843

Merged
chrisdavidmills merged 11 commits intomdn:mainfrom
chrisdavidmills:wasm-mutable-globals
Apr 27, 2026
Merged

Editorial review: Improve docs for Wasm mutable globals#43843
chrisdavidmills merged 11 commits intomdn:mainfrom
chrisdavidmills:wasm-mutable-globals

Conversation

@chrisdavidmills
Copy link
Copy Markdown
Contributor

Description

This PR documents Wasm mutable globals. We had a bit of this already, which this PR largely cleans up. Specifically, it:

  • Changes global from an instruction to a definition, and adds a lot more detail, including using the new template.
  • Adds a lot more detail to global.get and global.set, including using the new template.
  • Updates the WebAssembly.Global JS API docs to add more detail and add pages for value and valueOf

Motivation

Additional details

Related issues and pull requests

@chrisdavidmills chrisdavidmills requested a review from a team as a code owner April 17, 2026 18:17
@chrisdavidmills chrisdavidmills requested review from pepelsbey and removed request for a team April 17, 2026 18:17
@github-actions github-actions Bot added Content:wasm WebAssembly docs size/m [PR only] 51-500 LoC changed labels Apr 17, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

Preview URLs (10 pages)

Flaws (1)

Note! 9 documents with no flaws that don't need to be listed. 🎉

Found an unexpected or unresolvable flaw? Please report it here.

URL: /en-US/docs/WebAssembly/Reference/Definitions/global
Title: global: Wasm definition
Flaw count: 1

  • macros:
    • Macro jsxref produces link /en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Global/Global which is a redirect
External URLs (9)

URL: /en-US/docs/WebAssembly
Title: WebAssembly


URL: /en-US/docs/WebAssembly/Reference/Definitions/global
Title: global: Wasm definition

(comment last updated: 2026-04-27 17:04:29)

Copy link
Copy Markdown

@bvisness bvisness left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks solid and long overdue!

Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md
Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/javascript_interface/global/value/index.md Outdated
Comment thread files/en-us/webassembly/reference/javascript_interface/global/value/index.md Outdated
Comment thread files/en-us/webassembly/reference/variables/global.get/index.md Outdated
Comment thread files/en-us/webassembly/reference/variables/global.set/index.md Outdated
Comment thread files/en-us/webassembly/reference/variables/global.set/index.md Outdated
@diadehoy600-ui

This comment was marked as spam.

@bvisness
Copy link
Copy Markdown

I think it all looks good to me now except for the list-of-types thing and the constant-expression thing.

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

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 ;-)

@chrisdavidmills chrisdavidmills requested review from hamishwillee and removed request for pepelsbey April 21, 2026 09:09
@chrisdavidmills chrisdavidmills changed the title Improve docs for Wasm mutable globals Editorial review: Improve docs for Wasm mutable globals Apr 21, 2026
Comment thread files/en-us/webassembly/reference/variables/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/definitions/global/index.md
Comment thread files/en-us/webassembly/reference/javascript_interface/global/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/javascript_interface/global/global/index.md Outdated
Comment thread files/en-us/webassembly/reference/javascript_interface/global/value/index.md Outdated

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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FMI what happens if the conditions are not true? I mean it might fail, but what are the side effects, if any, of failure?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry, a TypeError is thrown. There are no side effects as such, just the value is not changed because it is immutable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wierd. From a docs point of view your fix is good though.

Comment thread files/en-us/webassembly/reference/javascript_interface/global/valueof/index.md Outdated
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Comment thread files/en-us/webassembly/reference/javascript_interface/global/valueof/index.md Outdated
Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hamishwillee, fancy a bit more Wasm? You know you do ;-)

I hate you :-)

Looks pretty good. A few nits/points for discussion.

chrisdavidmills and others added 3 commits April 24, 2026 09:51
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Copy link
Copy Markdown
Collaborator

@hamishwillee hamishwillee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 value property of a Global object instance allows you to directly get or set the global's value.

For the setter to work, the global must be mutable (the mutable option was set to true when it was declared). If this is not the case, a TypeError exception 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`.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the above, this is wrong because only some strings coerce to 0; others coerce to actual numeric values.

@bvisness
Copy link
Copy Markdown

The updates look good to me!

@chrisdavidmills
Copy link
Copy Markdown
Contributor Author

The updates look good to me!

Excellent. Let's get it merged!

@chrisdavidmills chrisdavidmills merged commit a21bf85 into mdn:main Apr 27, 2026
9 checks passed
@chrisdavidmills chrisdavidmills deleted the wasm-mutable-globals branch April 27, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:wasm WebAssembly docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants