Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions component-model/examples/tutorial/csharp/adder/Component.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ExampleWorld.wit.exports.docs.adder.v0_1_0;

public class AddImpl : IAdd
{
public static uint Add(uint x, uint y)
{
return x + y;
}
}
8 changes: 8 additions & 0 deletions component-model/examples/tutorial/csharp/adder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Pull in all imports of the `hostapp` world, namely the `add` interface.
// example.component refers to the package name defined in the WIT file.
using HostappWorld.wit.imports.docs.adder.v0_1_0;

uint left = 1;
uint right = 2;
var result = AddInterop.Add(left, right);
Console.WriteLine($"{left} + {right} = {result}");
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package docs:[email protected];

interface add {
add: func(x: u32, y: u32) -> u32;
}

world example {
export add;
}

world hostapp {
import add;
}
46 changes: 2 additions & 44 deletions component-model/src/language-support/c.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,56 +321,14 @@ The following section requires you to have [a Rust toolchain][rust] installed.
> (The `wasmtime` version is specified in [the Cargo configuration file][cargo-config]
> for the example host.)

This repository contains an [example WebAssembly host][example-host] written in Rust
that can run components that implement the `adder` world.

1. `git clone https://github.com/bytecodealliance/component-docs.git`
2. `cd component-docs/component-model/examples/example-host`
3. `cargo run --release -- 1 2 <PATH>/adder.wasm`
* The double dashes separate the flags passed to `cargo` from
the flags passed in to your code.
* The arguments 1 and 2 are the arguments to the adder.
* In place of `<PATH>`, substitute the directory that contains your
generated `adder.wasm` file (or `adder.component.wasm` if you used
the manual instructions).

> [!NOTE]
> When hosts run components that use WASI interfaces, they must *explicitly*
> [add WASI to the linker][add-to-linker] to run the built component.
{{#include example-host-part1.md}}

A successful run should show the following output
(of course, the paths to your example host and adder component will vary,
and you should substitute `adder.wasm` with `adder.component.wasm`
if you followed the manual instructions above):

```
cargo run --release -- 1 2 adder.wasm
Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host)
Finished `release` profile [optimized] target(s) in 7.85s
Running `target/debug/example-host 1 2 /path/to/adder.wasm`
1 + 2 = 3
```

If *not* configured correctly, you may see errors like the following:

```
cargo run --release -- 1 2 adder.wasm
Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host)
Finished `release` profile [optimized] target(s) in 7.85s
Running `target/release/example-host 1 2 /path/to/adder.component.wasm`
Error: Failed to instantiate the example world

Caused by:
0: component imports instance `wasi:io/[email protected]`, but a matching implementation was not found in the linker
1: instance export `error` has the wrong type
2: resource implementation is missing
```

This kind of error normally indicates that the host in question does not satisfy WASI imports.

[host]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host
[add-to-linker]: https://docs.wasmtime.dev/api/wasmtime_wasi/p2/fn.add_to_linker_sync.html
[example-host]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host
{{#include example-host-part2.md}}

## 7. Run the component from C/C++ Applications

Expand Down
Loading