forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.rs
More file actions
4765 lines (4341 loc) · 176 KB
/
component.rs
File metadata and controls
4765 lines (4341 loc) · 176 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! State relating to validating a WebAssembly component.
use super::{
check_max,
component_types::{
Abi, AliasableResourceId, ComponentAnyTypeId, ComponentCoreInstanceTypeId,
ComponentCoreModuleTypeId, ComponentCoreTypeId, ComponentDefinedType,
ComponentDefinedTypeId, ComponentEntityType, ComponentFuncType, ComponentFuncTypeId,
ComponentInstanceType, ComponentInstanceTypeId, ComponentType, ComponentTypeId,
ComponentValType, Context, CoreInstanceTypeKind, InstanceType, ModuleType, RecordType,
Remap, Remapping, ResourceId, SubtypeCx, TupleType, VariantCase, VariantType,
},
core::{InternRecGroup, Module},
types::{CoreTypeId, EntityType, TypeAlloc, TypeInfo, TypeList},
};
use crate::collections::index_map::Entry;
use crate::limits::*;
use crate::prelude::*;
use crate::validator::names::{ComponentName, ComponentNameKind, KebabStr, KebabString};
use crate::{
BinaryReaderError, CanonicalFunction, CanonicalOption, ComponentExportName,
ComponentExternalKind, ComponentOuterAliasKind, ComponentTypeRef, CompositeInnerType,
ExternalKind, FuncType, GlobalType, InstantiationArgKind, MemoryType, PackedIndex, RefType,
Result, SubType, TableType, TypeBounds, ValType, WasmFeatures,
};
use core::mem;
fn to_kebab_str<'a>(s: &'a str, desc: &str, offset: usize) -> Result<&'a KebabStr> {
match KebabStr::new(s) {
Some(s) => Ok(s),
None => {
if s.is_empty() {
bail!(offset, "{desc} name cannot be empty");
}
bail!(offset, "{desc} name `{s}` is not in kebab case");
}
}
}
pub(crate) struct ComponentState {
/// Whether this state is a concrete component, an instance type, or a
/// component type.
kind: ComponentKind,
features: WasmFeatures,
// Core index spaces
pub core_types: Vec<ComponentCoreTypeId>,
pub core_funcs: Vec<CoreTypeId>,
pub core_tags: Vec<CoreTypeId>,
pub core_modules: Vec<ComponentCoreModuleTypeId>,
pub core_instances: Vec<ComponentCoreInstanceTypeId>,
pub core_memories: Vec<MemoryType>,
pub core_tables: Vec<TableType>,
pub core_globals: Vec<GlobalType>,
// Component index spaces
pub types: Vec<ComponentAnyTypeId>,
pub funcs: Vec<ComponentFuncTypeId>,
pub values: Vec<(ComponentValType, bool)>,
pub instances: Vec<ComponentInstanceTypeId>,
pub components: Vec<ComponentTypeId>,
pub imports: IndexMap<String, ComponentEntityType>,
pub import_names: IndexSet<ComponentName>,
pub exports: IndexMap<String, ComponentEntityType>,
pub export_names: IndexSet<ComponentName>,
has_start: bool,
type_info: TypeInfo,
/// A mapping of imported resources in this component.
///
/// This mapping represents all "type variables" imported into the
/// component, or resources. This could be resources imported directly as
/// a top-level type import or additionally transitively through other
/// imported instances.
///
/// The mapping element here is a "path" which is a list of indexes into
/// the import map that will be generated for this component. Each index
/// is an index into an `IndexMap`, and each list is guaranteed to have at
/// least one element.
///
/// An example of this map is:
///
/// ```wasm
/// (component
/// ;; [0] - the first import
/// (import "r" (type (sub resource)))
///
/// ;; [1] - the second import
/// (import "r2" (type (sub resource)))
///
/// (import "i" (instance
/// ;; [2, 0] - the third import, and the first export the instance
/// (export "r3" (type (sub resource)))
/// ;; [2, 1] - the third import, and the second export the instance
/// (export "r4" (type (sub resource)))
/// ))
///
/// ;; ...
/// )
/// ```
///
/// The `Vec<usize>` here can be thought of as `Vec<String>` but a
/// (hopefully) more efficient representation.
///
/// Finally note that this map is listed as an "append only" map because all
/// insertions into it should always succeed. Any insertion which overlaps
/// with a previous entry indicates a bug in the validator which needs to be
/// corrected via other means.
//
// TODO: make these `SkolemResourceId` and then go fix all the compile
// errors, don't add skolem things into the type area
imported_resources: IndexMapAppendOnly<ResourceId, Vec<usize>>,
/// A mapping of "defined" resources in this component, or those which
/// are defined within the instantiation of this component.
///
/// Defined resources, as the name implies, can sort of be thought of as
/// "these are defined within the component". Note though that the means by
/// which a local definition can occur are not simply those defined in the
/// component but also in its transitively instantiated components
/// internally. This means that this set closes over many transitive
/// internal items in addition to those defined immediately in the component
/// itself.
///
/// The `Option<ValType>` in this mapping is whether or not the underlying
/// representation of the resource is known to this component. Immediately
/// defined resources, for example, will have `Some(I32)` here. Resources
/// that come from transitively defined components, for example, will have
/// `None`. In the type context all entries here are `None`.
///
/// Note that like `imported_resources` all insertions into this map are
/// expected to succeed to it's declared as append-only.
defined_resources: IndexMapAppendOnly<ResourceId, Option<ValType>>,
/// A mapping of explicitly exported resources from this component in
/// addition to the path that they're exported at.
///
/// For more information on the path here see the documentation for
/// `imported_resources`. Note that the indexes here index into the
/// list of exports of this component.
explicit_resources: IndexMap<ResourceId, Vec<usize>>,
/// The set of types which are considered "exported" from this component.
///
/// This is added to whenever a type export is found, or an instance export
/// which itself contains a type export. This additionally includes all
/// imported types since those are suitable for export as well.
///
/// This set is consulted whenever an exported item is added since all
/// referenced types must be members of this set.
exported_types: Set<ComponentAnyTypeId>,
/// Same as `exported_types`, but for imports.
imported_types: Set<ComponentAnyTypeId>,
/// The set of top-level resource exports and their names.
///
/// This context is used to validate method names such as `[method]foo.bar`
/// to ensure that `foo` is an exported resource and that the type mentioned
/// in a function type is actually named `foo`.
///
/// Note that imports/exports have disjoint contexts to ensure that they're
/// validated correctly. Namely you can't retroactively attach methods to an
/// import, for example.
toplevel_exported_resources: ComponentNameContext,
/// Same as `toplevel_exported_resources`, but for imports.
toplevel_imported_resources: ComponentNameContext,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ComponentKind {
Component,
InstanceType,
ComponentType,
}
/// Helper context used to track information about resource names for method
/// name validation.
#[derive(Default)]
struct ComponentNameContext {
/// A map from a resource type id to an index in the `all_resource_names`
/// set for the name of that resource.
resource_name_map: Map<AliasableResourceId, usize>,
/// All known resource names in this context, used to validate static method
/// names to by ensuring that static methods' resource names are somewhere
/// in this set.
all_resource_names: IndexSet<String>,
}
#[derive(Debug, Copy, Clone)]
pub enum ExternKind {
Import,
Export,
}
impl ExternKind {
pub fn desc(&self) -> &'static str {
match self {
ExternKind::Import => "import",
ExternKind::Export => "export",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum StringEncoding {
#[default]
Utf8,
Utf16,
CompactUtf16,
}
impl core::fmt::Display for StringEncoding {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::Utf8 => "utf8",
Self::Utf16 => "utf16",
Self::CompactUtf16 => "latin1-utf16",
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum Concurrency {
/// Synchronous.
#[default]
Sync,
/// Asynchronous.
Async {
/// When present, this is the function index of the async callback. When
/// omitted, we are either using stack-switching based asynchrony or are
/// in an operation that does not support the `callback` option (like
/// lowering).
callback: Option<u32>,
},
}
impl Concurrency {
pub(crate) fn is_sync(&self) -> bool {
matches!(self, Self::Sync)
}
pub(crate) fn is_async(&self) -> bool {
!self.is_sync()
}
}
#[derive(Clone, Copy)]
pub(crate) struct CanonicalOptions {
pub(crate) string_encoding: StringEncoding,
pub(crate) memory: Option<u32>,
pub(crate) realloc: Option<u32>,
pub(crate) post_return: Option<u32>,
pub(crate) concurrency: Concurrency,
pub(crate) core_type: Option<CoreTypeId>,
pub(crate) gc: bool,
}
impl CanonicalOptions {
pub(crate) fn require_sync(&self, offset: usize, where_: &str) -> Result<&Self> {
if !self.concurrency.is_sync() {
bail!(offset, "cannot specify `async` option on `{where_}`")
}
Ok(self)
}
pub(crate) fn require_memory(&self, offset: usize) -> Result<&Self> {
if self.memory.is_none() {
bail!(offset, "canonical option `memory` is required");
}
Ok(self)
}
pub(crate) fn require_realloc(&self, offset: usize) -> Result<&Self> {
// Memory is always required when `realloc` is required.
self.require_memory(offset)?;
if self.realloc.is_none() {
bail!(offset, "canonical option `realloc` is required")
}
Ok(self)
}
pub(crate) fn require_memory_if(
&self,
offset: usize,
when: impl Fn() -> bool,
) -> Result<&Self> {
if self.memory.is_none() && when() {
self.require_memory(offset)?;
}
Ok(self)
}
pub(crate) fn require_realloc_if(
&self,
offset: usize,
when: impl Fn() -> bool,
) -> Result<&Self> {
if self.realloc.is_none() && when() {
self.require_realloc(offset)?;
}
Ok(self)
}
pub(crate) fn check_lower(&self, offset: usize) -> Result<&Self> {
if self.post_return.is_some() {
bail!(
offset,
"canonical option `post-return` cannot be specified for lowerings"
);
}
if let Concurrency::Async { callback: Some(_) } = self.concurrency {
bail!(
offset,
"canonical option `callback` cannot be specified for lowerings"
);
}
if self.gc && self.core_type.is_none() {
bail!(
offset,
"cannot specify `gc` without also specifying a `core-type` for lowerings"
)
}
Ok(self)
}
pub(crate) fn check_lift(
&mut self,
types: &TypeList,
state: &ComponentState,
core_ty_id: CoreTypeId,
offset: usize,
) -> Result<&Self> {
debug_assert!(matches!(
types[core_ty_id].composite_type.inner,
CompositeInnerType::Func(_)
));
if let Some(idx) = self.post_return {
let post_return_func_ty = types[state.core_function_at(idx, offset)?].unwrap_func();
let core_ty = types[core_ty_id].unwrap_func();
if post_return_func_ty.params() != core_ty.results()
|| !post_return_func_ty.results().is_empty()
{
bail!(
offset,
"canonical option `post-return` uses a core function with an incorrect signature"
);
}
}
match self.concurrency {
Concurrency::Sync => {}
Concurrency::Async { callback: None } if !state.features.cm_async_stackful() => {
bail!(
offset,
"requires the component model async stackful feature"
)
}
Concurrency::Async { callback: None } => {}
Concurrency::Async {
callback: Some(idx),
} => {
let func_ty = types[state.core_function_at(idx, offset)?].unwrap_func();
if func_ty.params() != [ValType::I32; 3] && func_ty.params() != [ValType::I32] {
return Err(BinaryReaderError::new(
"canonical option `callback` uses a core function with an incorrect signature",
offset,
));
}
}
}
if self.core_type.is_some() {
bail!(
offset,
"canonical option `core-type` is not allowed in `canon lift`"
)
}
self.core_type = Some(core_ty_id);
Ok(self)
}
pub(crate) fn check_core_type(
&self,
types: &mut TypeAlloc,
actual: FuncType,
offset: usize,
) -> Result<CoreTypeId> {
if let Some(declared_id) = self.core_type {
let declared = types[declared_id].unwrap_func();
if actual.params() != declared.params() {
bail!(
offset,
"declared core type has `{:?}` parameter types, but actual lowering has \
`{:?}` parameter types",
declared.params(),
actual.params(),
);
}
if actual.results() != declared.results() {
bail!(
offset,
"declared core type has `{:?}` result types, but actual lowering has \
`{:?}` result types",
declared.results(),
actual.results(),
);
}
Ok(declared_id)
} else {
Ok(types.intern_func_type(actual, offset))
}
}
}
impl ComponentState {
pub fn new(kind: ComponentKind, features: WasmFeatures) -> Self {
Self {
kind,
features,
core_types: Default::default(),
core_modules: Default::default(),
core_instances: Default::default(),
core_funcs: Default::default(),
core_memories: Default::default(),
core_tables: Default::default(),
core_globals: Default::default(),
core_tags: Default::default(),
types: Default::default(),
funcs: Default::default(),
values: Default::default(),
instances: Default::default(),
components: Default::default(),
imports: Default::default(),
exports: Default::default(),
import_names: Default::default(),
export_names: Default::default(),
has_start: Default::default(),
type_info: TypeInfo::new(),
imported_resources: Default::default(),
defined_resources: Default::default(),
explicit_resources: Default::default(),
exported_types: Default::default(),
imported_types: Default::default(),
toplevel_exported_resources: Default::default(),
toplevel_imported_resources: Default::default(),
}
}
pub fn type_count(&self) -> usize {
self.core_types.len() + self.types.len()
}
pub fn instance_count(&self) -> usize {
self.core_instances.len() + self.instances.len()
}
pub fn function_count(&self) -> usize {
self.core_funcs.len() + self.funcs.len()
}
pub fn add_core_type(
components: &mut [Self],
ty: crate::CoreType,
types: &mut TypeAlloc,
offset: usize,
check_limit: bool,
) -> Result<()> {
let current = components.last_mut().unwrap();
if check_limit {
check_max(current.type_count(), 1, MAX_WASM_TYPES, "types", offset)?;
}
match ty {
crate::CoreType::Rec(rec) => {
current.canonicalize_and_intern_rec_group(types, rec, offset)?;
}
crate::CoreType::Module(decls) => {
let mod_ty = Self::create_module_type(components, decls.into_vec(), types, offset)?;
let id = ComponentCoreTypeId::Module(types.push_ty(mod_ty));
components.last_mut().unwrap().core_types.push(id);
}
}
Ok(())
}
pub fn add_core_module(
&mut self,
module: &Module,
types: &mut TypeAlloc,
offset: usize,
) -> Result<()> {
let imports = module.imports_for_module_type(offset)?;
// We have to clone the module's imports and exports here
// because we cannot take the data out of the `MaybeOwned`
// as it might be shared with a function validator.
let mod_ty = ModuleType {
info: TypeInfo::core(module.type_size),
imports,
exports: module.exports.clone(),
};
let mod_id = types.push_ty(mod_ty);
self.core_modules.push(mod_id);
Ok(())
}
pub fn add_core_instance(
&mut self,
instance: crate::Instance,
types: &mut TypeAlloc,
offset: usize,
) -> Result<()> {
let instance = match instance {
crate::Instance::Instantiate { module_index, args } => {
self.instantiate_core_module(module_index, args.into_vec(), types, offset)?
}
crate::Instance::FromExports(exports) => {
self.instantiate_core_exports(exports.into_vec(), types, offset)?
}
};
self.core_instances.push(instance);
Ok(())
}
pub fn add_type(
components: &mut Vec<Self>,
ty: crate::ComponentType,
types: &mut TypeAlloc,
offset: usize,
check_limit: bool,
) -> Result<()> {
assert!(!components.is_empty());
fn current(components: &mut Vec<ComponentState>) -> &mut ComponentState {
components.last_mut().unwrap()
}
let id = match ty {
crate::ComponentType::Defined(ty) => {
let ty = current(components).create_defined_type(ty, types, offset)?;
types.push(ty).into()
}
crate::ComponentType::Func(ty) => {
let ty = current(components).create_function_type(ty, types, offset)?;
types.push(ty).into()
}
crate::ComponentType::Component(decls) => {
let ty = Self::create_component_type(components, decls.into_vec(), types, offset)?;
types.push(ty).into()
}
crate::ComponentType::Instance(decls) => {
let ty = Self::create_instance_type(components, decls.into_vec(), types, offset)?;
types.push(ty).into()
}
crate::ComponentType::Resource { rep, dtor } => {
let component = current(components);
// Resource types cannot be declared in a type context, only
// within a component context.
if component.kind != ComponentKind::Component {
bail!(
offset,
"resources can only be defined within a concrete component"
);
}
// Current MVP restriction of the component model.
if rep != ValType::I32 {
bail!(offset, "resources can only be represented by `i32`");
}
// If specified validate that the destructor is both a valid
// function and has the correct signature.
if let Some(dtor) = dtor {
let ty = component.core_function_at(dtor, offset)?;
let ty = types[ty].composite_type.unwrap_func();
if ty.params() != [rep] || ty.results() != [] {
bail!(
offset,
"core function {dtor} has wrong signature for a destructor"
);
}
}
// As this is the introduction of a resource create a fresh new
// identifier for the resource. This is then added into the
// list of defined resources for this component, notably with a
// rep listed to enable getting access to various intrinsics
// such as `resource.rep`.
let id = types.alloc_resource_id();
component.defined_resources.insert(id.resource(), Some(rep));
id.into()
}
};
let current = current(components);
if check_limit {
check_max(current.type_count(), 1, MAX_WASM_TYPES, "types", offset)?;
}
current.types.push(id);
Ok(())
}
pub fn add_import(
&mut self,
import: crate::ComponentImport,
types: &mut TypeAlloc,
offset: usize,
) -> Result<()> {
let mut entity = self.check_type_ref(&import.ty, types, offset)?;
self.add_entity(
&mut entity,
Some((import.name.0, ExternKind::Import)),
types,
offset,
)?;
self.toplevel_imported_resources.validate_extern(
import.name.0,
ExternKind::Import,
&entity,
types,
offset,
&mut self.import_names,
&mut self.imports,
&mut self.type_info,
&self.features,
)?;
Ok(())
}
fn add_entity(
&mut self,
ty: &mut ComponentEntityType,
name_and_kind: Option<(&str, ExternKind)>,
types: &mut TypeAlloc,
offset: usize,
) -> Result<()> {
let kind = name_and_kind.map(|(_, k)| k);
let (len, max, desc) = match ty {
ComponentEntityType::Module(id) => {
self.core_modules.push(*id);
(self.core_modules.len(), MAX_WASM_MODULES, "modules")
}
ComponentEntityType::Component(id) => {
self.components.push(*id);
(self.components.len(), MAX_WASM_COMPONENTS, "components")
}
ComponentEntityType::Instance(id) => {
match kind {
Some(ExternKind::Import) => self.prepare_instance_import(id, types),
Some(ExternKind::Export) => self.prepare_instance_export(id, types),
None => {}
}
self.instances.push(*id);
(self.instance_count(), MAX_WASM_INSTANCES, "instances")
}
ComponentEntityType::Func(id) => {
self.funcs.push(*id);
(self.function_count(), MAX_WASM_FUNCTIONS, "functions")
}
ComponentEntityType::Value(ty) => {
self.check_value_support(offset)?;
let value_used = match kind {
Some(ExternKind::Import) | None => false,
Some(ExternKind::Export) => true,
};
self.values.push((*ty, value_used));
(self.values.len(), MAX_WASM_VALUES, "values")
}
ComponentEntityType::Type {
created,
referenced,
} => {
self.types.push(*created);
// Extra logic here for resources being imported and exported.
// Note that if `created` is the same as `referenced` then this
// is the original introduction of the resource which is where
// `self.{imported,defined}_resources` are updated.
if let ComponentAnyTypeId::Resource(id) = *created {
match kind {
Some(ExternKind::Import) => {
// A fresh new resource is being imported into a
// component. This arises from the import section of
// a component or from the import declaration in a
// component type. In both cases a new imported
// resource is injected with a fresh new identifier
// into our state.
if created == referenced {
self.imported_resources
.insert(id.resource(), vec![self.imports.len()]);
}
}
Some(ExternKind::Export) => {
// A fresh resource is being exported from this
// component. This arises as part of the
// declaration of a component type, for example. In
// this situation brand new resource identifier is
// allocated and a definition is added, unlike the
// import case where an imported resource is added.
// Notably the representation of this new resource
// is unknown so it's listed as `None`.
if created == referenced {
self.defined_resources.insert(id.resource(), None);
}
// If this is a type export of a resource type then
// update the `explicit_resources` list. A new
// export path is about to be created for this
// resource and this keeps track of that.
self.explicit_resources
.insert(id.resource(), vec![self.exports.len()]);
}
None => {}
}
}
(self.types.len(), MAX_WASM_TYPES, "types")
}
};
check_max(len, 0, max, desc, offset)?;
// Before returning perform the final validation of the type of the item
// being imported/exported. This will ensure that everything is
// appropriately named with respect to type definitions, resources, etc.
if let Some((name, kind)) = name_and_kind {
if !self.validate_and_register_named_types(Some(name), kind, ty, types) {
bail!(
offset,
"{} not valid to be used as {}",
ty.desc(),
kind.desc()
);
}
}
Ok(())
}
/// Validates that the `ty` referenced only refers to named types internally
/// and then inserts anything necessary, if applicable, to the defined sets
/// within this component.
///
/// This function will validate that `ty` only refers to named types. For
/// example if it's a record then all of its fields must refer to named
/// types. This consults either `self.imported_types` or
/// `self.exported_types` as specified by `kind`. Note that this is not
/// inherently recursive itself but it ends up being recursive since if
/// recursive members were named then all their components must also be
/// named. Consequently this check stops at the "one layer deep" position,
/// or more accurately the position where types must be named (e.g. tuples
/// aren't required to be named).
fn validate_and_register_named_types(
&mut self,
toplevel_name: Option<&str>,
kind: ExternKind,
ty: &ComponentEntityType,
types: &TypeAlloc,
) -> bool {
if let ComponentEntityType::Type { created, .. } = ty {
// If this is a top-level resource then register it in the
// appropriate context so later validation of method-like-names
// works out.
if let Some(name) = toplevel_name {
if let ComponentAnyTypeId::Resource(id) = *created {
let cx = match kind {
ExternKind::Import => &mut self.toplevel_imported_resources,
ExternKind::Export => &mut self.toplevel_exported_resources,
};
cx.register(name, id);
}
}
}
match self.kind {
ComponentKind::Component | ComponentKind::ComponentType => {}
ComponentKind::InstanceType => return true,
}
let set = match kind {
ExternKind::Import => &self.imported_types,
ExternKind::Export => &self.exported_types,
};
match ty {
// When a type is imported or exported than any recursive type
// referred to by that import/export must additionally be exported
// or imported. Here this walks the "first layer" of the type which
// delegates to `TypeAlloc::type_named_type_id` to determine whether
// the components of the type being named here are indeed all they
// themselves named.
ComponentEntityType::Type {
created,
referenced,
} => {
if !self.all_valtypes_named(types, *referenced, set) {
return false;
}
match kind {
// Imported types are both valid for import and valid for
// export.
ExternKind::Import => {
self.imported_types.insert(*created);
self.exported_types.insert(*created);
}
ExternKind::Export => {
self.exported_types.insert(*created);
}
}
true
}
// Instances are slightly nuanced here. The general idea is that if
// an instance is imported, then any type exported by the instance
// is then also exported. Additionally for exports. To get this to
// work out this arm will recursively call
// `validate_and_register_named_types` which means that types are
// inserted into `self.{imported,exported}_types` as-we-go rather
// than all at once.
//
// This then recursively validates that all items in the instance
// itself are valid to import/export, recursive instances are
// captured, and everything is appropriately added to the right
// imported/exported set.
ComponentEntityType::Instance(i) => types[*i]
.exports
.iter()
.all(|(_name, ty)| self.validate_and_register_named_types(None, kind, ty, types)),
// All types referred to by a function must be named.
ComponentEntityType::Func(id) => self.all_valtypes_named_in_func(types, *id, set),
ComponentEntityType::Value(ty) => types.type_named_valtype(ty, set),
// Components/modules are always "closed" or "standalone" and don't
// need validation with respect to their named types.
ComponentEntityType::Component(_) | ComponentEntityType::Module(_) => true,
}
}
fn all_valtypes_named(
&self,
types: &TypeAlloc,
id: ComponentAnyTypeId,
set: &Set<ComponentAnyTypeId>,
) -> bool {
match id {
// Resource types, in isolation, are always valid to import or
// export since they're either attached to an import or being
// exported.
//
// Note that further validation of this happens in `finish`, too.
ComponentAnyTypeId::Resource(_) => true,
// Component types are validated as they are constructed,
// so all component types are valid to export if they've
// already been constructed.
ComponentAnyTypeId::Component(_) => true,
ComponentAnyTypeId::Defined(id) => self.all_valtypes_named_in_defined(types, id, set),
ComponentAnyTypeId::Func(id) => self.all_valtypes_named_in_func(types, id, set),
ComponentAnyTypeId::Instance(id) => self.all_valtypes_named_in_instance(types, id, set),
}
}
fn all_valtypes_named_in_instance(
&self,
types: &TypeAlloc,
id: ComponentInstanceTypeId,
set: &Set<ComponentAnyTypeId>,
) -> bool {
// Instances must recursively have all referenced types named.
let ty = &types[id];
ty.exports.values().all(|ty| match ty {
ComponentEntityType::Module(_) => true,
ComponentEntityType::Func(id) => self.all_valtypes_named_in_func(types, *id, set),
ComponentEntityType::Type { created: id, .. } => {
self.all_valtypes_named(types, *id, set)
}
ComponentEntityType::Value(ComponentValType::Type(id)) => {
self.all_valtypes_named_in_defined(types, *id, set)
}
ComponentEntityType::Instance(id) => {
self.all_valtypes_named_in_instance(types, *id, set)
}
ComponentEntityType::Component(_)
| ComponentEntityType::Value(ComponentValType::Primitive(_)) => return true,
})
}
fn all_valtypes_named_in_defined(
&self,
types: &TypeAlloc,
id: ComponentDefinedTypeId,
set: &Set<ComponentAnyTypeId>,
) -> bool {
let ty = &types[id];
match ty {
// These types do not contain anything which must be
// named.
ComponentDefinedType::Primitive(_)
| ComponentDefinedType::Flags(_)
| ComponentDefinedType::Enum(_) => true,
// Referenced types of all these aggregates must all be
// named.
ComponentDefinedType::Record(r) => {
r.fields.values().all(|t| types.type_named_valtype(t, set))
}
ComponentDefinedType::Tuple(r) => {
r.types.iter().all(|t| types.type_named_valtype(t, set))
}
ComponentDefinedType::Variant(r) => r
.cases
.values()
.filter_map(|t| t.ty.as_ref())
.all(|t| types.type_named_valtype(t, set)),
ComponentDefinedType::Result { ok, err } => {
ok.as_ref()
.map(|t| types.type_named_valtype(t, set))
.unwrap_or(true)
&& err
.as_ref()
.map(|t| types.type_named_valtype(t, set))
.unwrap_or(true)
}
ComponentDefinedType::List(ty)
| ComponentDefinedType::FixedSizeList(ty, _)
| ComponentDefinedType::Option(ty) => types.type_named_valtype(ty, set),
ComponentDefinedType::Map(k, v) => {
types.type_named_valtype(k, set) && types.type_named_valtype(v, set)
}
// The resource referred to by own/borrow must be named.
ComponentDefinedType::Own(id) | ComponentDefinedType::Borrow(id) => {
set.contains(&ComponentAnyTypeId::from(*id))
}
ComponentDefinedType::Future(ty) => ty
.as_ref()
.map(|ty| types.type_named_valtype(ty, set))
.unwrap_or(true),
ComponentDefinedType::Stream(ty) => ty
.as_ref()
.map(|ty| types.type_named_valtype(ty, set))
.unwrap_or(true),
}
}
fn all_valtypes_named_in_func(
&self,
types: &TypeAlloc,
id: ComponentFuncTypeId,
set: &Set<ComponentAnyTypeId>,
) -> bool {
let ty = &types[id];
// Function types must have all their parameters/results named.
ty.params
.iter()
.map(|(_, ty)| ty)
.chain(&ty.result)
.all(|ty| types.type_named_valtype(ty, set))
}
/// Updates the type `id` specified, an identifier for a component instance
/// type, to be imported into this component.
///
/// Importing an instance type into a component specially handles the
/// defined resources registered in the instance type. Notably all
/// defined resources are "freshened" into brand new type variables and
/// these new variables are substituted within the type. This is what
/// creates a new `TypeId` and may update the `id` specified.
///
/// One side effect of this operation, for example, is that if an instance
/// type is used twice to import two different instances then the instances
/// do not share resource types despite sharing the same original instance