-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
237 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
# Build artifacts from examples | ||
**/jco/bindings | ||
**/examples/**/*.wasm | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
# Building a Calculator of Wasm Components | ||
|
||
This tutorial walks through how to compose a component to build a Wasm calculator. | ||
It is a rich example of using multiple components, targeting distinct worlds described across multiple WIT packages | ||
|
||
The first package consists of addition operations | ||
|
||
```wit | ||
//adder.wit | ||
package component-book:[email protected]; | ||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
world adder { | ||
export add; | ||
} | ||
``` | ||
|
||
The WIT package for the calculator consists of a world for each mathematical operator | ||
add an `op` enum that delineates each operator. The following example interface only | ||
has an `add` operation: | ||
|
||
```wit | ||
package bytecode-alliance:[email protected]; | ||
package component-book:[email protected]; | ||
interface calculate { | ||
enum op { | ||
|
@@ -15,17 +32,9 @@ interface calculate { | |
eval-expression: func(op: op, x: u32, y: u32) -> u32; | ||
} | ||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
world adder { | ||
export add; | ||
} | ||
world calculator { | ||
export calculate; | ||
import add; | ||
import component-book:adder/add@0.1.0; | ||
} | ||
``` | ||
|
||
|
@@ -43,6 +52,12 @@ wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adde | |
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm | ||
``` | ||
|
||
You can also use `wac` instead of `wasm-tools compose` if you would like to fetch these components from a registry instead: | ||
|
||
``` | ||
wac plug component-book:calculator-impl --plug component-book:adder-impl -o composed.wasm && wac plug component-book:command-impl --plug ./composed.wasm -o command.wasm | ||
``` | ||
|
||
Now, run the component with wasmtime: | ||
|
||
```sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
#[allow(dead_code)] | ||
pub mod exports { | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
pub mod adder { | ||
#[allow(dead_code, clippy::all)] | ||
pub mod add { | ||
#[used] | ||
|
@@ -27,17 +27,17 @@ pub mod exports { | |
} | ||
#[doc(hidden)] | ||
|
||
macro_rules! __export_docs_calculator_add_0_1_0_cabi{ | ||
macro_rules! __export_component_book_adder_add_0_1_0_cabi{ | ||
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { | ||
|
||
#[export_name = "bytecode_alliance:calculator/[email protected]#add"] | ||
#[export_name = "component-book:adder/[email protected]#add"] | ||
unsafe extern "C" fn export_add(arg0: i32,arg1: i32,) -> i32 { | ||
$($path_to_types)*::_export_add_cabi::<$ty>(arg0, arg1) | ||
} | ||
};); | ||
} | ||
#[doc(hidden)] | ||
pub(crate) use __export_docs_calculator_add_0_1_0_cabi; | ||
pub(crate) use __export_component_book_adder_add_0_1_0_cabi; | ||
} | ||
} | ||
} | ||
|
@@ -142,7 +142,7 @@ mod _rt { | |
macro_rules! __export_adder_impl { | ||
($ty:ident) => (self::export!($ty with_types_in self);); | ||
($ty:ident with_types_in $($path_to_types_root:tt)*) => ( | ||
$($path_to_types_root)*::exports::bytecode_alliance::calculator::add::__export_docs_calculator_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::bytecode_alliance::calculator::add); | ||
$($path_to_types_root)*::exports::component_book::adder::add::__export_component_book_adder_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::component_book::adder::add); | ||
) | ||
} | ||
#[doc(inline)] | ||
|
@@ -151,12 +151,12 @@ pub(crate) use __export_adder_impl as export; | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:adder:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 213] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07Z\x01A\x02\x01A\x02\x01\ | ||
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x19bytecode_alliance:calculator/add@\ | ||
0.1.0\x05\0\x04\x01\x1bbytecode_alliance:calculator/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\ | ||
\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-\ | ||
bindgen-rust\x060.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 223] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07d\x01A\x02\x01A\x02\x01\ | ||
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x1ecomponent-book:adder\ | ||
/add@0.1.0\x05\0\x04\x01\x20component-book:adder/[email protected]\x04\0\x0b\x0b\x01\0\ | ||
\x05adder\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.\ | ||
202.0\x10wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT! | ||
// Options used: | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
pub mod adder { | ||
#[allow(dead_code, clippy::all)] | ||
pub mod add { | ||
#[used] | ||
|
@@ -16,7 +16,7 @@ pub mod docs { | |
pub fn add(a: u32, b: u32) -> u32 { | ||
unsafe { | ||
#[cfg(target_arch = "wasm32")] | ||
#[link(wasm_import_module = "bytecode_alliance:calculator/[email protected]")] | ||
#[link(wasm_import_module = "component-book:adder/[email protected]")] | ||
extern "C" { | ||
#[link_name = "add"] | ||
fn wit_import(_: i32, _: i32) -> i32; | ||
|
@@ -36,7 +36,7 @@ pub mod docs { | |
#[allow(dead_code)] | ||
pub mod exports { | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
#[allow(dead_code, clippy::all)] | ||
|
@@ -92,17 +92,17 @@ pub mod exports { | |
} | ||
#[doc(hidden)] | ||
|
||
macro_rules! __export_docs_calculator_calculate_0_1_0_cabi{ | ||
macro_rules! __export_component_book_calculator_calculate_0_1_0_cabi{ | ||
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { | ||
|
||
#[export_name = "bytecode_alliance:calculator/[email protected]#eval-expression"] | ||
#[export_name = "component-book:calculator/[email protected]#eval-expression"] | ||
unsafe extern "C" fn export_eval_expression(arg0: i32,arg1: i32,arg2: i32,) -> i32 { | ||
$($path_to_types)*::_export_eval_expression_cabi::<$ty>(arg0, arg1, arg2) | ||
} | ||
};); | ||
} | ||
#[doc(hidden)] | ||
pub(crate) use __export_docs_calculator_calculate_0_1_0_cabi; | ||
pub(crate) use __export_component_book_calculator_calculate_0_1_0_cabi; | ||
} | ||
} | ||
} | ||
|
@@ -207,7 +207,7 @@ mod _rt { | |
macro_rules! __export_calculator_impl { | ||
($ty:ident) => (self::export!($ty with_types_in self);); | ||
($ty:ident with_types_in $($path_to_types_root:tt)*) => ( | ||
$($path_to_types_root)*::exports::bytecode_alliance::calculator::calculate::__export_docs_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::bytecode_alliance::calculator::calculate); | ||
$($path_to_types_root)*::exports::component_book::calculator::calculate::__export_component_book_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::component_book::calculator::calculate); | ||
) | ||
} | ||
#[doc(inline)] | ||
|
@@ -216,14 +216,14 @@ pub(crate) use __export_calculator_impl as export; | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:calculator:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 313] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb8\x01\x01A\x02\x01\ | ||
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x19bytecode_alliance:calcul\ | ||
ator/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\ | ||
\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fbytecode_alliance:calculator/\ | ||
[email protected]\x05\x01\x04\x01\x20bytecode_alliance:calculator/calculator@0.1.0\x04\0\x0b\x10\ | ||
\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-comp\ | ||
onent\x070.202.0\x10wit-bindgen-rust\x060.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 338] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd1\x01\x01A\x02\x01\ | ||
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x1ecomponent-b\ | ||
ook:adder/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02\ | ||
op\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01)component-book:ca\ | ||
lculator/[email protected]\x05\x01\x04\x01*component-book:calculator/calculator@0.\ | ||
1.0\x04\0\x0b\x10\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-b\ | ||
y\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Generated by `wit-bindgen` 0.24.0. DO NOT EDIT! | ||
// Options used: | ||
#[allow(dead_code)] | ||
pub mod docs { | ||
pub mod component_book { | ||
#[allow(dead_code)] | ||
pub mod calculator { | ||
#[allow(dead_code, clippy::all)] | ||
|
@@ -43,7 +43,7 @@ pub mod docs { | |
pub fn eval_expression(op: Op, x: u32, y: u32) -> u32 { | ||
unsafe { | ||
#[cfg(target_arch = "wasm32")] | ||
#[link(wasm_import_module = "bytecode_alliance:calculator/[email protected]")] | ||
#[link(wasm_import_module = "component-book:calculator/[email protected]")] | ||
extern "C" { | ||
#[link_name = "eval-expression"] | ||
fn wit_import(_: i32, _: i32, _: i32) -> i32; | ||
|
@@ -136,13 +136,13 @@ mod _rt { | |
#[cfg(target_arch = "wasm32")] | ||
#[link_section = "component-type:wit-bindgen:0.24.0:app:encoded world"] | ||
#[doc(hidden)] | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 246] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07}\x01A\x02\x01A\x02\x01\ | ||
B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01yy\0y\x04\0\ | ||
\x0feval-expression\x01\x02\x03\x01\x1fbytecode_alliance:calculator/[email protected]\x05\0\x04\ | ||
\x01\bytecode_alliance:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03app\x03\0\0\0G\x09produ\ | ||
cers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-bindgen-rust\x06\ | ||
0.24.0"; | ||
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 267] = *b"\ | ||
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x91\x01\x01A\x02\x01\ | ||
A\x02\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01y\ | ||
y\0y\x04\0\x0feval-expression\x01\x02\x03\x01)component-book:calculator/calculat\ | ||
[email protected]\x05\0\x04\x01#component-book:calculator/[email protected]\x04\0\x0b\x09\x01\0\x03\ | ||
app\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10\ | ||
wit-bindgen-rust\x060.24.0"; | ||
|
||
#[inline(never)] | ||
#[doc(hidden)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package component-book:adder@0.1.0; | ||
|
||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file is automatically generated by wit. | ||
# It is not intended for manual editing. | ||
version = 1 | ||
|
||
[[package]] | ||
name = "component-book:adder" | ||
|
||
[[package.version]] | ||
requirement = "^0.1.0" | ||
version = "0.1.0" | ||
digest = "sha256:6e82c02aac69186d371c2324e6c266a5b03a0a45015980a063e48bd56fe46f66" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
"component-book:adder" = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package component-book:adder@0.1.0; | ||
|
||
interface add { | ||
add: func(a: u32, b: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} |
Oops, something went wrong.