bytecodealliance / bytecodealliance/wit-bindgen

rust (question): create resource in host component and share to a guest component

Open
#1,103 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.5k
Forks
286
Avg merge
6h 32m
Merged PRs (30d)
19

Description

Short: I'm trying to take two components and compose them together. In the first component, I want to create a resource, and in the second component I want to *use* that resource. I can't quite figure out how to get wasm-bindgen to do what I want.

I'm using `cargo component`.

Long: I'm trying to use Advent of Code to learn about components. Overall, I have a very basic world/interface that looks like this, and a runner (written in wasmtime) that takes a component and an input and outputs the two answers.

```wit
package aoc:base;

interface day {
run: func(input: string) -> tuple;
}

world day-world {
export day;
}
```

Then for every day, I create two components called "parser" and "solver", and each one has a custom wit for that particular problem. The wits I currently have for [day 10](https://adventofcode.com/2024/day/10) are the following, which represent parsing the input as a two-dimensional grid of values (a topological map according to the AOC story), and solving the problem with that map.

```wit
package aoc2024:day10;

// Shared between the two components which are separate projects
interface solver {
solve-a: func(input: list>) -> u64;
solve-b: func(input: list>) -> u64;
}
```

```wit
package aoc2024:day10-parser;

// In the parser project
world parser {
export aoc:base/day;
import aoc2024:day10/solver;
}
```

```wit
package aoc2024:day10-solver;

// In the solver project
world solver {
export aoc2024:day10/solver;
import aoc:base/debug;
}
```

Conceptually, the parser parses the challenge input (`string`) into something close to the problem definition (`list>`), and the solver takes the parsed input and solves it for both parts.

I have been combining these with `wac plug day10_parser.wasm --plug day10_solver.wasm --out day10.wasm`. I would be willing to write a wac script if necessary.

(The only reason these components are separate is because I wanted to learn more about components. It's working so far.)

What I *tried* to do with Day 10 and couldn't, was to use a *resource*.

```wit
resource topographical-map {
map-width: func() -> u32;
map-height: func() -> u32;
height-at-location: func(x: u32, y: u32) -> u8;
}
```

I'm not sure exactly whether the topographical-map should be imported or exported.

This seems reasonable to me:

```wit
package aoc2024:day10;

interface types {
resource topographical-map {
map-width: func() -> u32;
map-height: func() -> u32;
height-at-location: func(x: u32, y: u32) -> u8;
}
}

interface solver {
use types.{topographical-map};
solve-a: func(input: borrow) -> u64;
solve-b: func(input: borrow) -> u64;
}
```

But when I went down this route, I couldn't find any way for the "parser" component to *create* a `topographical-map` from the input and then send a reference to it to the "solver" component.

I know this is a very open-ended question, but I tried various things for an hour or two and didn't end up any closer to figuring out how one component could play host of a resource to another component. Is this possible?

https://github.com/bryanburgers/advent-of-code-2024

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.