Stream: wasm

Topic: Dyna - instantiate, inspect, and invoke components


view this post on Zulip Ryan Levick (rylev) (Mar 06 2024 at 13:57):

I'd like to introduce a new project I'm working on: dyna

Dyna is a way to instantiate, inspect, and invoke components from within a component. If you'd like to interact with a component but can't/don't want to statically link components (using some tool like wac/wasm-tools compose), this might be the right tool for you. Here's what some Rust code looks like for calling a component (remember this code is itself running as Wasm):

// Read a component from disk
let component_bytes = std::fs::read("some_component.wasm").unwrap();
// Create a component engine and load the component
let engine = Engine::new();
let component = engine.load_component(&component_bytes).unwrap();
// Reflect on the component to gain insight into its exports
if component.wit().exports().iter().any(|e| {
    e.name == "hello-world" &&
        matches!(&e.kind, Export::Function(f) if f.params.is_empty())
    }) {
    // Call a component export and inspect its return value
    let val = component.call("hello-world", &[]).unwrap();
    assert!(matches!(&val[0], Val::String(s) if s == "Hello, World!"));
}

Let me know what you think!

Instantiate, inspect, and invoke Wasm components from within a component. - rylev/dyna

view this post on Zulip bjorn3 (Mar 06 2024 at 16:49):

Can you link the target component against yourself like dlopen with this?

view this post on Zulip Ryan Levick (rylev) (Mar 06 2024 at 17:03):

@bjorn3 can you explain a bit more? What is "yourself" in this case?

view this post on Zulip bjorn3 (Mar 06 2024 at 17:04):

What is "yourself" in this case?

The component which uses dyna to load another component.

view this post on Zulip Ryan Levick (rylev) (Mar 06 2024 at 17:06):

Still not sure if I fully understand the question, but dyna is indeed like a dlopen for components.

view this post on Zulip bjorn3 (Mar 06 2024 at 17:08):

Is it possible for the component using dyna to allow the dynamically loaded component to access all exports of the component that uses dyna?

view this post on Zulip bjorn3 (Mar 06 2024 at 17:08):

So component A loads component B and component B is given permission to call an export of component A.

view this post on Zulip Ryan Levick (rylev) (Mar 06 2024 at 17:10):

Hmm... that's not currently possible, but I think it should be possible to add. Right now component B uses an entirely different engine, store, and linker from component A, but this doesn't need to be the case.

view this post on Zulip Ryan Levick (rylev) (Mar 06 2024 at 17:14):

I guess the thing that I'm not sure would work is making the instance of component A the same in both cases. This should be easy to do with two instances of component A

view this post on Zulip Daniel Macovei (Mar 06 2024 at 17:42):

This looks really interesting! Specifically I'd be really curious to think about it from the perspective of managing registry dependencies. Right now most components are specifying their imports as instances, with the thought being that build steps will handle importing the components, instantiating and then providing those instances as specified in the original imports. So will be cool to think about what happens if these "packages" are actually specifying that they want components themselves because they have an opinion about how they're instantiated.


Last updated: Dec 23 2024 at 13:07 UTC