Is it possible to dynamically load a @safe function from a shared library ?

wjoe invalid at example.com
Fri Mar 13 15:16:06 UTC 2020


I've got a plug-in which is a shared library. Like this

module plugin;

@safe int VersionOfAPI()
{
    return 1;
}

this is builds to plugin.so

in main.d I'm loading the plugin and bind the those functions 
like so:

module app;
@safe:

alias apiverfn = int function();
apiverfn apiVersion;

void main()
{
    LoadPlugin("plugin.so");
}

void LoadPlugin(string path)
{
    void* plugin = loadLibrary(path);

    @trusted void* bindSymbol(void** pfn, const(char)* symbolName)
    {
        *pfn = dlsym(plugin, symbolName);
    }

    bindSymbol(&apiVersion, "VersionOfAPI");
}

The compiler now refuses to call bindSymbol:
   Error: function bindSymbol(void**, const(char)*) is not 
callable using argument types (int function() @safe*, string)  
cannot pass &apiVersion of type int function() @safe* to 
parameter void** pfn

It makes sense that the compiler refuses to assign a @system 
function* to a @safe one but dlsym is a @system function which 
returns a @system fn* and I know that the function is @safe.

Is it possible to convince the compiler to look the other way 
while binding @safe functions from the plugin ?


More information about the Digitalmars-d-learn mailing list