Skip to content

Commit a755690

Browse files
Merge pull request #136 from ritesh089/feature/isolatedwit
feat(isolatedwit): Moving the adder wit to a separate package.
2 parents 74beecf + 4ebfc44 commit a755690

File tree

14 files changed

+115
-91
lines changed

14 files changed

+115
-91
lines changed

Diff for: component-model/examples/tutorial/README.md

+23-11
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@ The WIT package for the calculator consists of a world for each mathematical ope
55
add an `op` enum that delineates each operator. The following example interface only
66
has an `add` operation:
77

8-
```wit
9-
package docs:calculator@0.1.0;
8+
```wit adder
9+
package docs:adder@0.1.0;
1010
11-
interface calculate {
12-
enum op {
13-
add,
14-
}
15-
eval-expression: func(op: op, x: u32, y: u32) -> u32;
16-
}
1711
1812
interface add {
1913
add: func(a: u32, b: u32) -> u32;
@@ -22,13 +16,30 @@ interface add {
2216
world adder {
2317
export add;
2418
}
19+
```
20+
21+
22+
```wit calculator
23+
package docs:[email protected];
24+
25+
interface calculate {
26+
enum op {
27+
add,
28+
}
29+
eval-expression: func(op: op, x: u32, y: u32) -> u32;
30+
}
2531
2632
world calculator {
2733
export calculate;
28-
import add;
34+
import docs:adder/add;
35+
}
36+
37+
world app {
38+
import calculate;
2939
}
3040
```
3141

42+
3243
To expand the exercise to add more components, add another operator world, expand the enum, and modify the `command` component to call it.
3344

3445
## Building and running the example
@@ -39,13 +50,14 @@ To compose a calculator component with an add operator, run the following:
3950
(cd calculator && cargo component build --release)
4051
(cd adder && cargo component build --release)
4152
(cd command && cargo component build --release)
53+
cd ..
4254
wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adder/target/wasm32-wasi/release/adder.wasm -o composed.wasm
43-
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm
55+
wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o final.wasm
4456
```
4557

4658
Now, run the component with wasmtime:
4759

4860
```sh
49-
wasmtime run command.wasm 1 2 add
61+
wasmtime run final.wasm 1 2 add
5062
1 + 2 = 3
5163
```

Diff for: component-model/examples/tutorial/adder/Cargo.lock

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: component-model/examples/tutorial/adder/Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
bitflags = "2.4.2"
87
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
98

109
[lib]
1110
crate-type = ["cdylib"]
1211

1312
[package.metadata.component]
14-
package = "docs:calculator"
13+
package = "docs:adder"
14+
15+
[package.metadata.component.dependencies]
1516

1617
[package.metadata.component.target]
17-
path = "../wit/calculator.wit"
18+
path = "../wit/adder"
1819
world = "adder"
19-
20-
[package.metadata.component.dependencies]

Diff for: component-model/examples/tutorial/adder/src/bindings.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod exports {
55
#[allow(dead_code)]
66
pub mod docs {
77
#[allow(dead_code)]
8-
pub mod calculator {
8+
pub mod adder {
99
#[allow(dead_code, clippy::all)]
1010
pub mod add {
1111
#[used]
@@ -27,17 +27,17 @@ pub mod exports {
2727
}
2828
#[doc(hidden)]
2929

30-
macro_rules! __export_docs_calculator_add_0_1_0_cabi{
30+
macro_rules! __export_docs_adder_add_0_1_0_cabi{
3131
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
3232

33-
#[export_name = "docs:calculator/[email protected]#add"]
33+
#[export_name = "docs:adder/[email protected]#add"]
3434
unsafe extern "C" fn export_add(arg0: i32,arg1: i32,) -> i32 {
3535
$($path_to_types)*::_export_add_cabi::<$ty>(arg0, arg1)
3636
}
3737
};);
3838
}
3939
#[doc(hidden)]
40-
pub(crate) use __export_docs_calculator_add_0_1_0_cabi;
40+
pub(crate) use __export_docs_adder_add_0_1_0_cabi;
4141
}
4242
}
4343
}
@@ -142,7 +142,7 @@ mod _rt {
142142
macro_rules! __export_adder_impl {
143143
($ty:ident) => (self::export!($ty with_types_in self););
144144
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
145-
$($path_to_types_root)*::exports::docs::calculator::add::__export_docs_calculator_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::calculator::add);
145+
$($path_to_types_root)*::exports::docs::adder::add::__export_docs_adder_add_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::adder::add);
146146
)
147147
}
148148
#[doc(inline)]
@@ -151,12 +151,12 @@ pub(crate) use __export_adder_impl as export;
151151
#[cfg(target_arch = "wasm32")]
152152
#[link_section = "component-type:wit-bindgen:0.24.0:adder:encoded world"]
153153
#[doc(hidden)]
154-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 213] = *b"\
155-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07Z\x01A\x02\x01A\x02\x01\
156-
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x19docs:calculator/add@\
157-
0.1.0\x05\0\x04\x01\x1bdocs:calculator/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\
158-
\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-\
159-
bindgen-rust\x060.24.0";
154+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 203] = *b"\
155+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07P\x01A\x02\x01A\x02\x01\
156+
B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x04\x01\x14docs:adder/add@0.1.0\
157+
\x05\0\x04\x01\x16docs:adder/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\0\0\0G\
158+
\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.202.0\x10wit-bindgen\
159+
-rust\x060.24.0";
160160

