Load binary of Import description. See "include/loader/loader.h".
| 8 | |
| 9 | // Load binary of Import description. See "include/loader/loader.h". |
| 10 | Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) { |
| 11 | auto ReportError = [this](auto E) { |
| 12 | return logLoadError(E, FMgr.getLastOffset(), ASTNodeAttr::Desc_Import); |
| 13 | }; |
| 14 | |
| 15 | // Read the module name. |
| 16 | EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) { |
| 17 | ImpDesc.setModuleName(S); |
| 18 | })); |
| 19 | |
| 20 | // Read the external name. |
| 21 | EXPECTED_TRY(FMgr.readName().map_error(ReportError).map([&](std::string S) { |
| 22 | ImpDesc.setExternalName(S); |
| 23 | })); |
| 24 | |
| 25 | // Read the external type. |
| 26 | EXPECTED_TRY(FMgr.readByte().map_error(ReportError).map([&](uint8_t B) { |
| 27 | ImpDesc.setExternalType(static_cast<ExternalType>(B)); |
| 28 | })); |
| 29 | |
| 30 | // Create the content node according to the external type. |
| 31 | switch (ImpDesc.getExternalType()) { |
| 32 | case ExternalType::Function: { |
| 33 | // Read the function type index. |
| 34 | EXPECTED_TRY(FMgr.readU32().map_error(ReportError).map([&](uint32_t Idx) { |
| 35 | ImpDesc.setExternalFuncTypeIdx(Idx); |
| 36 | })); |
| 37 | return {}; |
| 38 | } |
| 39 | case ExternalType::Table: { |
| 40 | // Read the table type node. |
| 41 | return loadType(ImpDesc.getExternalTableType()); |
| 42 | } |
| 43 | case ExternalType::Memory: { |
| 44 | // Read the memory type node. |
| 45 | return loadType(ImpDesc.getExternalMemoryType()); |
| 46 | } |
| 47 | case ExternalType::Global: { |
| 48 | // Read the global type node. |
| 49 | EXPECTED_TRY(loadType(ImpDesc.getExternalGlobalType())); |
| 50 | // Mutable global imports are for the ImportExportMutGlobals proposal. |
| 51 | if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var && |
| 52 | unlikely(!Conf.hasProposal(Proposal::ImportExportMutGlobals))) { |
| 53 | return logNeedProposal(ErrCode::Value::InvalidMut, |
| 54 | Proposal::ImportExportMutGlobals, |
| 55 | FMgr.getLastOffset(), ASTNodeAttr::Desc_Import); |
| 56 | } |
| 57 | return {}; |
| 58 | } |
| 59 | case ExternalType::Tag: { |
| 60 | if (!Conf.hasProposal(Proposal::ExceptionHandling)) { |
| 61 | return logNeedProposal(ErrCode::Value::MalformedImportKind, |
| 62 | Proposal::ExceptionHandling, FMgr.getLastOffset(), |
| 63 | ASTNodeAttr::Module); |
| 64 | } |
| 65 | // Read the Tag type node. |
| 66 | return loadType(ImpDesc.getExternalTagType()); |
| 67 | } |
nothing calls this directly
no test coverage detected