WebAssembly/module-linking

Unclear how text format uses type index

Closed this issue · 2 comments

Related to #43 (comment)
I'm confused on how we can use the type index.

The following syntax is allowed, as <core:functype> allows (func (type <typeidx>))

(adapter module
  (type $F (func (result i32)))
  (import "answer" (func (type $F)))
)

But none of the following have any such equivalent of (<kind> (type <typeidx>))

  • <core:tabletype>
  • <core:memtype>
  • <core:globaltype>
  • <instancetype>
  • <moduletype>

Do we need to add these to deftype?

  • (table (type <typeidx>))
  • (memory (type <typeidx>))
  • (global (type <typeidx>))
  • (instance (type <typeidx>))
  • (module (type <typeidx>))

Or is this already implied as part of the typeuse blurb? (I really don't understand what is typeuse)

First: table, memory and global types are special in the core spec because they don't go in the type section -- rather, they go "inline" in the table, memory and global definitions, resp. Essentially, type definitions (and the type section) are purely an "optimization" for "factoring out" structural type definitions from their use sites, and there's not much point in allowing table, memory and global type definitions to be factored out. So those we should probably keep as-is, matching core wasm.

For instance and module types, you're right: the final text format should allow a (type <u32>) to be supplied in place of an <id>. The AST sketch in the explainer plays a little fast and loose here -- to define the real text format, we'd need to introduce typeuse, which would, at every use-site of a type, give you three options: (1) inline type definition (i.e., what's currently in the explainer; this would be an abbreviation for an out-of-line type definition), (2) an <id>, (3) a (type <typeidx>). I'm not sure we need to go into this level of specificity in the explainer though?

FWIW, Binary.md is completely explicit about where the indices go.

Thanks for taking the time to explain the table/memory bit. I see that now here https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format#using_the_tables