|
| 1 | +use wac_graph::{types::Package, CompositionGraph, EncodeOptions}; |
| 2 | + |
| 3 | +fn main() { |
| 4 | + let mut graph = CompositionGraph::new(); |
| 5 | + |
| 6 | + // Register the package dependencies into the graph |
| 7 | + let package = Package::from_file( |
| 8 | + "hello", |
| 9 | + None, |
| 10 | + "../deps/example/hello.wasm", |
| 11 | + graph.types_mut(), |
| 12 | + ) |
| 13 | + .unwrap(); |
| 14 | + let hello = graph.register_package(package).unwrap(); |
| 15 | + let package = Package::from_file( |
| 16 | + "greeter", |
| 17 | + None, |
| 18 | + "../deps/example/greeter.wasm", |
| 19 | + graph.types_mut(), |
| 20 | + ) |
| 21 | + .unwrap(); |
| 22 | + let greeter = graph.register_package(package).unwrap(); |
| 23 | + |
| 24 | + // Instantiate the hello instance which does not have any arguments |
| 25 | + let hello_instance = graph.instantiate(hello); |
| 26 | + |
| 27 | + // Instantiate the greeter instance which has a single argument "hello" which is exported by the hello instance |
| 28 | + let greeter_instance = graph.instantiate(greeter); |
| 29 | + let hello_export = graph |
| 30 | + .alias_instance_export(hello_instance, "hello") |
| 31 | + .unwrap(); |
| 32 | + graph |
| 33 | + .set_instantiation_argument(greeter_instance, "hello", hello_export) |
| 34 | + .unwrap(); |
| 35 | + |
| 36 | + // Alias the "greet" export from the greeter instance |
| 37 | + let greet_export = graph |
| 38 | + .alias_instance_export(greeter_instance, "greet") |
| 39 | + .unwrap(); |
| 40 | + // Export the "greet" function from the composition |
| 41 | + graph.export(greet_export, "greet").unwrap(); |
| 42 | + |
| 43 | + // Encode the graph into a WASM binary |
| 44 | + let encoding = graph.encode(EncodeOptions::default()).unwrap(); |
| 45 | + std::fs::write("composition.wasm", encoding).unwrap(); |
| 46 | +} |
0 commit comments