Skip to content

Commit

Permalink
cleaner recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed Sep 15, 2024
1 parent 09737ba commit ba550f4
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions crates/wit-component/src/encoding/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,16 @@ impl InterfaceEncoder<'_> {
}
}

fn encode_instance_contents(enc: &mut InterfaceEncoder, interface: InterfaceId) -> Result<()> {
enc.push_instance();
let iface = &enc.resolve.interfaces[interface];
fn encode_instance_contents(&mut self, interface: InterfaceId) -> Result<InstanceType> {
self.push_instance();
let iface = &self.resolve.interfaces[interface];
let mut type_order = IndexSet::new();
for (_, id) in iface.types.iter() {
let ty = &enc.resolve.types[*id];
let ty = &self.resolve.types[*id];
if let TypeOwner::Interface(iface_id) = ty.owner {
enc.interface = Some(iface_id);
self.interface = Some(iface_id);
}
enc.encode_valtype(enc.resolve, &Type::Id(*id))?;
self.encode_valtype(self.resolve, &Type::Id(*id))?;
type_order.insert(*id);
}

Expand Down Expand Up @@ -309,23 +309,27 @@ impl InterfaceEncoder<'_> {
});

for (name, func) in funcs {
let ty = enc.encode_func_type(enc.resolve, func)?;
enc.ty
let ty = self.encode_func_type(self.resolve, func)?;
self.ty
.as_mut()
.unwrap()
.export(name, ComponentTypeRef::Func(ty));
}
Ok(())
}

fn encode_instance(&mut self, interface: InterfaceId) -> Result<u32> {
Self::encode_instance_contents(self, interface)?;
let iface = &self.resolve.interfaces[interface];
let mut instance = self.pop_instance();
for nest in &iface.nested {
self.encode_nested(&self.interface_path(nest.id), nest.id, &mut instance)?;
let ty = instance.ty();
ty.instance(&self.encode_instance_contents(nest.id)?);
instance.export(
&self.interface_path(nest.id),
ComponentTypeRef::Instance(instance.type_count() - 1),
);
}

Ok(instance)
}

fn encode_instance(&mut self, interface: InterfaceId) -> Result<u32> {
let instance = self.encode_instance_contents(interface)?;
let idx = self.outer.type_count();
self.outer.ty().instance(&instance);
self.import_map.insert(interface, self.instances);
Expand All @@ -350,30 +354,6 @@ impl InterfaceEncoder<'_> {
}
}

fn encode_nested<'a>(
&mut self,
name: &str,
iface_id: InterfaceId,
instance: &'a mut InstanceType,
) -> Result<()> {
let mut inst = InterfaceEncoder::new(&self.resolve);
Self::encode_instance_contents(&mut inst, iface_id)?;
let ty = instance.ty();
let nested_instance = &mut inst.pop_instance();
let iface = &self.resolve.interfaces[iface_id];
for deep_nest in &iface.nested {
let mut inst = InterfaceEncoder::new(&self.resolve);
inst.encode_nested(
&self.interface_path(deep_nest.id),
deep_nest.id,
nested_instance,
)?;
}
ty.instance(&nested_instance);
instance.export(name, ComponentTypeRef::Instance(instance.type_count() - 1));
Ok(())
}

fn push_instance(&mut self) {
assert!(self.ty.is_none());
assert!(self.saved_types.is_none());
Expand Down

0 comments on commit ba550f4

Please sign in to comment.