use crate::limits::{MAX_WASM_INSTANTIATION_ARGS, MAX_WASM_INSTANTIATION_EXPORTS};
use crate::prelude::*;
use crate::{
BinaryReader, ComponentExport, ComponentExternalKind, Export, FromReader, Result,
SectionLimited,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum InstantiationArgKind {
Instance,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct InstantiationArg<'a> {
pub name: &'a str,
pub kind: InstantiationArgKind,
pub index: u32,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Instance<'a> {
Instantiate {
module_index: u32,
args: Box<[InstantiationArg<'a>]>,
},
FromExports(Box<[Export<'a>]>),
}
pub type InstanceSectionReader<'a> = SectionLimited<'a, Instance<'a>>;
impl<'a> FromReader<'a> for Instance<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read_u8()? {
0x00 => Instance::Instantiate {
module_index: reader.read_var_u32()?,
args: reader
.read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
.collect::<Result<_>>()?,
},
0x01 => Instance::FromExports(
reader
.read_iter(MAX_WASM_INSTANTIATION_ARGS, "core instantiation arguments")?
.collect::<Result<_>>()?,
),
x => return reader.invalid_leading_byte(x, "core instance"),
})
}
}
impl<'a> FromReader<'a> for InstantiationArg<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(InstantiationArg {
name: reader.read()?,
kind: reader.read()?,
index: reader.read()?,
})
}
}
impl<'a> FromReader<'a> for InstantiationArgKind {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read_u8()? {
0x12 => InstantiationArgKind::Instance,
x => return reader.invalid_leading_byte(x, "instantiation arg kind"),
})
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ComponentInstantiationArg<'a> {
pub name: &'a str,
pub kind: ComponentExternalKind,
pub index: u32,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ComponentInstance<'a> {
Instantiate {
component_index: u32,
args: Box<[ComponentInstantiationArg<'a>]>,
},
FromExports(Box<[ComponentExport<'a>]>),
}
pub type ComponentInstanceSectionReader<'a> = SectionLimited<'a, ComponentInstance<'a>>;
impl<'a> FromReader<'a> for ComponentInstance<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(match reader.read_u8()? {
0x00 => ComponentInstance::Instantiate {
component_index: reader.read_var_u32()?,
args: reader
.read_iter(MAX_WASM_INSTANTIATION_ARGS, "instantiation arguments")?
.collect::<Result<_>>()?,
},
0x01 => ComponentInstance::FromExports(
(0..reader.read_size(MAX_WASM_INSTANTIATION_EXPORTS, "instantiation exports")?)
.map(|_| {
Ok(ComponentExport {
name: reader.read()?,
kind: reader.read()?,
index: reader.read()?,
ty: None,
})
})
.collect::<Result<_>>()?,
),
x => return reader.invalid_leading_byte(x, "instance"),
})
}
}
impl<'a> FromReader<'a> for ComponentInstantiationArg<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
Ok(ComponentInstantiationArg {
name: reader.read()?,
kind: reader.read()?,
index: reader.read()?,
})
}
}