mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 06:08:41 +01:00
add wrenHasVariable and wrenHasModule
This commit is contained in:
@ -32,6 +32,25 @@ static void otherModule(WrenVM* vm)
|
||||
wrenGetVariable(vm, "./test/api/get_variable_module", "Variable", 0);
|
||||
}
|
||||
|
||||
static void hasVariable(WrenVM* vm)
|
||||
{
|
||||
const char* module = wrenGetSlotString(vm, 1);
|
||||
const char* variable = wrenGetSlotString(vm, 2);
|
||||
|
||||
bool result = wrenHasVariable(vm, module, variable);
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenSetSlotBool(vm, 0, result);
|
||||
}
|
||||
|
||||
static void hasModule(WrenVM* vm)
|
||||
{
|
||||
const char* module = wrenGetSlotString(vm, 1);
|
||||
|
||||
bool result = wrenHasModule(vm, module);
|
||||
wrenEnsureSlots(vm, 1);
|
||||
wrenSetSlotBool(vm, 0, result);
|
||||
}
|
||||
|
||||
WrenForeignMethodFn getVariableBindMethod(const char* signature)
|
||||
{
|
||||
if (strcmp(signature, "static GetVariable.beforeDefined()") == 0) return beforeDefined;
|
||||
@ -39,6 +58,9 @@ WrenForeignMethodFn getVariableBindMethod(const char* signature)
|
||||
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;
|
||||
|
||||
if (strcmp(signature, "static Has.variable(_,_)") == 0) return hasVariable;
|
||||
if (strcmp(signature, "static Has.module(_)") == 0) return hasModule;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -8,6 +8,11 @@ class GetVariable {
|
||||
foreign static otherModule()
|
||||
}
|
||||
|
||||
class Has {
|
||||
foreign static variable(module, variable)
|
||||
foreign static module(module)
|
||||
}
|
||||
|
||||
System.print(GetVariable.beforeDefined()) // expect: null
|
||||
|
||||
var A = "a"
|
||||
@ -22,3 +27,13 @@ var B = "b"
|
||||
System.print(GetVariable.otherSlot()) // expect: b
|
||||
|
||||
System.print(GetVariable.otherModule()) // expect: value
|
||||
|
||||
|
||||
System.print(Has.variable("./test/api/get_variable_module", "Variable")) // expect: true
|
||||
System.print(Has.variable("./test/api/get_variable_module", "NotAVariable")) // expect: false
|
||||
System.print(Has.variable("./test/api/get_variable", "Has")) // expect: true
|
||||
System.print(Has.variable("./test/api/get_variable", "Fake")) // expect: false
|
||||
|
||||
System.print(Has.module("./test/api/get_variable_module")) // expect: true
|
||||
System.print(Has.module("./test/api/get_variable")) // expect: true
|
||||
System.print(Has.module("not a module")) // expect: false
|
||||
Reference in New Issue
Block a user