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->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);
|
||||||
|
2
memory.c
2
memory.c
@ -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();
|
||||||
|
@ -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
|
||||||
|
1
object.c
1
object.c
@ -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;
|
||||||
}
|
}
|
||||||
|
6
object.h
6
object.h
@ -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;
|
||||||
|
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->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: {
|
||||||
|
Loading…
Reference in New Issue
Block a user