Skip to content

Commit d7302c7

Browse files
committed
Updates for wasmparser apis
This was a bit more involved due to a big change in the way function return types are handled. The orginal change was done in WebAssembly/component-model#368 and put initially put behind a feature flag in bytecodealliance/wasm-tools#1670. In 0.226.0 version of wasmparser this feature flag was removed simplify the return results. This in turn simplifies the handling of the results here as well. Signed-off-by: James Sturtevant <[email protected]>
1 parent ccd94e8 commit d7302c7

File tree

7 files changed

+36
-55
lines changed

7 files changed

+36
-55
lines changed

src/hyperlight_component_util/src/elaborate.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ limitations under the License.
2424
//! substitute.rs for more details of the approach here).
2525
2626
use wasmparser::{
27-
ComponentAlias, ComponentDefinedType, ComponentFuncResult, ComponentFuncType,
28-
ComponentOuterAliasKind, ComponentType, ComponentTypeDeclaration, ComponentTypeRef,
29-
ComponentValType, CompositeInnerType, CoreType, InstanceTypeDeclaration, ModuleTypeDeclaration,
30-
OuterAliasKind, PrimitiveValType, TypeBounds, TypeRef,
27+
ComponentAlias, ComponentDefinedType, ComponentFuncType, ComponentOuterAliasKind,
28+
ComponentType, ComponentTypeDeclaration, ComponentTypeRef, ComponentValType,
29+
CompositeInnerType, CoreType, InstanceTypeDeclaration, ModuleTypeDeclaration, OuterAliasKind,
30+
PrimitiveValType, TypeBounds, TypeRef,
3131
};
3232

3333
use crate::etypes::{
34-
self, BoundedTyvar, Component, CoreDefined, CoreExportDecl, CoreExternDesc, CoreModule,
34+
BoundedTyvar, Component, CoreDefined, CoreExportDecl, CoreExternDesc, CoreModule,
3535
CoreOrComponentExternDesc, Ctx, Defined, ExternDecl, ExternDesc, FloatWidth, Func, Handleable,
3636
Instance, IntWidth, Name, Param, QualifiedInstance, RecordField, Resource, ResourceId,
3737
TypeBound, Tyvar, Value, VariantCase,
@@ -366,6 +366,7 @@ impl<'p, 'a> Ctx<'p, 'a> {
366366
PrimitiveValType::F64 => Value::F(FloatWidth::F64),
367367
PrimitiveValType::Char => Value::Char,
368368
PrimitiveValType::String => Value::String,
369+
PrimitiveValType::ErrorContext => panic!("async not yet supported"),
369370
}),
370371
}
371372
}
@@ -428,9 +429,12 @@ impl<'p, 'a> Ctx<'p, 'a> {
428429
Defined::Handleable(h) => Ok(Value::Borrow(h.clone())),
429430
_ => Err(Error::HandleToNonResource),
430431
},
431-
ComponentDefinedType::Future(_)
432-
| ComponentDefinedType::Stream(_)
433-
| ComponentDefinedType::ErrorContext => panic!("async not yet supported"),
432+
ComponentDefinedType::Future(_) | ComponentDefinedType::Stream(_) => {
433+
panic!("async not yet supported")
434+
}
435+
ComponentDefinedType::FixedSizeList(vt, _) => {
436+
Ok(Value::List(Box::new(self.elab_value(vt)?)))
437+
}
434438
}
435439
}
436440

@@ -446,18 +450,9 @@ impl<'p, 'a> Ctx<'p, 'a> {
446450
})
447451
})
448452
.collect::<Result<Vec<_>, Error<'a>>>()?,
449-
result: match &ft.results {
450-
ComponentFuncResult::Unnamed(vt) => etypes::Result::Unnamed(self.elab_value(vt)?),
451-
ComponentFuncResult::Named(rs) => etypes::Result::Named(
452-
rs.iter()
453-
.map(|(n, vt)| {
454-
Ok(Param {
455-
name: Name { name: n },
456-
ty: self.elab_value(vt)?,
457-
})
458-
})
459-
.collect::<Result<Vec<_>, Error<'a>>>()?,
460-
),
453+
result: match &ft.result {
454+
Some(vt) => Some(self.elab_value(vt)?),
455+
None => None,
461456
},
462457
})
463458
}

src/hyperlight_component_util/src/etypes.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,11 @@ pub struct Param<'a> {
136136
pub ty: Value<'a>,
137137
}
138138

139-
#[derive(Debug, Clone)]
140-
pub enum Result<'a> {
141-
Unnamed(Value<'a>),
142-
Named(Vec<Param<'a>>),
143-
}
144-
145139
/// functype_e in the specification
146140
#[derive(Debug, Clone)]
147141
pub struct Func<'a> {
148142
pub params: Vec<Param<'a>>,
149-
pub result: Result<'a>,
143+
pub result: Option<Value<'a>>,
150144
}
151145

152146
/// In the spec, this does not exist, but a validation rule ensures an

src/hyperlight_component_util/src/hl.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use proc_macro2::{Ident, TokenStream};
1919
use quote::{format_ident, quote};
2020

