Task: Starting a web browser

C: 

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



void C_fileAllocate(WrenVM* vm) {
    FILE** pfp = (FILE**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(FILE*));
    const char *filename = wrenGetSlotString(vm, 1);
    const char *mode = wrenGetSlotString(vm, 2);
    *pfp = fopen(filename, mode);
}

void C_write(WrenVM* vm) {
    FILE* fp = *(FILE**)wrenGetSlotForeign(vm, 0);
    const char *s = wrenGetSlotString(vm, 1);
    fputs(s, fp);
}

void C_close(WrenVM* vm) {
    FILE* fp = *(FILE**)wrenGetSlotForeign(vm, 0);
    fclose(fp);
}

void C_remove(WrenVM* vm) {
    const char *filename = wrenGetSlotString(vm, 1);
    remove(filename);
}

void C_flushAll(WrenVM* vm) {
    fflush(NULL);
}

void C_system(WrenVM* vm) {
    const char *s = wrenGetSlotString(vm, 1);
    system(s);
}

void C_sleep(WrenVM* vm) {
    int seconds = (int)wrenGetSlotDouble(vm, 1);
    sleep(seconds);
}

WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
    WrenForeignClassMethods methods;
    methods.allocate = NULL;
    methods.finalize = NULL;
    if (strcmp(module, "main") == 0) {
        if (strcmp(className, "File") == 0) {
            methods.allocate = C_fileAllocate;
        }
    }
    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, "File") == 0) {
            if (!isStatic && strcmp(signature, "write(_)") == 0)   return C_write;
            if (!isStatic && strcmp(signature, "close()") == 0)    return C_close;
            if ( isStatic && strcmp(signature, "remove(_)") == 0)  return C_remove;
            if ( isStatic && strcmp(signature, "flushAll()") == 0) return C_flushAll;
        } else if (strcmp(className, "C") == 0) {
            if ( isStatic && strcmp(signature, "system(_)") == 0)  return C_system;
            if ( isStatic && strcmp(signature, "sleep(_)") == 0)   return C_sleep;
        }
    }
    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;
}

static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
    if( result.source) free((void*)result.source);
}

WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
    WrenLoadModuleResult result = {0};
    if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
        result.onComplete = loadModuleComplete;
        char fullName[strlen(name) + 6];
        strcpy(fullName, name);
        strcat(fullName, ".wren");
        result.source = readFile(fullName);
    }
    return result;
}

int main(int argc, char **argv) {
    WrenConfiguration config;
    wrenInitConfiguration(&config);
    config.writeFn = &writeFn;
    config.errorFn = &errorFn;
    config.bindForeignClassFn = &bindForeignClass;
    config.bindForeignMethodFn = &bindForeignMethod;
    config.loadModuleFn = &loadModule;
    WrenVM* vm = wrenNewVM(&config);
    const char* module = "main";
    const char* fileName = "starting_web_browser.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;
}