Stream: cargo-component

Topic: ✔ rules for wasi imports?


view this post on Zulip James Mart (Jan 03 2024 at 19:23):

Hi, new to rust->wasm component develoment.

Trying to understand this. If I build a new component following the rust component docs cargo component new --reactor add, then I extract the .wit file from the generated wasm, I see a very simple:

package root:component;

world root {
  export add: func(x: s32, y: s32) -> s32;
}

But if I now change the component to return a string rather than an s32, then I build the component with a new wit file:

package component:add;
world example {
    export add: func(x: s32, y: s32) -> string;
}

extracting the .wit file from the resulting components shows a new interface that includes all wasi imports:

package root:component;

world root {
  import wasi:cli/environment@0.2.0-rc-2023-12-05;
  import wasi:cli/exit@0.2.0-rc-2023-12-05;
  import wasi:io/error@0.2.0-rc-2023-11-10;
  import wasi:io/streams@0.2.0-rc-2023-11-10;
  import wasi:cli/stdin@0.2.0-rc-2023-12-05;
  import wasi:cli/stdout@0.2.0-rc-2023-12-05;
  import wasi:cli/stderr@0.2.0-rc-2023-12-05;
  import wasi:clocks/wall-clock@0.2.0-rc-2023-11-10;
  import wasi:filesystem/types@0.2.0-rc-2023-11-10;
  import wasi:filesystem/preopens@0.2.0-rc-2023-11-10;

  export add: func(x: s32, y: s32) -> string;
}

Is this expected? If so, why?

view this post on Zulip Peter Huene (Jan 03 2024 at 19:47):

My guess would be that the bindings generated to marshal the string pull in the panic infrastructure from the Rust standard library, which will call the WASI preview1 versions of things like getting environment variables, printing to the console, and exiting. cargo-component will automatically adapt the stdlib's preview1 calls to preview2

view this post on Zulip James Mart (Jan 03 2024 at 20:05):

Interesting, thanks

view this post on Zulip Notification Bot (Jan 03 2024 at 20:05):

James Mart has marked this topic as resolved.


Last updated: Nov 25 2024 at 19:03 UTC