| 91 | } |
| 92 | |
| 93 | Result Context::loadModule(const fs::path& name, Flags<LoadSettings> settings, ChiModule** toFill) { |
| 94 | assert(!name.empty() && "Name should not be empty when calling chi::Context::loadModule"); |
| 95 | |
| 96 | Result res; |
| 97 | |
| 98 | auto requestedModCtx = res.addScopedContext({{"Requested Module Name", name.generic_string()}}); |
| 99 | |
| 100 | if (settings & LoadSettings::Fetch) { |
| 101 | res += fetchModule(name, bool(settings & LoadSettings::FetchRecursive)); |
| 102 | if (!res) { return res; } |
| 103 | } |
| 104 | |
| 105 | // check for built-in modules |
| 106 | if (name == "lang") { |
| 107 | if (langModule() != nullptr) { |
| 108 | if (toFill != nullptr) { *toFill = langModule(); } |
| 109 | return {}; |
| 110 | } |
| 111 | auto mod = std::make_unique<LangModule>(*this); |
| 112 | if (toFill != nullptr) { *toFill = mod.get(); } |
| 113 | addModule(std::move(mod)); |
| 114 | return {}; |
| 115 | } |
| 116 | |
| 117 | // see if it's already loaded |
| 118 | { |
| 119 | auto mod = moduleByFullName(name); |
| 120 | if (mod) { |
| 121 | if (toFill != nullptr) { *toFill = mod; } |
| 122 | return {}; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (workspacePath().empty()) { |
| 127 | res.addEntry("E52", "Cannot load module without a workspace path", {}); |
| 128 | return res; |
| 129 | } |
| 130 | |
| 131 | // find it in the workspace |
| 132 | fs::path fullPath = workspacePath() / "src" / name; |
| 133 | fullPath.replace_extension(".chimod"); |
| 134 | |
| 135 | if (!fs::is_regular_file(fullPath)) { |
| 136 | res.addEntry("EUKN", "Failed to find module", |
| 137 | {{"Workspace Path", workspacePath().string()}, |
| 138 | {"Expected Path", fullPath.generic_string()}}); |
| 139 | return res; |
| 140 | } |
| 141 | |
| 142 | // load the JSON |
| 143 | nlohmann::json readJson = {}; |
| 144 | try { |
| 145 | fs::ifstream inFile{fullPath}; |
| 146 | |
| 147 | inFile >> readJson; |
| 148 | } catch (std::exception& e) { |
| 149 | res.addEntry("EUKN", "Failed to parse json", {{"Error", e.what()}}); |
| 150 | return res; |