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!
Can you link the target component against yourself like dlopen
with this?
@bjorn3 can you explain a bit more? What is "yourself" in this case?
What is "yourself" in this case?
The component which uses dyna to load another component.
Still not sure if I fully understand the question, but dyna
is indeed like a dlopen
for components.
Is it possible for the component using dyna to allow the dynamically loaded component to access all exports of the component that uses dyna?
So component A loads component B and component B is given permission to call an export of component A.
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.
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
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: Nov 22 2024 at 16:03 UTC