Wrap dlopen/dlsym
This commit is contained in:
@@ -7,6 +7,14 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <uthash.h>
|
#include <uthash.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: check if this works
|
||||||
|
#include <minwindef.h>
|
||||||
|
#define PATH_MAX MAX_PATH
|
||||||
|
#else
|
||||||
|
#include <limits.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// TYPES
|
// TYPES
|
||||||
//
|
//
|
||||||
@@ -352,6 +360,11 @@ struct GroundExecutionResult {
|
|||||||
} as;
|
} as;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GroundOpenSharedObjects {
|
||||||
|
char path[PATH_MAX];
|
||||||
|
void* handle;
|
||||||
|
UT_hash_handle hh;
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// INTERFACE
|
// INTERFACE
|
||||||
@@ -522,6 +535,12 @@ struct _Ground {
|
|||||||
|
|
||||||
char* (*BytecodeValue)(GroundBytecodeValue* value);
|
char* (*BytecodeValue)(GroundBytecodeValue* value);
|
||||||
} Stringify;
|
} Stringify;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
struct GroundOpenSharedObjects* objects;
|
||||||
|
void* (*openSharedObject)(char* id);
|
||||||
|
void* (*getFunction)(void* handle, char* id);
|
||||||
|
} FFI;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct _Ground Ground;
|
extern struct _Ground Ground;
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ sources = files(
|
|||||||
|
|
||||||
'src/Copy/BytecodeValue.c',
|
'src/Copy/BytecodeValue.c',
|
||||||
|
|
||||||
|
'src/FFI/openSharedObject.c',
|
||||||
|
'src/FFI/getFunction.c',
|
||||||
|
|
||||||
'src/Free/Function.c',
|
'src/Free/Function.c',
|
||||||
'src/Free/List.c',
|
'src/Free/List.c',
|
||||||
'src/Free/Object.c',
|
'src/Free/Object.c',
|
||||||
|
|||||||
22
src/FFI/getFunction.c
Normal file
22
src/FFI/getFunction.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "../../include/ground.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: Windows support
|
||||||
|
#else
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void* _GroundFFIGetFunction(void* handle, char* id) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: Windows support
|
||||||
|
#else
|
||||||
|
void* function = dlsym(handle, id);
|
||||||
|
char* error = dlerror();
|
||||||
|
if (error != NULL) {
|
||||||
|
Ground.Log.Error(error);
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return function;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
45
src/FFI/openSharedObject.c
Normal file
45
src/FFI/openSharedObject.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "../../include/ground.h"
|
||||||
|
|
||||||
|
#include <ffi.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: Windows support
|
||||||
|
#else
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void* _GroundFFIOpenSharedObject(char* id) {
|
||||||
|
|
||||||
|
struct GroundOpenSharedObjects* object = NULL;
|
||||||
|
HASH_FIND_STR(Ground.FFI.objects, id, object);
|
||||||
|
|
||||||
|
if (object != NULL) {
|
||||||
|
return object->handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* handle = NULL;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: Windows support
|
||||||
|
#else
|
||||||
|
// Assume POSIX platform
|
||||||
|
handle = dlopen(id, RTLD_LAZY);
|
||||||
|
if (handle == NULL) {
|
||||||
|
Ground.Log.Error(dlerror());
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
object = malloc(sizeof(struct GroundOpenSharedObjects));
|
||||||
|
if (object == NULL) {
|
||||||
|
Ground.Log.Error("malloc failed in Ground.FFI.openSharedObject");
|
||||||
|
Ground.Flags.error = true;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
object->handle = handle;
|
||||||
|
snprintf(object->path, sizeof(object->path), "%s", id);
|
||||||
|
HASH_ADD_STR(Ground.FFI.objects, path, object);
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
@@ -122,6 +122,8 @@ char* _GroundStringifyValue(GroundValue* value);
|
|||||||
|
|
||||||
char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value);
|
char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value);
|
||||||
|
|
||||||
|
void* _GroundFFIOpenSharedObject(char* id);
|
||||||
|
void* _GroundFFIGetFunction(void* handle, char* id);
|
||||||
|
|
||||||
struct _Ground Ground = {
|
struct _Ground Ground = {
|
||||||
|
|
||||||
@@ -273,4 +275,10 @@ struct _Ground Ground = {
|
|||||||
|
|
||||||
.BytecodeValue = _GroundStringifyBytecodeValue,
|
.BytecodeValue = _GroundStringifyBytecodeValue,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
.FFI = {
|
||||||
|
.objects = NULL,
|
||||||
|
.openSharedObject = _GroundFFIOpenSharedObject,
|
||||||
|
.getFunction = _GroundFFIGetFunction,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user