e01.org

copy: Wasm table instruction

The table.copy table instruction copies element references from one table location to another.

The **`table.copy`** [table instruction](/docs/WebAssembly/Reference/Table) copies element references from one [`table`](/docs/WebAssembly/Reference/Definitions/table) location to another.

tabbed-taller

```wat interactive-example
(module
(table $first_table 2 funcref)
(table $second_table 3 funcref)

(func $f1 (result i32)
i32.const 42
)
(func $f2 (result i32)
i32.const 100
)

(elem $funcs (table $first_table) (i32.const 0) funcref (ref.func $f1) (ref.func $f2))

(func (export "copy")
i32.const 1 ;; destination table offset
i32.const 0 ;; source table offset
i32.const 2 ;; number of elements to copy
table.copy $second_table $first_table
)

(func (export "accessTable") (param $index i32) (result i32)
local.get $index
call_indirect $second_table (result i32)
)
)
```

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

In the above example, we define two tables: `$first_table` with a capacity of two elements, and `$second_table` with a capacity of three elements. We then define two functions that each return a different integer and store those functions in `$first_table` using an active [`elem`](/docs/WebAssembly/Reference/Definitions/elem) definition.

We then define two exported functions:

- `copy()`, which uses a `table.copy` instruction to copy the two references from `$first_table` into the second and third element slots of `$second_table`.
- `accessTable()`, which calls (via `call_indirect`) the element referenced in `$second_table` in the slot number equal to the function's parameter. It then returns the value returned by the called function.

In the JavaScript, we call the two Wasm functions, then log the value returned from `accessTable()` to the console, which is equal to the return value of the function stored in the third slot of `$second_table`.

## Syntax

```plain
table.copy dest_table source_table
```

- `table.copy`
- : The `table.copy` instruction type. Must always be included first.
- `dest_table`
- : The identifier for the `table` you want to copy the references into.
- `source_table`
- : The identifier for the `table` you want to copy the references from.

`dest_table` and `source_table` 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 `dest_table` or `source_table` are omitted, they default to `0`.

### Type

```plain
[dest_offset source_offset length] -> []
```

- `dest_offset`
- : An integer representing the offset to start writing the copied element references to, in the destination table. 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 integer representing the offset to start copying element references from, in the source table. This will be an `i32` or an `i64`, to match the `index_type` the `table` was defined with.
- `length`
- : An integer representing the number of references to copy. This will be an `i32` or an `i64`, to match the `index_type` the `table` was defined with. When copying between a 32-bit index table and a 64-bit index table, an `i32` must be used for the `length`.

> [!NOTE]
> `table.copy` copies references in an overlap-aware way. In other words, the copy proceeds as if the source references identified by `source_offset` and `length` was first copied into a temporary value before being copied to `dest_offset`, meaning that if your source and destination data overlap, they don't interfere with one another and the source is cleanly copied into the destination region as expected.

### Traps

If any copied element reference would be out of bounds in the source or destination, the instruction traps.

### Binary encoding

| Instruction | Binary format | Example text => binary |
| ------------ | ----------------------------------- | ----------------------------------------- |
| `table.copy` | `0xfc 14:u32 x:tableidx x:tableidx` | `table.copy 0 0` => `0xfc 0x0e 0x00 0x00` |

## Specifications

## Browser compatibility

## See also

- [`table`](/docs/WebAssembly/Reference/Definitions/table) definition

source: MDN Web Docs mirror · page

Cite this

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

JSON record · API collection · citation policy