grow:Wasm 文本指令
grow 内存指令 会将内存实例的大小增加指定的页数。
如果操作成功,该指令会将内存的先前大小(以页为单位)添加到栈顶;如果操作失败,则会添加 -1。目前每页为 64KiB。
试一试
(module
(import "console" "log" (func $log (param i32)))
(memory 1 2) ;; start with one memory page, and max of 2 pages
(func $main
;; grow memory by 1 page
;; grow returns in 1 for success and -1 for failure
;; will fail if you change to more more than 1 page
(memory.grow (i32.const 1))
call $log ;; log the result
)
(start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });
语法
增长默认内存
wat
;; Grow default memory by a number of pages indicated by the top value on the stack
i32.const 3 ;; Number of pages to grow the memory (3)
memory.grow ;; Grow the memory (by 3 pages)
;; the top item on the stack will now either be the previous number of pages (success) or `-1` (failure)
;; grow default memory by two pages using an S-function
(memory.grow (i32.const 2))
增长指定内存(如果支持多内存)
wat
;; Grow memory with index 1
i32.const 1 ;; Number of pages to grow specified memory (1)
memory.grow (memory 1) ;; Grow memory index 1
;; Grow memory with name $memory1
i32.const 1 ;; Number of pages to grow specified memory (1)
memory.grow (memory $memory1) ;; Grow $memory1 by 1 page
;; Grow memory with name $memory1 by three pages using an S-function
(memory.grow (memory $memory1) (i32.const 3))
;; Will return -1 as max value is 4!
指令和操作码
| 指令 | 二进制操作码 |
|---|---|
memory.grow |
0x40 |
示例
增长默认内存
添加到 Wasm 模块的第一个内存是默认内存,索引为 0。我们可以通过首先添加一个指定内存增长量的变量,然后调用 grow 来增长此内存。
下面的代码显示了一个演示此的 WAT 文件
wat
(module
(import "console" "log" (func $log (param i32)))
(memory 1 2) ;; default memory with one page and max of 2 pages
(func $main
;; grow default memory by 1 page
i32.const 1
memory.grow
call $log ;; log the result (previous no. pages = 1)
;; grow default memory, using an S-function
(memory.grow (i32.const 1))
call $log ;; log the result (-1: max is 2 pages for default memory declared above!)
)
(start $main) ;; call immediately on loading
)
上面我们不需要在 grow 指令中指定内存索引,但我们可以使用默认内存的名称或索引(0)来做到这一点。下面的示例显示了这一点。
为了完整起见,我们可以使用上面文件 grow.wasm 的编译版本,其代码与下面所示类似(log 函数被导入到模块中,并由模块调用)
js
start();
async function start() {
const importObject = {
console: {
log(arg) {
console.log(arg);
},
},
};
const result = await WebAssembly.instantiateStreaming(
fetch("grow.wasm"),
importObject,
);
}
start();
增长指定内存
当内存定义在 Wasm 模块中时,它们会从零开始按顺序分配索引号。你可以通过在 grow 指令之后指定 memory 指令以及所需的索引或名称(如果它有的话)来增长特定内存。如果你不指定特定内存,则使用索引为 0 的默认内存。
下面的模块显示了如何直接按索引引用内存。
wat
(module
(import "console" "log" (func $log (param i32)))
(memory 1 2) ;; Default memory with one page and max of 2 pages
(memory $memory1 1 4) ;; Memory with index 1, initial 1 page, max 4 pages
(func $main
;; grow memory with index 1 by 1 page
i32.const 1
memory.grow (memory 1)
call $log ;; log the result (previous no. pages = 1)
)
(start $main)
)
$main 函数的主体也可以使用以下任何选项来编写
wat
i32.const 1
memory.grow (memory $memory1) ;; referencing memory by name
;; Using S-functions
(memory.grow (memory 1) (i32.const 1)) ;; reference memory by index
(memory.grow (memory $memory1) (i32.const 1)) ;; reference memory by name
我们在示例中没有使用默认内存。但如果你愿意,也可以选择指定此索引
wat
i32.const 1
memory.grow (memory 0) ;; referencing memory by index
;; Using S-functions
(memory.grow (memory 0) (i32.const 1)) ;; reference default memory by index
;; We can't reference this particular default memory by name, because it doesn't have one!
可以使用与第一个示例相同的 JavaScript 代码加载 WAT 文件。
规范
| 规范 |
|---|
| 未知规范 # syntax-instr-memory |
浏览器兼容性
webassembly.api.Memory.grow
加载中…
webassembly.multiMemory
加载中…
注意:multiMemory 兼容性表显示了 grow 可以与指定内存一起使用的版本。