| 1 |
use crate::component::*; |
| 2 |
use crate::core; |
| 3 |
use crate::core::EncodeOptions; |
| 4 |
use crate::token::{Id, NameAnnotation}; |
| 5 |
use wasm_encoder::{ |
| 6 |
CanonicalFunctionSection, ComponentAliasSection, ComponentCoreTypeEncoder, |
| 7 |
ComponentDefinedTypeEncoder, ComponentExportSection, ComponentImportSection, |
| 8 |
ComponentInstanceSection, ComponentNameSection, ComponentSection, ComponentSectionId, |
| 9 |
ComponentStartSection, ComponentTypeEncoder, ComponentTypeSection, CoreTypeSection, |
| 10 |
InstanceSection, NameMap, NestedComponentSection, RawSection, |
| 11 |
}; |
| 12 |
|
| 13 |
pub fn encode(component: &Component<'_>, options: &EncodeOptions) -> Vec<u8> { |
| 14 |
match &component.kind { |
| 15 |
ComponentKind::Text(fields) => { |
| 16 |
encode_fields(&component.id, &component.name, fields, options).finish() |
| 17 |
} |
| 18 |
ComponentKind::Binary(bytes) => bytes.iter().flat_map(|b| b.iter().copied()).collect(), |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
fn encode_fields( |
| 23 |
// TODO: use the id and name for a future names section |
| 24 |
component_id: &Option<Id<'_>>, |
| 25 |
component_name: &Option<NameAnnotation<'_>>, |
| 26 |
fields: &[ComponentField<'_>], |
| 27 |
options: &EncodeOptions, |
| 28 |
) -> wasm_encoder::Component { |
| 29 |
let mut e = Encoder::default(); |
| 30 |
|
| 31 |
for field in fields { |
| 32 |
match field { |
| 33 |
ComponentField::CoreModule(m) => e.encode_core_module(m, options), |
| 34 |
ComponentField::CoreInstance(i) => e.encode_core_instance(i), |
| 35 |
ComponentField::CoreType(t) => e.encode_core_type(t), |
| 36 |
ComponentField::CoreRec(t) => e.encode_core_rec(t), |
| 37 |
ComponentField::Component(c) => e.encode_component(c, options), |
| 38 |
ComponentField::Instance(i) => e.encode_instance(i), |
| 39 |
ComponentField::Alias(a) => e.encode_alias(a), |
| 40 |
ComponentField::Type(t) => e.encode_type(t), |
| 41 |
ComponentField::CanonicalFunc(f) => e.encode_canonical_func(f), |
| 42 |
ComponentField::CoreFunc(_) | ComponentField::Func(_) => { |
| 43 |
unreachable!("should be expanded already") |
| 44 |
} |
| 45 |
ComponentField::Start(s) => e.encode_start(s), |
| 46 |
ComponentField::Import(i) => e.encode_import(i), |
| 47 |
ComponentField::Export(ex) => e.encode_export(ex), |
| 48 |
ComponentField::Custom(c) => e.encode_custom(c), |
| 49 |
ComponentField::Producers(c) => e.encode_producers(c), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
e.flush(None); |
| 54 |
e.encode_names(component_id, component_name); |
| 55 |
|
| 56 |
e.component |
| 57 |
} |
| 58 |
|
| 59 |
fn encode_core_type(encoder: ComponentCoreTypeEncoder, ty: &CoreTypeDef) { |
| 60 |
match ty { |
| 61 |
CoreTypeDef::Def(def) => { |
| 62 |
encoder.core().subtype(&def.to_subtype()); |
| 63 |
} |
| 64 |
CoreTypeDef::Module(t) => { |
| 65 |
encoder.module(&t.into()); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
fn encode_type(encoder: ComponentTypeEncoder, ty: &TypeDef) { |
| 71 |
match ty { |
| 72 |
TypeDef::Defined(t) => { |
| 73 |
encode_defined_type(encoder.defined_type(), t); |
| 74 |
} |
| 75 |
TypeDef::Func(f) => { |
| 76 |
let mut encoder = encoder.function(); |
| 77 |
encoder.params(f.params.iter().map(|p| (p.name, &p.ty))); |
| 78 |
|
| 79 |
if f.results.len() == 1 && f.results[0].name.is_none() { |
| 80 |
encoder.result(&f.results[0].ty); |
| 81 |
} else { |
| 82 |
encoder.results(f.results.iter().map(|r| (r.name.unwrap_or(""), &r.ty))); |
| 83 |
} |
| 84 |
} |
| 85 |
TypeDef::Component(c) => { |
| 86 |
encoder.component(&c.into()); |
| 87 |
} |
| 88 |
TypeDef::Instance(i) => { |
| 89 |
encoder.instance(&i.into()); |
| 90 |
} |
| 91 |
TypeDef::Resource(i) => { |
| 92 |
let dtor = i.dtor.as_ref().map(|i| i.idx.into()); |
| 93 |
encoder.resource(i.rep.into(), dtor); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
fn encode_defined_type(encoder: ComponentDefinedTypeEncoder, ty: &ComponentDefinedType) { |
| 99 |
match ty { |
| 100 |
ComponentDefinedType::Primitive(p) => encoder.primitive((*p).into()), |
| 101 |
ComponentDefinedType::Record(r) => { |
| 102 |
encoder.record(r.fields.iter().map(|f| (f.name, &f.ty))); |
| 103 |
} |
| 104 |
ComponentDefinedType::Variant(v) => { |
| 105 |
encoder.variant(v.cases.iter().map(|c| { |
| 106 |
( |
| 107 |
c.name, |
| 108 |
c.ty.as_ref().map(Into::into), |
| 109 |
c.refines.as_ref().map(Into::into), |
| 110 |
) |
| 111 |
})); |
| 112 |
} |
| 113 |
ComponentDefinedType::List(l) => { |
| 114 |
encoder.list(l.element.as_ref()); |
| 115 |
} |
| 116 |
ComponentDefinedType::Tuple(t) => { |
| 117 |
encoder.tuple(t.fields.iter()); |
| 118 |
} |
| 119 |
ComponentDefinedType::Flags(f) => { |
| 120 |
encoder.flags(f.names.iter().copied()); |
| 121 |
} |
| 122 |
ComponentDefinedType::Enum(e) => { |
| 123 |
encoder.enum_type(e.names.iter().copied()); |
| 124 |
} |
| 125 |
ComponentDefinedType::Option(o) => { |
| 126 |
encoder.option(o.element.as_ref()); |
| 127 |
} |
| 128 |
ComponentDefinedType::Result(e) => { |
| 129 |
encoder.result( |
| 130 |
e.ok.as_deref().map(Into::into), |
| 131 |
e.err.as_deref().map(Into::into), |
| 132 |
); |
| 133 |
} |
| 134 |
ComponentDefinedType::Own(i) => encoder.own((*i).into()), |
| 135 |
ComponentDefinedType::Borrow(i) => encoder.borrow((*i).into()), |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[derive(Default)] |
| 140 |
struct Encoder<'a> { |
| 141 |
component: wasm_encoder::Component, |
| 142 |
current_section_id: Option<u8>, |
| 143 |
|
| 144 |
// Core sections |
| 145 |
// Note: module sections are written immediately |
| 146 |
core_instances: InstanceSection, |
| 147 |
core_types: CoreTypeSection, |
| 148 |
|
| 149 |
// Component sections |
| 150 |
// Note: custom, component, start sections are written immediately |
| 151 |
instances: ComponentInstanceSection, |
| 152 |
aliases: ComponentAliasSection, |
| 153 |
types: ComponentTypeSection, |
| 154 |
funcs: CanonicalFunctionSection, |
| 155 |
imports: ComponentImportSection, |
| 156 |
exports: ComponentExportSection, |
| 157 |
|
| 158 |
core_func_names: Vec<Option<&'a str>>, |
| 159 |
core_table_names: Vec<Option<&'a str>>, |
| 160 |
core_memory_names: Vec<Option<&'a str>>, |
| 161 |
core_global_names: Vec<Option<&'a str>>, |
| 162 |
core_type_names: Vec<Option<&'a str>>, |
| 163 |
core_module_names: Vec<Option<&'a str>>, |
| 164 |
core_instance_names: Vec<Option<&'a str>>, |
| 165 |
func_names: Vec<Option<&'a str>>, |
| 166 |
value_names: Vec<Option<&'a str>>, |
| 167 |
type_names: Vec<Option<&'a str>>, |
| 168 |
component_names: Vec<Option<&'a str>>, |
| 169 |
instance_names: Vec<Option<&'a str>>, |
| 170 |
} |
| 171 |
|
| 172 |
impl<'a> Encoder<'a> { |
| 173 |
fn encode_custom(&mut self, custom: &Custom) { |
| 174 |
// Flush any in-progress section before encoding the customs section |
| 175 |
self.flush(None); |
| 176 |
self.component.section(&custom.to_section()); |
| 177 |
} |
| 178 |
|
| 179 |
fn encode_producers(&mut self, custom: &core::Producers) { |
| 180 |
self.flush(None); |
| 181 |
self.component.section(&custom.to_section()); |
| 182 |
} |
| 183 |
|
| 184 |
fn encode_core_module(&mut self, module: &CoreModule<'a>, options: &EncodeOptions) { |
| 185 |
// Flush any in-progress section before encoding the module |
| 186 |
self.flush(None); |
| 187 |
|
| 188 |
self.core_module_names |
| 189 |
.push(get_name(&module.id, &module.name)); |
| 190 |
|
| 191 |
match &module.kind { |
| 192 |
CoreModuleKind::Import { .. } => unreachable!("should be expanded already"), |
| 193 |
CoreModuleKind::Inline { fields } => { |
| 194 |
// TODO: replace this with a wasm-encoder based encoding (should return `wasm_encoder::Module`) |
| 195 |
let data = crate::core::binary::encode(&module.id, &module.name, fields, options); |
| 196 |
self.component.section(&RawSection { |
| 197 |
id: ComponentSectionId::CoreModule.into(), |
| 198 |
data: &data, |
| 199 |
}); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
fn encode_core_instance(&mut self, instance: &CoreInstance<'a>) { |
| 205 |
self.core_instance_names |
| 206 |
.push(get_name(&instance.id, &instance.name)); |
| 207 |
match &instance.kind { |
| 208 |
CoreInstanceKind::Instantiate { module, args } => { |
| 209 |
self.core_instances.instantiate( |
| 210 |
module.into(), |
| 211 |
args.iter().map(|arg| (arg.name, (&arg.kind).into())), |
| 212 |
); |
| 213 |
} |
| 214 |
CoreInstanceKind::BundleOfExports(exports) => { |
| 215 |
self.core_instances.export_items(exports.iter().map(|e| { |
| 216 |
let (kind, index) = (&e.item).into(); |
| 217 |
(e.name, kind, index) |
| 218 |
})); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
self.flush(Some(self.core_instances.id())); |
| 223 |
} |
| 224 |
|
| 225 |
fn encode_core_type(&mut self, ty: &CoreType<'a>) { |
| 226 |
self.core_type_names.push(get_name(&ty.id, &ty.name)); |
| 227 |
encode_core_type(self.core_types.ty(), &ty.def); |
| 228 |
self.flush(Some(self.core_types.id())); |
| 229 |
} |
| 230 |
|
| 231 |
fn encode_core_rec(&mut self, ty: &core::Rec<'a>) { |
| 232 |
for ty in ty.types.iter() { |
| 233 |
self.core_type_names.push(get_name(&ty.id, &ty.name)); |
| 234 |
} |
| 235 |
self.core_types |
| 236 |
.ty() |
| 237 |
.core() |
| 238 |
.rec(ty.types.iter().map(|t| t.to_subtype())); |
| 239 |
self.flush(Some(self.core_types.id())); |
| 240 |
} |
| 241 |
|
| 242 |
fn encode_component(&mut self, component: &NestedComponent<'a>, options: &EncodeOptions) { |
| 243 |
self.component_names |
| 244 |
.push(get_name(&component.id, &component.name)); |
| 245 |
// Flush any in-progress section before encoding the component |
| 246 |
self.flush(None); |
| 247 |
|
| 248 |
match &component.kind { |
| 249 |
NestedComponentKind::Import { .. } => unreachable!("should be expanded already"), |
| 250 |
NestedComponentKind::Inline(fields) => { |
| 251 |
self.component |
| 252 |
.section(&NestedComponentSection(&encode_fields( |
| 253 |
&component.id, |
| 254 |
&component.name, |
| 255 |
fields, |
| 256 |
options, |
| 257 |
))); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
fn encode_instance(&mut self, instance: &Instance<'a>) { |
| 263 |
self.instance_names |
| 264 |
.push(get_name(&instance.id, &instance.name)); |
| 265 |
match &instance.kind { |
| 266 |
InstanceKind::Import { .. } => unreachable!("should be expanded already"), |
| 267 |
InstanceKind::Instantiate { component, args } => { |
| 268 |
self.instances.instantiate( |
| 269 |
component.into(), |
| 270 |
args.iter().map(|arg| { |
| 271 |
let (kind, index) = (&arg.kind).into(); |
| 272 |
(arg.name, kind, index) |
| 273 |
}), |
| 274 |
); |
| 275 |
} |
| 276 |
InstanceKind::BundleOfExports(exports) => { |
| 277 |
self.instances.export_items(exports.iter().map(|e| { |
| 278 |
let (kind, index) = (&e.kind).into(); |
| 279 |
(e.name.0, kind, index) |
| 280 |
})); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
self.flush(Some(self.instances.id())); |
| 285 |
} |
| 286 |
|
| 287 |
fn encode_alias(&mut self, alias: &Alias<'a>) { |
| 288 |
let name = get_name(&alias.id, &alias.name); |
| 289 |
self.aliases.alias((&alias.target).into()); |
| 290 |
match &alias.target { |
| 291 |
AliasTarget::Export { kind, .. } => { |
| 292 |
self.names_for_component_export_alias(*kind).push(name); |
| 293 |
} |
| 294 |
AliasTarget::CoreExport { kind, .. } => { |
| 295 |
self.names_for_core_export_alias(*kind).push(name); |
| 296 |
} |
| 297 |
AliasTarget::Outer { kind, .. } => { |
| 298 |
self.names_for_component_outer_alias(*kind).push(name); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
self.flush(Some(self.aliases.id())); |
| 303 |
} |
| 304 |
|
| 305 |
fn encode_start(&mut self, start: &Start) { |
| 306 |
// Flush any in-progress section before encoding the start section |
| 307 |
self.flush(None); |
| 308 |
|
| 309 |
self.component.section(&ComponentStartSection { |
| 310 |
function_index: start.func.into(), |
| 311 |
args: start.args.iter().map(|a| a.idx.into()).collect::<Vec<_>>(), |
| 312 |
results: start.results.len() as u32, |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
fn encode_type(&mut self, ty: &Type<'a>) { |
| 317 |
self.type_names.push(get_name(&ty.id, &ty.name)); |
| 318 |
encode_type(self.types.ty(), &ty.def); |
| 319 |
self.flush(Some(self.types.id())); |
| 320 |
} |
| 321 |
|
| 322 |
fn encode_canonical_func(&mut self, func: &CanonicalFunc<'a>) { |
| 323 |
let name = get_name(&func.id, &func.name); |
| 324 |
match &func.kind { |
| 325 |
CanonicalFuncKind::Lift { ty, info } => { |
| 326 |
self.func_names.push(name); |
| 327 |
self.funcs.lift( |
| 328 |
info.func.idx.into(), |
| 329 |
ty.into(), |
| 330 |
info.opts.iter().map(Into::into), |
| 331 |
); |
| 332 |
} |
| 333 |
CanonicalFuncKind::Lower(info) => { |
| 334 |
self.core_func_names.push(name); |
| 335 |
self.funcs |
| 336 |
.lower(info.func.idx.into(), info.opts.iter().map(Into::into)); |
| 337 |
} |
| 338 |
CanonicalFuncKind::ResourceNew(info) => { |
| 339 |
self.core_func_names.push(name); |
| 340 |
self.funcs.resource_new(info.ty.into()); |
| 341 |
} |
| 342 |
CanonicalFuncKind::ResourceDrop(info) => { |
| 343 |
self.core_func_names.push(name); |
| 344 |
self.funcs.resource_drop(info.ty.into()); |
| 345 |
} |
| 346 |
CanonicalFuncKind::ResourceRep(info) => { |
| 347 |
self.core_func_names.push(name); |
| 348 |
self.funcs.resource_rep(info.ty.into()); |
| 349 |
} |
| 350 |
CanonicalFuncKind::ThreadSpawn(info) => { |
| 351 |
self.core_func_names.push(name); |
| 352 |
self.funcs.thread_spawn(info.ty.into()); |
| 353 |
} |
| 354 |
CanonicalFuncKind::ThreadHwConcurrency(_info) => { |
| 355 |
self.core_func_names.push(name); |
| 356 |
self.funcs.thread_hw_concurrency(); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
self.flush(Some(self.funcs.id())); |
| 361 |
} |
| 362 |
|
| 363 |
fn encode_import(&mut self, import: &ComponentImport<'a>) { |
| 364 |
let name = get_name(&import.item.id, &import.item.name); |
| 365 |
self.names_for_item_kind(&import.item.kind).push(name); |
| 366 |
self.imports |
| 367 |
.import(import.name.0, (&import.item.kind).into()); |
| 368 |
self.flush(Some(self.imports.id())); |
| 369 |
} |
| 370 |
|
| 371 |
fn encode_export(&mut self, export: &ComponentExport<'a>) { |
| 372 |
let name = get_name(&export.id, &export.debug_name); |
| 373 |
let (kind, index) = (&export.kind).into(); |
| 374 |
self.exports.export( |
| 375 |
export.name.0, |
| 376 |
kind, |
| 377 |
index, |
| 378 |
export.ty.as_ref().map(|ty| (&ty.0.kind).into()), |
| 379 |
); |
| 380 |
match &export.kind { |
| 381 |
ComponentExportKind::CoreModule(_) => self.core_module_names.push(name), |
| 382 |
ComponentExportKind::Func(_) => self.func_names.push(name), |
| 383 |
ComponentExportKind::Instance(_) => self.instance_names.push(name), |
| 384 |
ComponentExportKind::Value(_) => self.value_names.push(name), |
| 385 |
ComponentExportKind::Component(_) => self.component_names.push(name), |
| 386 |
ComponentExportKind::Type(_) => self.type_names.push(name), |
| 387 |
} |
| 388 |
self.flush(Some(self.exports.id())); |
| 389 |
} |
| 390 |
|
| 391 |
fn flush(&mut self, section_id: Option<u8>) { |
| 392 |
if self.current_section_id == section_id { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
if let Some(id) = self.current_section_id { |
| 397 |
match id { |
| 398 |
// 0 => custom sections are written immediately |
| 399 |
// 1 => core modules sections are written immediately |
| 400 |
2 => { |
| 401 |
assert_eq!(id, self.core_instances.id()); |
| 402 |
self.component.section(&self.core_instances); |
| 403 |
self.core_instances = Default::default(); |
| 404 |
} |
| 405 |
3 => { |
| 406 |
assert_eq!(id, self.core_types.id()); |
| 407 |
self.component.section(&self.core_types); |
| 408 |
self.core_types = Default::default(); |
| 409 |
} |
| 410 |
// 4 => components sections are written immediately |
| 411 |
5 => { |
| 412 |
assert_eq!(id, self.instances.id()); |
| 413 |
self.component.section(&self.instances); |
| 414 |
self.instances = Default::default(); |
| 415 |
} |
| 416 |
6 => { |
| 417 |
assert_eq!(id, self.aliases.id()); |
| 418 |
self.component.section(&self.aliases); |
| 419 |
self.aliases = Default::default(); |
| 420 |
} |
| 421 |
7 => { |
| 422 |
assert_eq!(id, self.types.id()); |
| 423 |
self.component.section(&self.types); |
| 424 |
self.types = Default::default(); |
| 425 |
} |
| 426 |
8 => { |
| 427 |
assert_eq!(id, self.funcs.id()); |
| 428 |
self.component.section(&self.funcs); |
| 429 |
self.funcs = Default::default(); |
| 430 |
} |
| 431 |
// 9 => start sections are written immediately |
| 432 |
10 => { |
| 433 |
assert_eq!(id, self.imports.id()); |
| 434 |
self.component.section(&self.imports); |
| 435 |
self.imports = Default::default(); |
| 436 |
} |
| 437 |
11 => { |
| 438 |
assert_eq!(id, self.exports.id()); |
| 439 |
self.component.section(&self.exports); |
| 440 |
self.exports = Default::default(); |
| 441 |
} |
| 442 |
_ => unreachable!("unknown incremental component section id: {}", id), |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
self.current_section_id = section_id |
| 447 |
} |
| 448 |
|
| 449 |
fn encode_names( |
| 450 |
&mut self, |
| 451 |
component_id: &Option<Id<'_>>, |
| 452 |
component_name: &Option<NameAnnotation<'_>>, |
| 453 |
) { |
| 454 |
let mut names = ComponentNameSection::new(); |
| 455 |
if let Some(name) = get_name(component_id, component_name) { |
| 456 |
names.component(name); |
| 457 |
} |
| 458 |
|
| 459 |
let mut funcs = |list: &[Option<&str>], append: fn(&mut ComponentNameSection, &NameMap)| { |
| 460 |
let mut map = NameMap::new(); |
| 461 |
for (i, entry) in list.iter().enumerate() { |
| 462 |
if let Some(name) = entry { |
| 463 |
map.append(i as u32, name); |
| 464 |
} |
| 465 |
} |
| 466 |
if !map.is_empty() { |
| 467 |
append(&mut names, &map); |
| 468 |
} |
| 469 |
}; |
| 470 |
|
| 471 |
funcs(&self.core_func_names, ComponentNameSection::core_funcs); |
| 472 |
funcs(&self.core_table_names, ComponentNameSection::core_tables); |
| 473 |
funcs(&self.core_memory_names, ComponentNameSection::core_memories); |
| 474 |
funcs(&self.core_global_names, ComponentNameSection::core_globals); |
| 475 |
funcs(&self.core_type_names, ComponentNameSection::core_types); |
| 476 |
funcs(&self.core_module_names, ComponentNameSection::core_modules); |
| 477 |
funcs( |
| 478 |
&self.core_instance_names, |
| 479 |
ComponentNameSection::core_instances, |
| 480 |
); |
| 481 |
funcs(&self.func_names, ComponentNameSection::funcs); |
| 482 |
funcs(&self.value_names, ComponentNameSection::values); |
| 483 |
funcs(&self.type_names, ComponentNameSection::types); |
| 484 |
funcs(&self.component_names, ComponentNameSection::components); |
| 485 |
funcs(&self.instance_names, ComponentNameSection::instances); |
| 486 |
|
| 487 |
if !names.is_empty() { |
| 488 |
self.component.section(&names); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
fn names_for_component_export_alias( |
| 493 |
&mut self, |
| 494 |
kind: ComponentExportAliasKind, |
| 495 |
) -> &mut Vec<Option<&'a str>> { |
| 496 |
match kind { |
| 497 |
ComponentExportAliasKind::Func => &mut self.func_names, |
| 498 |
ComponentExportAliasKind::CoreModule => &mut self.core_module_names, |
| 499 |
ComponentExportAliasKind::Value => &mut self.value_names, |
| 500 |
ComponentExportAliasKind::Type => &mut self.type_names, |
| 501 |
ComponentExportAliasKind::Component => &mut self.component_names, |
| 502 |
ComponentExportAliasKind::Instance => &mut self.instance_names, |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
fn names_for_component_outer_alias( |
| 507 |
&mut self, |
| 508 |
kind: ComponentOuterAliasKind, |
| 509 |
) -> &mut Vec<Option<&'a str>> { |
| 510 |
match kind { |
| 511 |
ComponentOuterAliasKind::CoreModule => &mut self.core_module_names, |
| 512 |
ComponentOuterAliasKind::CoreType => &mut self.core_type_names, |
| 513 |
ComponentOuterAliasKind::Component => &mut self.component_names, |
| 514 |
ComponentOuterAliasKind::Type => &mut self.type_names, |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
fn names_for_core_export_alias(&mut self, kind: core::ExportKind) -> &mut Vec<Option<&'a str>> { |
| 519 |
match kind { |
| 520 |
core::ExportKind::Func => &mut self.core_func_names, |
| 521 |
core::ExportKind::Global => &mut self.core_global_names, |
| 522 |
core::ExportKind::Table => &mut self.core_table_names, |
| 523 |
core::ExportKind::Memory => &mut self.core_memory_names, |
| 524 |
core::ExportKind::Tag => unimplemented!(), |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
fn names_for_item_kind(&mut self, kind: &ItemSigKind) -> &mut Vec<Option<&'a str>> { |
| 529 |
match kind { |
| 530 |
ItemSigKind::CoreModule(_) => &mut self.core_module_names, |
| 531 |
ItemSigKind::Func(_) => &mut self.func_names, |
| 532 |
ItemSigKind::Component(_) => &mut self.component_names, |
| 533 |
ItemSigKind::Instance(_) => &mut self.instance_names, |
| 534 |
ItemSigKind::Value(_) => &mut self.value_names, |
| 535 |
ItemSigKind::Type(_) => &mut self.type_names, |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
fn get_name<'a>(id: &Option<Id<'a>>, name: &Option<NameAnnotation<'a>>) -> Option<&'a str> { |
| 541 |
name.as_ref().map(|n| n.name).or_else(|| { |
| 542 |
id.and_then(|id| { |
| 543 |
if id.is_gensym() { |
| 544 |
None |
| 545 |
} else { |
| 546 |
Some(id.name()) |
| 547 |
} |
| 548 |
}) |
| 549 |
}) |
| 550 |
} |
| 551 |
|
| 552 |
impl Custom<'_> { |
| 553 |
fn to_section(&self) -> wasm_encoder::CustomSection<'_> { |
| 554 |
let mut ret = Vec::new(); |
| 555 |
for list in self.data.iter() { |
| 556 |
ret.extend_from_slice(list); |
| 557 |
} |
| 558 |
wasm_encoder::CustomSection { |
| 559 |
name: self.name.into(), |
| 560 |
data: ret.into(), |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
impl From<&CoreInstantiationArgKind<'_>> for wasm_encoder::ModuleArg { |
| 566 |
fn from(kind: &CoreInstantiationArgKind) -> Self { |
| 567 |
match kind { |
| 568 |
CoreInstantiationArgKind::Instance(i) => { |
| 569 |
wasm_encoder::ModuleArg::Instance(i.idx.into()) |
| 570 |
} |
| 571 |
CoreInstantiationArgKind::BundleOfExports(..) => { |
| 572 |
unreachable!("should be expanded already") |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
impl From<&CoreItemRef<'_, core::ExportKind>> for (wasm_encoder::ExportKind, u32) { |
| 579 |
fn from(item: &CoreItemRef<'_, core::ExportKind>) -> Self { |
| 580 |
match &item.kind { |
| 581 |
core::ExportKind::Func => (wasm_encoder::ExportKind::Func, item.idx.into()), |
| 582 |
core::ExportKind::Table => (wasm_encoder::ExportKind::Table, item.idx.into()), |
| 583 |
core::ExportKind::Memory => (wasm_encoder::ExportKind::Memory, item.idx.into()), |
| 584 |
core::ExportKind::Global => (wasm_encoder::ExportKind::Global, item.idx.into()), |
| 585 |
core::ExportKind::Tag => (wasm_encoder::ExportKind::Tag, item.idx.into()), |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
impl<T> From<&ItemRef<'_, T>> for u32 { |
| 591 |
fn from(i: &ItemRef<'_, T>) -> Self { |
| 592 |
assert!(i.export_names.is_empty()); |
| 593 |
i.idx.into() |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
impl<T> From<&CoreTypeUse<'_, T>> for u32 { |
| 598 |
fn from(u: &CoreTypeUse<'_, T>) -> Self { |
| 599 |
match u { |
| 600 |
CoreTypeUse::Inline(_) => unreachable!("should be expanded already"), |
| 601 |
CoreTypeUse::Ref(r) => r.idx.into(), |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
impl<T> From<&ComponentTypeUse<'_, T>> for u32 { |
| 607 |
fn from(u: &ComponentTypeUse<'_, T>) -> Self { |
| 608 |
match u { |
| 609 |
ComponentTypeUse::Inline(_) => unreachable!("should be expanded already"), |
| 610 |
ComponentTypeUse::Ref(r) => r.idx.into(), |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
impl From<&ComponentValType<'_>> for wasm_encoder::ComponentValType { |
| 616 |
fn from(r: &ComponentValType) -> Self { |
| 617 |
match r { |
| 618 |
ComponentValType::Inline(ComponentDefinedType::Primitive(p)) => { |
| 619 |
Self::Primitive((*p).into()) |
| 620 |
} |
| 621 |
ComponentValType::Ref(i) => Self::Type(u32::from(*i)), |
| 622 |
ComponentValType::Inline(_) => unreachable!("should be expanded by now"), |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
impl From<PrimitiveValType> for wasm_encoder::PrimitiveValType { |
| 628 |
fn from(p: PrimitiveValType) -> Self { |
| 629 |
match p { |
| 630 |
PrimitiveValType::Bool => Self::Bool, |
| 631 |
PrimitiveValType::S8 => Self::S8, |
| 632 |
PrimitiveValType::U8 => Self::U8, |
| 633 |
PrimitiveValType::S16 => Self::S16, |
| 634 |
PrimitiveValType::U16 => Self::U16, |
| 635 |
PrimitiveValType::S32 => Self::S32, |
| 636 |
PrimitiveValType::U32 => Self::U32, |
| 637 |
PrimitiveValType::S64 => Self::S64, |
| 638 |
PrimitiveValType::U64 => Self::U64, |
| 639 |
PrimitiveValType::F32 => Self::F32, |
| 640 |
PrimitiveValType::F64 => Self::F64, |
| 641 |
PrimitiveValType::Char => Self::Char, |
| 642 |
PrimitiveValType::String => Self::String, |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
impl From<&Refinement<'_>> for u32 { |
| 648 |
fn from(r: &Refinement) -> Self { |
| 649 |
match r { |
| 650 |
Refinement::Index(..) => unreachable!("should be resolved by now"), |
| 651 |
Refinement::Resolved(i) => *i, |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
impl From<&ItemSigKind<'_>> for wasm_encoder::ComponentTypeRef { |
| 657 |
fn from(k: &ItemSigKind) -> Self { |
| 658 |
match k { |
| 659 |
ItemSigKind::Component(c) => Self::Component(c.into()), |
| 660 |
ItemSigKind::CoreModule(m) => Self::Module(m.into()), |
| 661 |
ItemSigKind::Instance(i) => Self::Instance(i.into()), |
| 662 |
ItemSigKind::Value(v) => Self::Value((&v.0).into()), |
| 663 |
ItemSigKind::Func(f) => Self::Func(f.into()), |
| 664 |
ItemSigKind::Type(TypeBounds::Eq(t)) => { |
| 665 |
Self::Type(wasm_encoder::TypeBounds::Eq((*t).into())) |
| 666 |
} |
| 667 |
ItemSigKind::Type(TypeBounds::SubResource) => { |
| 668 |
Self::Type(wasm_encoder::TypeBounds::SubResource) |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
impl From<&ComponentType<'_>> for wasm_encoder::ComponentType { |
| 675 |
fn from(ty: &ComponentType) -> Self { |
| 676 |
let mut encoded = wasm_encoder::ComponentType::new(); |
| 677 |
|
| 678 |
for decl in &ty.decls { |
| 679 |
match decl { |
| 680 |
ComponentTypeDecl::CoreType(t) => { |
| 681 |
encode_core_type(encoded.core_type(), &t.def); |
| 682 |
} |
| 683 |
ComponentTypeDecl::Type(t) => { |
| 684 |
encode_type(encoded.ty(), &t.def); |
| 685 |
} |
| 686 |
ComponentTypeDecl::Alias(a) => { |
| 687 |
encoded.alias((&a.target).into()); |
| 688 |
} |
| 689 |
ComponentTypeDecl::Import(i) => { |
| 690 |
encoded.import(i.name.0, (&i.item.kind).into()); |
| 691 |
} |
| 692 |
ComponentTypeDecl::Export(e) => { |
| 693 |
encoded.export(e.name.0, (&e.item.kind).into()); |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
encoded |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
impl From<&InstanceType<'_>> for wasm_encoder::InstanceType { |
| 703 |
fn from(ty: &InstanceType) -> Self { |
| 704 |
let mut encoded = wasm_encoder::InstanceType::new(); |
| 705 |
|
| 706 |
for decl in &ty.decls { |
| 707 |
match decl { |
| 708 |
InstanceTypeDecl::CoreType(t) => { |
| 709 |
encode_core_type(encoded.core_type(), &t.def); |
| 710 |
} |
| 711 |
InstanceTypeDecl::Type(t) => { |
| 712 |
encode_type(encoded.ty(), &t.def); |
| 713 |
} |
| 714 |
InstanceTypeDecl::Alias(a) => { |
| 715 |
encoded.alias((&a.target).into()); |
| 716 |
} |
| 717 |
InstanceTypeDecl::Export(e) => { |
| 718 |
encoded.export(e.name.0, (&e.item.kind).into()); |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
encoded |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
impl From<&ModuleType<'_>> for wasm_encoder::ModuleType { |
| 728 |
fn from(ty: &ModuleType) -> Self { |
| 729 |
let mut encoded = wasm_encoder::ModuleType::new(); |
| 730 |
|
| 731 |
for decl in &ty.decls { |
| 732 |
match decl { |
| 733 |
ModuleTypeDecl::Type(t) => { |
| 734 |
encoded.ty().subtype(&t.to_subtype()); |
| 735 |
} |
| 736 |
ModuleTypeDecl::Rec(rec) => { |
| 737 |
encoded.ty().rec(rec.types.iter().map(|t| t.to_subtype())); |
| 738 |
} |
| 739 |
ModuleTypeDecl::Alias(a) => match &a.target { |
| 740 |
AliasTarget::Outer { |
| 741 |
outer, |
| 742 |
index, |
| 743 |
kind: ComponentOuterAliasKind::CoreType, |
| 744 |
} => { |
| 745 |
encoded.alias_outer_core_type(u32::from(*outer), u32::from(*index)); |
| 746 |
} |
| 747 |
_ => unreachable!("only outer type aliases are supported"), |
| 748 |
}, |
| 749 |
ModuleTypeDecl::Import(i) => { |
| 750 |
encoded.import(i.module, i.field, i.item.to_entity_type()); |
| 751 |
} |
| 752 |
ModuleTypeDecl::Export(name, item) => { |
| 753 |
encoded.export(name, item.to_entity_type()); |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
encoded |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
impl From<&InstantiationArgKind<'_>> for (wasm_encoder::ComponentExportKind, u32) { |
| 763 |
fn from(kind: &InstantiationArgKind) -> Self { |
| 764 |
match kind { |
| 765 |
InstantiationArgKind::Item(i) => i.into(), |
| 766 |
InstantiationArgKind::BundleOfExports(..) => unreachable!("should be expanded already"), |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
impl From<&ComponentExportKind<'_>> for (wasm_encoder::ComponentExportKind, u32) { |
| 772 |
fn from(kind: &ComponentExportKind) -> Self { |
| 773 |
match kind { |
| 774 |
ComponentExportKind::CoreModule(m) => { |
| 775 |
(wasm_encoder::ComponentExportKind::Module, m.idx.into()) |
| 776 |
} |
| 777 |
ComponentExportKind::Func(f) => (wasm_encoder::ComponentExportKind::Func, f.idx.into()), |
| 778 |
ComponentExportKind::Value(v) => { |
| 779 |
(wasm_encoder::ComponentExportKind::Value, v.idx.into()) |
| 780 |
} |
| 781 |
ComponentExportKind::Type(t) => (wasm_encoder::ComponentExportKind::Type, t.idx.into()), |
| 782 |
ComponentExportKind::Component(c) => { |
| 783 |
(wasm_encoder::ComponentExportKind::Component, c.idx.into()) |
| 784 |
} |
| 785 |
ComponentExportKind::Instance(i) => { |
| 786 |
(wasm_encoder::ComponentExportKind::Instance, i.idx.into()) |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
impl From<ComponentOuterAliasKind> for wasm_encoder::ComponentOuterAliasKind { |
| 793 |
fn from(kind: ComponentOuterAliasKind) -> Self { |
| 794 |
match kind { |
| 795 |
ComponentOuterAliasKind::CoreModule => Self::CoreModule, |
| 796 |
ComponentOuterAliasKind::CoreType => Self::CoreType, |
| 797 |
ComponentOuterAliasKind::Type => Self::Type, |
| 798 |
ComponentOuterAliasKind::Component => Self::Component, |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
impl From<ComponentExportAliasKind> for wasm_encoder::ComponentExportKind { |
| 804 |
fn from(kind: ComponentExportAliasKind) -> Self { |
| 805 |
match kind { |
| 806 |
ComponentExportAliasKind::CoreModule => Self::Module, |
| 807 |
ComponentExportAliasKind::Func => Self::Func, |
| 808 |
ComponentExportAliasKind::Value => Self::Value, |
| 809 |
ComponentExportAliasKind::Type => Self::Type, |
| 810 |
ComponentExportAliasKind::Component => Self::Component, |
| 811 |
ComponentExportAliasKind::Instance => Self::Instance, |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
impl From<&CanonOpt<'_>> for wasm_encoder::CanonicalOption { |
| 817 |
fn from(opt: &CanonOpt) -> Self { |
| 818 |
match opt { |
| 819 |
CanonOpt::StringUtf8 => Self::UTF8, |
| 820 |
CanonOpt::StringUtf16 => Self::UTF16, |
| 821 |
CanonOpt::StringLatin1Utf16 => Self::CompactUTF16, |
| 822 |
CanonOpt::Memory(m) => Self::Memory(m.idx.into()), |
| 823 |
CanonOpt::Realloc(f) => Self::Realloc(f.idx.into()), |
| 824 |
CanonOpt::PostReturn(f) => Self::PostReturn(f.idx.into()), |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
impl<'a> From<&AliasTarget<'a>> for wasm_encoder::Alias<'a> { |
| 830 |
fn from(target: &AliasTarget<'a>) -> Self { |
| 831 |
match target { |
| 832 |
AliasTarget::Export { |
| 833 |
instance, |
| 834 |
name, |
| 835 |
kind, |
| 836 |
} => wasm_encoder::Alias::InstanceExport { |
| 837 |
instance: (*instance).into(), |
| 838 |
kind: (*kind).into(), |
| 839 |
name, |
| 840 |
}, |
| 841 |
AliasTarget::CoreExport { |
| 842 |
instance, |
| 843 |
name, |
| 844 |
kind, |
| 845 |
} => wasm_encoder::Alias::CoreInstanceExport { |
| 846 |
instance: (*instance).into(), |
| 847 |
kind: (*kind).into(), |
| 848 |
name, |
| 849 |
}, |
| 850 |
AliasTarget::Outer { outer, index, kind } => wasm_encoder::Alias::Outer { |
| 851 |
count: (*outer).into(), |
| 852 |
kind: (*kind).into(), |
| 853 |
index: (*index).into(), |
| 854 |
}, |
| 855 |
} |
| 856 |
} |
| 857 |
} |