-
Notifications
You must be signed in to change notification settings - Fork 5
/
modules.d
128 lines (108 loc) · 3.06 KB
/
modules.d
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
module til.modules;
import std.array : split;
import std.file : dirEntries, SpanMode;
import std.path : asAbsolutePath, asNormalizedPath;
import std.process : environment;
import core.sys.posix.dlfcn;
import std.string : strip, toStringz;
import til.nodes;
debug
{
import std.stdio;
}
CommandHandler[string][string] sourcesCache;
string[] modulesPath;
static this()
{
string home = environment["HOME"];
string til_path = environment.get(
"TIL_PATH",
home ~ "/.dub/packages"
);
foreach(p; til_path.split(":"))
{
modulesPath ~= to!string(asAbsolutePath(
to!string(asNormalizedPath(p))
));
}
}
bool importModule(Process escopo, string modulePath)
{
return importModule(escopo, modulePath, modulePath);
}
bool importModule(Process escopo, string modulePath, string prefix)
{
CommandHandler[string] source;
try {
source = importFromSharedLibrary(escopo, modulePath, prefix);
}
catch(Exception ex)
{
debug {stderr.writeln(ex);}
return false;
}
// Save on cache:
escopo.importNamesFrom(source, prefix);
sourcesCache[modulePath] = source;
return true;
}
// Import commands from a .so:
CommandHandler[string] importFromSharedLibrary(
Process escopo, string libraryPath, string moduleAlias
)
{
// We don't want users informing the library preffix and suffix:
libraryPath = "libtil_" ~ libraryPath ~ ".so";
debug {stderr.writeln("libraryPath:", libraryPath);}
// (Like `libtil_vectors.so`)
char* lastError;
foreach(path; modulesPath)
{
debug {stderr.writeln("path:",path);}
// Scan directories recursively searching for a match
// with libraryPath:
foreach(dirEntry; path.dirEntries(libraryPath, SpanMode.depth, true))
{
debug {stderr.writeln(" dirEntry:", dirEntry);}
auto libraryPathZ = dirEntry.toStringz;
// lh = "library handler"
void* lh = dlopen(libraryPathZ, RTLD_LAZY);
if (!lh)
{
lastError = cast(char *)dlerror();
debug {stderr.writeln(" dlerror: ", lastError);}
continue;
}
// Get the commands from inside the shared object:
auto getCommands = cast(CommandHandlerMap function(Process))dlsym(
lh, "getCommands"
);
const char* error = dlerror();
if (error)
{
throw new Exception("dlsym error: " ~ to!string(error));
}
auto libraryCommands = getCommands(escopo);
return libraryCommands;
}
}
throw new Exception("dlopen error: " ~ to!string(lastError));
};
void importNamesFrom(
Process escopo, CommandHandlerMap source, string prefix
)
{
foreach(name, command; source)
{
string cmdPath;
if (name is null)
{
cmdPath = prefix;
}
else
{
cmdPath = prefix ~ "." ~ name;
}
escopo.commands[cmdPath] = command;
}
}