Sha256: b071cbe20bfd1f5a9a254cdf92a5b8a06b4a11c13c64b2eeb10630718a999a01

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

#include <ruby.h>
#include <dlfcn.h>
#include "extconf.h"

static void (*__polycrystal_init)() = NULL;
static void (*__polycrystal_module_run)() = NULL;

static VALUE load_library(VALUE _self, VALUE path){
    char * cpath = StringValueCStr(path);
    void * handle = dlopen(cpath, RTLD_NOW|RTLD_LOCAL);
    if(handle != NULL){
        __polycrystal_init = (void (*)())dlsym(handle, "__polycrystal_init");
        if(__polycrystal_init != NULL){
            __polycrystal_init();
        }else{
            rb_raise(rb_eRuntimeError, "Load __polycrystal_init failed from %s: %s", cpath, dlerror());
            return Qnil;
        }
        __polycrystal_module_run = (void (*)())dlsym(handle, "__polycrystal_module_run");
        if(__polycrystal_module_run != NULL){
            __polycrystal_module_run();
        }else{
            rb_raise(rb_eRuntimeError, "Load __polycrystal_module_run failed from %s: %s", cpath, dlerror());
            return Qnil;
        }
    }else{
        rb_raise(rb_eRuntimeError, "Can't load library at %s: %s", cpath, dlerror());
        return Qnil;
    }
    return Qtrue;
}


void Init_polycrystal() {
    VALUE mod = rb_define_module("Polycrystal");
    VALUE class = rb_define_class_under(mod, "Loader", rb_cObject);
    rb_define_private_method(class, "load_library", load_library, 1);
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polycrystal-0.1.3 ext/polycrystal/polycrystal.c
polycrystal-0.1.2 ext/polycrystal/polycrystal.c
polycrystal-0.1.1 ext/polycrystal/polycrystal.c
polycrystal-0.1.0-x86_64-darwin-21 ext/polycrystal/polycrystal.c