WebAssembly.Table.prototype.grow()

Baseline 已廣泛支援

此功能已非常成熟,可在多種裝置和瀏覽器版本上使用。自 2017 年 10 月以來,它已在各大瀏覽器中可用。

grow() 屬性方法 WebAssembly.Table 物件將指定數量的元素增加Table例項的大小,並使用提供的值進行填充。

語法

js
grow(delta)
grow(delta, value)

引數

delta

您希望表擴充套件的元素數量。

value 可選

用於填充新分配空間的元素。

返回值

表的先前長度。

異常

RangeError

在以下情況之一中丟擲

  • 如果當前大小加上delta超過了Table例項的最大大小容量。
  • 如果客戶端沒有足夠的記憶體進行分配。
TypeError

如果value不是表的元素型別值,則丟擲。

示例

使用 grow

以下示例建立了一個新的 WebAssembly Table 例項,初始大小為 2,最大大小為 10

js
const table = new WebAssembly.Table({
  element: "anyfunc",
  initial: 2,
  maximum: 10,
});

使用 Table.grow() 將表擴充套件 1 個元素

js
console.log(table.length); // 2
table.grow(1);
console.log(table.length); // 3

使用帶值的 grow

以下示例建立了一個新的 WebAssembly Table 例項,初始大小為 0,最大大小為 4,並用一個物件填充它

js
const myObject = { hello: "world" };

const table = new WebAssembly.Table({
  element: "externref",
  initial: 0,
  maximum: 4,
});

使用 WebAssembly.grow() 將表擴充套件 4 個單位並用值填充

js
table.grow(4, myObject);
console.log(myObject === table.get(2)); // true

規範

規範
WebAssembly JavaScript 介面
# dom-table-grow

瀏覽器相容性

另見