Skip to content

Commit 861ab29

Browse files
committed
Adding a multiply operation implemented as a hyperlight host function
Signed-off-by: Mark Rossett <[email protected]>
1 parent d06b47f commit 861ab29

File tree

6 files changed

+68
-9
lines changed

6 files changed

+68
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The [calculator](./wit/calculator.wit) itself is comprised of three wasm compone
66
- `calculator` - the main component that implements the calculator interface (written in Rust)
77
- `adder` - a component that implements the addition operation (written in Rust)
88
- `subtractor` - a component that implements the subtraction operation (written in Javascript)
9+
- Additionally there is a `multiply` operator that is implemented as a hyperlight host function.
910

1011
The [example](./example/) program loads the composed calculator/wasm component into a hyperlight-sandbox along with a wasm runtime and invokes functions as hyperlight-guest calls.
1112

components/calculator/src/bindings.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ pub mod hyperlight_wasm_examples {
5757
}
5858
}
5959
}
60+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
61+
pub mod multiply {
62+
#[used]
63+
#[doc(hidden)]
64+
static __FORCE_SECTION_REF: fn() = super::super::super::__link_custom_section_describing_imports;
65+
use super::super::super::_rt;
66+
#[allow(unused_unsafe, clippy::all)]
67+
pub fn multiply(x: u32, y: u32) -> u32 {
68+
unsafe {
69+
#[cfg(target_arch = "wasm32")]
70+
#[link(
71+
wasm_import_module = "hyperlight-wasm-examples:calculator/multiply"
72+
)]
73+
unsafe extern "C" {
74+
#[link_name = "multiply"]
75+
fn wit_import0(_: i32, _: i32) -> i32;
76+
}
77+
#[cfg(not(target_arch = "wasm32"))]
78+
unsafe extern "C" fn wit_import0(_: i32, _: i32) -> i32 {
79+
unreachable!()
80+
}
81+
let ret = unsafe { wit_import0(_rt::as_i32(&x), _rt::as_i32(&y)) };
82+
ret as u32
83+
}
84+
}
85+
}
6086
}
6187
}
6288
#[rustfmt::skip]
@@ -75,6 +101,7 @@ pub mod exports {
75101
pub enum Op {
76102
Add,
77103
Subtract,
104+
Multiply,
78105
}
79106
impl ::core::fmt::Debug for Op {
80107
fn fmt(
@@ -84,6 +111,7 @@ pub mod exports {
84111
match self {
85112
Op::Add => f.debug_tuple("Op::Add").finish(),
86113
Op::Subtract => f.debug_tuple("Op::Subtract").finish(),
114+
Op::Multiply => f.debug_tuple("Op::Multiply").finish(),
87115
}
88116
}
89117
}
@@ -96,6 +124,7 @@ pub mod exports {
96124
match val {
97125
0 => Op::Add,
98126
1 => Op::Subtract,
127+
2 => Op::Multiply,
99128
_ => panic!("invalid enum discriminant"),
100129
}
101130
}
@@ -238,16 +267,18 @@ pub(crate) use __export_calculator_impl as export;
238267
)]
239268
#[doc(hidden)]
240269
#[allow(clippy::octal_escapes)]
241-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 439] = *b"\
242-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb6\x02\x01A\x02\x01\
243-
A\x06\x01B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x03\0'hyperlight-wasm-\
270+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 524] = *b"\
271+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x8b\x03\x01A\x02\x01\
272+
A\x08\x01B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x03\0'hyperlight-wasm-\
244273
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\x0e\
247-
evalexpression\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";
274+
\x01\0\x03\0,hyperlight-wasm-examples:calculator/subtract\x05\x01\x01B\x02\x01@\x02\
275+
\x01xy\x01yy\0y\x04\0\x08multiply\x01\0\x03\0,hyperlight-wasm-examples:calculato\
276+
r/multiply\x05\x02\x01B\x04\x01m\x03\x03add\x08subtract\x08multiply\x04\0\x02op\x03\
277+
\0\0\x01@\x03\x02op\x01\x01xy\x01yy\0y\x04\0\x0eevalexpression\x01\x02\x04\0-hyp\
278+
erlight-wasm-examples:calculator/calculate\x05\x03\x04\0.hyperlight-wasm-example\
279+
s:calculator/calculator\x04\0\x0b\x10\x01\0\x0acalculator\x03\0\0\0G\x09producer\
280+
s\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x060.4\
281+
1.0";
251282
#[inline(never)]
252283
#[doc(hidden)]
253284
pub fn __link_custom_section_describing_imports() {

components/calculator/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bindings::exports::hyperlight_wasm_examples::calculator::calculate::{Guest,
44
// Bring imported functions from other components into scope
55
use bindings::hyperlight_wasm_examples::calculator::add::add;
66
use bindings::hyperlight_wasm_examples::calculator::subtract::subtract;
7+
use bindings::hyperlight_wasm_examples::calculator::multiply::multiply;
78

89
struct Component;
910

@@ -12,6 +13,7 @@ impl Guest for Component {
1213
match op {
1314
Op::Add => add(x,y),
1415
Op::Subtract => subtract(x, y),
16+
Op::Multiply => multiply(x, y),
1517
}
1618
}
1719
}

example/calculator-composed.wit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package hyperlight-wasm-examples:calculator;
22

3+
// In this example multiply will be implemented as a hyperlight host function.
4+
interface multiply {
5+
multiply: func(x: u32, y: u32) -> u32;
6+
}
7+
38
interface calculate {
49
enum op {
510
add,
611
subtract,
12+
multiply,
713
}
814
evalexpression: func(op: op, x:u32, y:u32) -> u32;
915
}
1016

1117
world calculator {
1218
export calculate;
19+
import multiply;
1320
}

example/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ use bindings::hyperlight_wasm_examples::calculator::Calculate;
1010
mod state;
1111
use state::MyState;
1212

13+
impl bindings::hyperlight_wasm_examples::calculator::Multiply for MyState {
14+
fn r#multiply(&mut self, r#x: u32, r#y: u32) -> u32 {
15+
r#x * r#y
16+
}
17+
}
18+
1319
impl bindings::hyperlight_wasm_examples::calculator::CalculatorImports for MyState {
20+
type Multiply = MyState;
21+
fn r#multiply(&mut self) -> impl ::core::borrow::BorrowMut<Self::Multiply> {
22+
self
23+
}
1424
}
1525

1626
fn main() {
@@ -42,4 +52,6 @@ fn main() {
4252
println!("13 - 5 = {r}");
4353
let r = calc_inst.evalexpression(Op::Add, 6, 7);
4454
println!("6 + 7 = {r}");
55+
let r = calc_inst.evalexpression(Op::Multiply, 4,3);
56+
println!("4 * 3 = {r}");
4557
}

wit/calculator.wit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ interface subtract {
1212
subtract: func(x: u32, y: u32) -> u32;
1313
}
1414

15+
interface multiply {
16+
multiply: func(x: u32, y: u32) -> u32;
17+
}
18+
1519
world subtractor {
1620
export subtract;
1721
}
@@ -20,6 +24,7 @@ interface calculate {
2024
enum op {
2125
add,
2226
subtract,
27+
multiply,
2328
}
2429
evalexpression: func(op: op, x:u32, y:u32) -> u32;
2530
}
@@ -28,4 +33,5 @@ world calculator {
2833
export calculate;
2934
import add;
3035
import subtract;
36+
import multiply;
3137
}

0 commit comments

Comments
 (0)