Skip to content

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

@bryanburgers

Description

@bryanburgers

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.

package aoc:base;

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

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 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.

package aoc2024:day10;

// Shared between the two components which are separate projects
interface solver {    
    solve-a: func(input: list<list<u8>>) -> u64;
    solve-b: func(input: list<list<u8>>) -> u64;
}
package aoc2024:day10-parser;

// In the parser project
world parser {
    export aoc:base/day;
    import aoc2024:day10/solver;
}
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<list<u8>>), 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.

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:

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<topographical-map>) -> u64;
    solve-b: func(input: borrow<topographical-map>) -> 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions