Skip to content

Commit b211b3d

Browse files
committed
Handle fixed list with different API
Signed-off-by: James Sturtevant <[email protected]>
1 parent d1e3e78 commit b211b3d

File tree

9 files changed

+56
-6
lines changed

9 files changed

+56
-6
lines changed

src/hyperlight_component_util/src/elaborate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ impl<'p, 'a> Ctx<'p, 'a> {
432432
ComponentDefinedType::Future(_) | ComponentDefinedType::Stream(_) => {
433433
panic!("async not yet supported")
434434
}
435-
ComponentDefinedType::FixedSizeList(vt, _) => {
436-
Ok(Value::List(Box::new(self.elab_value(vt)?)))
435+
ComponentDefinedType::FixedSizeList(vt, size) => {
436+
Ok(Value::FixList(Box::new(self.elab_value(vt)?), *size))
437437
}
438438
}
439439
}

src/hyperlight_component_util/src/etypes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum Value<'a> {
8383
Char,
8484
String,
8585
List(Box<Value<'a>>),
86+
FixList(Box<Value<'a>>, u32),
8687
Record(Vec<RecordField<'a>>),
8788
Tuple(Vec<Value<'a>>),
8889
Flags(Vec<Name<'a>>),

src/hyperlight_component_util/src/hl.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ pub fn emit_hl_unmarshal_value(s: &mut State, id: Ident, vt: &Value) -> TokenStr
223223
(#retid, cursor)
224224
}
225225
}
226+
Value::FixList(vt, _size) => {
227+
let inid = format_ident!("{}_elem", id);
228+
let vtun = emit_hl_unmarshal_value(s, inid.clone(), vt);
229+
quote! {
230+
let mut cursor = 0;
231+
let arr = ::core::array::from_fn(|_i| {
232+
let #inid = &#id[cursor..];
233+
let (x, b) = { #vtun };
234+
cursor += b;
235+
x
236+
});
237+
(arr, cursor)
238+
}
239+
}
226240
Value::Record(_) => panic!("record not at top level of valtype"),
227241
Value::Tuple(vts) => {
228242
let inid = format_ident!("{}_elem", id);
@@ -515,6 +529,25 @@ pub fn emit_hl_marshal_value(s: &mut State, id: Ident, vt: &Value) -> TokenStrea
515529
#retid
516530
}
517531
}
532+
Value::FixList(vt, _size) => {
533+
// Optimize for byte arrays ie. [u8; N]
534+
if matches!(vt.as_ref(), Value::S(_) | Value::U(_) | Value::F(_)) {
535+
quote! {
536+
alloc::vec::Vec::from(#id.as_slice())
537+
}
538+
} else {
539+
let retid = format_ident!("{}_fixlist", id);
540+
let inid = format_ident!("{}_elem", id);
541+
let vtun = emit_hl_marshal_value(s, inid.clone(), vt);
542+
quote! {
543+
let mut #retid = alloc::vec::Vec::new();
544+
for #inid in #id {
545+
#retid.extend({ #vtun })
546+
}
547+
#retid
548+
}
549+
}
550+
}
518551
Value::Record(_) => panic!("record not at top level of valtype"),
519552
Value::Tuple(vts) => {
520553
let retid = format_ident!("{}_tuple", id);

src/hyperlight_component_util/src/rtypes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ pub fn emit_value(s: &mut State, vt: &Value) -> TokenStream {
281281
let vt = emit_value(s, vt);
282282
quote! { alloc::vec::Vec<#vt> }
283283
}
284+
Value::FixList(vt, size) => {
285+
let vt = emit_value(s, vt);
286+
let size = *size as usize;
287+
quote! { [#vt; #size] }
288+
}
284289
Value::Record(_) => panic!("record not at top level of valtype"),
285290
Value::Tuple(vts) => {
286291
let vts = vts.iter().map(|vt| emit_value(s, vt)).collect::<Vec<_>>();

src/hyperlight_component_util/src/substitute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ where
9696
Value::Char => Value::Char,
9797
Value::String => Value::String,
9898
Value::List(vt) => Value::List(Box::new(self.value(vt)?)),
99+
Value::FixList(vt, size) => Value::FixList(Box::new(self.value(vt)?), *size),
99100
Value::Record(rfs) => Value::Record(self.record_fields(rfs)?),
100101
Value::Variant(vcs) => Value::Variant(self.variant_cases(vcs)?),
101102
Value::Flags(ns) => Value::Flags(ns.clone()),

src/hyperlight_component_util/src/wf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl<'p, 'a> Ctx<'p, 'a> {
213213
Value::Char => Ok(()),
214214
Value::String => Ok(()),
215215
Value::List(vt) => self.wf_value(p_, vt),
216+
Value::FixList(vt, _) => self.wf_value(p_, vt),
216217
Value::Record(rfs) => anon_err.and(self.wf_record_fields(p_, rfs)),
217218
Value::Variant(vcs) => anon_err.and(self.wf_variant_cases(p_, vcs)),
218219
Value::Flags(ns) => anon_err.and(error_if_duplicates_by(

src/hyperlight_host/tests/wit_test.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ impl test::wit::Roundtrip for Host {
155155
) -> test::wit::roundtrip::Testenum {
156156
x
157157
}
158-
fn roundtrip_fix_list(&mut self, x: Vec<u8>) -> Vec<u8> {
158+
fn roundtrip_fix_list(&mut self, x: [u8; 4]) -> [u8; 4] {
159+
x
160+
}
161+
fn roundtrip_fix_list_string(&mut self, x: [String; 4]) -> [String; 4] {
159162
x
160163
}
161164

@@ -334,10 +337,11 @@ mod wit_test {
334337
make_test! { roundtrip_flags_large, in arb_largeflags() }
335338
make_test! { roundtrip_variant, in arb_testvariant() }
336339
make_test! { roundtrip_enum, in arb_testenum() }
337-
make_test! { roundtrip_fix_list, : Vec<u8> }
340+
make_test! { roundtrip_fix_list, : [u8; 4] }
341+
make_test! { roundtrip_fix_list_string, : [String; 4] }
338342

339343
#[test]
340-
fn test_simple_func() {
344+
fn test_roundtrip_no_result() {
341345
sb().roundtrip().roundtrip_no_result(42);
342346
}
343347

src/tests/rust_guests/witguest/guest.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface roundtrip {
2727
roundtrip-result: func(x: result<char, string>) -> result<char, string>;
2828
roundtrip-no-result: func(x: u32);
2929
roundtrip-fix-list: func(x: list<u8, 4>) -> list<u8, 4>;
30+
roundtrip-fix-list-string: func(x: list<string, 4>) -> list<string, 4>;
3031

3132
record testrecord {
3233
contents: string,

src/tests/rust_guests/witguest/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern crate hyperlight_guest;
2222

2323
mod bindings;
2424
use bindings::*;
25+
use alloc::string::String;
2526

2627
struct Guest {}
2728

@@ -113,9 +114,12 @@ impl test::wit::Roundtrip for Guest {
113114
) -> test::wit::roundtrip::Testenum {
114115
(Host {}).roundtrip_enum(x)
115116
}
116-
fn roundtrip_fix_list(&mut self, x: Vec<u8>) -> Vec<u8> {
117+
fn roundtrip_fix_list(&mut self, x: [u8; 4]) -> [u8; 4] {
117118
(Host {}).roundtrip_fix_list(x)
118119
}
120+
fn roundtrip_fix_list_string(&mut self, x: [String; 4]) -> [String; 4] {
121+
(Host {}).roundtrip_fix_list_string(x)
122+
}
119123
fn roundtrip_no_result(&mut self, x: u32) {
120124
(Host {}).roundtrip_no_result(x)
121125
}

0 commit comments

Comments
 (0)