Lazy load optional libraries and let the host override them.

This commit is contained in:
Bob Nystrom
2016-03-17 07:49:43 -07:00
parent 984fa1ddd1
commit a5f95b90e9
6 changed files with 115 additions and 80 deletions
+10 -17
View File
@@ -25,29 +25,22 @@ void metaCompile(WrenVM* vm)
vm->apiStack[0] = fn != NULL ? OBJ_VAL(fn) : NULL_VAL;
}
static WrenForeignMethodFn bindMetaForeignMethods(WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature)
const char* wrenMetaSource()
{
return metaModuleSource;
}
WrenForeignMethodFn wrenMetaBindForeignMethod(WrenVM* vm,
const char* className,
bool isStatic,
const char* signature)
{
// There is only one foreign method in the meta module.
ASSERT(strcmp(module, "meta") == 0, "Should be in meta module.");
ASSERT(strcmp(className, "Meta") == 0, "Should be in Meta class.");
ASSERT(isStatic, "Should be static.");
ASSERT(strcmp(signature, "compile_(_)") == 0, "Should be compile method.");
return metaCompile;
}
void wrenLoadMetaModule(WrenVM* vm)
{
WrenBindForeignMethodFn previousBindFn = vm->config.bindForeignMethodFn;
vm->config.bindForeignMethodFn = bindMetaForeignMethods;
wrenInterpretInModule(vm, "meta", metaModuleSource);
vm->config.bindForeignMethodFn = previousBindFn;
}
#endif
+5 -1
View File
@@ -7,7 +7,11 @@
// This module defines the Meta class and its associated methods.
#if WREN_OPT_META
void wrenLoadMetaModule(WrenVM* vm);
const char* wrenMetaSource();
WrenForeignMethodFn wrenMetaBindForeignMethod(WrenVM* vm,
const char* className,
bool isStatic,
const char* signature);
#endif
+26 -38
View File
@@ -102,55 +102,43 @@ static void randomInt0(WrenVM* vm)
wrenSetSlotDouble(vm, 0, (double)advanceState(well));
}
// TODO: The way these are wired up is pretty verbose and tedious. Also, the
// CLI has its own separate way of handling this. Figure out something cleaner.
static WrenForeignMethodFn bindForeignMethods(WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature)
const char* wrenRandomSource()
{
ASSERT(strcmp(module, "random") == 0, "Should be in random module.");
ASSERT(strcmp(className, "Random") == 0, "Should be in Random class.");
if (strcmp(signature, "<allocate>") == 0) return randomAllocate;
if (strcmp(signature, "seed_()") == 0) return randomSeed0;
if (strcmp(signature, "seed_(_)") == 0) return randomSeed1;
if (strcmp(signature, "seed_(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)") == 0)
{
return randomSeed16;
}
if (strcmp(signature, "float()") == 0) return randomFloat;
if (strcmp(signature, "int()") == 0) return randomInt0;
ASSERT(false, "Unknown method.");
return NULL;
return randomModuleSource;
}
static WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module,
const char* className)
WrenForeignClassMethods wrenRandomBindForeignClass(WrenVM* vm,
const char* module,
const char* className)
{
ASSERT(strcmp(className, "Random") == 0, "Should be in Random class.");
WrenForeignClassMethods methods;
methods.allocate = randomAllocate;
methods.finalize = NULL;
return methods;
}
// TODO: Lots of duplication between here and meta.
void wrenLoadRandomModule(WrenVM* vm)
WrenForeignMethodFn wrenRandomBindForeignMethod(WrenVM* vm,
const char* className,
bool isStatic,
const char* signature)
{
WrenBindForeignMethodFn previousBindMethodFn = vm->config.bindForeignMethodFn;
vm->config.bindForeignMethodFn = bindForeignMethods;
WrenBindForeignClassFn previousBindClassFn = vm->config.bindForeignClassFn;
vm->config.bindForeignClassFn = bindForeignClass;
wrenInterpretInModule(vm, "random", randomModuleSource);
vm->config.bindForeignMethodFn = previousBindMethodFn;
vm->config.bindForeignClassFn = previousBindClassFn;
ASSERT(strcmp(className, "Random") == 0, "Should be in Random class.");
if (strcmp(signature, "<allocate>") == 0) return randomAllocate;
if (strcmp(signature, "seed_()") == 0) return randomSeed0;
if (strcmp(signature, "seed_(_)") == 0) return randomSeed1;
if (strcmp(signature, "seed_(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)") == 0)
{
return randomSeed16;
}
if (strcmp(signature, "float()") == 0) return randomFloat;
if (strcmp(signature, "int()") == 0) return randomInt0;
ASSERT(false, "Unknown method.");
return NULL;
}
#endif
+8 -1
View File
@@ -6,7 +6,14 @@
#if WREN_OPT_RANDOM
void wrenLoadRandomModule(WrenVM* vm);
const char* wrenRandomSource();
WrenForeignClassMethods wrenRandomBindForeignClass(WrenVM* vm,
const char* module,
const char* className);
WrenForeignMethodFn wrenRandomBindForeignMethod(WrenVM* vm,
const char* className,
bool isStatic,
const char* signature);
#endif