global.set: Wasm text instruction
The global.set instruction sets the values of a global variable.
Try it
(module
(import "console" "log" (func $log (param i32)))
(global $var (mut i32) (i32.const 0))
(func $main
i32.const 10 ;; load a number onto the stack
global.set $var ;; set the $var
global.get $var ;; load $var onto the stack
call $log ;; log the result
)
(start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });
Syntax
global.set identifier
global.set-
The
global.setinstruction type. Must always be included first. identifier-
An identifier for the global whose value you want to set. This can be one of the following:
name-
The identifying name set for the global when it was first intialized. This must begin with a
$symbol, for example$my_global. index-
The global's index number, for example
0for the first global in the Wasm script,1for the second, etc.
Type
[new_value] -> []
Opcodes
| Instruction | Binary format | Example text => binary |
|---|---|---|
global.set |
0x24 |
global.set 0 => 0x24 0x00 |