-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add documentation and walkthroughs for examples (#514)
* feat(examples): add documentation and walkthroughs for examples This commit adds documentation along with walkthroughs for how to use `jco` when building and deploying WebAssembly components. Signed-off-by: Victor Adossi <[email protected]> * refactor(ci): use the all target for examples Signed-off-by: Victor Adossi <[email protected]> * chore(ci): run examples on every push to main Signed-off-by: Victor Adossi <[email protected]> --------- Signed-off-by: Victor Adossi <[email protected]> Co-authored-by: Guy Bedford <[email protected]>
- Loading branch information
1 parent
4f07888
commit d70b4d4
Showing
23 changed files
with
642 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Examples | ||
|
||
This folder contains examples of using `jco`, `componentize-js`, and related Javascript WebAssembly runtime tooling. | ||
|
||
The following sections are present: | ||
|
||
- [Example Components](./components) - "Just show me the code" examples of how to build JS projects that use `jco` | ||
- [Guides](./guides) - Walkthroughs of concepts and functionality to keep in mind while using `jco` and associated tooling | ||
|
||
> [!NOTE] | ||
> Note sure what WebAssembly "Components" are, relative to WebAssembly modules? | ||
> | ||
> For an in-depth multi-language guide to the concepts and advanced features behind the Component Model, | ||
> [read the Component Model book][cm-book]. | ||
[cm-book]: https://component-model.bytecodealliance.org/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## Example Components | ||
|
||
Most (if not all) individual example projects are standard Javascript projects commonplace ecosystem tooling, whether | ||
for server side ([NodeJS][nodejs] -- `npm`, etc) or the browser. | ||
|
||
A brief description of the examples contained in this folder: | ||
|
||
| Example | Description | | ||
|--------------------------------------------------|--------------------------------------------------------------------------------------------------| | ||
| [`add`](./add) | `export`s basic functionality with simple types | | ||
| [`string-reverse`](./string-reverse) | `export`s basic functionality with a slightly more involved WIT interface and more complex types | | ||
| [`string-reverse-upper`](./string-reverse-upper) | `import`s functionality to build more advanced computation to `export` | | ||
|
||
[nodejs]: https://nodejs.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
*.wasm | ||
*.wasm | ||
pnpm-lock.yaml |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// If this import listed below is missing, please run `npm run transpile` | ||
import { add } from "./dist/transpiled/add.mjs"; | ||
import { add } from "./dist/transpiled/add.js"; | ||
|
||
console.log("1 + 2 = " + add(1, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
*.wasm | ||
*.wasm | ||
pnpm-lock.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+5.09 KB
(100%)
examples/components/string-reverse-upper/string-reverse-upper.incomplete.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
*.wasm | ||
*.wasm | ||
pnpm-lock.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 00 - Tooling Setup | ||
|
||
To follow along with the guides, you'll need to have the Javscript for WebAssembly toolchain installed, | ||
which means installing [`jco`][jco] and related tooling. | ||
|
||
[`jco`][jco] is a fully native JS tool for working with the emerging WebAssembly | ||
Components specification in JavaScript. | ||
|
||
> [!NOTE] | ||
> [Typescript][ts] can *also* be used, given that it is transpiled to JS first by relevant tooling (`tsc`). | ||
> `jco` includes a `jco types` subcommand for generating typings that can be used with a Typescript codebase. | ||
[jco]: https://github.com/bytecodealliance/jco | ||
[ts]: https://typescriptlang.org | ||
|
||
### Installing `jco` | ||
|
||
[`jco`][jco] and [`componentize-js`][componentize-js] can be installed with standard NodeJS tooling: | ||
|
||
```console | ||
npm install -g @bytecodealliance/componentize-js @bytecodealliance/jco | ||
``` | ||
|
||
> [!NOTE] | ||
> `jco` and `componentize-js` can be installed in a project-local manner with `npm install -D` | ||
[ComponentizeJS][componentize-js] provides tooling used by `jco` to transpile JS to Wasm, so installing both packages is required. | ||
|
||
[componentize-js]: https://github.com/bytecodealliance/ComponentizeJS | ||
|
||
### Contribute to this Guide! | ||
|
||
The goal for this guide is to leave zero lingering questions. If you had substantial doubts/unaddressed questions | ||
while going through this guide, [open up an issue](https://github.com/bytecodealliance/jco/issues/new) and we'll improve the docs together. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# 01 - Building a WebAssembly Component with `jco` | ||
|
||
> [!NOTE] | ||
> This guide assumes you have dependencies like `jco` installed already. | ||
> | ||
> Consider referring to the "Tooling setup" guide if your environment hasn't been set up yet. | ||
The first step in building a WebAssembly component is creating or downloading the interface that | ||
defines what your component can do. This usually means creating or downloading the | ||
[WebAssembly Interface Types ("WIT")][wit] world you would like to "target" with your component. | ||
|
||
## Writing the WIT | ||
|
||
The [example `add` component][examples-add] showcases a component that adds two numbers together. | ||
|
||
The WIT interface looks like the following: | ||
|
||
```wit | ||
package example:adder; | ||
world component { | ||
export add: func(x: s32, y: s32) -> s32; | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
> `export`ing the `add` interface meants that runners of the WebAssembly binary will be able to *call* that function. | ||
> | ||
> To learn more about the WIT syntax, check out the full [WIT specification][wit] | ||
## Writing the Javascript | ||
|
||
Along with this WIT interface, we can write a JavaScript module that implements the exported `add` function in the `adder` world: | ||
|
||
```js | ||
export function add(x, y) { | ||
return x + y; | ||
} | ||
``` | ||
> [!WARN] | ||
> jco only deals with ES modules, so ensure to set `"type": "module"` in your `package.json` if necessary | ||
> [!NOTE] | ||
> In the code above, the JS module *itself* is the `component` world, and `add` function export is satisfied with the `add` JS function. | ||
## Building a WebAssembly Component | ||
|
||
With the WIT and Javascript in place, we can use [`jco`][jco] to create a WebAssembly component from the JS module, using `jco componentize`. | ||
|
||
Our component is *so simple* (reminiscent of [Core WebAssembly][wasm-core], which deals primarily in numeric values) | ||
that we're actually *not using* any of the [WebAssembly System Interface][wasi] -- this means that we can `--disable` it when we invoke `jco componentize`. | ||
|
||
From the [`examples/components/add`][examples-add] folder, you can run `jco componentize`: | ||
|
||
```console | ||
jco componentize \ | ||
add.js \ | ||
--wit path/to/add/world.wit \ | ||
--world-name component \ | ||
--out add.wasm \ | ||
--disable all | ||
``` | ||
|
||
> [!NOTE] | ||
> You can exclude the `--disable` option and the component will build just fine! | ||
You should see output like the following: | ||
|
||
``` | ||
OK Successfully written add.wasm. | ||
``` | ||
|
||
> [!NOTE] | ||
> As the `add` example is a regular [NodeJS][nodejs] project, you can run `npm install && npm run build` | ||
> without having `jco` and `componentize-js` installed globally. | ||
[wit]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md | ||
[nodejs]: https://nodejs.org/en | ||
[wasi]: https://wasi.dev/ | ||
[wasm-core]: https://webassembly.github.io/spec/core/ | ||
[jco]: https://github.com/bytecodealliance/jco | ||
[examples-add]: https://github.com/bytecodealliance/jco/tree/main/examples/components/add | ||
|
||
## FAQ | ||
|
||
### Is this a Reactor component? | ||
|
||
"Reactor" components are WebAssembly components that can be called repeatedly over time. This serves uses cases like building HTTP handlers, | ||
but also for serving as libraries in other components. | ||
|
||
Reactor components (and components in general) 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. | ||
|
||
They're analogous to libraries of functionality rather than an executable (a "command" component). By contrast, command components must export | ||
a `_start` function, and *usually* export the [`wasi:cli/run` interface][github-wasi-cli], so that other | ||
|
||
[docs-wit]: ../design/wit.md | ||
[docs-component-model]: ../design/why-component-model.md | ||
[github-wasi-cli]: https://github.com/WebAssembly/wasi-cli | ||
|
||
### Contribute to this Guide! | ||
|
||
The goal for this guide is to leave zero lingering questions. If you had substantial doubts/unaddressed questions | ||
while going through this guide, [open up an issue](https://github.com/bytecodealliance/jco/issues/new) and we'll improve the docs together. |
Oops, something went wrong.