size: Wasm table instruction

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since November 2021.

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

Try it

(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)
  )
)
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}")).then((result) => {
  const value = result.instance.exports.run();
  console.log(value);
});

Syntax

table.size identifier
table.size

The table.size instruction type. Must always be included first.

identifier Optional

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 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 script, 1 for the second, etc.

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

Type

[] -> [length]
length

An i32 equal to the current number of elements contained inside the table.

Opcodes

Instruction Binary opcode
table.size 𝟶𝚡𝙵𝙲 16:𝚞𝟹𝟸 (variable-width LEB128)

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 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() 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 exports object, passing it the outputElem element as a parameter.

js
const outputElem = document.querySelector("p");

const obj = {
  output: function (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 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
(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:

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

Specification
Unknown specification
# syntax-instr-table

Browser compatibility

See also