Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
/* 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()
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
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++;
|
||||
}
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
/* 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue