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, WrenForeignMethodFn wrenBindIOForeignMethod(WrenVM* vm, const char* className,
const char* signature) 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, "writeString_(_)") == 0) return ioWriteString;
if (strcmp(signature, "clock") == 0) return ioClock; if (strcmp(signature, "clock") == 0) return ioClock;

View File

@ -2,9 +2,13 @@
#if WREN_USE_LIB_META #if WREN_USE_LIB_META
#include <string.h>
// This string literal is generated automatically from meta.wren. Do not edit. // This string literal is generated automatically from meta.wren. Do not edit.
static const char* libSource = static const char* libSource =
"class Meta {}\n"; "class Meta {\n"
" foreign static eval(source)\n"
"}\n";
void metaEval(WrenVM* vm) void metaEval(WrenVM* vm)
{ {
@ -16,7 +20,16 @@ void metaEval(WrenVM* vm)
void wrenLoadMetaLibrary(WrenVM* vm) void wrenLoadMetaLibrary(WrenVM* vm)
{ {
wrenInterpret(vm, "", libSource); 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 #endif

View File

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

View File

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