1
0
forked from Mirror/wren

WrenValue -> WrenHandle.

This commit is contained in:
Bob Nystrom
2016-05-20 20:55:28 -07:00
parent 5d98d20175
commit 0a060a9678
22 changed files with 235 additions and 266 deletions

View File

@ -32,11 +32,11 @@ static void call(WrenVM* vm)
wrenInterpret(otherVM, testScript);
WrenValue* method = wrenMakeCallHandle(otherVM, "method(_,_,_,_)");
WrenHandle* method = wrenMakeCallHandle(otherVM, "method(_,_,_,_)");
wrenEnsureSlots(otherVM, 1);
wrenGetVariable(otherVM, "main", "Test", 0);
WrenValue* testClass = wrenGetSlotValue(otherVM, 0);
WrenHandle* testClass = wrenGetSlotHandle(otherVM, 0);
double startTime = (double)clock() / CLOCKS_PER_SEC;
@ -44,7 +44,7 @@ static void call(WrenVM* vm)
for (int i = 0; i < iterations; i++)
{
wrenEnsureSlots(otherVM, 5);
wrenSetSlotValue(otherVM, 0, testClass);
wrenSetSlotHandle(otherVM, 0, testClass);
wrenSetSlotDouble(otherVM, 1, 1.0);
wrenSetSlotDouble(otherVM, 2, 2.0);
wrenSetSlotDouble(otherVM, 3, 3.0);
@ -57,8 +57,8 @@ static void call(WrenVM* vm)
double elapsed = (double)clock() / CLOCKS_PER_SEC - startTime;
wrenReleaseValue(otherVM, testClass);
wrenReleaseValue(otherVM, method);
wrenReleaseHandle(otherVM, testClass);
wrenReleaseHandle(otherVM, method);
wrenFreeVM(otherVM);
if (result == (1.0 + 2.0 + 3.0 + 4.0) * iterations)

View File

@ -7,117 +7,117 @@ void callRunTests(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "main", "Call", 0);
WrenValue* callClass = wrenGetSlotValue(vm, 0);
WrenHandle* callClass = wrenGetSlotHandle(vm, 0);
WrenValue* noParams = wrenMakeCallHandle(vm, "noParams");
WrenValue* zero = wrenMakeCallHandle(vm, "zero()");
WrenValue* one = wrenMakeCallHandle(vm, "one(_)");
WrenValue* two = wrenMakeCallHandle(vm, "two(_,_)");
WrenValue* unary = wrenMakeCallHandle(vm, "-");
WrenValue* binary = wrenMakeCallHandle(vm, "-(_)");
WrenValue* subscript = wrenMakeCallHandle(vm, "[_,_]");
WrenValue* subscriptSet = wrenMakeCallHandle(vm, "[_,_]=(_)");
WrenHandle* noParams = wrenMakeCallHandle(vm, "noParams");
WrenHandle* zero = wrenMakeCallHandle(vm, "zero()");
WrenHandle* one = wrenMakeCallHandle(vm, "one(_)");
WrenHandle* two = wrenMakeCallHandle(vm, "two(_,_)");
WrenHandle* unary = wrenMakeCallHandle(vm, "-");
WrenHandle* binary = wrenMakeCallHandle(vm, "-(_)");
WrenHandle* subscript = wrenMakeCallHandle(vm, "[_,_]");
WrenHandle* subscriptSet = wrenMakeCallHandle(vm, "[_,_]=(_)");
// Different arity.
wrenEnsureSlots(vm, 1);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenCall(vm, noParams);
wrenEnsureSlots(vm, 1);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenCall(vm, zero);
wrenEnsureSlots(vm, 2);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.0);
wrenCall(vm, one);
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.0);
wrenSetSlotDouble(vm, 2, 2.0);
wrenCall(vm, two);
// Operators.
wrenEnsureSlots(vm, 1);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenCall(vm, unary);
wrenEnsureSlots(vm, 2);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.0);
wrenCall(vm, binary);
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.0);
wrenSetSlotDouble(vm, 2, 2.0);
wrenCall(vm, subscript);
wrenEnsureSlots(vm, 4);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.0);
wrenSetSlotDouble(vm, 2, 2.0);
wrenSetSlotDouble(vm, 3, 3.0);
wrenCall(vm, subscriptSet);
// Returning a value.
WrenValue* getValue = wrenMakeCallHandle(vm, "getValue()");
WrenHandle* getValue = wrenMakeCallHandle(vm, "getValue()");
wrenEnsureSlots(vm, 1);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenCall(vm, getValue);
WrenValue* value = wrenGetSlotValue(vm, 0);
WrenHandle* value = wrenGetSlotHandle(vm, 0);
// Different argument types.
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotBool(vm, 1, true);
wrenSetSlotBool(vm, 2, false);
wrenCall(vm, two);
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotDouble(vm, 1, 1.2);
wrenSetSlotDouble(vm, 2, 3.4);
wrenCall(vm, two);
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotString(vm, 1, "string");
wrenSetSlotString(vm, 2, "another");
wrenCall(vm, two);
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotNull(vm, 1);
wrenSetSlotValue(vm, 2, value);
wrenSetSlotHandle(vm, 2, value);
wrenCall(vm, two);
// Truncate a string, or allow null bytes.
wrenEnsureSlots(vm, 3);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
wrenSetSlotBytes(vm, 1, "string", 3);
wrenSetSlotBytes(vm, 2, "b\0y\0t\0e", 7);
wrenCall(vm, two);
// Call ignores with extra temporary slots on stack.
wrenEnsureSlots(vm, 10);
wrenSetSlotValue(vm, 0, callClass);
wrenSetSlotHandle(vm, 0, callClass);
for (int i = 1; i < 10; i++)
{
wrenSetSlotDouble(vm, i, i * 0.1);
}
wrenCall(vm, one);
wrenReleaseValue(vm, callClass);
wrenReleaseValue(vm, noParams);
wrenReleaseValue(vm, zero);
wrenReleaseValue(vm, one);
wrenReleaseValue(vm, two);
wrenReleaseValue(vm, getValue);
wrenReleaseValue(vm, value);
wrenReleaseValue(vm, unary);
wrenReleaseValue(vm, binary);
wrenReleaseValue(vm, subscript);
wrenReleaseValue(vm, subscriptSet);
wrenReleaseHandle(vm, callClass);
wrenReleaseHandle(vm, noParams);
wrenReleaseHandle(vm, zero);
wrenReleaseHandle(vm, one);
wrenReleaseHandle(vm, two);
wrenReleaseHandle(vm, getValue);
wrenReleaseHandle(vm, value);
wrenReleaseHandle(vm, unary);
wrenReleaseHandle(vm, binary);
wrenReleaseHandle(vm, subscript);
wrenReleaseHandle(vm, subscriptSet);
}