2121
use crate::emit::{State, kebab_to_cons, kebab_to_var};
22-
use crate::etypes::{self, Defined, Handleable, TypeBound, Tyvar, Value};
22+
use crate::etypes::{Defined, Handleable, TypeBound, Tyvar, Value};
2323
use crate::rtypes;
2424

2525
/// Construct a string that can be used "on the wire" to identify a
@@ -653,14 +653,13 @@ pub fn emit_hl_unmarshal_param(s: &mut State, id: Ident, pt: &Value) -> TokenStr
653653
///
654654
/// Precondition: the result type must only be a named result if there
655655
/// are no names in it (i.e. a unit type)
656-
pub fn emit_hl_unmarshal_result(s: &mut State, id: Ident, rt: &etypes::Result) -> TokenStream {
656+
pub fn emit_hl_unmarshal_result(s: &mut State, id: Ident, rt: &Option<Value<'_>>) -> TokenStream {
657657
match rt {
658-
etypes::Result::Named(rs) if rs.is_empty() => quote! { () },
659-
etypes::Result::Unnamed(vt) => {
658+
Some(vt) => {
660659
let toks = emit_hl_unmarshal_value(s, id, vt);
661660
quote! { { #toks }.0 }
662661
}
663-
_ => panic!("named results not supported"),
662+
None => quote! { () },
664663
}
665664
}
666665

@@ -678,13 +677,12 @@ pub fn emit_hl_marshal_param(s: &mut State, id: Ident, pt: &Value) -> TokenStrea
678677
///
679678
/// Precondition: the result type must only be a named result if there
680679
/// are no names in it (a unit type)
681-
pub fn emit_hl_marshal_result(s: &mut State, id: Ident, rt: &etypes::Result) -> TokenStream {
680+
pub fn emit_hl_marshal_result(s: &mut State, id: Ident, rt: &Option<Value<'_>>) -> TokenStream {
682681
match rt {
683-
etypes::Result::Named(rs) if rs.is_empty() => quote! { ::alloc::vec::Vec::new() },
684-
etypes::Result::Unnamed(vt) => {
682+
None => quote! { ::alloc::vec::Vec::new() },
683+
Some(vt) => {
685684
let toks = emit_hl_marshal_value(s, id, vt);
686685
quote! { { #toks } }
687686
}
688-
_ => panic!("named results not supported"),
689687
}
690688
}

src/hyperlight_component_util/src/rtypes.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::emit::{
3030
};
3131
use crate::etypes::{
3232
Component, Defined, ExternDecl, ExternDesc, Func, Handleable, ImportExport, Instance, Param,
33-
Result, TypeBound, Tyvar, Value,
33+
TypeBound, Tyvar, Value,
3434
};
3535

3636
/// When referring to an instance or resource trait, emit a token
@@ -521,11 +521,10 @@ pub fn emit_func_param(s: &mut State, p: &Param) -> TokenStream {
521521
///
522522
/// Precondition: the result type must only be a named result if there
523523
/// are no names in it (i.e. a unit type)
524-
pub fn emit_func_result(s: &mut State, r: &Result) -> TokenStream {
524+
pub fn emit_func_result(s: &mut State, r: &Option<Value<'_>>) -> TokenStream {
525525
match r {
526-
Result::Unnamed(vt) => emit_value(s, vt),
527-
Result::Named(rs) if rs.is_empty() => quote! { () },
528-
_ => panic!("multiple named function results are not currently supported"),
526+
Some(vt) => emit_value(s, vt),
527+
None => quote! { () },
529528
}
530529
}
531530

src/hyperlight_component_util/src/substitute.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,10 @@ where
134134
pts.iter().map(|pt| self.param(pt)).collect()
135135
}
136136

137-
fn result(
138-
&self,
139-
rt: &crate::etypes::Result<'a>,
140-
) -> Result<crate::etypes::Result<'a>, Self::Error> {
137+
fn result(&self, rt: &Option<Value<'a>>) -> Result<Option<Value<'a>>, Self::Error> {
141138
Ok(match rt {
142-
crate::etypes::Result::Unnamed(vt) => crate::etypes::Result::Unnamed(self.value(vt)?),
143-
crate::etypes::Result::Named(pts) => crate::etypes::Result::Named(self.params(pts)?),
139+
Some(vt) => Some(self.value(vt)?),
140+
None => None,
144141
})
145142
}
146143

src/hyperlight_component_util/src/wf.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,8 @@ impl<'p, 'a> Ctx<'p, 'a> {
268268
.iter()
269269
.try_for_each(|fp: &'r Param<'a>| self.wf_value(param_pos, &fp.ty))?;
270270
match &ft.result {
271-
crate::etypes::Result::Unnamed(vt) => self.wf_value(result_pos, vt),
272-
crate::etypes::Result::Named(ps) => ps
273-
.iter()
274-
.try_for_each(|fp: &'r Param<'a>| self.wf_value(result_pos, &fp.ty)),
271+
Some(vt) => self.wf_value(result_pos, vt),
272+
None => Ok(()),
275273
}
276274
}
277275
fn wf_type_bound<'r>(

src/tests/rust_guests/witguest/Cargo.lock

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

0 commit comments

Comments
 (0)