MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / populateInstanceFromType

Function populateInstanceFromType

lib/validator/component_validator.cpp:72–120  ·  view source on GitHub ↗

Shallow populate: copy an InstanceType's exportdecls into the instance at InstIdx. InstanceType exportdecls get `IT` resolved in the current scope so alias-export can chase shapes through un-ascribed chains. Precondition: `IT` must be owned by the current ComponentContext scope — nested TypeIndex lookups resolve against that scope.

Source from the content-addressed store, hash-verified

70// Precondition: `IT` must be owned by the current ComponentContext scope —
71// nested TypeIndex lookups resolve against that scope.
72void populateInstanceFromType(ComponentContext &Ctx, uint32_t InstIdx,
73 const AST::Component::InstanceType &IT) {
74 for (const auto &Decl : IT.getDecl()) {
75 if (!Decl.isExportDecl()) {
76 continue;
77 }
78 const auto &Exp = Decl.getExport();
79 const auto &ED = Exp.getExternDesc();
80 auto ST = descTypeToSortType(ED.getDescType());
81 if (!ST.has_value()) {
82 // The InstanceExport::ST field is component-side only and has no
83 // variant for `(core module)`. Skip the export rather than crash;
84 // follow-up: extend InstanceExport to carry a core-sort alternative
85 // so nested alias lookups through a core-module export can resolve.
86 spdlog::debug(
87 " populateInstanceFromType: skipping `(core module)` export "
88 "'{}'"sv,
89 Exp.getName());
90 continue;
91 }
92 const AST::Component::InstanceType *NestedIT = nullptr;
93 if (ED.getDescType() ==
94 AST::Component::ExternDesc::DescType::InstanceType) {
95 NestedIT = Ctx.getInstanceType(ED.getTypeIndex());
96 // Fallback: when IT is being processed inside a
97 // componenttype/instancetype scope, nested inline InstanceTypes live
98 // in IT's own local type-index space rather than the current scope's
99 // typestate. Walk IT's type-declaring decls to locate the N-th one.
100 if (NestedIT == nullptr) {
101 uint32_t TargetIdx = ED.getTypeIndex();
102 uint32_t LocalIdx = 0;
103 for (const auto &LocalDecl : IT.getDecl()) {
104 if (!LocalDecl.isType()) {
105 continue;
106 }
107 if (LocalIdx == TargetIdx) {
108 const auto *LocalDT = LocalDecl.getType();
109 if (LocalDT != nullptr && LocalDT->isInstanceType()) {
110 NestedIT = &LocalDT->getInstanceType();
111 }
112 break;
113 }
114 LocalIdx++;
115 }
116 }
117 }
118 Ctx.addInstanceExport(InstIdx, Exp.getName(), *ST, NestedIT);
119 }
120}
121
122// Returns the first export in `RequiredIT` missing from the instance at
123// `ProvidedInstIdx`, or whose stored SortType doesn't match the required

Callers 1

validateMethod · 0.85

Calls 13

descTypeToSortTypeFunction · 0.85
debugFunction · 0.85
isExportDeclMethod · 0.80
getDescTypeMethod · 0.80
getInstanceTypeMethod · 0.80
isInstanceTypeMethod · 0.80
addInstanceExportMethod · 0.80
getDeclMethod · 0.45
has_valueMethod · 0.45
getNameMethod · 0.45
getTypeIndexMethod · 0.45
isTypeMethod · 0.45

Tested by

no test coverage detected