Add an API to load a top-level variable into a slot.

This commit is contained in:
Bob Nystrom
2015-12-26 10:53:14 -08:00
parent 15043b897f
commit 1d16e85a85
8 changed files with 112 additions and 1 deletions
+44
View File
@@ -0,0 +1,44 @@
#include <string.h>
#include "get_variable.h"
static void beforeDefined(WrenVM* vm)
{
wrenGetVariable(vm, "main", "A", 0);
}
static void afterDefined(WrenVM* vm)
{
wrenGetVariable(vm, "main", "A", 0);
}
static void afterAssigned(WrenVM* vm)
{
wrenGetVariable(vm, "main", "A", 0);
}
static void otherSlot(WrenVM* vm)
{
wrenEnsureSlots(vm, 3);
wrenGetVariable(vm, "main", "B", 2);
// Move it into return position.
const char* string = wrenGetSlotString(vm, 2);
wrenSetSlotString(vm, 0, string);
}
static void otherModule(WrenVM* vm)
{
wrenGetVariable(vm, "get_variable_module", "Variable", 0);
}
WrenForeignMethodFn getVariableBindMethod(const char* signature)
{
if (strcmp(signature, "static GetVariable.beforeDefined()") == 0) return beforeDefined;
if (strcmp(signature, "static GetVariable.afterDefined()") == 0) return afterDefined;
if (strcmp(signature, "static GetVariable.afterAssigned()") == 0) return afterAssigned;
if (strcmp(signature, "static GetVariable.otherSlot()") == 0) return otherSlot;
if (strcmp(signature, "static GetVariable.otherModule()") == 0) return otherModule;
return NULL;
}
+3
View File
@@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn getVariableBindMethod(const char* signature);
+24
View File
@@ -0,0 +1,24 @@
import "get_variable_module"
class GetVariable {
foreign static beforeDefined()
foreign static afterDefined()
foreign static afterAssigned()
foreign static otherSlot()
foreign static otherModule()
}
System.print(GetVariable.beforeDefined()) // expect: null
var A = "a"
System.print(GetVariable.afterDefined()) // expect: a
A = "changed"
System.print(GetVariable.afterAssigned()) // expect: changed
var B = "b"
System.print(GetVariable.otherSlot()) // expect: b
System.print(GetVariable.otherModule()) // expect: value
+3
View File
@@ -0,0 +1,3 @@
// nontest
var Variable = "value"
+5 -1
View File
@@ -6,6 +6,7 @@
#include "benchmark.h"
#include "call.h"
#include "get_variable.h"
#include "foreign_class.h"
#include "lists.h"
#include "slots.h"
@@ -33,7 +34,10 @@ static WrenForeignMethodFn bindForeignMethod(
method = benchmarkBindMethod(fullName);
if (method != NULL) return method;
method = getVariableBindMethod(fullName);
if (method != NULL) return method;
method = foreignClassBindMethod(fullName);
if (method != NULL) return method;