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->type = type;
compiler->scopeDepth = 0; compiler->scopeDepth = 0;
compiler->function = krk_newFunction(); compiler->function = krk_newFunction();
compiler->function->globalsContext = vm.globals; compiler->function->globalsContext = (KrkInstance*)vm.module;
compiler->localCount = 0; compiler->localCount = 0;
compiler->localsSpace = 8; compiler->localsSpace = 8;
compiler->locals = GROW_ARRAY(Local,NULL,0,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->name);
krk_markObject((KrkObj*)function->docstring); krk_markObject((KrkObj*)function->docstring);
krk_markObject((KrkObj*)function->chunk.filename); krk_markObject((KrkObj*)function->chunk.filename);
krk_markObject((KrkObj*)function->globalsContext);
markArray(&function->requiredArgNames); markArray(&function->requiredArgNames);
markArray(&function->keywordArgNames); markArray(&function->keywordArgNames);
markArray(&function->chunk.constants); markArray(&function->chunk.constants);
@ -225,6 +226,7 @@ static void markRoots() {
krk_markObject((KrkObj*)upvalue); krk_markObject((KrkObj*)upvalue);
} }
krk_markObject((KrkObj*)vm.builtins); krk_markObject((KrkObj*)vm.builtins);
krk_markObject((KrkObj*)vm.objectClass);
if (vm.module) krk_markObject((KrkObj*)vm.module); if (vm.module) krk_markObject((KrkObj*)vm.module);
krk_markTable(&vm.modules); krk_markTable(&vm.modules);
krk_markCompilerRoots(); krk_markCompilerRoots();

View File

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

View File

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

View File

@ -66,6 +66,8 @@ typedef struct {
KrkString * name; KrkString * name;
} KrkLocalEntry; } KrkLocalEntry;
struct KrkInstance;
typedef struct { typedef struct {
KrkObj obj; KrkObj obj;
short requiredArgs; short requiredArgs;
@ -81,7 +83,7 @@ typedef struct {
KrkLocalEntry * localNames; KrkLocalEntry * localNames;
unsigned char collectsArguments:1; unsigned char collectsArguments:1;
unsigned char collectsKeywords:1; unsigned char collectsKeywords:1;
KrkTable * globalsContext; struct KrkInstance * globalsContext;
} KrkFunction; } KrkFunction;
typedef struct { typedef struct {
@ -110,7 +112,7 @@ typedef struct KrkClass {
KrkObj * _eq; KrkObj * _eq;
} KrkClass; } KrkClass;
typedef struct { typedef struct KrkInstance {
KrkObj obj; KrkObj obj;
KrkClass * _class; KrkClass * _class;
KrkTable fields; 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->ip = closure->function->chunk.code;
frame->slots = (vm.stackTop - argCount) - vm.stack; frame->slots = (vm.stackTop - argCount) - vm.stack;
frame->outSlots = (vm.stackTop - argCount - extra) - vm.stack; frame->outSlots = (vm.stackTop - argCount - extra) - vm.stack;
vm.globals = closure->function->globalsContext; vm.globals = &closure->function->globalsContext->fields;
return 1; return 1;
} }
@ -3052,6 +3052,7 @@ static inline size_t readBytes(CallFrame * frame, int num) {
*/ */
static KrkValue run() { static KrkValue run() {
CallFrame* frame = &vm.frames[vm.frameCount - 1]; CallFrame* frame = &vm.frames[vm.frameCount - 1];
vm.globals = &frame->closure->function->globalsContext->fields;
while (1) { while (1) {
#ifdef ENABLE_TRACING #ifdef ENABLE_TRACING
@ -3096,12 +3097,12 @@ static KrkValue run() {
return result; return result;
} }
vm.stackTop = &vm.stack[frame->outSlots]; vm.stackTop = &vm.stack[frame->outSlots];
vm.globals = &vm.frames[vm.frameCount - 1].closure->function->globalsContext->fields;
if (vm.frameCount == (size_t)vm.exitOnFrame) { if (vm.frameCount == (size_t)vm.exitOnFrame) {
return result; return result;
} }
krk_push(result); krk_push(result);
frame = &vm.frames[vm.frameCount - 1]; frame = &vm.frames[vm.frameCount - 1];
vm.globals = frame->closure->function->globalsContext;
break; break;
} }
case OP_EQUAL: { case OP_EQUAL: {