Stream: general

Topic: How to define an instancetype with a resource?


view this post on Zulip Nathaniel Cook (Dec 08 2025 at 21:39):

How do I generate a instance type definition that includes a resource?

I have a wit interface like:

    interface df-interface {
        resource data-frame;
        fromcsv: func(path: string) -> data-frame;
        show: func(frame: data-frame) -> s64;
    }

My compiler generates an instance type like:

(type (;0;)
               (instance
                 (type (;0;) (resource (rep i32)))
                 (export (;1;) "DataFrame" (type (eq 0)))
                 (type (;2;) (func (param "path" string) (result 1)))
                 (export (;0;) "fromcsv" (func (type 2)))
                 (type (;3;) (resource (rep i32)))
                 (export (;4;) "DataFrame" (type (eq 3)))
                 (type (;5;) (func (param "frame" 4) (result s64)))
                 (export (;1;) "show" (func (type 5)))
               )
             )

The idea is that I can then import an instance of that type. However the wasmparser validation reports that resources can only be defined in concrete components (i.e. not from within the instance type definition)

I don't see another way to define the instance type while defining the resource outside of it.

Do I need to define these functions as methods on the dataframe resource? I plan to have many _constructors_ of a data-frame resource i.e. fromjson etc. So I decided it best to define functions instead.

view this post on Zulip Alex Crichton (Dec 08 2025 at 21:41):

For this you can use wasm-tools component wit -t which encodes a *.wit package in wasm. For example with this input:

package a:b;

interface df-interface {
  resource data-frame;
  fromcsv: func(path: string) -> data-frame;
  show: func(frame: data-frame) -> s64;
}

it outputs:

$ wasm-tools component wit foo.wit -t
(component
  (type (;0;)
    (component
      (type (;0;)
        (instance
          (export (;0;) "data-frame" (type (sub resource)))
          (type (;1;) (own 0))
          (type (;2;) (func (param "path" string) (result 1)))
          (export (;0;) "fromcsv" (func (type 2)))
          (type (;3;) (func (param "frame" 1) (result s64)))
          (export (;1;) "show" (func (type 3)))
        )
      )
      (export (;0;) "a:b/df-interface" (instance (type 0)))
    )
  )
  (export (;1;) "df-interface" (type 0))
  (@custom "package-docs" "\01{}")
  (@producers
    (processed-by "wit-component" "0.243.0")
  )
)

view this post on Zulip Nathaniel Cook (Dec 09 2025 at 00:06):

This worked, thanks for the help and direction to answer similar questions in the future. :pray:


Last updated: Jan 09 2026 at 13:15 UTC