Get Meta working with foreign methods.

This commit is contained in:
Bob Nystrom
2015-03-25 20:20:26 -07:00
parent 8dcd336638
commit 9c414b5da5
5 changed files with 40 additions and 13 deletions

View File

@ -1 +1,3 @@
class Meta {}
class Meta {
foreign static eval(source)
}

View File

@ -148,7 +148,7 @@ void wrenLoadIOLibrary(WrenVM* vm)
WrenForeignMethodFn wrenBindIOForeignMethod(WrenVM* vm, const char* className,
const char* signature)
{
ASSERT(strcmp(className, "IO") == 0, "Should only have IO class.");
if (strcmp(className, "IO") != 0) return NULL;
if (strcmp(signature, "writeString_(_)") == 0) return ioWriteString;
if (strcmp(signature, "clock") == 0) return ioClock;

View File

@ -2,9 +2,13 @@
#if WREN_USE_LIB_META
#include <string.h>
// This string literal is generated automatically from meta.wren. Do not edit.
static const char* libSource =
"class Meta {}\n";
"class Meta {\n"
" foreign static eval(source)\n"
"}\n";
void metaEval(WrenVM* vm)
{
@ -16,7 +20,16 @@ void metaEval(WrenVM* vm)
void wrenLoadMetaLibrary(WrenVM* vm)
{
wrenInterpret(vm, "", libSource);
wrenDefineStaticMethod(vm, "Meta", "eval(_)", metaEval);
}
WrenForeignMethodFn wrenBindMetaForeignMethod(WrenVM* vm,
const char* className,
const char* signature)
{
if (strcmp(className, "Meta") != 0) return NULL;
if (strcmp(signature, "eval(_)") == 0) return metaEval;
return NULL;
}
#endif

View File

@ -9,6 +9,10 @@
void wrenLoadMetaLibrary(WrenVM* vm);
WrenForeignMethodFn wrenBindMetaForeignMethod(WrenVM* vm,
const char* className,
const char* signature);
#endif
#endif

View File

@ -60,15 +60,15 @@ WrenVM* wrenNewVM(WrenConfiguration* configuration)
vm->heapScalePercent = 100 + configuration->heapGrowthPercent;
}
ObjString* name = AS_STRING(CONST_STRING(vm, "main"));
ObjString* name = AS_STRING(CONST_STRING(vm, "core"));
wrenPushRoot(vm, (Obj*)name);
// Implicitly create a "main" module for the REPL or entry script.
ObjModule* mainModule = wrenNewModule(vm, name);
wrenPushRoot(vm, (Obj*)mainModule);
// Implicitly create a "core" module for the built in libraries.
ObjModule* coreModule = wrenNewModule(vm, name);
wrenPushRoot(vm, (Obj*)coreModule);
vm->modules = wrenNewMap(vm);
wrenMapSet(vm, vm->modules, NULL_VAL, OBJ_VAL(mainModule));
wrenMapSet(vm, vm->modules, NULL_VAL, OBJ_VAL(coreModule));
wrenPopRoot(vm); // mainModule.
wrenPopRoot(vm); // name.
@ -295,10 +295,18 @@ static WrenForeignMethodFn findForeignMethod(WrenVM* vm,
}
// Otherwise, try the built-in libraries.
#if WREN_USE_LIB_IO
fn = wrenBindIOForeignMethod(vm, className, signature);
if (fn != NULL) return fn;
#endif
if (strcmp(moduleName, "core") == 0)
{
#if WREN_USE_LIB_IO
fn = wrenBindIOForeignMethod(vm, className, signature);
if (fn != NULL) return fn;
#endif
#if WREN_USE_LIB_META
fn = wrenBindMetaForeignMethod(vm, className, signature);
if (fn != NULL) return fn;
#endif
}
// TODO: Report a runtime error on failure to find it.
return NULL;