Fix tracking what should be 'global' through function calls?
This commit is contained in:
parent
902d2222b5
commit
fbf4dda818
@ -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);
|
||||
|
2
memory.c
2
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();
|
||||
|
@ -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
|
||||
|
1
object.c
1
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;
|
||||
}
|
||||
|
6
object.h
6
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;
|
||||
|
2
test/testImportContext.krk
Normal file
2
test/testImportContext.krk
Normal file
@ -0,0 +1,2 @@
|
||||
import dummy
|
||||
dummy.foo()
|
5
vm.c
5
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: {
|
||||
|
Loading…
Reference in New Issue
Block a user