2023-10-03 14:45:12 +02:00
|
|
|
/**************************************************************
|
|
|
|
_ __ _ _
|
|
|
|
_ __ ___| |_ _ __ ___ / _| ___ _ __| |_| |__
|
|
|
|
| '__/ _ \ __| '__/ _ \| |_ / _ \| '__| __| '_ \
|
|
|
|
| | | __/ |_| | | (_) | _| (_) | | | |_| | | |
|
|
|
|
|_| \___|\__|_| \___/|_| \___/|_| \__|_| |_|
|
|
|
|
for nga
|
2023-03-29 02:20:33 +02:00
|
|
|
|
2023-10-03 14:45:12 +02:00
|
|
|
(c) Charles Childers, Luke Parrish, Marc Simpsonn,
|
|
|
|
Jay Skeer, Kenneth Keating
|
|
|
|
|
|
|
|
**************************************************************/
|
2023-03-29 02:20:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_FFI
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
typedef void (*External)(void *);
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V *handles[32];
|
2023-03-29 02:20:33 +02:00
|
|
|
External funcs[32000];
|
|
|
|
int nlibs, nffi;
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V open_library(NgaState *vm) {
|
2023-03-29 02:20:33 +02:00
|
|
|
handles[nlibs] = dlopen(string_extract(vm, stack_pop(vm)), RTLD_LAZY);
|
|
|
|
stack_push(vm, nlibs);
|
|
|
|
nlibs++;
|
|
|
|
}
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V map_symbol(NgaState *vm) {
|
2023-03-29 02:20:33 +02:00
|
|
|
int h;
|
|
|
|
h = stack_pop(vm);
|
|
|
|
char *s = string_extract(vm, stack_pop(vm));
|
|
|
|
funcs[nffi] = dlsym(handles[h], s);
|
|
|
|
stack_push(vm, nffi);
|
|
|
|
nffi++;
|
|
|
|
}
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V invoke(NgaState *vm) {
|
2023-03-29 02:20:33 +02:00
|
|
|
funcs[stack_pop(vm)](vm);
|
|
|
|
}
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V io_ffi(NgaState *vm) {
|
2023-03-29 02:20:33 +02:00
|
|
|
switch (stack_pop(vm)) {
|
|
|
|
case 0: open_library(vm); break;
|
|
|
|
case 1: map_symbol(vm); break;
|
|
|
|
case 2: invoke(vm); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 00:49:52 +01:00
|
|
|
V query_ffi(NgaState *vm) {
|
2023-03-29 02:20:33 +02:00
|
|
|
stack_push(vm, 0);
|
2023-12-09 19:15:54 +01:00
|
|
|
stack_push(vm, DEVICE_FFI);
|
2023-03-29 02:20:33 +02:00
|
|
|
}
|
|
|
|
#endif
|