%include <dlfcn.h>,<string.h>
%prefix smop_s1p_loader
%prototype SMOP__MildewSOLoader
%idconst continuation
%RI.id MildewSOLoader
%{
typedef struct dlhandle {
void* handle;
struct dlhandle* next;
} dlhandle;
static dlhandle* dlhandles;
void smop_s1p_close_dlhandles() {
dlhandle* handle = dlhandles;
while (handle) {
dlhandle* next = handle->next;
if (dlclose(handle->handle)) {
fprintf("error while unloading: %s\n",dlerror());
}
free(handle);
handle = next;
}
dlhandles = NULL;
}
%}
%method load
SMOP__Object* continuation = SMOP_DISPATCH(interpreter, SMOP_RI(interpreter),
SMOP__ID__continuation,
SMOP__NATIVE__capture_create(interpreter,
(SMOP__Object*[]) {SMOP_REFERENCE(interpreter,interpreter),NULL},
(SMOP__Object*[]) {NULL}
));
SMOP__Object* filename_idconst = SMOP__NATIVE__capture_positional(interpreter,capture,1);
SMOP__Object* scope = SMOP__NATIVE__capture_positional(interpreter,capture,2);
int len;
char* filename_nonull = SMOP__NATIVE__idconst_fetch(filename_idconst,&len);
char* filename = malloc(sizeof(char) * (len+1));
strncpy(filename,filename_nonull,len);
filename[len] = '\0';
void* handle = dlopen(filename,RTLD_LAZY | RTLD_GLOBAL);
dlhandle* dlh = malloc(sizeof(dlhandle));
dlh->handle = handle;
dlh->next = dlhandles;
dlhandles = dlh;
if (!handle) {
printf("Loader.load(%s): %s\n",filename,dlerror());
abort();
}
free(filename);
void* (*init)(SMOP__Object*,SMOP__Object*,SMOP__Object*) = dlsym(handle,"init");
if (!init) {
printf("Loader.load: %s\n",dlerror());
abort();
}
init(interpreter,scope,continuation);
%method new
ret = smop_nagc_alloc(sizeof(smop_s1p_loader_struct));
ret->RI = (SMOP__ResponderInterface*) RI;
%method FETCH
___VALUE_FETCH___
%method STORE
___VALUE_STORE___