load:Wasm 文字指令
load 記憶體指令用於從記憶體載入數字到堆疊。
有用於從記憶體載入到 i32、i64、f32 和 f64 的 load 指令。對於整數,有單獨的指令變體,用於從記憶體載入更窄的有符號數字和無符號數字,並將其擴充套件為更寬的型別。例如,可以使用 i32.load8_u 載入一個無符號的 8 位數字並將其轉換為 i32。所有變體都 列在下方。
試一試
(module
(memory $memory 1)
(export "memory" (memory $memory))
(func (export "load_first_item_in_mem") (param $num i32) (result i32)
i32.const 0
;; load first item in memory and return the result
i32.load
)
)
const url = "{%wasm-url%}";
const result = await WebAssembly.instantiateStreaming(fetch(url));
const load_first_item_in_mem = result.instance.exports.load_first_item_in_mem;
const memory = result.instance.exports.memory;
const dataView = new DataView(memory.buffer);
// Store 30 at the beginning of memory
dataView.setUint32(0, 30, true);
console.log(load_first_item_in_mem(100));
// Expected output: 30
語法
從預設記憶體載入
wat
;; Load from default memory at offset specified by value on top of stack
i32.const 0 ;; Stack variable containing memory offset (0) of number to be loaded.
i32.load ;; Load from specified offset in default memory
;; Load from same location using an S-function
(i32.load (i32.const 0))
從指定記憶體載入(如果支援多記憶體)
wat
;; Load from memory specified by index
i32.const 0 ;; offset in memory to load from (0)
i32.load (memory 1) ;; load from memory index 1
;; Load from memory specified by name
i32.const 1 ;; offset in memory to load from (1)
i32.load (memory $memory1) ;; load from named memory $memory1
;; Load from memory specified by name using an S-function
(i32.load (memory $memory1) (i32.const 0))
指令和操作碼
| 指令 | 二進位制操作碼 |
|---|---|
i32.load |
0x28 |
i64.load |
0x29 |
f32.load |
0x2a |
f64.load |
0x2b |
i32.load8_s |
0x2c |
i32.load8_u |
0x2d |
i32.load16_s |
0x2e |
i32.load16_u |
0x2f |
i64.load8_s |
0x30 |
i64.load8_u |
0x31 |
i64.load16_s |
0x32 |
i64.load16_u |
0x33 |
i64.load32_s |
0x34 |
i64.load32_u |
0x35 |
示例
從預設記憶體載入項
新增到 Wasm 模組的第一個記憶體是預設記憶體,索引為 0。我們可以透過新增一個變數來指定要載入數字在預設記憶體中的偏移量到堆疊,然後呼叫 load 來從該記憶體載入。
下面的程式碼顯示了一個演示此的 WAT 檔案
wat
(module
;; Define memory named $memory and export
(memory $memory 1) ;; First memory declared is default, with index 0
(export "memory" (memory $memory))
;; Exported function to load first item in default memory
(func (export "load_first_item_in_mem") (param $num i32) (result i32)
;; load 0-offset item in memory and return the result
i32.const 0
i32.load
)
)
上面我們不需要在 load 指令中指定記憶體,但我們可以使用預設記憶體的名稱或索引來完成。這在下面的示例中顯示。
為了完整起見,我們可以使用上面檔案 load_single.wasm 的編譯版本,其程式碼與下面所示類似
js
// await on the specified .wasm file to be fetched and loaded
const result = await WebAssembly.instantiateStreaming(
fetch("load_single.wasm"),
);
// Get the exported function that we will call below
const load_first_item_in_mem = result.instance.exports.load_first_item_in_mem;
// Get the exported memory and store 30 at the 0 offset
const memory = result.instance.exports.memory;
const dataView = new DataView(memory.buffer);
dataView.setUint32(0, 30, true);
// Log the result of calling the exported Wasm function
console.log(load_first_item_in_mem(100)); // 30
從指定記憶體載入項
當記憶體定義在 Wasm 模組中時,它們會按順序分配從零開始的索引號。您可以透過在 load 指令之後指定 memory 指令和所需的索引或名稱來從特定記憶體載入。如果您不指定特定記憶體,則使用索引為 0 的預設記憶體。
下面的模組顯示了您如何直接透過索引引用記憶體。
wat
(module
;; Define memory for the module
(memory $memory0 1) ;; First (default) memory with memory index 0 (and 1 page)
(memory $memory1 1) ;; Second memory with index 1, named $memory1
(export "memory" (memory $memory1)) ;; Export $memory1
;; Exported function to load first item in default memory
(func (export "load_first_item_in_mem") (param $num i32) (result i32)
;; load 0-offset item in memory index 1 and return the result
i32.const 0
i32.load (memory 1)
)
)
函式體也可以使用以下任何選項編寫
wat
i32.const 0
i32.load (memory $memory1) ;; referencing memory by name
;; Using S-functions
(i32.load (memory 1) (i32.const 0)) ;; reference memory by index
(i32.load (memory $memory1) (i32.const 0)) ;; reference memory by name
我們在示例中沒有使用預設記憶體。但您也可以選擇指定此索引,如果您願意的話
wat
i32.const 0
i32.load (memory 0) ;; referencing memory by index
;; Using S-functions
(i32.load (i32.const 0))
(i32.load (memory 0) (i32.const 0)) ;; reference memory by index
(i32.load (memory $memory0) (i32.const 0)) ;; reference memory by name
可以使用與第一個示例相同的 JavaScript 程式碼載入 WAT 檔案。
規範
| 規範 |
|---|
| 未知規範 # syntax-instr-memory |
瀏覽器相容性
webassembly.api.Memory
載入中…
webassembly.multiMemory
載入中…
注意: multiMemory 相容性表指示了 load 可用於指定記憶體的版本。