Fix a couple of bugs in the REPL.

- Not sure what was going on, but fix #456. It makes more sense for the
  REPL to invoke the compiled code as a fiber than a function anyway.

- Flush stdout before reading from stdin since System.print() no longer
  does that automatically.
This commit is contained in:
Bob Nystrom
2017-10-13 07:58:57 -07:00
parent 97b2e1c818
commit 9661a5b999
12 changed files with 174 additions and 127 deletions
+9 -15
View File
@@ -9,30 +9,23 @@
void metaCompile(WrenVM* vm)
{
// Evaluate the code in the module where the calling function was defined.
// That's one stack frame back from the top since the top-most frame is the
// helper eval() method in Meta itself.
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);
// Compile it.
ObjFn* fn = wrenCompile(vm, module, source, isExpression, printErrors);
// TODO: Allow passing in module?
ObjFiber* fiber = wrenCompileSource(vm, "main", source,
isExpression, printErrors);
// Return the result. We can't use the public API for this since we have a
// bare ObjFn.
if (fn == NULL)
// bare ObjFiber*.
if (fiber == NULL)
{
vm->apiStack[0] = NULL_VAL;
}
else
{
wrenPushRoot(vm, (Obj*)fn);
vm->apiStack[0] = OBJ_VAL(wrenNewClosure(vm, fn));
wrenPopRoot(vm);
vm->apiStack[0] = OBJ_VAL(fiber);
}
}
@@ -60,7 +53,8 @@ void metaGetModuleVariables(WrenVM* vm) {
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);
names->elements.data[i] = wrenNewStringLength(vm,
name->buffer, name->length);
}
}
+8 -3
View File
@@ -10,11 +10,11 @@ class Meta {
static eval(source) {
if (!(source is String)) Fiber.abort("Source code must be a string.")
var fn = compile_(source, false, false)
var fiber = compile_(source, false, false)
// TODO: Include compile errors.
if (fn == null) Fiber.abort("Could not compile source code.")
if (fiber == null) Fiber.abort("Could not compile source code.")
Fiber.new(fn).call()
fiber.call()
}
static compileExpression(source) {
@@ -22,6 +22,11 @@ class Meta {
return compile_(source, true, true)
}
static compile(source) {
if (!(source is String)) Fiber.abort("Source code must be a string.")
return compile_(source, false, true)
}
foreign static compile_(source, isExpression, printErrors)
foreign static getModuleVariables_(module)
}
+8 -3
View File
@@ -12,11 +12,11 @@ static const char* metaModuleSource =
" static eval(source) {\n"
" if (!(source is String)) Fiber.abort(\"Source code must be a string.\")\n"
"\n"
" var fn = compile_(source, false, false)\n"
" var fiber = compile_(source, false, false)\n"
" // TODO: Include compile errors.\n"
" if (fn == null) Fiber.abort(\"Could not compile source code.\")\n"
" if (fiber == null) Fiber.abort(\"Could not compile source code.\")\n"
"\n"
" Fiber.new(fn).call()\n"
" fiber.call()\n"
" }\n"
"\n"
" static compileExpression(source) {\n"
@@ -24,6 +24,11 @@ static const char* metaModuleSource =
" return compile_(source, true, true)\n"
" }\n"
"\n"
" static compile(source) {\n"
" if (!(source is String)) Fiber.abort(\"Source code must be a string.\")\n"
" return compile_(source, false, true)\n"
" }\n"
"\n"
" foreign static compile_(source, isExpression, printErrors)\n"
" foreign static getModuleVariables_(module)\n"
"}\n";