24
test/api/handle.c Normal file
View File

@ -0,0 +1,24 @@
#include <string.h>
#include "handle.h"
static WrenHandle* handle;
static void setValue(WrenVM* vm)
{
handle = wrenGetSlotHandle(vm, 1);
}
static void getValue(WrenVM* vm)
{
wrenSetSlotHandle(vm, 0, handle);
wrenReleaseHandle(vm, handle);
}
WrenForeignMethodFn handleBindMethod(const char* signature)
{
if (strcmp(signature, "static Handle.value=(_)") == 0) return setValue;
if (strcmp(signature, "static Handle.value") == 0) return getValue;
return NULL;
}

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

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

11
test/api/handle.wren Normal file
View File

@ -0,0 +1,11 @@
class Handle {
foreign static value=(value)
foreign static value
}
Handle.value = ["list", "of", "strings"]
// Make sure the handle lives through a GC.
System.gc()
System.print(Handle.value) // expect: [list, of, strings]

View File

@ -8,10 +8,10 @@
#include "call.h"
#include "get_variable.h"
#include "foreign_class.h"
#include "handle.h"
#include "lists.h"
#include "new_vm.h"
#include "slots.h"
#include "value.h"
// The name of the currently executing API test.
const char* testName;
@ -42,6 +42,9 @@ static WrenForeignMethodFn bindForeignMethod(
method = foreignClassBindMethod(fullName);
if (method != NULL) return method;
method = handleBindMethod(fullName);
if (method != NULL) return method;
method = listsBindMethod(fullName);
if (method != NULL) return method;
@ -50,9 +53,6 @@ static WrenForeignMethodFn bindForeignMethod(
method = slotsBindMethod(fullName);
if (method != NULL) return method;
method = valueBindMethod(fullName);
if (method != NULL) return method;
fprintf(stderr,
"Unknown foreign method '%s' for test '%s'\n", fullName, testName);

View File

@ -22,13 +22,13 @@ static void getSlots(WrenVM* vm)
if (wrenGetSlotDouble(vm, 3) != 12.34) result = false;
if (strcmp(wrenGetSlotString(vm, 4), "str") != 0) result = false;
WrenValue* value = wrenGetSlotValue(vm, 5);
WrenHandle* handle = wrenGetSlotHandle(vm, 5);
if (result)
{
// Otherwise, return the value so we can tell if we captured it correctly.
wrenSetSlotValue(vm, 0, value);
wrenReleaseValue(vm, value);
wrenSetSlotHandle(vm, 0, handle);
wrenReleaseHandle(vm, handle);
}
else
{
@ -39,7 +39,7 @@ static void getSlots(WrenVM* vm)
static void setSlots(WrenVM* vm)
{
WrenValue* value = wrenGetSlotValue(vm, 1);
WrenHandle* handle = wrenGetSlotHandle(vm, 1);
wrenSetSlotBool(vm, 1, true);
wrenSetSlotBytes(vm, 2, "by\0te", 5);
@ -64,8 +64,8 @@ static void setSlots(WrenVM* vm)
if (result)
{
// Move the value into the return position.
wrenSetSlotValue(vm, 0, value);
wrenReleaseValue(vm, value);
wrenSetSlotHandle(vm, 0, handle);
wrenReleaseHandle(vm, handle);
}
else
{

View File

@ -1,24 +0,0 @@
#include <string.h>
#include "value.h"
static WrenValue* value;
static void setValue(WrenVM* vm)
{
value = wrenGetSlotValue(vm, 1);
}
static void getValue(WrenVM* vm)
{
wrenSetSlotValue(vm, 0, value);
wrenReleaseValue(vm, value);
}
WrenForeignMethodFn valueBindMethod(const char* signature)
{
if (strcmp(signature, "static Value.value=(_)") == 0) return setValue;
if (strcmp(signature, "static Value.value") == 0) return getValue;
return NULL;
}

View File

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

View File

@ -1,14 +0,0 @@
class Value {
foreign static value=(value)
foreign static value
}
Value.value = ["list", "of", "strings"]
// Do some stuff to trigger a GC (at least when GC stress testing enabled).
var s = "string"
for (i in 1...10) {
s = s + " more"
}
System.print(Value.value) // expect: [list, of, strings]