Skip to content

Commit a787381

Browse files
committed
Revise C# language support section
Added more details where necessary. Factored code examples out into separate files and included them. Fixed a bug where the package name in the Program.cs file (in the "host-app" project) didn't match the package name in the .wit Factored out duplicated text in "Run the component from the example host" section that also appears in the C section; included this text from separate .md files.
1 parent c420ce1 commit a787381

File tree

9 files changed

+210
-176
lines changed

9 files changed

+210
-176
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AdderWorld;
2+
3+
public class AddImpl : IAdderWorld
4+
{
5+
public static uint Add(uint x, uint y)
6+
{
7+
return x + y;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ExampleWorld.wit.exports.docs.adder.v0_1_0;
2+
3+
public class AddImpl : IAdd
4+
{
5+
public static uint Add(uint x, uint y)
6+
{
7+
return x + y;
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Pull in all imports of the `hostapp` world, namely the `add` interface.
2+
// example.component refers to the package name defined in the WIT file.
3+
using HostappWorld.wit.imports.docs.adder.v0_1_0;
4+
5+
uint left = 1;
6+
uint right = 2;
7+
var result = AddInterop.Add(left, right);
8+
Console.WriteLine($"{left} + {right} = {result}");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package docs:adder@0.1.0;
2+
3+
interface add {
4+
add: func(x: u32, y: u32) -> u32;
5+
}
6+
7+
world example {
8+
export add;
9+
}
10+
11+
world hostapp {
12+
import add;
13+
}

component-model/src/language-support/c.md

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -321,56 +321,14 @@ The following section requires you to have [a Rust toolchain][rust] installed.
321321
> (The `wasmtime` version is specified in [the Cargo configuration file][cargo-config]
322322
> for the example host.)
323323
324-
This repository contains an [example WebAssembly host][example-host] written in Rust
325-
that can run components that implement the `adder` world.
326-
327-
1. `git clone https://github.com/bytecodealliance/component-docs.git`
328-
2. `cd component-docs/component-model/examples/example-host`
329-
3. `cargo run --release -- 1 2 <PATH>/adder.wasm`
330-
* The double dashes separate the flags passed to `cargo` from
331-
the flags passed in to your code.
332-
* The arguments 1 and 2 are the arguments to the adder.
333-
* In place of `<PATH>`, substitute the directory that contains your
334-
generated `adder.wasm` file (or `adder.component.wasm` if you used
335-
the manual instructions).
336-
337-
> [!NOTE]
338-
> When hosts run components that use WASI interfaces, they must *explicitly*
339-
> [add WASI to the linker][add-to-linker] to run the built component.
324+
{{#include example-host-part1.md}}
340325

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

346-
```
347-
cargo run --release -- 1 2 adder.wasm
348-
Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host)
349-
Finished `release` profile [optimized] target(s) in 7.85s
350-
Running `target/debug/example-host 1 2 /path/to/adder.wasm`
351-
1 + 2 = 3
352-
```
353-
354-
If *not* configured correctly, you may see errors like the following:
355-
356-
```
357-
cargo run --release -- 1 2 adder.wasm
358-
Compiling example-host v0.1.0 (/path/to/component-docs/component-model/examples/example-host)
359-
Finished `release` profile [optimized] target(s) in 7.85s
360-
Running `target/release/example-host 1 2 /path/to/adder.component.wasm`
361-
Error: Failed to instantiate the example world
362-
363-
Caused by:
364-
0: component imports instance `wasi:io/[email protected]`, but a matching implementation was not found in the linker
365-
1: instance export `error` has the wrong type
366-
2: resource implementation is missing
367-
```
368-
369-
This kind of error normally indicates that the host in question does not satisfy WASI imports.
370-
371-
[host]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host
372-
[add-to-linker]: https://docs.wasmtime.dev/api/wasmtime_wasi/p2/fn.add_to_linker_sync.html
373-
[example-host]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host
331+
{{#include example-host-part2.md}}
374332

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

0 commit comments

Comments
 (0)