init: Wasm table instruction
The table.init table instruction manually copies the references from a passive elem definition into a table.
The **`table.init`** [table instruction](/docs/WebAssembly/Reference/Table) manually copies the references from a [passive](/docs/WebAssembly/Reference/Definitions/elem#passive_form) [`elem`](/docs/WebAssembly/Reference/Definitions/elem) definition into a [`table`](/docs/WebAssembly/Reference/Definitions/table).
tabbed-taller
```wat interactive-example
(module
(table $return_values 2 funcref)
(func $f1 (result i32)
i32.const 42
)
(func $f2 (result i32)
i32.const 100
)
(elem $funcs funcref (ref.func $f1) (ref.func $f2))
(func (export "init")
i32.const 0 ;; destination table index
i32.const 0 ;; offset into elem segment
i32.const 2 ;; number of elements to copy
table.init $funcs
elem.drop $funcs
)
(func (export "accessTable") (param $index i32) (result i32)
local.get $index
call_indirect (result i32)
)
)
```
```js interactive-example
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}")).then((result) => {
result.instance.exports.init();
const value = result.instance.exports.accessTable(1);
console.log(value);
});
```
In the above example, we define a `table`, two functions, and an `elem` segment called `$funcs` that references the two functions. We then invoke `table.init` to copy the references from the `$funcs` `elem` over to the `table`, and drop the `elem` segment using [`elem.drop`](/docs/WebAssembly/Reference/Elem/drop) when it is no longer needed.
## Syntax
```plain
table.init table_identifier elem_identifier
```
- `table.init`
- : The `table.init` instruction type. Must always be included first.
- `table_identifier`
- : The identifier for the `table` you want to insert the references into. 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 defined. 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 omitted, `table_identifier` defaults to `0`.
- `elem_identifier`
- : The identifier for the `elem` you want to copy references from. This can be one of the following:
- `name`
- : An identifying name [set for the `elem`](/docs/WebAssembly/Reference/Definitions/elem#name) when it was first defined. This must begin with a `$` symbol, for example `$my_elem`.
- `index`
- : The `elem`'s index number, for example `0` for the first `elem` in the wasm module, `1` for the second, etc.
### Type
```plain
[dest_offset source_offset length] -> []
```
- `dest_offset`
- : An integer representing the table index to start copying the element references at. This will be an [`i32`](/docs/WebAssembly/Reference/Value_types/i32) or an [`i64`](/docs/WebAssembly/Reference/Value_types/i64), to match the [`index_type`](/docs/WebAssembly/Reference/Definitions/table#index_type) the `table` was defined with.
- `source_offset`
- : An `i32` representing the starting offset in the `elem` [`element_list`](/docs/WebAssembly/Reference/Definitions/elem#element_list) to start copying the element references from.
- `length`
- : An `i32` representing the number of references to copy.
### Traps
The `table.init` instruction traps if:
- The `dest_offset` plus the `length` exceeds the size of the `table`.
- The `source_offset` plus the `length` exceeds the size of the `elem` segment.
- The [`elem.drop`](/docs/WebAssembly/Reference/Elem/drop) instruction was previously called on the [`elem`](/docs/WebAssembly/Reference/Definitions/elem) segment referenced in [`elem_identifier`](#elem_identifier).
### Binary encoding
| Instruction | Binary format | Example text => binary |
| ------------ | ---------------------------------- | ----------------------------------------- |
| `table.init` | `0xfc 12:u32 x:tableidx x:elemidx` | `table.init 0 0` => `0xfc 0x0c 0x00 0x00` |
## Specifications
## Browser compatibility
## See also
- [`table`](/docs/WebAssembly/Reference/Definitions/table) definition
- [`elem`](/docs/WebAssembly/Reference/Definitions/elem) definition
Cite this
"init: Wasm table instruction". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Table/init JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy