diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index 33de9b05..faf26820 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -49,18 +49,19 @@ void wrenInitConfiguration(WrenConfiguration* config) WrenVM* wrenNewVM(WrenConfiguration* config) { - WrenVM* vm; + WrenReallocateFn reallocate = defaultReallocate; + if (config != NULL) reallocate = config->reallocateFn; + + WrenVM* vm = (WrenVM*)reallocate(NULL, sizeof(*vm)); + memset(vm, 0, sizeof(WrenVM)); + // Copy the configuration if given one. if (config != NULL) { - vm = (WrenVM*)config->reallocateFn(NULL, sizeof(*vm)); - memset(vm, 0, sizeof(WrenVM)); memcpy(&vm->config, config, sizeof(WrenConfiguration)); } else { - vm = (WrenVM*)defaultReallocate(NULL, sizeof(*vm)); - memset(vm, 0, sizeof(WrenVM)); wrenInitConfiguration(&vm->config); } @@ -68,8 +69,7 @@ WrenVM* wrenNewVM(WrenConfiguration* config) vm->grayCount = 0; // TODO: Tune this. vm->grayCapacity = 4; - vm->gray = (Obj**)vm->config.reallocateFn(NULL, - vm->grayCapacity * sizeof(Obj*)); + vm->gray = (Obj**)reallocate(NULL, vm->grayCapacity * sizeof(Obj*)); vm->nextGC = vm->config.initialHeapSize; wrenSymbolTableInit(&vm->methodNames); diff --git a/test/api/main.c b/test/api/main.c index 1a4b94ef..752581a4 100644 --- a/test/api/main.c +++ b/test/api/main.c @@ -9,6 +9,7 @@ #include "get_variable.h" #include "foreign_class.h" #include "lists.h" +#include "new_vm.h" #include "slots.h" #include "value.h" @@ -44,6 +45,9 @@ static WrenForeignMethodFn bindForeignMethod( method = listsBindMethod(fullName); if (method != NULL) return method; + method = newVMBindMethod(fullName); + if (method != NULL) return method; + method = slotsBindMethod(fullName); if (method != NULL) return method; diff --git a/test/api/new_vm.c b/test/api/new_vm.c new file mode 100644 index 00000000..4901a8c5 --- /dev/null +++ b/test/api/new_vm.c @@ -0,0 +1,21 @@ +#include + +#include "new_vm.h" + +static void nullConfig(WrenVM* vm) +{ + WrenVM* otherVM = wrenNewVM(NULL); + + // We should be able to execute code. + WrenInterpretResult result = wrenInterpret(otherVM, "1 + 2"); + wrenSetSlotBool(vm, 0, result == WREN_RESULT_SUCCESS); + + wrenFreeVM(otherVM); +} + +WrenForeignMethodFn newVMBindMethod(const char* signature) +{ + if (strcmp(signature, "static VM.nullConfig()") == 0) return nullConfig; + + return NULL; +} diff --git a/test/api/new_vm.h b/test/api/new_vm.h new file mode 100644 index 00000000..9531a1de --- /dev/null +++ b/test/api/new_vm.h @@ -0,0 +1,3 @@ +#include "wren.h" + +WrenForeignMethodFn newVMBindMethod(const char* signature); diff --git a/test/api/new_vm.wren b/test/api/new_vm.wren new file mode 100644 index 00000000..451c81e2 --- /dev/null +++ b/test/api/new_vm.wren @@ -0,0 +1,6 @@ +class VM { + foreign static nullConfig() +} +// TODO: Other configuration settings. + +System.print(VM.nullConfig()) // expect: true diff --git a/util/xcode/wren.xcodeproj/project.pbxproj b/util/xcode/wren.xcodeproj/project.pbxproj index 7d129dc9..001c1346 100644 --- a/util/xcode/wren.xcodeproj/project.pbxproj +++ b/util/xcode/wren.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ 29A4273A1BDBE435001E6E22 /* wren_opt_random.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29A427331BDBE435001E6E22 /* wren_opt_random.wren.inc */; }; 29A4273B1BDBE435001E6E22 /* wren_opt_random.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29A427331BDBE435001E6E22 /* wren_opt_random.wren.inc */; }; 29C8A9331AB71FFF00DEC81D /* vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C8A9311AB71FFF00DEC81D /* vm.c */; }; + 29C946981C88F14F00B4A4F3 /* new_vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C946961C88F14F00B4A4F3 /* new_vm.c */; }; 29D025E31C19CD1000A3BB28 /* process.c in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E01C19CD1000A3BB28 /* process.c */; }; 29D025E41C19CD1000A3BB28 /* process.c in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E01C19CD1000A3BB28 /* process.c */; }; 29D025E51C19CD1000A3BB28 /* process.wren.inc in Sources */ = {isa = PBXBuildFile; fileRef = 29D025E21C19CD1000A3BB28 /* process.wren.inc */; }; @@ -125,6 +126,8 @@ 29AB1F061816E3AD004B501E /* wren */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = wren; sourceTree = BUILT_PRODUCTS_DIR; }; 29C8A9311AB71FFF00DEC81D /* vm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vm.c; path = ../../src/cli/vm.c; sourceTree = ""; }; 29C8A9321AB71FFF00DEC81D /* vm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vm.h; path = ../../src/cli/vm.h; sourceTree = ""; }; + 29C946961C88F14F00B4A4F3 /* new_vm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = new_vm.c; path = ../../test/api/new_vm.c; sourceTree = ""; }; + 29C946971C88F14F00B4A4F3 /* new_vm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = new_vm.h; path = ../../test/api/new_vm.h; sourceTree = ""; }; 29D009A61B7E3993000CE58C /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = main.c; path = ../../test/api/main.c; sourceTree = ""; }; 29D009A81B7E39A8000CE58C /* foreign_class.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = foreign_class.c; path = ../../test/api/foreign_class.c; sourceTree = ""; }; 29D009A91B7E39A8000CE58C /* foreign_class.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = foreign_class.h; path = ../../test/api/foreign_class.h; sourceTree = ""; }; @@ -269,6 +272,8 @@ 2949AA8C1C2F14F000B106BA /* get_variable.h */, 29932D521C210F8D00099DEE /* lists.c */, 29932D531C210F8D00099DEE /* lists.h */, + 29C946961C88F14F00B4A4F3 /* new_vm.c */, + 29C946971C88F14F00B4A4F3 /* new_vm.h */, 29D009AA1B7E39A8000CE58C /* slots.c */, 29D009AB1B7E39A8000CE58C /* slots.h */, 29D009AC1B7E39A8000CE58C /* value.c */, @@ -383,6 +388,7 @@ 29729F321BA70A620099CA20 /* io.c in Sources */, 29932D541C210F8D00099DEE /* lists.c in Sources */, 291647C81BA5EC5E006142EE /* modules.c in Sources */, + 29C946981C88F14F00B4A4F3 /* new_vm.c in Sources */, 2949AA8D1C2F14F000B106BA /* get_variable.c in Sources */, 29DC14A11BBA2FEC008A8274 /* scheduler.c in Sources */, 29A427391BDBE435001E6E22 /* wren_opt_random.c in Sources */,