-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssll.d
More file actions
197 lines (168 loc) · 5.28 KB
/
ssll.d
File metadata and controls
197 lines (168 loc) · 5.28 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
///
module ssll;
import core.stdc.stdlib : free, malloc;
import core.stdc.string : memcpy;
public import std.meta : AliasSeq;
public import std.traits : hasUDA, getUDAs, ReturnType, Parameters,
ParameterIdentifierTuple;
version (Posix)
{
import core.sys.posix.dlfcn : dlopen, dlclose, RTLD_LAZY;
alias LibHandler = void*; ///
}
else version (Windows)
{
import core.sys.windows.winbase : LoadLibraryA, FreeLibrary;
import core.sys.windows.windef : HINSTANCE;
alias LibHandler = HINSTANCE; ///
}
else static assert(0, "unknown platform");
///
auto api(string lname="lib", Linkage linkage=Linkage.c) @property
{ return ApiUDA(lname, linkage); }
///
auto api(Linkage linkage) @property { return ApiUDA("lib", linkage); }
///
enum Linkage : string
{
c = "C", ///
d = "D", ///
cpp = "C++", ///
windows = "Windows", ///
objC = "Objective-C", ///
system = "System" ///
}
@nogc nothrow:
struct ApiUDA { string libname; Linkage linkage; }
///
LibHandler loadLibrary(string name)
{
const ln = name.length;
if (ln == 0) return null;
auto buf = cast(char*)malloc(ln+1);
if (buf is null) return null;
scope (exit) free(buf);
memcpy(buf, name.ptr, ln);
buf[ln] = '\0';
version (Posix) return dlopen(buf, RTLD_LAZY);
version (Windows) return LoadLibraryA(buf);
}
///
void unloadLibrary(ref LibHandler lib)
{
version (Posix) dlclose(&lib);
version (Windows) FreeLibrary(lib);
lib = null;
}
/// used in SSLL_CALL mixin
template commaSeparated(string[] arr)
{
template r(string[] a)
{
static if (a.length == 0) enum r = "";
else static if (a.length == 1) enum r = a[0];
else enum r = r!(a[0..$/2]) ~ ", " ~ r!(a[$/2..$]);
}
enum commaSeparated = r!arr;
}
///
enum SSLL_CALL = q{
enum __dimmy_symbol__;
alias __self_function__ = AliasSeq!(__traits(parent, __dimmy_symbol__))[0];
enum __self_function_name__ = __traits(identifier, __self_function__);
enum __function_pointer_name__ = apiFunctionPointerName!(__self_function_name__);
version (ssllCheckLoadingSymbols)
if (mixin(__function_pointer_name__ ~ " is null"))
assert(0, `function '` ~ __self_function_name__ ~ `' not loaded, call '`
~ __MODULE__ ~ `.loadApiSybols() before`);
mixin((is(ReturnType!__self_function__ == void) ? "" : "return ") ~
apiFunctionPointerName!(__traits(identifier, __self_function__)) ~
"(" ~ commaSeparated!([ParameterIdentifierTuple!__self_function__]) ~ ");");
};
///
enum LoadApiSymbolsVerbose
{
none, ///
message, ///
assertion ///
}
///
mixin template SSLL_INIT()
{
alias apiFuncs = funcsByUDA!(__traits(parent, loadApiSymbols), ApiUDA);
void loadApiSymbols(LoadApiSymbolsVerbose verbose=LoadApiSymbolsVerbose.none)
{
version (Posix)
{
import core.sys.posix.dlfcn : dlsym;
alias getSymbol = dlsym;
}
version (Windows)
{
import core.sys.windows.windows : GetProcAddress;
alias getSymbol = GetProcAddress;
}
foreach (f; apiFuncs)
{
enum libname = getUDAs!(f, ApiUDA)[$-1].libname;
enum fname = __traits(identifier, f) ~ '\0';
enum pname = apiFunctionPointerName!(fname[0..$-1]);
mixin(pname ~ " = cast(typeof(" ~ pname ~ "))getSymbol("
~ libname ~ ", fname.ptr);");
if (mixin(pname ~ " is null"))
{
with (LoadApiSymbolsVerbose) final switch (verbose)
{
case none: break;
case message:
import core.stdc.stdio : printf;
printf("can't find '%s' function\n", fname.ptr);
break;
case assertion: assert(0, fname[0..$-1]);
}
}
}
}
mixin funcPointers!apiFuncs;
}
template apiFunctionPointerName(string f)
{ enum apiFunctionPointerName = "__" ~ f ~"_fnc_ptr"; }
template funcsByUDA(alias symbol, uda)
{
template impl(lst...)
{
static if (lst.length == 0)
{
alias impl = AliasSeq!();
}
else static if (lst.length == 1)
{
static if (is(typeof(__traits(getMember, symbol, lst[0])) == function))
{
alias ff = AliasSeq!(__traits(getMember, symbol, lst[0]))[0];
static if (hasUDA!(ff, uda)) alias impl = AliasSeq!(ff);
else alias impl = AliasSeq!();
}
else alias impl = AliasSeq!();
}
else alias impl = AliasSeq!(impl!(lst[0..$/2]), impl!(lst[$/2..$]));
}
alias funcsByUDA = impl!(__traits(allMembers, symbol));
}
mixin template funcPointers(funcs...)
{
static if (funcs.length == 0) {}
else static if (funcs.length == 1)
{
alias __this = funcs[0];
enum linkage = getUDAs!(__this, ApiUDA)[$-1].linkage;
mixin(`private __gshared extern(`~ linkage ~
`) @nogc nothrow ReturnType!__this function(Parameters!__this) ` ~
apiFunctionPointerName!(__traits(identifier, __this)) ~ `;`);
}
else
{
mixin funcPointers!(funcs[0..$/2]);
mixin funcPointers!(funcs[$/2..$]);
}
}