Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ create: async func(
) -> result<component, error>;
```

The WIT can be given as `text`, with any packages the world depends on defined inline in nested `package ns:name { ... }` blocks. It can also be given as `encoded`, a WIT package encoded as wasm, e.g. from `wasm-tools component wit --wasm` or `wkg build`, which carries its dependencies with it. Or it can be given as `parsed`, the `wit` record returned by `componentized:component/wit#extract`, e.g. from the [`extract-wit`](https://github.com/componentized/component/tree/main/components/extract-wit) component. `world` works the same as the CLI's `--world` flag, and `overrides` given as `wave(...)` holds the same WAVE as an `--overrides` file. `overrides` is a variant so other ways to give values can be added later.
The WIT can be given as `text`, with any packages the world depends on defined inline in nested `package ns:name { ... }` blocks. It can also be given as `encoded`, a WIT package encoded as wasm, e.g. from `wasm-tools component wit --wasm` or `wkg build`, which carries its dependencies with it. Or it can be given as `parsed`, the `wit` record returned by `componentized:component/wit#parse`, e.g. from the [`wit-tools`](https://github.com/componentized/component/tree/main/components/wit-tools) component. `world` works the same as the CLI's `--world` flag, and `overrides` given as `wave(...)` holds the same WAVE as an `--overrides` file. `overrides` is a variant so other ways to give values can be added later.

`extract` also accepts components, and so can the factory: when the `parsed` WIT was extracted from a component rather than a WIT package, the factory implements that component's world unless `world` names another one. Components don't carry doc comments, so there are no `@value` tags; every value must come from `overrides`.

The factory imports `componentized:component/types` and `componentized:component/wit`, but only for their types, not their functions, so hosts can satisfy them with empty instances. Hosts must support the component model async ABI and maps; with `wasmtime run`, enable them with `-W component-model-async=y,component-model-map=y`:
The factory imports `componentized:component/types` and `componentized:component/wit`, but only for their types, not their functions, so hosts can satisfy them with empty instances. Hosts must support the component model async ABI and maps; with `wasmtime run`, enable them with `-W component-model-map=y`:

```sh
wasmtime run -W component-model-async=y,component-model-map=y \
--invoke 'create(text("package a:b; world w { /// @value 42\n export answer: func() -> u32; }"), none, none)' \
lib/factory.wasm
wasmtime run -W component-model-map=y \
--invoke 'create(wit("package a:b; world w { /// @value 42\n export answer: func() -> u32; }"), none, none)' \
target/components/factory/factory.wasm
```

## Build
Expand All @@ -172,9 +172,9 @@ Prereqs:
make components
```

The build creates each component in [`components`](./components) into `lib`, e.g. the factory at `lib/factory.wasm`, along with `lib/interface.wasm`, the `componentized:constants` WIT package. Each component is also built with debug info, e.g. `lib/factory.debug.wasm`.
The build creates each component in [`components`](./components) into `target/components`, e.g. the factory at `target/components/factory/factory.wasm`, along with `target/components/interface.wasm`, the `componentized:constants` WIT package. Each component is also built with debug info, e.g. `target/components/factory/factory.debug.wasm`.

To run the tests, which exercise the CLI and the components, using the `extract-wit` component from `componentized:component`, which the build fetches to `lib/dep-extract-wit.wasm`:
To run the tests, which exercise the CLI and the components, using the `wit-tools` component from `componentized:component`, which the build fetches to `target/components/dep-wit-tools/dep-wit-tools.wasm`:

```sh
make test
Expand Down
13 changes: 0 additions & 13 deletions components/dep-extract-wit/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions components/dep-extract-wit/dep-extract-wit.wkg

This file was deleted.

13 changes: 13 additions & 0 deletions components/dep-wit-tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `dep-wit-tools`

The [`wit-tools`](https://github.com/componentized/component/tree/main/components/wit-tools) component from `componentized:component`, used by the tests to exercise the factory with parsed WIT.

## Interfaces

Imports:

- `componentized:component/types@0.1.0`: the `wasm` and `error-code` types

Exports:

- `componentized:component/wit@0.1.0`: extracts the WIT from a WIT package or component
2 changes: 2 additions & 0 deletions components/dep-wit-tools/dep-wit-tools.wkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ghcr.io/componentized/component/wit-tools:0.1.0@sha256:b164d67630abc2aaff9912d2453eb1dfe3c7beae727f47462412785fc7938fa2
ghcr.io/componentized/component/wit-tools:0.1.0_debug@sha256:72af127083933b53049e11fc985ce17bb5eb8b2a030d7d071bd6414d84eca88e
4 changes: 2 additions & 2 deletions components/factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Create components whose exported functions return constant values, from a WIT wo

Imports:

- `componentized:component/types@0.0.0-dev`: the `component` and `error` types, only types are imported
- `componentized:component/wit@0.0.0-dev`: the `wit` type for parsed WIT, only types are imported
- `componentized:component/types@0.1.0`: the `component` and `error` types, only types are imported
- `componentized:component/wit@0.1.0`: the `wit` type for parsed WIT, only types are imported

Exports:

Expand Down
34 changes: 18 additions & 16 deletions components/factory/tests/factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Exercises the factory component built by make into `target/components/factory/factory.wasm`,
//! along with `target/components/dep-extract-wit/dep-extract-wit.wasm` from
//! along with `target/components/dep-wit-tools/dep-wit-tools.wasm` from
//! `componentized:component`, which make also fetches.

use anyhow::{Context, Result, anyhow, bail};
Expand All @@ -10,8 +10,8 @@ use wasmtime::{
component::{Component, Linker, Val},
};

const TYPES: &str = "componentized:component/types@0.0.0-dev";
const WIT_INTERFACE: &str = "componentized:component/wit@0.0.0-dev";
const TYPES: &str = "componentized:component/types@0.1.0";
const WIT_INTERFACE: &str = "componentized:component/wit@0.1.0";
const FACTORY_INTERFACE: &str = "componentized:constants/factory@0.2.0-dev";

fn engine() -> &'static Engine {
Expand Down Expand Up @@ -48,9 +48,9 @@ fn factory() -> Result<&'static Component> {
load("factory", &CACHE)
}

fn extract_wit() -> Result<&'static Component> {
fn wit_tools() -> Result<&'static Component> {
static CACHE: OnceLock<Result<Component, String>> = OnceLock::new();
load("dep-extract-wit", &CACHE)
load("dep-wit-tools", &CACHE)
}

