You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: component-model/src/language-support/javascript.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -216,7 +216,7 @@ With `jco transpile` any WebAssembly binary (compiled from any language) can be
216
216
217
217
Reactor components are WebAssembly components that are long running and meant to be called repeatedly over time. They're analogous to libraries of functionality rather than an executable (a "command" component).
218
218
219
-
Components expose their interfaces via [WebAssembly Interface Types][docs-wit], hand-in-hand with the [Component Model][docs-component-model] which enables components to use higher level types interchangably.
219
+
Components expose their interfaces via [WebAssembly Interface Types][docs-wit], hand-in-hand with the [Component Model][docs-component-model] which enables components to use higher level types interchangeably.
220
220
221
221
222
222
[docs-wit]: ../design/wit.md
@@ -312,7 +312,7 @@ You should see output like the following:
312
312
OK Successfully written string-reverse.wasm.
313
313
```
314
314
315
-
Now that we have a WebAssembly binary, we can *also* use `jco` to run it in a native JavaScript context by *transpiling* the WebAsssembly binary (which could have come from anywhere!) to a JavaScript module.
315
+
Now that we have a WebAssembly binary, we can *also* use `jco` to run it in a native JavaScript context by *transpiling* the WebAssembly binary (which could have come from anywhere!) to a JavaScript module.
Copy file name to clipboardexpand all lines: component-model/src/language-support/rust.md
+84-42
Original file line number
Diff line number
Diff line change
@@ -1,59 +1,106 @@
1
1
# Components in Rust
2
2
3
-
Rust has first-class support for the component model via [the `cargo component` tool](https://github.com/bytecodealliance/cargo-component). It is a `cargo` subcommand for
4
-
creating WebAssembly components using Rust as the component's implementation language.
3
+
Rust has first-class support for the component model via the [`cargo-component`
4
+
tool](https://github.com/bytecodealliance/cargo-component). We will be using
5
+
the `cargo component` subcommand to create WebAssembly components using Rust as
6
+
the component's implementation language.
5
7
6
-
## Installing `cargo component`
8
+
> [!NOTE]
9
+
> You can find more details about `cargo-component` on [crates.io](https://crates.io/crates/cargo-component).
First `cd` into the `tutorial` directory found in the repo we just cloned:
38
+
```sh
39
+
cd component-docs/component-model/examples/tutorial
40
+
```
41
+
42
+
Now create a new WebAssembly component package called `add`:
21
43
```sh
22
44
$ cargo component new add --lib &&cd add
23
45
```
24
46
25
-
Note that `cargo component` generates the necessary bindings as a module called `bindings`.
47
+
> [!NOTE]
48
+
> `cargo-component` generates the necessary bindings for us in a module called `bindings` found in `src/bindings.rs`.
49
+
50
+
Next change our generated `wit/world.wit` to match `example:component`:
51
+
```wit
52
+
{{#include ../../examples/example-host/add.wit}}
53
+
```
26
54
27
-
Next, update `wit/world.wit` to match `add.wit` and modify the component package reference to change the
28
-
package name to `example:component`. The `component` section of `Cargo.toml` should look like the following:
55
+
Since our WIT file changed names (from `component:add` to
56
+
`example:component`) this needs to be reflected in our `Cargo.toml`,
57
+
let's change the `package.metadata.component.package` string to `"example:component"`:
29
58
30
59
```toml
31
60
[package.metadata.component]
32
61
package = "example:component"
33
62
```
34
63
35
-
`cargo component` will generate bindings for the world specified in a package's `Cargo.toml`. In particular, it will create a `Guest` trait that a component should implement. Since our `example` world has no interfaces, the trait lives directly under the bindings module. Implement the `Guest` trait in `add/src/lib.rs` such that it satisfies the `example` world, adding an `add` function:
64
+
`cargo component` will generate bindings for the
65
+
`example:component/example` world we specified `Cargo.toml`, it will also
66
+
create a `Guest` trait that a component can implement.
67
+
Since our `example:component/example` world has no interfaces, our
68
+
generated `Guest` trait lives at the top of the `bindings` module.
69
+
70
+
Implementing the `Guest` trait for our `Component` struct requires us
71
+
to match the structure specified by `example:component/example`, we do this by
72
+
implementing the `add` method with the equivalent function signature. Our
73
+
`src/lib.rs` should look similar to the following:
The [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) exports a function. However, you'll often prefer to export an interface, either to comply with an existing specification or to capture a set of functions and types that tend to go together. For example, to implement the following world:
127
+
Notice how our `example` world currently exports `add` as a function. It's
128
+
often preferable to export an interface rather than a function, either to
129
+
comply with an existing specification or to capture several functions and types
130
+
at once.
131
+
For example, to implement the [`docs:adder/adder`][docs-adder] world:
The world file (`wit/world.wit`) generated for you by `cargo component new --lib` doesn't specify any imports.
113
146
114
-
> `cargo component build`, by default, uses the Rust `wasm32-wasi` target, and therefore automatically imports any required WASI interfaces - no action is needed from you to import these. This section is about importing custom WIT interfaces from library components.
147
+
> [!NOTE]
148
+
> This section is about importing custom WIT interfaces from library components.
149
+
> By default, `cargo component build` imports any required [WASI interfaces](https://wasi.dev/interfaces)
150
+
> for us without needing to explicitly declare them.
115
151
116
152
If your component consumes other components, you can edit the `world.wit` file to import their interfaces.
117
153
@@ -139,10 +175,13 @@ Because the `docs:adder` package is in a different project, we must first tell `
Note that the path is to the adder project's WIT _directory_, not to the `world.wit` file. A WIT package may be spread across multiple files in the same directory; `cargo component` will look at all the files.
181
+
> [!NOTE]
182
+
> The path for `docs:adder` is relative to the `wit`_directory_, not to the `world.wit` file.
183
+
>
184
+
> A WIT package may be spread across multiple files in the same directory; `cargo component` will search them all.
146
185
147
186
### Calling the import from Rust
148
187
@@ -534,3 +573,6 @@ If you are hosting a Wasm runtime, you can export a resource from your host for
0 commit comments