-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoader.cpp
More file actions
230 lines (197 loc) · 8 KB
/
Loader.cpp
File metadata and controls
230 lines (197 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright (c) 2019-2025 Javier Pimás, Jan Vrany, Labware.
See (MIT) license in root directory.
*/
#include "Loader.h"
#include "Bootstrap/Bootstrapper.h"
#include "Bootstrap/SourceModuleLoader.h"
#include "FileImageSegment.h"
#include <iostream>
namespace Egg {
Loader::Loader(const std::string& modulesDir)
: _modulesDir(modulesDir), _runtime(nullptr), _kernel(nullptr) {
}
Loader::~Loader() {
}
std::string Loader::findModulesDir_() {
namespace fs = std::filesystem;
if (fs::exists(_modulesDir))
return _modulesDir;
std::vector<std::string> prefixes = {
"../../../../", "../../../", "../../", "../", "./"
};
for (const auto& prefix : prefixes) {
auto candidate = prefix + _modulesDir;
if (fs::exists(candidate))
return candidate;
}
return "";
}
bool Loader::hasEmsFile_(const std::string& name) {
auto searched = "image-segments/" + name + ".ems";
for (const auto& dir : {"./", "../"}) {
if (std::filesystem::exists(std::filesystem::path(dir) / searched))
return true;
}
return false;
}
bool Loader::hasSourceDir_(const std::string& name) {
namespace fs = std::filesystem;
auto modulesRoot = findModulesDir_();
if (modulesRoot.empty()) return false;
auto moduleDir = fs::path(modulesRoot) / name;
return fs::exists(moduleDir) && fs::is_directory(moduleDir);
}
Runtime* Loader::loadKernel() {
namespace fs = std::filesystem;
auto modulesRoot = findModulesDir_();
auto kernelPath = (fs::path(modulesRoot) / "Kernel").string();
if (!fs::exists(kernelPath)) {
std::cerr << "Cannot bootstrap: Kernel module source directory not found at "
<< kernelPath << std::endl;
return nullptr;
}
Bootstrapper bootstrapper(kernelPath, this);
_runtime = bootstrapper.bootstrap();
_kernel = _runtime->_kernel;
// Register the kernel module as loaded so Kernel load: #Kernel works
_loadedModules["Kernel"] = _kernel->_exports["__module__"];
return _runtime;
}
HeapObject* Loader::loadModule_(const std::string& name) {
// 1. Already loaded?
auto it = _loadedModules.find(name);
if (it != _loadedModules.end())
return it->second;
HeapObject* module = nullptr;
// 2. .ems file available?
if (hasEmsFile_(name)) {
auto imageSegment = _segments.count(name) ? _segments[name] : loadModuleFromFile(name + ".ems");
module = imageSegment->_exports["__module__"];
}
// 3. Source directory available?
else if (hasSourceDir_(name)) {
namespace fs = std::filesystem;
auto modulesRoot = findModulesDir_();
auto modulePath = (fs::path(modulesRoot) / name).string();
SourceModuleLoader sourceLoader(_runtime);
module = sourceLoader.loadModuleFromSource(modulePath);
}
else {
error(("Module not found: " + name).c_str());
return nullptr;
}
_loadedModules[name] = module;
return module;
}
// .ems loading support methods
FileImageSegment* Loader::loadModuleFromFile(const std::string &filename) {
auto filepath = this->findInPath(filename);
auto stream = std::ifstream(filepath, std::ios::binary);
auto imageSegment = new FileImageSegment(&stream);
std::vector<Object*> imports;
this->bindModuleImports(imageSegment, imports);
imageSegment->fixPointerSlots(imports);
this->_runtime->addSegmentSpace_(imageSegment);
return imageSegment;
}
void Loader::bindModuleImports(FileImageSegment *imageSegment, std::vector<Object*> &imports) {
for (size_t i = 0; i < imageSegment->_importDescriptors.size(); i++) {
imports.push_back(this->bindModuleImport(imageSegment, imageSegment->_importDescriptors[i]));
}
}
Object* Loader::bindModuleImport(FileImageSegment* imageSegment, std::vector<std::uint32_t> &descriptor) {
auto linker = this->importStringAt_(imageSegment, descriptor[0]);
HeapObject *token;
if (descriptor.size() == 1)
token = this->_kernel->_exports["nil"];
else if (descriptor.size() == 2)
token = this->importStringAt_(imageSegment, descriptor[1]);
else {
std::vector<HeapObject*> array;
for (size_t i = 1; i < descriptor.size(); i++)
array.push_back(this->importStringAt_(imageSegment, descriptor[i]));
token = this->transferArray(array);
}
auto ref = this->_runtime->sendLocal_to_with_with_("linker:token:", (Object*)this->_kernel->_exports["SymbolicReference"], (Object*)linker, (Object*)token);
return this->_runtime->sendLocal_to_("link", ref);
}
HeapObject* Loader::importStringAt_(FileImageSegment* imageSegment, uint32_t index) {
return this->transferSymbol(imageSegment->importStringAt_(index));
}
HeapObject* Loader::transferSymbol(std::string &str) {
return this->_runtime->addSymbol_(str);
}
HeapObject* Loader::transferArray(std::vector<HeapObject*> &array) {
return this->_runtime->newArray_(array);
}
HeapObject* Loader::transferArray(std::vector<Object*> &array) {
return this->_runtime->newArray_(array);
}
std::filesystem::path Loader::findInPath(const std::string &filename) {
std::vector<std::string> dirs({"./", "../"});
auto searched = "image-segments/" + filename;
for (auto& dir : dirs) {
auto filePath = std::filesystem::path(dir) / searched;
if (std::filesystem::exists(filePath))
return filePath;
}
auto str = std::string("could not find module snapshot file ") + filename;
error(str.c_str());
std::terminate();
}
// Bare testing support
FileImageSegment* Loader::bareLoadModuleFromFile(const std::string &filename) {
auto filepath = this->findInPath(filename);
auto stream = std::ifstream(filepath);
auto imageSegment = new FileImageSegment(&stream);
std::vector<Object*> imports;
for (size_t i = 0; i < imageSegment->_importDescriptors.size(); i++) {
std::vector<uint32_t> &descriptor = imageSegment->_importDescriptors[i];
auto import = this->bareBindModuleImport(imageSegment, descriptor);
std::cout << "import " << i << " is: " << import->printString() << std::endl;
imports.push_back(import);
}
imageSegment->fixPointerSlots(imports);
return imageSegment;
}
Object* Loader::bareBindModuleImport(FileImageSegment* imageSegment, std::vector<std::uint32_t> &descriptor) {
auto linker = imageSegment->importStringAt_(descriptor[0]);
std::vector<std::string> tokens;
for (size_t i = 1; i < descriptor.size(); i++)
tokens.push_back(imageSegment->importStringAt_(descriptor[i]));
if (linker == "asSymbol") {
auto symbol = _runtime->existingSymbolFrom_(tokens[0]);
if (symbol == nullptr) {
symbol = (Object*)_runtime->newString_(tokens[0]);
}
return (Object*)symbol;
}
if (linker == "nil")
return (Object*)_runtime->_nilObj;
if (linker == "true")
return (Object*)_runtime->_trueObj;
if (linker == "false")
return (Object*)_runtime->_falseObj;
if (linker == "asClass")
return (Object*)_kernel->_exports[tokens[1]];
if (linker == "asMetaclass")
return (Object*)_runtime->speciesOf_((Object*)_kernel->_exports[tokens[1]]);
if (linker == "asBehavior")
return (Object*)_runtime->speciesInstanceBehavior_(_kernel->_exports[tokens[1]]);
if (linker == "asMetaclassBehavior")
return (Object*)_runtime->speciesInstanceBehavior_(_runtime->speciesOf_((Object*)_kernel->_exports[tokens[1]]));
if (linker == "asModule")
return (Object*)_kernel->_exports["__module__"];
if (linker == "symbolTable")
return (Object*)_kernel->_exports["SymbolTable"];
if (linker == "nilToken") {
auto hashTable = _kernel->_exports["HashTable"];
auto symbol = _runtime->existingSymbolFrom_("NilToken");
auto binding = (SAssociationBinding*)_runtime->_evaluator->context()->staticBindingForCvar_in_(symbol, hashTable);
return binding->valueWithin_(_runtime->_evaluator->context());
}
ASSERT(false);
std::terminate();
}
} // namespace Egg