161161
#[inline(never)]
162162
#[doc(hidden)]

Diff for: component-model/examples/tutorial/adder/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#[allow(warnings)]
12
mod bindings;
23

3-
use crate::bindings::exports::docs::calculator::add::Guest;
4+
use bindings::exports::docs::adder::add::Guest;
45

56
struct Component;
67

Diff for: component-model/examples/tutorial/calculator/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: component-model/examples/tutorial/calculator/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
bitflags = "2.4.2"
87
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
98

109
[lib]
@@ -13,8 +12,11 @@ crate-type = ["cdylib"]
1312
[package.metadata.component]
1413
package = "docs:calculator"
1514

15+
[package.metadata.component.target.dependencies]
16+
"docs:adder" = { path = "../wit/adder" } # directory containing the WIT package
17+
1618
[package.metadata.component.target]
17-
path = "../wit/calculator.wit"
19+
path = "../wit/calculator"
1820
world = "calculator"
1921

2022
[package.metadata.component.dependencies]

Diff for: component-model/examples/tutorial/calculator/src/bindings.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[allow(dead_code)]
44
pub mod docs {
55
#[allow(dead_code)]
6-
pub mod calculator {
6+
pub mod adder {
77
#[allow(dead_code, clippy::all)]
88
pub mod add {
99
#[used]
@@ -16,7 +16,7 @@ pub mod docs {
1616
pub fn add(a: u32, b: u32) -> u32 {
1717
unsafe {
1818
#[cfg(target_arch = "wasm32")]
19-
#[link(wasm_import_module = "docs:calculator/[email protected]")]
19+
#[link(wasm_import_module = "docs:adder/[email protected]")]
2020
extern "C" {
2121
#[link_name = "add"]
2222
fn wit_import(_: i32, _: i32) -> i32;
@@ -216,14 +216,14 @@ pub(crate) use __export_calculator_impl as export;
216216
#[cfg(target_arch = "wasm32")]
217217
#[link_section = "component-type:wit-bindgen:0.24.0:calculator:encoded world"]
218218
#[doc(hidden)]
219-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 313] = *b"\
220-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb8\x01\x01A\x02\x01\
221-
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x19docs:calcul\
222-
ator/[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\
223-
\x01\x01xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fdocs:calculator/\
224-
calculate@0.1.0\x05\x01\x04\x01\x20docs:calculator/[email protected]\x04\0\x0b\x10\
225-
\x01\0\x0acalculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-comp\
226-
onent\x070.202.0\x10wit-bindgen-rust\x060.24.0";
219+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 308] = *b"\
220+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb3\x01\x01A\x02\x01\
221+
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x14docs:adder/\
222+
[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01\
223+
xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fdocs:calculator/calculat\
224+
e@0.1.0\x05\x01\x04\x01\x20docs:calculator/[email protected]\x04\0\x0b\x10\x01\0\x0a\
225+
calculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070\
226+
.202.0\x10wit-bindgen-rust\x060.24.0";
227227

228228
#[inline(never)]
229229
#[doc(hidden)]

Diff for: component-model/examples/tutorial/calculator/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#[allow(warnings)]
12
mod bindings;
23

34
use bindings::exports::docs::calculator::calculate::{Guest, Op};
45

56
// Bring the imported add function into scope
6-
use bindings::docs::calculator::add::add;
7+
use bindings::docs::adder::add::add;
78

89
struct Component;
910

Diff for: component-model/examples/tutorial/command/Cargo.lock

+8-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: component-model/examples/tutorial/command/Cargo.toml

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ name = "command"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[package.metadata.component]
7-
package = "docs:calculator"
6+
[dependencies]
7+
anyhow = "1"
8+
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
9+
clap = { version = "4.3.19", features = ["derive"] }
810

911
[package.metadata.component.target]
10-
path = "../wit/calculator.wit"
12+
path = "../wit/calculator"
1113
world = "app"
1214

13-
[dependencies]
14-
anyhow = "1"
15-
clap = { version = "4.3.19", features = ["derive"] }
16-
bitflags = "2.4.2"
17-
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
15+
[package.metadata.component.target.dependencies]
16+
"docs:adder" = { path = "../wit/adder" }
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package docs:adder@0.1.0;
2+
3+
interface add {
4+
add: func(a: u32, b: u32) -> u32;
5+
}
6+
7+
world adder {
8+
export add;
9+
}

Diff for: component-model/examples/tutorial/wit/calculator.wit renamed to component-model/examples/tutorial/wit/calculator/world.wit

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@ interface calculate {
77
eval-expression: func(op: op, x: u32, y: u32) -> u32;
88
}
99

10-
interface add {
11-
add: func(a: u32, b: u32) -> u32;
12-
}
13-
14-
world adder {
15-
export add;
16-
}
17-
1810
world calculator {
1911
export calculate;
20-
import add;
12+
import docs:adder/add@0.1.0;
2113
}
2214

2315
world app {

0 commit comments

Comments
 (0)