#include "wren_opt_meta.h"
#if WREN_OPT_META
#include <string.h>
#include "wren_vm.h"
#include "wren_opt_meta.wren.inc"
void metaCompile(WrenVM* vm)
{
ObjClosure* caller = vm->fiber->frames[vm->fiber->numFrames - 2].closure;
ObjModule* module = caller->fn->module;
const char* source = wrenGetSlotString(vm, 1);
bool isExpression = wrenGetSlotBool(vm, 2);
bool printErrors = wrenGetSlotBool(vm, 3);
ObjFn* fn = wrenCompile(vm, module, source, isExpression, printErrors);
if (fn == NULL)
{
vm->apiStack[0] = NULL_VAL;
}
else
{
wrenPushRoot(vm, (Obj*)fn);
vm->apiStack[0] = OBJ_VAL(wrenNewClosure(vm, fn));
wrenPopRoot(vm);
}
}
void metaGetModuleVariables(WrenVM* vm) {
wrenEnsureSlots(vm, 3);
Value moduleValue = wrenMapGet(vm->modules, vm->apiStack[1]);
if (IS_UNDEFINED(moduleValue))
{
vm->apiStack[0] = NULL_VAL;
return;
}
ObjModule* module = AS_MODULE(moduleValue);
ObjList* names = wrenNewList(vm, module->variableNames.count);
vm->apiStack[0] = OBJ_VAL(names);
for (int i = 0; i < names->elements.count; i++)
{
names->elements.data[i] = NULL_VAL;
}
for (int i = 0; i < names->elements.count; i++)
{
String* name = &module->variableNames.data[i];
names->elements.data[i] = wrenNewString(vm, name->buffer, name->length);
}
}
const char* wrenMetaSource()
{
return metaModuleSource;
}
WrenForeignMethodFn wrenMetaBindForeignMethod(WrenVM* vm,
const char* className,
bool isStatic,
const char* signature)
{
ASSERT(strcmp(className, "Meta") == 0, "Should be in Meta class.");
ASSERT(isStatic, "Should be static.");
if (strcmp(signature, "compile_(_,_,_)") == 0)
{
return metaCompile;
}
if (strcmp(signature, "getModuleVariables_(_)") == 0)
{
return metaGetModuleVariables;
}
ASSERT(false, "Unknown method.");
return NULL;
}
#endif