-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcffi.cpp
More file actions
440 lines (404 loc) · 12.3 KB
/
cffi.cpp
File metadata and controls
440 lines (404 loc) · 12.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <covscript/impl/impl.hpp>
#include <covscript/cni.hpp>
#include <covscript/dll.hpp>
#include <cstring>
#include <cstdint>
#include <ffi.h>
enum class cffi_type {
ffi_void, ffi_pointer, ffi_double, ffi_float,
ffi_schar, ffi_sshort, ffi_sint, ffi_slong,
ffi_uchar, ffi_ushort, ffi_uint, ffi_ulong,
ffi_sint8, ffi_sint16, ffi_sint32, ffi_sint64,
ffi_uint8, ffi_uint16, ffi_uint32, ffi_uint64,
ffi_string
};
bool is_integer(cffi_type t) noexcept
{
switch (t) {
case cffi_type::ffi_schar:
case cffi_type::ffi_sshort:
case cffi_type::ffi_sint:
case cffi_type::ffi_slong:
case cffi_type::ffi_uchar:
case cffi_type::ffi_ushort:
case cffi_type::ffi_uint:
case cffi_type::ffi_ulong:
case cffi_type::ffi_sint8:
case cffi_type::ffi_sint16:
case cffi_type::ffi_sint32:
case cffi_type::ffi_sint64:
case cffi_type::ffi_uint8:
case cffi_type::ffi_uint16:
case cffi_type::ffi_uint32:
case cffi_type::ffi_uint64:
return true;
default:
return false;
}
}
bool is_float(cffi_type t) noexcept
{
switch (t) {
case cffi_type::ffi_double:
case cffi_type::ffi_float:
return true;
default:
return false;
}
}
ffi_type* get_actual_type(cffi_type t) noexcept
{
switch (t) {
default:
case cffi_type::ffi_void:
return &ffi_type_void;
case cffi_type::ffi_pointer:
case cffi_type::ffi_string:
return &ffi_type_pointer;
case cffi_type::ffi_double:
return &ffi_type_double;
case cffi_type::ffi_float:
return &ffi_type_float;
case cffi_type::ffi_schar:
return &ffi_type_schar;
case cffi_type::ffi_sshort:
return &ffi_type_sshort;
case cffi_type::ffi_sint:
return &ffi_type_sint;
case cffi_type::ffi_slong:
return &ffi_type_slong;
case cffi_type::ffi_uchar:
return &ffi_type_uchar;
case cffi_type::ffi_ushort:
return &ffi_type_ushort;
case cffi_type::ffi_uint:
return &ffi_type_uint;
case cffi_type::ffi_ulong:
return &ffi_type_ulong;
case cffi_type::ffi_sint8:
return &ffi_type_sint8;
case cffi_type::ffi_sint16:
return &ffi_type_sint16;
case cffi_type::ffi_sint32:
return &ffi_type_sint32;
case cffi_type::ffi_sint64:
return &ffi_type_sint64;
case cffi_type::ffi_uint8:
return &ffi_type_uint8;
case cffi_type::ffi_uint16:
return &ffi_type_uint16;
case cffi_type::ffi_uint32:
return &ffi_type_uint32;
case cffi_type::ffi_uint64:
return &ffi_type_uint64;
}
}
struct resource_holder {
resource_holder() = default;
resource_holder(const resource_holder&) = delete;
resource_holder(resource_holder &&) noexcept = delete;
virtual ~resource_holder() = default;
virtual cs::var get_var() const = 0;
virtual void *get_ptr() const = 0;
};
template<typename T>
struct resource_holder_impl final : public resource_holder {
T data;
template<typename ...ArgsT>
resource_holder_impl(ArgsT &&...args) : data(std::forward<ArgsT>(args)...) {}
virtual ~resource_holder_impl() = default;
virtual cs::var get_var() const
{
return data;
}
virtual void *get_ptr() const
{
return (void*)&data;
}
};
// Optimize for numeric
template<typename T>struct numeric_holder {};
template<typename T>
struct resource_holder_impl<numeric_holder<T>> final : public resource_holder {
T data;
resource_holder_impl() = default;
resource_holder_impl(T val) : data(val) {}
virtual cs::var get_var() const {
return cs::var::make<cs::numeric>(data);
}
virtual void *get_ptr() const
{
return (void*)&data;
}
};
// Optimize for strings
template<>
struct resource_holder_impl<const char*> final : public resource_holder {
const char *data;
resource_holder_impl() = default;
resource_holder_impl(const char *str) : data(str) {}
virtual ~resource_holder_impl() = default;
virtual cs::var get_var() const
{
return cs::var::make<cs::string>(data);
}
virtual void *get_ptr() const
{
return (void*)&data;
}
};
template<>
struct resource_holder_impl<cs::string> final : public resource_holder {
char *data;
resource_holder_impl(const cs::string &str)
{
data = new char[str.size()];
std::strcpy(data, str.c_str());
}
virtual ~resource_holder_impl()
{
delete[] data;
}
virtual cs::var get_var() const
{
return cs::var::make<cs::string>(data);
}
virtual void *get_ptr() const
{
return (void*)&data;
}
};
resource_holder* make_resource_holder(cffi_type t) noexcept
{
switch (t) {
default:
case cffi_type::ffi_pointer:
return new resource_holder_impl<void *>();
case cffi_type::ffi_string:
return new resource_holder_impl<const char *>();
case cffi_type::ffi_double:
return new resource_holder_impl<numeric_holder<double>>();
case cffi_type::ffi_float:
return new resource_holder_impl<numeric_holder<float>>();
case cffi_type::ffi_schar:
return new resource_holder_impl<char>();
case cffi_type::ffi_sshort:
return new resource_holder_impl<numeric_holder<short>>();
case cffi_type::ffi_sint:
return new resource_holder_impl<numeric_holder<int>>();
case cffi_type::ffi_slong:
return new resource_holder_impl<numeric_holder<long>>();
case cffi_type::ffi_uchar:
return new resource_holder_impl<numeric_holder<unsigned char>>();
case cffi_type::ffi_ushort:
return new resource_holder_impl<numeric_holder<unsigned short>>();
case cffi_type::ffi_uint:
return new resource_holder_impl<numeric_holder<unsigned int>>();
case cffi_type::ffi_ulong:
return new resource_holder_impl<numeric_holder<unsigned long>>();
case cffi_type::ffi_sint8:
return new resource_holder_impl<numeric_holder<int8_t>>();
case cffi_type::ffi_sint16:
return new resource_holder_impl<numeric_holder<int16_t>>();
case cffi_type::ffi_sint32:
return new resource_holder_impl<numeric_holder<int32_t>>();
case cffi_type::ffi_sint64:
return new resource_holder_impl<numeric_holder<int64_t>>();
case cffi_type::ffi_uint8:
return new resource_holder_impl<numeric_holder<uint8_t>>();
case cffi_type::ffi_uint16:
return new resource_holder_impl<numeric_holder<uint16_t>>();
case cffi_type::ffi_uint32:
return new resource_holder_impl<numeric_holder<uint32_t>>();
case cffi_type::ffi_uint64:
return new resource_holder_impl<numeric_holder<uint64_t>>();
}
}
class cffi_simple_callable final {
void (*target_func)() = nullptr;
public:
cffi_simple_callable(void (*ptr)()) : target_func(ptr) {}
cs::var operator()(cs::vector &args) const
{
std::unique_ptr<ffi_type *[]> arg_types(new ffi_type *[args.size()]);
std::unique_ptr<void *[]> arg_data(new void *[args.size()]);
std::vector<std::unique_ptr<resource_holder>> hosted_res;
for (std::size_t i = 0; i < args.size(); ++i)
{
const cs::var &it = args[i];
if (it.type() == typeid(cs::numeric)) {
const cs::numeric &num = it.const_val<cs::numeric>();
if (num.is_integer()) {
hosted_res.emplace_back(new resource_holder_impl<cs::numeric_integer>(num.as_integer()));
arg_types[i] = &ffi_type_sint64;
}
else {
hosted_res.emplace_back(new resource_holder_impl<cs::numeric_float>(num.as_float()));
arg_types[i] = &ffi_type_longdouble;
}
}
else if (it.type() == typeid(cs::string)) {
hosted_res.emplace_back(new resource_holder_impl<cs::string>(it.const_val<cs::string>()));
arg_types[i] = &ffi_type_pointer;
}
else if (it.type() == typeid(void *)) {
hosted_res.emplace_back(new resource_holder_impl<void *>(it.const_val<void *>()));
arg_types[i] = &ffi_type_pointer;
}
else if (it == cs::null_pointer) {
hosted_res.emplace_back(new resource_holder_impl<void *>(nullptr));
arg_types[i] = &ffi_type_pointer;
}
else
throw cs::runtime_error("Unsupported type in cffi.");
arg_data[i] = hosted_res.back()->get_ptr();
}
ffi_cif cif;
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, args.size(), &ffi_type_void, arg_types.get()) == FFI_OK)
ffi_call(&cif, target_func, nullptr, arg_data.get());
return cs::null_pointer;
}
};
class cffi_callable final {
// Properties
cffi_type restype = cffi_type::ffi_void;
std::vector<cffi_type> argtypes;
void (*target_func)() = nullptr;
// Internal Data
mutable ffi_cif _ffi_cif;
std::vector<ffi_type *> _ffi_types;
public:
cffi_callable(void (*ptr)(), cffi_type rt, std::vector<cffi_type> ats) : target_func(ptr), restype(rt), argtypes(std::move(ats))
{
_ffi_types.resize(argtypes.size());
for (std::size_t i = 0; i < argtypes.size(); ++i)
_ffi_types[i] = get_actual_type(argtypes[i]);
if (ffi_prep_cif(&_ffi_cif, FFI_DEFAULT_ABI, argtypes.size(), get_actual_type(restype), _ffi_types.data()) != FFI_OK)
throw cs::runtime_error("Init libffi CIF failed!");
}
cs::var operator()(cs::vector &args) const
{
if (args.size() != argtypes.size())
throw cs::runtime_error("Unmatched argument size.");
std::unique_ptr<void *[]> arg_data(new void *[args.size()]);
std::vector<std::unique_ptr<resource_holder>> hosted_res;
for (std::size_t i = 0; i < args.size(); ++i)
{
const cs::var &it = args[i];
if (it.type() == typeid(cs::numeric)) {
const cs::numeric &num = it.const_val<cs::numeric>();
if (num.is_integer()) {
if (!is_integer(argtypes[i]))
throw cs::lang_error("Unmatched type in arguments.");
hosted_res.emplace_back(new resource_holder_impl<cs::numeric_integer>(num.as_integer()));
}
else {
if (!is_float(argtypes[i]))
throw cs::lang_error("Unmatched type in arguments.");
hosted_res.emplace_back(new resource_holder_impl<cs::numeric_float>(num.as_float()));
}
}
else if (it.type() == typeid(cs::string)) {
if (argtypes[i] != cffi_type::ffi_string)
throw cs::lang_error("Unmatched type in arguments.");
hosted_res.emplace_back(new resource_holder_impl<cs::string>(it.const_val<cs::string>()));
}
else if (it.type() == typeid(void *)) {
if (argtypes[i] != cffi_type::ffi_pointer)
throw cs::lang_error("Unmatched type in arguments.");
hosted_res.emplace_back(new resource_holder_impl<void *>(it.const_val<void *>()));
}
else if (it == cs::null_pointer) {
if (argtypes[i] != cffi_type::ffi_pointer && !is_integer(argtypes[i]))
throw cs::lang_error("Unmatched type in arguments.");
hosted_res.emplace_back(new resource_holder_impl<void *>(nullptr));
}
else
throw cs::runtime_error("Unsupported type in cffi.");
arg_data[i] = hosted_res.back()->get_ptr();
}
if (restype != cffi_type::ffi_void)
{
std::unique_ptr<resource_holder> return_data(make_resource_holder(restype));
ffi_call(&_ffi_cif, target_func, return_data->get_ptr(), arg_data.get());
return return_data->get_var();
}
else
{
ffi_call(&_ffi_cif, target_func, nullptr, arg_data.get());
return cs::null_pointer;
}
}
};
struct dll_holder {
void *handle = nullptr;
dll_holder(std::string_view path)
{
handle = cs::dll::open(path);
}
dll_holder(const dll_holder &) = delete;
~dll_holder()
{
if (handle != nullptr)
cs::dll::close(handle);
}
void *get_address(std::string_view sym)
{
return cs::dll::find_symbol(handle, sym);
}
};
using dll_type = std::shared_ptr<dll_holder>;
CNI_ROOT_NAMESPACE {
using namespace cs;
dll_type import_lib(const std::string& path)
{
return std::make_shared<dll_holder>(path);
}
CNI(import_lib)
CNI_NAMESPACE(lib)
{
callable import_func(const dll_type &dll, const std::string &name) {
return callable(cffi_simple_callable((void(*)())dll->get_address(name)));
}
CNI(import_func)
callable import_func_s(const dll_type &dll, const std::string &name, cffi_type restype, const array &ats) {
std::vector<cffi_type> argtypes;
for (auto &it : ats)
argtypes.emplace_back(it.const_val<cffi_type>());
return callable(cffi_callable((void(*)())dll->get_address(name), restype, std::move(argtypes)));
}
CNI(import_func_s)
}
CNI_NAMESPACE(utils)
{
var make_integer(void *ptr) {
return var::make<numeric>((std::size_t)ptr);
}
CNI(make_integer)
var make_string(void *ptr) {
return var::make<string>((const char*)ptr);
}
CNI(make_string)
bool is_nullptr(void *ptr) {
return ptr == nullptr;
}
CNI(is_nullptr)
}
CNI_NAMESPACE(types)
{
CNI_VALUE(void, cffi_type::ffi_void)
CNI_VALUE(pointer, cffi_type::ffi_pointer)
CNI_VALUE(double, cffi_type::ffi_double)
CNI_VALUE(float, cffi_type::ffi_float)
CNI_VALUE(schar, cffi_type::ffi_schar)
CNI_VALUE(sshort, cffi_type::ffi_sshort)
CNI_VALUE(sint, cffi_type::ffi_sint)
CNI_VALUE(slong, cffi_type::ffi_slong)
CNI_VALUE(uchar, cffi_type::ffi_uchar)
CNI_VALUE(ushort, cffi_type::ffi_ushort)
CNI_VALUE(uint, cffi_type::ffi_uint)
CNI_VALUE(ulong, cffi_type::ffi_ulong)
CNI_VALUE(string, cffi_type::ffi_string)
}
}
CNI_ENABLE_TYPE_EXT(lib, dll_type)