wasmex icon indicating copy to clipboard operation
wasmex copied to clipboard

Support interface imports

Open superchris opened this issue 6 months ago • 5 comments
trafficstars

Currently top level function interfaces are supported, but importing interfaces does not work. Need to implement this

superchris avatar Apr 23 '25 17:04 superchris

@superchris I was coding a bit on vacations and ended up implementing this :) (because I needed it for a project)

let me get the crappy version I have in shape and add tests :)

tessi avatar Apr 26 '25 19:04 tessi

@superchris https://github.com/tessi/wasmex/pull/764 landed! it does a fair bit more than just allowing you to shadow existing WASI imports. (it extends the component type conversion quite a bit) - most notably:

  • options are now :none or {:some, term()} instead of implicitly converting nil to None cases. This allows us to have a {:some, nil} which was not possible before
  • records allow for optional ok/error inner types. If there is no inner type, we return just :ok/:error instead of the respective tuples
  • records do not auto-convers casing anymore. I got into quite some hairy debugging with missing fields (my field had a number like foo-bar42 which the kebab casing didn't like as foo_bar42 ended up being converted to foo-bar-42. Long story short: I removed this magic and would say that we could implement an elixir-helper if we really want this behavior as an option
  • records now tell you about missing fields during type conversion instead of just crashing at runtime
  • a ton of missing type conversions implemented

I would let CI have a look and probably merge soon'ish. If you want to discuss changes, we can always adapt before the next release :)

tessi avatar Apr 27 '25 21:04 tessi

PR was merged, I'll let this issue open for you @superchris - when you had a chance to have a look (take your time! 🙏 ) and you're happy, feel free to close :)

tessi avatar Apr 30 '25 08:04 tessi

I have an issue, I try to configure the logging interface.

      Wasmex.Components.start_link(%{
        path: wasm,
        wasi: %Wasmex.Wasi.WasiP2Options{},
        imports: %{
          "wasi:logging/[email protected]" => %{
            "log" => {:fn, fn severity, ctx, message -> IO.inspect({severity, ctx, message}) end}
          }
        }
      })

I get this error:

{:error, "exported function `log` not found."}

Is there something I am missing?

ps: when i remove the imports I get ofcourse the correct error that it is not found:

{:error, "component imports instance `wasi:logging/[email protected]`, but a matching implementation was not found in the linker"}

extra context the logging interface looks like this:

package wasi:[email protected] {
  interface logging {
    enum level {
      trace,
      debug,
      info,
      warn,
      error,
      critical,
    }

    log: func(level: level, context: string, message: string);
  }
}

thomas9911 avatar Jun 03 '25 11:06 thomas9911

Ignore my comment, I was looking at the wrong problem! It works like a charm!

require Logger

Wasmex.Components.start_link(%{
  path: wasm,
  wasi: %Wasmex.Wasi.WasiP2Options{},
  imports: %{
    "wasi:logging/[email protected]" => %{
      "log" => {:fn, fn level, _context, message -> Logger.log(level, message) end}
    }
  }
})

thomas9911 avatar Jun 03 '25 12:06 thomas9911

closing this - the discussion about removing automatic field naming is continued in #852

tessi avatar Aug 28 '25 08:08 tessi