3

Can someone please share the syntax to create a table statically having certain elements and reference it? I could not find any web assembly code doing so.

Also some details like can I have multiple columns, what happens if index doesn't lie in the table etc. would be helpful?

2
  • It's not clear from the question what kind of table do you want. Is it a constant data or dynamic? Also, which language do you use for webassembly sources? Looks like you're imaging a database table, but it doesn't work this way for wasm. Commented Nov 14, 2017 at 17:47
  • @nzeemin I am not exactly sure by what you mean by source language. I am writing web assembly s-exp. Table has constant data. I'll not modify it at run time. I only want to refer based on indexes at run time. Commented Nov 14, 2017 at 19:07

1 Answer 1

8

(Updated for Wasm 2.0 in in 2022)

In the text format, a table is declared with its table type, i.e., (initial) size and element type -- currently funcref and externref are the only supported types:

(table $name 100 funcref)

Since Wasm 2.0, there can be multiple tables in a module.

A table can be initialised within a module by providing one or more active element segments:

(elem (i32.const 0) $f1 $f2 $f3)

where the expression denotes the offset and is followed by a list of functions defined in the module.

Since Wasm 2.0, tables can furhter be accessed and modified through instructions like table.get, table.set, table.size, table.grow, etc.

Before 2.0, the only way to use a table was through the call_indirect instruction:

(call_indirect (type $t) (arg1) ... (argn) (index))

You can find a few simple examples in the Wasm spec test suite, e.g. here:

A table does not have columns. As for execution, out of bounds access causes a trap, as does accessing an uninitialised index or a type mismatch.

Sign up to request clarification or add additional context in comments.

3 Comments

"currently funcref and externref are the only supported typse", perhaps you meant anyfunc and externref?
@mallwright, I think anyfunc is only still used in the JS API, for historical reasons. Inside Wasm itself, it is known as funcref.
It seems like the two are not compatible though (see: stackoverflow.com/q/74044249/5164339)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.