Skip to content

Conversation

@abelstuker
Copy link
Contributor

Closes #299.

This extends the implementation of global variables in the VM with mutability and import / export semantics.
It provides encapsulation of globals, mutability checks and import resolution.
This change restructures the global variable to align with the accepted proposal https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md.

The host program can now define globals for import in the module as follows:

// Define some mutable global state (initialized to 0)
def_glob(some_mutable_state, I32, true, {.int32 = 0});
// Install the global
install_global(some_mutable_state);
;; Import the mutable global state
(global $some_mutable_state (import "env" "some_mutable_state") i32)

;; Modify the mutable global in a function
(global.set $some_mutable_state 2)

Conversely, the host can also access globals exported in the module:

;; Export some global state (initialized to 3)
(global $some_other_state (mut i32) (i32.const 3))
(export "some_other_state" (global $some_other_state))
// Retrieve the mutable global
uint32_t some_other_state_idx = this->get_export_global_idx(m, "some_other_state");
Global *some_other_state = m->globals[some_other_state_idx];
// Modify the mutable global
some_other_state->value->value.uint32 = 5;

@abelstuker abelstuker marked this pull request as ready for review November 14, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📑 Import/Export of Mutable Globals

2 participants