const: Wasm-Text-Instruktion
Die const WebAssembly numerische Instruktion wird verwendet, um Zahlen zu deklarieren.
Probieren Sie es aus
(module
(import "console" "log" (func $log (param i32)))
(func $main
i32.const 10 ;; load a number onto the stack
call $log ;; log the number
)
(start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });
Syntax
value_type.const
value_type-
Der Typ des Wertes, auf dem die Instruktion ausgeführt wird. Die folgenden Typen unterstützen
const:i32i64f32f64v128
const-
Die
const-Instruktion. Muss immer nach demvalue_typeund einem Punkt (.) eingefügt werden.
Typ
[] -> [value]
Binäre Kodierung
| Instruktion | Binäres Äquivalent | Beispieltext => binär |
|---|---|---|
i32.const |
0x41 i:leb128 |
i32.const 2 => 0x41 0x02 |
i64.const |
0x42 i:leb128 |
i64.const 1 => 0x42 0x01 |
f32.const |
0x43 f:float32 |
f32.const 2.0 => 0x43 0x00 0x00 0x00 0x40 |
f64.const |
0x44 f:float64 |
f64.const 1.0 => 0x44 0x00 0x00 0x00 0x00 0x00 0x00 0xF0 0x3F |
v128.const |
0xFD 0x0C (b:byte)¹⁶ |
v128.const f32x4 0x9 0xa 0xb 0xc => 0xFD 0x0C 0x00 0x00 0x10 0x41 0x00 0x00 0x20 0x41 0x00 0x00 0x30 0x41 0x00 0x00 0x40 0x41 |