e01.org

grow: Wasm table instruction

The table.grow table instruction increases the size of a table by a specified number of elements.

The **`table.grow`** [table instruction](/docs/WebAssembly/Reference/Table) increases the size of a table by a specified number of elements.

tabbed-taller

```wat interactive-example
(module
;; Define function type
(type $ret_i32 (func (result i32)))

;; table with 0 function slots
(table $return_funcs 0 funcref)

;; Define a function of the specified type
(func $f1 (type $ret_i32)
(i32.const 42)
)

(elem declare func $f1)

(func $populate
(table.grow $return_funcs
ref.null func
(i32.const 1)
)
(drop)
(table.set $return_funcs
(i32.const 0)
(ref.func $f1)
)
)

(func (export "run") (result i32)
(call $populate)
(call_indirect (type $ret_i32) (i32.const 0))
)
)
```

```js interactive-example
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}")).then((result) => {
const value = result.instance.exports.run();
console.log(value);
});
```

## Syntax

```plain
table.grow identifier
```

- `table.grow`
- : The `table.grow` instruction type. Must always be included first.
- `identifier`
- : An identifier for the table you want to grow. This can be one of the following:
- `name`
- : An identifying name [set for the table](/docs/WebAssembly/Reference/Definitions/table#name) when it was first created. This must begin with a `$` symbol, for example `$my_table`.
- `index`
- : The table's index number, for example `0` for the first table in the wasm module, `1` for the second, etc.

If the `identifier` is omitted, it will default to `0`.

### Type

```plain
[initial_value, grow_amount] -> [previous_length]
```

- `initial_value`
- : The initial value to set for the new table elements. Its type must be the same as the [`type`](/docs/WebAssembly/Reference/Definitions/table#type) initially set when the table was created.
- `grow_amount`
- : The number of elements to grow the table by. This must be an `i32` value, for example `(i32.const 1)`.
- `previous_length`
- : An `i32` equal to the size of the table before the `grow` instruction is applied to it, or `-1` if the table failed to grow, for example due to an out-of-memory (OOM) error or the new size being greater than the [table's maximum size](/docs/WebAssembly/Reference/Definitions/table#max_size).

To retrieve the new table size after the `grow` instruction is applied to it, use the [`table.size`](/docs/WebAssembly/Reference/Table/size) instruction.

### Opcodes

| Instruction | Binary format | Example text => binary |
| ------------ | ------------------------- | ------------------------------------------------------------------------------------ |
| `table.grow` | `0xfc 15:u32 𝑥:table_idx` | `(table.grow (i32.const 1) (ref.null func))` => `0xfc 0x0f 0x00 0x41 0x01 0xd0 0x70` |

## Description

`table.grow` is used to increase the size of a table by a specified number of elements.

A wasm table can be grown from JavaScript using the [`table.grow()`](/docs/WebAssembly/Reference/JavaScript_interface/Table/grow) method.

## Examples

### Creating, growing, and setting a table

This example shows how to create a table, grow its size, dynamically change the functions stored in it, and then call the function stored in the table at each point.

#### JavaScript

In our script, we start by grabbing a reference to a p element that we will output results to. We then define an `obj` object containing a function called `output()` that adds a given value to the `textContent` of a given element.

We then compile and instantiate our Wasm module using the [`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) method, importing the `obj` object in the process.

When the result is returned, we invoke the exported Wasm `run()` function available on the WebAssembly [`Instance`](/docs/WebAssembly/Reference/JavaScript_interface/Instance) [`exports`](/docs/WebAssembly/Reference/JavaScript_interface/Instance/exports) object, passing it the `outputElem` element as a parameter.

```html hidden live-sample___basic-usage
<p></p>
```

```js live-sample___basic-usage
const outputElem = document.querySelector("p");

const obj = {
output(elem, val) {
elem.textContent += `${val} `;
},
};

WebAssembly.instantiateStreaming(fetch("{%wasm-url%}"), {
obj,
}).then((result) => {
value = result.instance.exports.run(outputElem);
});
```

#### Wasm

In our Wasm module, we first import the JavaScript `output()` function, making sure to declare that it has two parameters, an [`externref`](/docs/WebAssembly/Reference/Value_types/externref) and an `i32`.

Next, we define a function `type` called `$ret_i32`, which returns an `i32` value. We then define two functions based on this type called `$f1` and `$f2`, which return the values defined within, and forward-declare them using `(elem declare func $f1 $f2)` so they can be referenced later on. Next, we define a `table` called `$func_table`, which stores function references (hence `funcref` being specified) and is initially empty.

Finally, we export the `run()` function, which takes an `externref` named `$elem` as a parameter. Inside the function body, we:

- Use `table.grow` to grow the table size by `1`, with an initial `ref.null` value, checking whether the operation result is `-1`, which would indicate failure.
- Set our table element to contain the `$f1` function using [`table.set`](/docs/WebAssembly/Reference/Table/set), then call the imported `$output` function, passing it as parameters the `$elem` `externref` passed into the `output()` function, and the value returned by the `$f1` function, which is being referenced from the table using `(call_indirect (type $ret_i32) (i32.const 0))`.
- Set our table element to contain the `$f2` function using `table.set`, then call the `output()` function again.

```wat live-sample___basic-usage
(module
;; Import output function
(import "obj" "output" (func $output (param externref) (param i32)))

;; Define function type
(type $ret_i32 (func (result i32)))

;; Define basic functions that return i32s
(func $f1 (result i32)
(i32.const 42)
)
(func $f2 (result i32)
(i32.const 100)
)

(elem declare func $f1 $f2)

;; Define an initially empty table of funcrefs
(table $func_table 0 funcref)

(func (export "run") (param $elem externref)
;; Grow the table by 1, setting the initial value to null.
;; Check the result for -1, which indicates failure.
(if
(i32.eq
(table.grow $func_table
ref.null func
(i32.const 1)
)
i32.const -1
)

;; Trap if we failed to grow the table
(then unreachable)
)

;; Set the first function in the table to f1
(table.set $func_table
(i32.const 0)
(ref.func $f1)
)

;; Call the output function, to output the table
;; function's return value to the DOM
(call $output
(local.get $elem)
(call_indirect (type $ret_i32) (i32.const 0))
)

;; Set the first function in the table to f2
(table.set $func_table
(i32.const 0)
(ref.func $f2)
)

;; Call the output function, to output the table
;; function's return value to the DOM
(call $output
(local.get $elem)
(call_indirect (type $ret_i32) (i32.const 0))
)
)
)
```

#### Result

The output is as follows:

100%

This makes sense, as each time the `output()` function is run from inside the wasm module, the value passed into it as its second parameter is printed into our result `<p>` in the DOM. Each value is the value returned by the `$f1` and `$f2` functions — `42` and `100` respectively.

## Specifications

## Browser compatibility

## See also

- [`table.set`](/docs/WebAssembly/Reference/Table/set)
- [`table.size`](/docs/WebAssembly/Reference/Table/size)

source: MDN Web Docs mirror · page

Cite this

"grow: Wasm table instruction". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Table/grow

JSON record · API collection · citation policy