/// Calls an async exported function, satisfying type-only imports with empty
Expand Down Expand Up @@ -165,15 +165,18 @@ fn create(
}
}

/// Extracts the WIT from a component or encoded WIT package with
/// `componentized:component/wit#extract`.
fn extract(bytes: Vec<u8>) -> Result<Val> {
/// Parse the WIT from a component or encoded WIT package with
/// `componentized:component/wit#parse`.
fn parse(bytes: Vec<u8>) -> Result<Val> {
let result = call_async(
extract_wit()?,
wit_tools()?,
&[TYPES],
WIT_INTERFACE,
"extract",
vec![bytes_val(bytes)],
"parse",
vec![Val::Variant(
"wasm".into(),
Some(Box::new(bytes_val(bytes))),
)],
)?;
unwrap_result(result)?.map_err(|e| anyhow!(e))
}
Expand Down Expand Up @@ -251,7 +254,7 @@ fn it_creates_components_from_encoded_wit() -> Result<()> {

#[test]
fn it_creates_components_from_parsed_wit() -> Result<()> {
let wit = extract(encode(WIT)?)?;
let wit = parse(encode(WIT)?)?;
let component = create(Wit::Parsed(wit), Some("config"), None)?.map_err(|e| anyhow!(e))?;
assert_eq!(call_config(&component)?, vec![string("hello"), point(1, 2)]);
Ok(())
Expand All @@ -262,8 +265,7 @@ fn it_creates_identical_components_from_encoded_and_parsed_wit() -> Result<()> {
// exercises every supported type, including aliases and imported types
let encoded = encode("crates/componentized-constants/tests/fixtures/all")?;
let from_encoded = create(Wit::Wasm(encoded.clone()), None, None)?.map_err(|e| anyhow!(e))?;
let from_parsed =
create(Wit::Parsed(extract(encoded)?), None, None)?.map_err(|e| anyhow!(e))?;
let from_parsed = create(Wit::Parsed(parse(encoded)?), None, None)?.map_err(|e| anyhow!(e))?;
assert!(from_encoded == from_parsed, "components differ");
Ok(())
}
Expand All @@ -272,7 +274,7 @@ fn it_creates_identical_components_from_encoded_and_parsed_wit() -> Result<()> {
fn it_reimplements_the_world_of_a_parsed_component() -> Result<()> {
// components don't carry docs, so every value comes from the overrides
let original = create(Wit::Wit(WIT), Some("config"), None)?.map_err(|e| anyhow!(e))?;
let wit = extract(original)?;
let wit = parse(original)?;
let component = create(
Wit::Parsed(wit),
None,
Expand All @@ -296,7 +298,7 @@ fn it_returns_errors() -> Result<()> {
assert!(!err.is_empty());

let original = create(Wit::Wit(WIT), Some("config"), None)?.map_err(|e| anyhow!(e))?;
let err = create(Wit::Parsed(extract(original)?), None, None)?.expect_err("missing values");
let err = create(Wit::Parsed(parse(original)?), None, None)?.expect_err("missing values");
assert!(
err.contains("missing value for `example:factory/constants#greeting`"),
"{err}"
Expand Down
6 changes: 3 additions & 3 deletions components/math/wkg.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name = "componentized:component"
registry = "componentized.dev"

[[packages.versions]]
requirement = "=0.0.0-dev"
version = "0.0.0-dev"
digest = "sha256:2d7677a89ef49bef5d8d20848c431c76208753858f4871929066397d62542af7"
requirement = "=0.1.0"
version = "0.1.0"
digest = "sha256:b025920ca99a32febc514636a1dd04d1ad65cb8773d10d8e09a62445106ae90b"

[[packages]]
name = "wasi:config"
Expand Down
6 changes: 3 additions & 3 deletions components/types/wkg.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name = "componentized:component"
registry = "componentized.dev"

[[packages.versions]]
requirement = "=0.0.0-dev"
version = "0.0.0-dev"
digest = "sha256:2d7677a89ef49bef5d8d20848c431c76208753858f4871929066397d62542af7"
requirement = "=0.1.0"
version = "0.1.0"
digest = "sha256:b025920ca99a32febc514636a1dd04d1ad65cb8773d10d8e09a62445106ae90b"

[[packages]]
name = "wasi:config"
Expand Down
6 changes: 3 additions & 3 deletions components/wkg.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name = "componentized:component"
registry = "componentized.dev"

[[packages.versions]]
requirement = "=0.0.0-dev"
version = "0.0.0-dev"
digest = "sha256:2d7677a89ef49bef5d8d20848c431c76208753858f4871929066397d62542af7"
requirement = "=0.1.0"
version = "0.1.0"
digest = "sha256:b025920ca99a32febc514636a1dd04d1ad65cb8773d10d8e09a62445106ae90b"

[[packages]]
name = "wasi:config"
Expand Down
4 changes: 2 additions & 2 deletions wit/factory.wit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Creates components whose exported functions return constant values.
interface factory {
use componentized:component/types@0.0.0-dev.{wasm, error-code};
use componentized:component/wit@0.0.0-dev.{wit, wit-source};
use componentized:component/types@0.1.0.{wasm, error-code};
use componentized:component/wit@0.1.0.{wit, wit-source};

/// Values replacing those from the WIT. The overrides are a record with
/// an optional field per world export. Interface exports are named by the
Expand Down
6 changes: 3 additions & 3 deletions wkg.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name = "componentized:component"
registry = "componentized.dev"

[[packages.versions]]
requirement = "=0.0.0-dev"
version = "0.0.0-dev"
digest = "sha256:2d7677a89ef49bef5d8d20848c431c76208753858f4871929066397d62542af7"
requirement = "=0.1.0"
version = "0.1.0"
digest = "sha256:b025920ca99a32febc514636a1dd04d1ad65cb8773d10d8e09a62445106ae90b"

[[packages]]
name = "wasi:config"
Expand Down
Loading