1
0
forked from Mirror/wren

Add a test for creating a VM without a config.

This commit is contained in:
Bob Nystrom
2016-03-03 14:31:47 -08:00
parent 41e30f47d8
commit 84bcb5801c
6 changed files with 47 additions and 7 deletions

View File

@ -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);

View File

@ -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;

21
test/api/new_vm.c Normal file
View File

@ -0,0 +1,21 @@
#include <string.h>
#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;
}

3
test/api/new_vm.h Normal file
View File

@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn newVMBindMethod(const char* signature);

6
test/api/new_vm.wren Normal file
View File

@ -0,0 +1,6 @@
class VM {
foreign static nullConfig()
}
// TODO: Other configuration settings.
System.print(VM.nullConfig()) // expect: true

View File

@ -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 = "<group>"; };
29C8A9321AB71FFF00DEC81D /* vm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vm.h; path = ../../src/cli/vm.h; sourceTree = "<group>"; };
29C946961C88F14F00B4A4F3 /* new_vm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = new_vm.c; path = ../../test/api/new_vm.c; sourceTree = "<group>"; };
29C946971C88F14F00B4A4F3 /* new_vm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = new_vm.h; path = ../../test/api/new_vm.h; sourceTree = "<group>"; };
29D009A61B7E3993000CE58C /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = main.c; path = ../../test/api/main.c; sourceTree = "<group>"; };
29D009A81B7E39A8000CE58C /* foreign_class.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = foreign_class.c; path = ../../test/api/foreign_class.c; sourceTree = "<group>"; };
29D009A91B7E39A8000CE58C /* foreign_class.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = foreign_class.h; path = ../../test/api/foreign_class.h; sourceTree = "<group>"; };
@ -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 */,