copy: Wasm table instruction
The table.copy table instruction copies element references from one table location to another.
Try it
(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)
)
)
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 definition.
We then define two exported functions:
copy(), which uses atable.copyinstruction to copy the two references from$first_tableinto the second and third element slots of$second_table.accessTable(), which calls (viacall_indirect) the element referenced in$second_tablein 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
table.copy dest_table source_table
table.copy-
The
table.copyinstruction type. Must always be included first. dest_tableOptional-
The identifier for the
tableyou want to copy the references into. source_tableOptional-
The identifier for the
tableyou want to copy the references from.
dest_table and source_table can be one of the following:
name-
An identifying name set for the
tablewhen it was first defined. This must begin with a$symbol, for example$my_table. index-
The
table's index number, for example0for the firsttablein the wasm module,1for the second, etc.
If dest_table or source_table are omitted, they default to 0.
Type
[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
i32or ani64, to match theindex_typethetablewas defined with. source_offset-
An integer representing the offset to start copying element references from, in the source table. This will be an
i32or ani64, to match theindex_typethetablewas defined with. length-
An integer representing the number of references to copy. This will be an
i32or ani64, to match theindex_typethetablewas defined with. When copying between a 32-bit index table and a 64-bit index table, ani32must be used for thelength.
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
| Specification |
|---|
| WebAssembly Core Specification> # -hrefsyntax-instr-tablemathsftablecopyxy> |
Browser compatibility
See also
tabledefinition