Fix tracking what should be 'global' through function calls?

This commit is contained in:
K. Lange 2021-01-07 10:39:09 +09:00
parent 902d2222b5
commit fbf4dda818
7 changed files with 14 additions and 6 deletions

View File

@ -138,7 +138,7 @@ static void initCompiler(Compiler * compiler, FunctionType type) {
compiler->type = type;
compiler->scopeDepth = 0;
compiler->function = krk_newFunction();
compiler->function->globalsContext = vm.globals;
compiler->function->globalsContext = (KrkInstance*)vm.module;
compiler->localCount = 0;
compiler->localsSpace = 8;
compiler->locals = GROW_ARRAY(Local,NULL,0,8);

View File

@ -130,6 +130,7 @@ static void blackenObject(KrkObj * object) {
krk_markObject((KrkObj*)function->name);
krk_markObject((KrkObj*)function->docstring);
krk_markObject((KrkObj*)function->chunk.filename);
krk_markObject((KrkObj*)function->globalsContext);
markArray(&function->requiredArgNames);
markArray(&function->keywordArgNames);
markArray(&function->chunk.constants);
@ -225,6 +226,7 @@ static void markRoots() {
krk_markObject((KrkObj*)upvalue);
}
krk_markObject((KrkObj*)vm.builtins);
krk_markObject((KrkObj*)vm.objectClass);
if (vm.module) krk_markObject((KrkObj*)vm.module);
krk_markTable(&vm.modules);
krk_markCompilerRoots();

View File

@ -2,7 +2,7 @@
let __doc__ = "I am a module."
def foo():
print("Hello, world:", __doc__)
print("Hello, world:", __name__, __doc__)
let x = 5
let y = 7

View File

@ -122,6 +122,7 @@ KrkClass * krk_newClass(KrkString * name) {
_class->_tostr = NULL;
_class->_call = NULL;
_class->_init = NULL;
_class->_eq = NULL;
return _class;
}

View File

@ -66,6 +66,8 @@ typedef struct {
KrkString * name;
} KrkLocalEntry;
struct KrkInstance;
typedef struct {
KrkObj obj;
short requiredArgs;
@ -81,7 +83,7 @@ typedef struct {
KrkLocalEntry * localNames;
unsigned char collectsArguments:1;
unsigned char collectsKeywords:1;
KrkTable * globalsContext;
struct KrkInstance * globalsContext;
} KrkFunction;
typedef struct {
@ -110,7 +112,7 @@ typedef struct KrkClass {
KrkObj * _eq;
} KrkClass;
typedef struct {
typedef struct KrkInstance {
KrkObj obj;
KrkClass * _class;
KrkTable fields;

View File

@ -0,0 +1,2 @@
import dummy
dummy.foo()

5
vm.c
View File

@ -1057,7 +1057,7 @@ _finishArg:
frame->ip = closure->function->chunk.code;
frame->slots = (vm.stackTop - argCount) - vm.stack;
frame->outSlots = (vm.stackTop - argCount - extra) - vm.stack;
vm.globals = closure->function->globalsContext;
vm.globals = &closure->function->globalsContext->fields;
return 1;
}
@ -3052,6 +3052,7 @@ static inline size_t readBytes(CallFrame * frame, int num) {
*/
static KrkValue run() {
CallFrame* frame = &vm.frames[vm.frameCount - 1];
vm.globals = &frame->closure->function->globalsContext->fields;
while (1) {
#ifdef ENABLE_TRACING
@ -3096,12 +3097,12 @@ static KrkValue run() {
return result;
}
vm.stackTop = &vm.stack[frame->outSlots];
vm.globals = &vm.frames[vm.frameCount - 1].closure->function->globalsContext->fields;
if (vm.frameCount == (size_t)vm.exitOnFrame) {
return result;
}
krk_push(result);
frame = &vm.frames[vm.frameCount - 1];
vm.globals = frame->closure->function->globalsContext;
break;
}
case OP_EQUAL: {