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-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -218,7 +218,7 @@ With `jco transpile` any WebAssembly binary (compiled from any language) can be
218
218
219
219
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).
220
220
221
-
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.
221
+
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.
222
222
223
223
224
224
[docs-wit]: ../design/wit.md
@@ -314,7 +314,7 @@ You should see output like the following:
314
314
OK Successfully written string-reverse.wasm.
315
315
```
316
316
317
-
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.
317
+
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.
Now, use `cargo component` to build the component, being sure to optimize with a release build.
74
+
Now, let's build our component, being sure to optimize with a release build:
88
75
89
-
```console
76
+
```sh
90
77
cargo component build --release
91
78
```
92
79
93
-
> **WARNING:** Building with `--release` removes all debug-related information from the resulting .wasm file. When prototyping or testing locally, you might want to avoid `--release` to obtain useful backtraces in case of errors (for example, with `wasmtime::WasmBacktraceDetails::Enable`). Note: the resulting .wasm file will be considerably larger (likely 4MB+).
80
+
> [!WARNING]
81
+
> Building with `--release` removes all debug-related information from the resulting `.wasm` file.
82
+
>
83
+
> When prototyping or testing locally, you might want to avoid `--release` to
84
+
> obtain useful backtraces in case of errors (for example, with
The command above should produce the output below:
95
+
96
+
```wit
99
97
package root:component;
100
98
101
99
world root {
@@ -108,27 +106,31 @@ package docs:adder@0.1.0 {
108
106
}
109
107
```
110
108
111
-
### Running a Component from Rust Applications
109
+
110
+
### Running a Component
112
111
113
112
To verify that our component works, lets run it from a Rust application that knows how to run a
114
-
component targeting the [`adder` world][adder-wit].
113
+
component targeting the [`adder` world](#adding-the-wit-world).
115
114
116
115
The application uses [`wasmtime`](https://github.com/bytecodealliance/wasmtime) crates to generate
117
116
Rust bindings, bring in WASI worlds, and execute the component.
118
117
119
-
```sh
118
+
```console
120
119
$ cd examples/example-host
121
120
$ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/adder.wasm
122
121
1 + 2 = 3
123
122
```
124
123
125
-
## Importing an interface with `cargo component`
124
+
## Importing an interface
126
125
127
-
The world file (`wit/world.wit`) generated for you by `cargo component new --lib` doesn't specify any imports.
126
+
The world file (`wit/world.wit`) we generated doesn't specify any imports.
127
+
If your component consumes other components, you can edit the `world.wit` file to import their interfaces.
128
128
129
-
> `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.
129
+
> [!NOTE]
130
+
> This section is about importing custom WIT interfaces from library components.
131
+
> By default, `cargo-component` imports any required [WASI interfaces](https://wasi.dev/interfaces)
132
+
> for us without needing to explicitly declare them.
130
133
131
-
If your component consumes other components, you can edit the `world.wit` file to import their interfaces.
132
134
133
135
For example, suppose you have created and built an adder component as explained in the [exporting an interface section](#exporting-an-interface-with-cargo-component) and want to use that component in a calculator component. Here is a partial example world for a calculator that imports the add interface:
134
136
@@ -157,7 +159,10 @@ 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.
162
+
> [!NOTE]
163
+
> The path for `docs:adder` is relative to the `wit`_directory_, not to the `world.wit` file.
164
+
>
165
+
> A WIT package may be spread across multiple files in the same directory; `cargo component` will search them all.
161
166
162
167
### Calling the import from Rust
163
168
@@ -283,7 +288,7 @@ As mentioned above, `cargo component build` doesn't generate a WIT file for a co
283
288
284
289
6. Run the composed component:
285
290
286
-
```sh
291
+
```console
287
292
$ wasmtime run ./my-composed-command.wasm
288
293
1 + 1 = 579 # might need to go back and do some work on the calculator implementation
289
294
```
@@ -551,4 +556,6 @@ If you are hosting a Wasm runtime, you can export a resource from your host for
0 commit comments