static int runRepl() { WrenVM* vm = wrenNewVM(reallocate); for (;;) { printf("> "); char line[MAX_LINE]; fgets(line, MAX_LINE, stdin); // TODO: Handle failure. wrenInterpret(vm, line); // TODO: Figure out how this should work with wren API. /* ObjFn* fn = compile(vm, line); if (fn != NULL) { Value result = interpret(vm, fn); printf("= "); printValue(result); printf("\n"); } */ } wrenFreeVM(vm); return 0; }
static void ensureOutsideForeign(WrenVM* vm) { // To test the behavior outside of a foreign method (which we're currently // in), create a new separate VM. WrenConfiguration config; wrenInitConfiguration(&config); WrenVM* otherVM = wrenNewVM(&config); int before = wrenGetSlotCount(otherVM); wrenEnsureSlots(otherVM, 20); int after = wrenGetSlotCount(otherVM); // Use the slots to make sure they're available. for (int i = 0; i < 20; i++) { wrenSetSlotDouble(otherVM, i, i); } int sum = 0; for (int i = 0; i < 20; i++) { sum += (int)wrenGetSlotDouble(otherVM, i); } wrenFreeVM(otherVM); char result[100]; sprintf(result, "%d -> %d (%d)", before, after, sum); wrenSetSlotString(vm, 0, result); }
static int runFile(const char* path) { char* source = readFile(path); WrenVM* vm = wrenNewVM(reallocate); int result = wrenInterpret(vm, source); wrenFreeVM(vm); free(source); return result; }
Wren::Wren() : vm_( nullptr ), foreignMethods_() { WrenConfiguration configuration{}; wrenInitConfiguration( &configuration ); configuration.bindForeignMethodFn = ForeignMethodProvider; configuration.loadModuleFn = LoadModuleFnWrapper; configuration.bindForeignClassFn = ForeignClassProvider; configuration.writeFn = WriteFnWrapper; vm_ = wrenNewVM( &configuration ); }
static void initVM() { WrenConfiguration config; wrenInitConfiguration(&config); config.bindForeignMethodFn = bindForeignMethod; config.bindForeignClassFn = bindForeignClass; config.loadModuleFn = readModule; config.writeFn = write; // Since we're running in a standalone process, be generous with memory. config.initialHeapSize = 1024 * 1024 * 100; vm = wrenNewVM(&config); // Initialize the event loop. loop = (uv_loop_t*)malloc(sizeof(uv_loop_t)); uv_loop_init(loop); }
int main(int argc, char *argv[]) { const char* s = argc > 1 ? argv[1] : DEFAULT_SCRIPT; const int c = argc > 2 ? atoi(argv[2]) : DEFAULT_COUNT; count += c; char* script = readFile(s); if (script == NULL) { fprintf(stderr, "Could not locate %s\n", s); return 1; } WrenConfiguration config; wrenInitConfiguration(&config); config.bindForeignMethodFn = bindForeignMethod; //config.bindForeignClassFn = bindForeignClass; //config.loadModuleFn = readModule; config.writeFn = write; config.errorFn = reportError; // Since we're running in a standalone process, be generous with memory. config.initialHeapSize = 1024 * 1024 * 100; WrenVM* vm = wrenNewVM(&config); //fprintf(stdout, "Found hello.wren\n%s\n", script); WrenInterpretResult result = wrenInterpret(vm, script); if (result == WREN_RESULT_SUCCESS) { printf("%lld\n", ts_stop - ts_start); } else { fprintf(stderr, "Error executing hello.wren %d\n", result); } wrenFreeVM(vm); free(script); return result; }