From fbf4dda8189c99345458f60c85fb308d07389dd2 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 7 Jan 2021 10:39:09 +0900 Subject: [PATCH] Fix tracking what should be 'global' through function calls? --- compiler.c | 2 +- memory.c | 2 ++ modules/dummy.krk | 2 +- object.c | 1 + object.h | 6 ++++-- test/testImportContext.krk | 2 ++ vm.c | 5 +++-- 7 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 test/testImportContext.krk diff --git a/compiler.c b/compiler.c index 3d030d4..c2f9f5d 100644 --- a/compiler.c +++ b/compiler.c @@ -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); diff --git a/memory.c b/memory.c index 4401279..caaed9c 100644 --- a/memory.c +++ b/memory.c @@ -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(); diff --git a/modules/dummy.krk b/modules/dummy.krk index 21fc7dc..1b293b2 100644 --- a/modules/dummy.krk +++ b/modules/dummy.krk @@ -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 diff --git a/object.c b/object.c index 762d4c2..96a8000 100644 --- a/object.c +++ b/object.c @@ -122,6 +122,7 @@ KrkClass * krk_newClass(KrkString * name) { _class->_tostr = NULL; _class->_call = NULL; _class->_init = NULL; + _class->_eq = NULL; return _class; } diff --git a/object.h b/object.h index 6289855..c8b03c1 100644 --- a/object.h +++ b/object.h @@ -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; diff --git a/test/testImportContext.krk b/test/testImportContext.krk new file mode 100644 index 0000000..df2d7b5 --- /dev/null +++ b/test/testImportContext.krk @@ -0,0 +1,2 @@ +import dummy +dummy.foo() diff --git a/vm.c b/vm.c index 8979f14..5d582b2 100644 --- a/vm.c +++ b/vm.c @@ -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: {