Only import libraries once

This commit is contained in:
2025-08-30 11:12:53 +10:00
parent 8d80416c5c
commit cea66aa583

View File

@@ -314,6 +314,9 @@ map<string, void*> loadedLibraries;
// Map of function name to function pointer
map<string, void*> externalFunctions;
// Libraries currently imported
vector<string> libraries;
// Conversion functions
GroundValue literalToGroundValue(const Literal& lit) {
GroundValue gv;
@@ -1898,6 +1901,17 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
error("First argument for use requires a string literal");
}
string libName = get<string>(get<Literal>(l.args[0]).val);
bool imported = false;
for (string lib : libraries) {
if (lib == libName) {
imported = true;
break;
}
}
if (imported) break;
string groundLibsDir = getenv("GROUND_LIBS");
if (filesystem::exists(useName)) {
@@ -1917,6 +1931,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
importing.push(get<string>(get<Literal>(l.args[0]).val));
Literal ret = exec(parser(lexer(in)), false);
importing.pop();
libraries.push_back(libName);
}
break;
case Instructions::Extern:
@@ -1936,6 +1951,16 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
error("First argument for extern requires a string literal");
}
bool imported = false;
for (string lib : libraries) {
if (lib == libName) {
imported = true;
break;
}
}
if (imported) break;
// Add appropriate extension
string fullLibName = libName;
#ifdef _WIN32