Skip to content

Commit 9c8d219

Browse files
committed
Adding calculator component written in Rust
Signed-off-by: Mark Rossett <marosset@microsoft.com>
1 parent 70eaf25 commit 9c8d219

7 files changed

Lines changed: 344 additions & 0 deletions

File tree

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rust-analyzer.linkedProjects": [
3+
"components/adder/Cargo.toml",
4+
"components/calculator/Cargo.toml"
5+
]
6+
}

Justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ init-subtractor:
66

77
build-subtractor:
88
cd components/subtractor && npm run build:component
9+
10+
build-calculator:
11+
cd components/calculator && cargo component build

components/calculator/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/calculator/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "calculator"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
wit-bindgen-rt = { version = "0.41.0", features = ["bitflags"] }
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[package.metadata.component]
13+
package = "hyperlight-wasm-examples:calculator"
14+
15+
[package.metadata.component.dependencies]
16+
17+
[package.metadata.component.target]
18+
path = "../../wit/calculator.wit"
19+
world = "calculator"
20+
21+
[package.metadata.component.target.dependencies]
22+
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
2+
// Options used:
3+
// * runtime_path: "wit_bindgen_rt"
4+
#[rustfmt::skip]
5+
#[allow(dead_code, clippy::all)]
6+
pub mod hyperlight_wasm_examples {
7+
pub mod calculator {
8+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
9+
pub mod add {
10+
#[used]
11+
#[doc(hidden)]
12+
static __FORCE_SECTION_REF: fn() = super::super::super::__link_custom_section_describing_imports;
13+
use super::super::super::_rt;
14+
#[allow(unused_unsafe, clippy::all)]
15+
pub fn add(x: u32, y: u32) -> u32 {
16+
unsafe {
17+
#[cfg(target_arch = "wasm32")]
18+
#[link(
19+
wasm_import_module = "hyperlight-wasm-examples:calculator/add"
20+
)]
21+
unsafe extern "C" {
22+
#[link_name = "add"]
23+
fn wit_import0(_: i32, _: i32) -> i32;
24+
}
25+
#[cfg(not(target_arch = "wasm32"))]
26+
unsafe extern "C" fn wit_import0(_: i32, _: i32) -> i32 {
27+
unreachable!()
28+
}
29+
let ret = unsafe { wit_import0(_rt::as_i32(&x), _rt::as_i32(&y)) };
30+
ret as u32
31+
}
32+
}
33+
}
34+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
35+
pub mod subtract {
36+
#[used]
37+
#[doc(hidden)]
38+
static __FORCE_SECTION_REF: fn() = super::super::super::__link_custom_section_describing_imports;
39+
use super::super::super::_rt;
40+
#[allow(unused_unsafe, clippy::all)]
41+
pub fn subtract(x: u32, y: u32) -> u32 {
42+
unsafe {
43+
#[cfg(target_arch = "wasm32")]
44+
#[link(
45+
wasm_import_module = "hyperlight-wasm-examples:calculator/subtract"
46+
)]
47+
unsafe extern "C" {
48+
#[link_name = "subtract"]
49+
fn wit_import0(_: i32, _: i32) -> i32;
50+
}
51+
#[cfg(not(target_arch = "wasm32"))]
52+
unsafe extern "C" fn wit_import0(_: i32, _: i32) -> i32 {
53+
unreachable!()
54+
}
55+
let ret = unsafe { wit_import0(_rt::as_i32(&x), _rt::as_i32(&y)) };
56+
ret as u32
57+
}
58+
}
59+
}
60+
}
61+
}
62+
#[rustfmt::skip]
63+
#[allow(dead_code, clippy::all)]
64+
pub mod exports {
65+
pub mod hyperlight_wasm_examples {
66+
pub mod calculator {
67+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
68+
pub mod calculate {
69+
#[used]
70+
#[doc(hidden)]
71+
static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports;
72+
use super::super::super::super::_rt;
73+
#[repr(u8)]
74+
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
75+
pub enum Op {
76+
Add,
77+
Subtract,
78+
}
79+
impl ::core::fmt::Debug for Op {
80+
fn fmt(
81+
&self,
82+
f: &mut ::core::fmt::Formatter<'_>,
83+
) -> ::core::fmt::Result {
84+
match self {
85+
Op::Add => f.debug_tuple("Op::Add").finish(),
86+
Op::Subtract => f.debug_tuple("Op::Subtract").finish(),
87+
}
88+
}
89+
}
90+
impl Op {
91+
#[doc(hidden)]
92+
pub unsafe fn _lift(val: u8) -> Op {
93+
if !cfg!(debug_assertions) {
94+
return ::core::mem::transmute(val);
95+
}
96+
match val {
97+
0 => Op::Add,
98+
1 => Op::Subtract,
99+
_ => panic!("invalid enum discriminant"),
100+
}
101+
}
102+
}
103+
#[doc(hidden)]
104+
#[allow(non_snake_case)]
105+
pub unsafe fn _export_eval_expression_cabi<T: Guest>(
106+
arg0: i32,
107+
arg1: i32,
108+
arg2: i32,
109+
) -> i32 {
110+
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
111+
let result0 = T::eval_expression(
112+
Op::_lift(arg0 as u8),
113+
arg1 as u32,
114+
arg2 as u32,
115+
);
116+
_rt::as_i32(result0)
117+
}
118+
pub trait Guest {
119+
fn eval_expression(op: Op, x: u32, y: u32) -> u32;
120+
}
121+
#[doc(hidden)]
122+
macro_rules! __export_hyperlight_wasm_examples_calculator_calculate_cabi {
123+
($ty:ident with_types_in $($path_to_types:tt)*) => {
124+
const _ : () = { #[unsafe (export_name =
125+
"hyperlight-wasm-examples:calculator/calculate#eval-expression")]
126+
unsafe extern "C" fn export_eval_expression(arg0 : i32, arg1 :
127+
i32, arg2 : i32,) -> i32 { unsafe { $($path_to_types)*::
128+
_export_eval_expression_cabi::<$ty > (arg0, arg1, arg2) } } };
129+
};
130+
}
131+
#[doc(hidden)]
132+
pub(crate) use __export_hyperlight_wasm_examples_calculator_calculate_cabi;
133+
}
134+
}
135+
}
136+
}
137+
#[rustfmt::skip]
138+
mod _rt {
139+
#![allow(dead_code, clippy::all)]
140+
pub fn as_i32<T: AsI32>(t: T) -> i32 {
141+
t.as_i32()
142+
}
143+
pub trait AsI32 {
144+
fn as_i32(self) -> i32;
145+
}
146+
impl<'a, T: Copy + AsI32> AsI32 for &'a T {
147+
fn as_i32(self) -> i32 {
148+
(*self).as_i32()
149+
}
150+
}
151+
impl AsI32 for i32 {
152+
#[inline]
153+
fn as_i32(self) -> i32 {
154+
self as i32
155+
}
156+
}
157+
impl AsI32 for u32 {
158+
#[inline]
159+
fn as_i32(self) -> i32 {
160+
self as i32
161+
}
162+
}
163+
impl AsI32 for i16 {
164+
#[inline]
165+
fn as_i32(self) -> i32 {
166+
self as i32
167+
}
168+
}
169+
impl AsI32 for u16 {
170+
#[inline]
171+
fn as_i32(self) -> i32 {
172+
self as i32
173+
}
174+
}
175+
impl AsI32 for i8 {
176+
#[inline]
177+
fn as_i32(self) -> i32 {
178+
self as i32
179+
}
180+
}
181+
impl AsI32 for u8 {
182+
#[inline]
183+
fn as_i32(self) -> i32 {
184+
self as i32
185+
}
186+
}
187+
impl AsI32 for char {
188+
#[inline]
189+
fn as_i32(self) -> i32 {
190+
self as i32
191+
}
192+
}
193+
impl AsI32 for usize {
194+
#[inline]
195+
fn as_i32(self) -> i32 {
196+
self as i32
197+
}
198+
}
199+
#[cfg(target_arch = "wasm32")]
200+
pub fn run_ctors_once() {
201+
wit_bindgen_rt::run_ctors_once();
202+
}
203+
}
204+
/// Generates `#[unsafe(no_mangle)]` functions to export the specified type as
205+
/// the root implementation of all generated traits.
206+
///
207+
/// For more information see the documentation of `wit_bindgen::generate!`.
208+
///
209+
/// ```rust
210+
/// # macro_rules! export{ ($($t:tt)*) => (); }
211+
/// # trait Guest {}
212+
/// struct MyType;
213+
///
214+
/// impl Guest for MyType {
215+
/// // ...
216+
/// }
217+
///
218+
/// export!(MyType);
219+
/// ```
220+
#[allow(unused_macros)]
221+
#[doc(hidden)]
222+
macro_rules! __export_calculator_impl {
223+
($ty:ident) => {
224+
self::export!($ty with_types_in self);
225+
};
226+
($ty:ident with_types_in $($path_to_types_root:tt)*) => {
227+
$($path_to_types_root)*::
228+
exports::hyperlight_wasm_examples::calculator::calculate::__export_hyperlight_wasm_examples_calculator_calculate_cabi!($ty
229+
with_types_in $($path_to_types_root)*::
230+
exports::hyperlight_wasm_examples::calculator::calculate);
231+
};
232+
}
233+
#[doc(inline)]
234+
pub(crate) use __export_calculator_impl as export;
235+
#[cfg(target_arch = "wasm32")]
236+
#[unsafe(
237+
link_section = "component-type:wit-bindgen:0.41.0:hyperlight-wasm-examples:calculator:calculator:encoded world"
238+
)]
239+
#[doc(hidden)]
240+
#[allow(clippy::octal_escapes)]
241+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 440] = *b"\
242+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb7\x02\x01A\x02\x01\
243+
A\x06\x01B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x03\0'hyperlight-wasm-\
244+
examples:calculator/add\x05\0\x01B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x08subtract\
245+
\x01\0\x03\0,hyperlight-wasm-examples:calculator/subtract\x05\x01\x01B\x04\x01m\x02\
246+
\x03add\x08subtract\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01xy\x01yy\0y\x04\0\x0f\
247+
eval-expression\x01\x02\x04\0-hyperlight-wasm-examples:calculator/calculate\x05\x02\
248+
\x04\0.hyperlight-wasm-examples:calculator/calculator\x04\0\x0b\x10\x01\0\x0acal\
249+
culator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.22\
250+
7.1\x10wit-bindgen-rust\x060.41.0";
251+
#[inline(never)]
252+
#[doc(hidden)]
253+
pub fn __link_custom_section_describing_imports() {
254+
wit_bindgen_rt::maybe_link_cabi_realloc();
255+
}

components/calculator/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
mod bindings;
2+
use bindings::exports::hyperlight_wasm_examples::calculator::calculate::{Guest, Op};
3+
4+
// Bring imported functions from other components into scope
5+
use bindings::hyperlight_wasm_examples::calculator::add::add;
6+
use bindings::hyperlight_wasm_examples::calculator::subtract::subtract;
7+
8+
struct Component;
9+
10+
impl Guest for Component {
11+
fn eval_expression(op: Op, x: u32, y: u32) -> u32 {
12+
match op {
13+
Op::Add => add(x,y),
14+
Op::Subtract => subtract(x, y),
15+
}
16+
}
17+
}
18+
19+
bindings::export!(Component with_types_in bindings);

wit/calculator.wit

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ interface subtract {
1414

1515
world subtractor {
1616
export subtract;
17+
}
18+
19+
interface calculate {
20+
enum op {
21+
add,
22+
subtract,
23+
}
24+
eval-expression: func(op: op, x:u32, y:u32) -> u32;
25+
}
26+
27+
world calculator {
28+
export calculate;
29+
import add;
30+
import subtract;
1731
}

0 commit comments

Comments
 (0)