Call a function in a shared library: Difference between revisions

Content added Content deleted
(Added Wren)
Line 2,026: Line 2,026:
Debug.Print ffun(x, y)
Debug.Print ffun(x, y)
End Sub</lang>
End Sub</lang>

=={{header|Wren}}==
{{trans|C}}
An embedded program so we can ask the C host to call the shared library function for us.
<lang ecmascript>/* call_shared_library_function.wren */

var RTLD_LAZY = 1

foreign class DL {
construct open(file, mode) {}

foreign call(symbol, arg)

foreign close()
}

class My {
static openimage(s) {
System.print("internal openimage opens %(s)...")
if (!__handle) __handle = 0
__handle = __handle + 1
return __handle - 1
}
}

var file = "fake.img"
var imglib = DL.open("./fakeimglib.so", RTLD_LAZY)
var imghandle = (imglib != null) ? imglib.call("openimage", file) : My.openimage(file)
System.print("opened with handle %(imghandle)")
if (imglib != null) imglib.close()</lang>
<br>
We also need to create the shared library, fakeimglib.so, and place it in the current directory.
<lang c>/*
gcc -c -fpic fakeimglib.c
gcc -shared fakeimglib.o -o fakeimglib.so
*/
#include <stdio.h>

int openimage(const char *s) {
static int handle = 100;
fprintf(stderr, "opening %s\n", s);
return handle++;
}</lang>
<br>
Finally, we embed the Wren script in the following C program, compile and run it:
<lang c>/* gcc call_shared_library_function.c -o call_shared_library_function -ldl -lwren -lm */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "wren.h"

/* C <=> Wren interface functions */

void C_dlAllocate(WrenVM* vm) {
const char *file = wrenGetSlotString(vm, 1);
int mode = (int)wrenGetSlotDouble(vm, 2);
void *imglib = dlopen(file, mode);
if (imglib == NULL) wrenSetSlotNull(vm, 0);
void** pimglib = (void**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(void*));
*pimglib = imglib;
}

void C_call(WrenVM* vm) {
void *imglib = *(void**)wrenGetSlotForeign(vm, 0);
const char *symbol = wrenGetSlotString(vm, 1);
const char *arg = wrenGetSlotString(vm, 2);
int (*extopenimage)(const char *);
extopenimage = dlsym(imglib, symbol);
int imghandle = extopenimage(arg);
wrenSetSlotDouble(vm, 0, (double)imghandle);
}

void C_close(WrenVM* vm) {
void *imglib = *(void**)wrenGetSlotForeign(vm, 0);
dlclose(imglib);
}

WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "DL") == 0) {
methods.allocate = C_dlAllocate;
}
}
return methods;
}

WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "DL") == 0) {
if (!isStatic && strcmp(signature, "call(_,_)") == 0) return C_call;
if (!isStatic && strcmp(signature, "close()") == 0) return C_close;
}
}
return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}

void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}

char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}

int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "call_shared_library_function.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>

{{out}}
<pre>
Same as C example depending on whether fakeimglib.so is present in the current directory or not.
</pre>


=={{header|zkl}}==
=={{header|zkl}}==