load:Wasm 文本指令

load 内存指令用于从内存加载数字到堆栈。

有用于从内存加载到 i32i64f32f64load 指令。对于整数,有单独的指令变体,用于从内存加载更窄的有符号数字和无符号数字,并将其扩展为更宽的类型。例如,可以使用 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 可用于指定内存的版本。