Skip to content

Commit 75c6cd7

Browse files
committed
Add Dart documentation
1 parent 0117479 commit 75c6cd7

11 files changed

Lines changed: 426 additions & 3 deletions

File tree

‎component-model/src/SUMMARY.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Building a simple component](./building-a-simple-component.md)
2323
- [C/C++](./language-support/building-a-simple-component/c.md)
2424
- [C#](./language-support/building-a-simple-component/csharp.md)
25+
- [Dart](./language-support/building-a-simple-component/dart.md)
2526
- [Go](./language-support/building-a-simple-component/go.md)
2627
- [JavaScript](./language-support/building-a-simple-component/javascript.md)
2728
- [Python](./language-support/building-a-simple-component/python.md)
@@ -31,16 +32,19 @@
3132
- [WebAssembly Text Format (WAT)](./language-support/building-a-simple-component/wat.md)
3233
- [Other Languages](./language-support/building-a-simple-component/other-languages.md)
3334
- [Importing and reusing components](./importing-and-reusing-components.md)
35+
- [Dart](./language-support/importing-and-reusing-components/dart.md)
3436
- [Rust](./language-support/importing-and-reusing-components/rust.md)
3537
- [Javascript](./language-support/importing-and-reusing-components/javascript.md)
3638
- [Other Languages](./language-support/importing-and-reusing-components/other-languages.md)
3739
- [Creating runnable components](./creating-runnable-components.md)
40+
- [Dart](./language-support/creating-runnable-components/dart.md)
3841
- [Rust](./language-support/creating-runnable-components/rust.md)
3942
- [Javascript](./language-support/creating-runnable-components/javascript.md)
4043
- [Other languages](./language-support/creating-runnable-components/other-languages.md)
4144
- [Using WIT resources](./using-wit-resources.md)
4245
- [Rust](./language-support/using-wit-resources/rust.md)
4346
- [Using HTTP in components](./using-http-in-components.md)
47+
- [Dart](./language-support/using-http-in-components/dart.md)
4448
- [Rust](./language-support/using-http-in-components/rust.md)
4549
- [Running Components](./running-components.md)
4650
- [Wasmtime](./running-components/wasmtime.md)

‎component-model/src/building-a-simple-component.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This guide is implemented for various languages:
1515
|----------------------------------------------------------------------------------------|
1616
| [C/C++](./language-support/building-a-simple-component/c.md) |
1717
| [C#](./language-support/building-a-simple-component/csharp.md) |
18+
| [Dart](./language-support/building-a-simple-component/dart.md) |
1819
| [Go](./language-support/building-a-simple-component/go.md) |
1920
| [JavaScript](./language-support/building-a-simple-component/javascript.md) |
2021
| [Python](./language-support/building-a-simple-component/python.md) |

‎component-model/src/creating-runnable-components.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This guide is implemented for various languages:
5353

5454
| Language |
5555
|---------------------------------------------------------------------------------------|
56+
| [Dart](./language-support/creating-runnable-components/dart.md) |
5657
| [Rust](./language-support/creating-runnable-components/rust.md) |
5758
| [Javascript](./language-support/creating-runnable-components/javascript.md) |
5859
| [Other Languages](./language-support/creating-runnable-components/other-languages.md) |

‎component-model/src/importing-and-reusing-components.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This guide is implemented for various languages:
2121

2222
| Language |
2323
|-------------------------------------------------------------------------------------------|
24+
| [Dart](./language-support/importing-and-reusing-components/dart.md) |
2425
| [Rust](./language-support/importing-and-reusing-components/rust.md) |
2526
| [Javascript](./language-support/importing-and-reusing-components/javascript.md) |
2627
| [Other languages](./language-support/importing-and-reusing-components/other-languages.md) |

‎component-model/src/introduction.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ This documentation is aimed at _users_ of the component model: developers of lib
1515
|--------------------------|----------------------|-------------------|
1616
| [Why Components?] | [C/C++] | [Composing] |
1717
| [Components] | [C#] | [Running] |
18-
| [Interfaces] | [Go] | [Distributing] |
19-
| [Worlds] | [JavaScript] | |
18+
| [Interfaces] | [Dart] | [Distributing] |
19+
| [Worlds] | [Go] | [Distributing] |
20+
| | [JavaScript] | |
2021
| | [Python] | |
2122
| | [Rust] | |
22-
| | [MoonBit] | |
23+
| | [MoonBit] | |
2324

2425
[Why Components?]: ./design/why-component-model.md
2526
[Components]: ./design/components.md
@@ -28,6 +29,7 @@ This documentation is aimed at _users_ of the component model: developers of lib
2829

2930
[C/C++]: ./language-support/building-a-simple-component/c.md
3031
[C#]: ./language-support/building-a-simple-component/csharp.md
32+
[Dart]: ./language-support/building-a-simple-component/dart.md
3133
[Go]: ./language-support/building-a-simple-component/go.md
3234
[JavaScript]: ./language-support/building-a-simple-component/javascript.md
3335
[Python]: ./language-support/building-a-simple-component/python.md

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ without using a higher-level language front-end.
2727
- [Building a Component with `wit-bindgen` and `wasm-tools`](./language-support/building-a-simple-component/c.md#building-a-component-with-wit-bindgen-and-wasm-tools)
2828
- [Running a Component from C/C++ Applications](./language-support/building-a-simple-component/c.md#running-a-component-from-cc-applications)
2929
- [C# Tooling](./language-support/building-a-simple-component/csharp.md)
30+
- [Dart Tooling](./language-support/building-a-simple-component/dart.md)
3031
- [Go Tooling](./language-support/building-a-simple-component/go.md)
3132
- [JavaScript Tooling](./language-support/building-a-simple-component/javascript.md)
3233
- [Building a Component with `jco`](./language-support/building-a-simple-component/javascript.md#building-a-component-with-jco)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Dart Tooling
2+
3+
> [!WARNING]
4+
> Compiling Dart to non-web WebAssembly targets is experimental.
5+
> This guide requires Dart version `3.14.0-251.0.dev` or later.
6+
7+
WebAssembly components in Dart can be built with the [wasm_tools package](https://github.com/simolus3/wasm.dart/) on pub.dev.
8+
9+
This guide walks through building a Dart component that implements
10+
the `adder` world defined in the [`adder/world.wit` package][docs-adder].
11+
The component will implement the `adder` world, which contains an `add` interface with an `add` function.
12+
13+
Keep in mind that this is a basic intro. For more examples, please see the [dart-samples][examples]
14+
from the `wasm_tools` package or TODO: running a component.
15+
16+
If you still have questions, feel free to open an issue on [the repository][wasm_tools]
17+
or reach out on [Zulip][chat].
18+
19+
## 1. Create your Dart project
20+
21+
Begin by creating a fresh Dart project:
22+
23+
```sh
24+
dart create -t cli dart_wasm_adder
25+
cd dart_wasm_adder
26+
```
27+
28+
## 2. Install the tools
29+
30+
All tools requires to create WebAssembly components from Dart can be installed via pub:
31+
32+
```sh
33+
dart pub add wasm_components dev:wasm_tools
34+
```
35+
36+
The `wasm_components` package provides runtime support for component models
37+
(like an allocator or async task management), while `wasm_tools` contains `witgen`
38+
and tools to compile Dart to components.
39+
40+
## 3. Generate bindings for the Wasm component
41+
42+
Since we will be implementing the [`adder` world][docs-adder], we can copy the WIT to our project.
43+
Create a file named `adder.wit` and paste the following code into it:
44+
45+
```wit
46+
{{#include ../../../examples/tutorial/wit/adder/world.wit}}
47+
```
48+
49+
Generate Dart code and required metadata for the compiler with
50+
51+
```sh
52+
dart run wasm_tools witgen -i adder.wit
53+
```
54+
55+
This generates:
56+
57+
- `lib/src/components/docs_adder.dart` containing interfaces (only `add` in this example).
58+
- `lib/src/components/docs_adder_adder.dart` containing bindings for the `adder` world.
59+
- `lib/src/components/docs_adder_adder.json` containing metadata used to turn WebAssembly
60+
modules emitted by `dart2wasm` into components.
61+
62+
The JSON file describe which component imports and exports a Dart program needs.
63+
The toolchain reads it via [link hooks]. All packages defining component imports
64+
or exports need one, including the `dart_wasm_adder` package. Create a
65+
`hook/link.dart` file with these contents:
66+
67+
```dart
68+
import 'dart:convert';
69+
import 'dart:io';
70+
71+
import 'package:hooks/hooks.dart';
72+
import 'package:wasm_tools/hooks.dart';
73+
74+
void main(List<String> args) => link(args, (input, output) async {
75+
if (input.config.buildWasmComponent) {
76+
final abi = input.packageRoot.resolve('lib/src/components/docs_adder_adder.json');
77+
78+
output.dependencies.add(abi);
79+
output.assets.webAssemblyComponents.add(
80+
WasmComponentAsset(
81+
encoded: json.decode(
82+
File(abi.toFilePath()).readAsStringSync(),
83+
) as Map<String, Object?>,
84+
),
85+
);
86+
}
87+
});
88+
```
89+
90+
## 4. Implement the `add` function
91+
92+
The generated `adderComponent` function is used to define this component:
93+
It receives imported interfaces as parameters (in this case, there aren't any)
94+
and returns the exported interface.
95+
96+
For this example, replace `bin/dart_wasm_adder.dart` with:
97+
98+
```dart
99+
import 'package:dart_wasm_adder/src/components/docs_adder.dart';
100+
import 'package:dart_wasm_adder/src/components/docs_adder_adder.dart';
101+
102+
import 'package:wasm_components/wasm_components.dart';
103+
104+
void main() {
105+
adderComponent((_) => const _Add());
106+
}
107+
108+
final class const _Add() implements Add {
109+
@override
110+
int add({required int x, required int y}) {
111+
return x + y;
112+
}
113+
}
114+
```
115+
116+
## 5. Testing the `add` component
117+
118+
With all generated bindings set up, the component can be compiled:
119+
120+
```sh
121+
dart run wasm_tools compile bin/dart_wasm_adder.dart
122+
```
123+
124+
This creates `bin/dart_wasm_adder.wasm`, a component we can invoke from the
125+
CLI with [wasmtime]:
126+
127+
```console
128+
$ wasmtime run --invoke 'add(1, 2)' bin/dart_wasm_adder.wasm
129+
3
130+
```
131+
132+
With this, we have successfully built and run a basic WebAssembly component with Dart 🎉
133+
134+
[wasm_tools]: https://github.com/simolus3/wasm.dart/
135+
[examples]: https://github.com/simolus3/wasm.dart/tree/main/pkg/wasm_tools/example
136+
[docs-adder]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/tutorial/wit/adder/world.wit
137+
[chat]: https://bytecodealliance.zulipchat.com/#narrow/channel/394175-SIG-Guest-Languages/topic/Dart.20subgroup/with/614175797
138+
[link hooks]: https://dart.dev/tools/hooks
139+
[wasmtime]: https://wasmtime.dev/
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Creating Runnable Components (Dart)
2+
3+
> [!WARNING]
4+
> Compiling Dart to non-web WebAssembly targets is experimental.
5+
> This guide requires Dart version `3.14.0-251.0.dev` or later.
6+
7+
## Creating a command component
8+
9+
A _command_ is a component with a specific export that allows it to be executed directly by `wasmtime`
10+
(or other `wasi:cli` hosts). Ignoring the specifics of defining components in Dart, this is the
11+
equivalent of running a plain Dart program with a `main()` function.
12+
13+
### 1. Create a new Dart project
14+
15+
To create a command with Dart, start with a fresh Dart package:
16+
17+
```sh
18+
dart create -t cli dart_wasm_cli
19+
```
20+
21+
To configure this package for WebAssembly components, add tooling dependencies.
22+
Additionally, the `wasi` package provides generated bindings to WASI definitions,
23+
meaning that running `dart run wasm_tools witgen` won't be necessary.
24+
25+
```sh
26+
dart pub add wasi wasm_components dev:wasm_tools
27+
```
28+
29+
### 2. Write the relevant Dart
30+
31+
The following code can be inserted into `bin/dart_wasm_cli.dart`:
32+
33+
```dart
34+
import 'dart:async';
35+
import 'dart:convert';
36+
import 'dart:typed_data';
37+
38+
import 'package:wasi/cli/command.dart';
39+
import 'package:wasi/cli.dart';
40+
import 'package:wasm_components/wasm_components.dart';
41+
42+
void main() {
43+
commandComponent((imports) => _Run(imports.cliStdout));
44+
}
45+
46+
final class _Run(final Stdout stdout) implements Run {
47+
@override
48+
Future<Result<void, void>> run() async {
49+
final out = StreamController<Uint8List>();
50+
final stdoutDone = stdout.writeViaStream(data: out.stream);
51+
52+
out.add(utf8.encode('Hello world!\n'));
53+
out.close();
54+
await stdoutDone;
55+
56+
return const .ok(null);
57+
}
58+
}
59+
```
60+
61+
### 3. Build the component
62+
63+
To build the component, use `wasm_tools compile`:
64+
65+
```sh
66+
dart run wasm_tools compile bin/dart_wasm_cli.dart
67+
```
68+
69+
The [link hook] of the `wasi` package detects that this Dart program defines a
70+
command component and configures the relevant imports and exports.
71+
This allows the component to target `wasi:cli/command@0.3.0`.
72+
73+
### 4. Run the component with `wasmtime`
74+
75+
To run your command component:
76+
77+
```sh
78+
wasmtime run bin/dart_wasm_cli.wasm
79+
```
80+
81+
[link hook]: https://github.com/simolus3/wasm.dart/blob/ec9f801ebe258fc4c8dc0dceec35af8b9447627f/pkg/wasi/hook/link.dart#L17-L21

0 commit comments

Comments
 (0)