MCPcopy Create free account
hub / github.com/chigraph/chigraph / loadModule

Method loadModule

libchigraph/src/Context.cpp:93–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91}
92
93Result 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;

Callers 12

ContextTests.cppFile · 0.80
DebuggerTests.cppFile · 0.80
JSONSerializer.cppFile · 0.80
mainFunction · 0.80
addDependencyMethod · 0.80
runFunction · 0.80
compileFunction · 0.80

Calls 4

addScopedContextMethod · 0.80
getMethod · 0.80
addEntryMethod · 0.80
updateLastEditTimeMethod · 0.80

Tested by 1

mainFunction · 0.64