e01.org

size: Wasm table instruction

The table.size table instruction returns the current size of the table.

The **`table.size`** [table instruction](/docs/WebAssembly/Reference/Table) returns the current size of the table.

tabbed-taller

```wat interactive-example
(module
;; table with 0 function slots
(table $my_table 0 funcref)

(func (export "run") (result i32)
;; Grow the table by 1, setting the initial values to null.
(table.grow $my_table
ref.null func
(i32.const 1)
)
(drop)

(table.size $my_table)
)
)
```

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

## Syntax

```plain
table.size identifier
```

- `table.size`
- : The `table.size` instruction type. Must always be included first.
- `identifier`
- : The identifier for the table you want to retrieve the size of. 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
[] -> [length]
```

- length
- : An `i32` equal to the current number of elements contained inside the table.

### Opcodes

| Instruction | Binary format | Example text => binary |
| ------------ | ------------------------- | ---------------------------------- |
| `table.size` | `0xFC 16:u32 𝑥:table_idx` | `table.size 0` => `0xfc 0x10 0x00` |

## Description

`table.size` is used to return the size of a table.

A wasm table size can be retrieved via JavaScript using the [`table.length`](/docs/WebAssembly/Reference/JavaScript_interface/Table/length) property.

## Examples

### Observing increases in table size

This example shows how to create a table and observe its size as the table grows using `table.size`.

#### 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 `table` that stores function references (hence `funcref` being specified) and is 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.
- Call the imported `$output` function, passing it as parameters the `$elem` `externref` passed into the `output()` function, and the return value of the `table.size` instruction. This results in the table size being outputted to the DOM.
- Repeat the last two steps again, resulting in the table being grown by one more element, and the size being outputted to the DOM again.

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

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

(func (export "run") (param $elem externref)
;; Grow the table by 1, setting the initial values to null.
(table.grow
ref.null func
(i32.const 1)
)
(drop)

;; Call the output function, to output the table size to the DOM
(call $output
(local.get $elem)
(table.size)
)

;; Grow the table by 1, setting the initial values to null.
(table.grow
ref.null func
(i32.const 1)
)
(drop)

;; Call the output function, to output the table size to the DOM
(call $output
(local.get $elem)
(table.size)
)
)
)
```

#### 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 table size at each point — `1` and `2` respectively.

## Specifications

## Browser compatibility

## See also

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

source: MDN Web Docs mirror · page

Cite this

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

JSON record · API collection · citation policy