mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-16 20:28:04 +01:00
Get Meta working with foreign methods.
This commit is contained in:
@ -1 +1,3 @@
|
||||
class Meta {}
|
||||
class Meta {
|
||||
foreign static eval(source)
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -9,6 +9,10 @@
|
||||
|
||||
void wrenLoadMetaLibrary(WrenVM* vm);
|
||||
|
||||
WrenForeignMethodFn wrenBindMetaForeignMethod(WrenVM* vm,
|
||||
const char* className,
|
||||
const char* signature);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user