Change how module imports work to support a package importing contained modules
This commit is contained in:
parent
20b4142420
commit
a7e110cad9
@ -1 +1,2 @@
|
||||
from foo.extra import exportablething
|
||||
print("Imported foo.__init__ as", __name__)
|
||||
|
1
modules/foo/extra.krk
Normal file
1
modules/foo/extra.krk
Normal file
@ -0,0 +1 @@
|
||||
let exportablething = "hi"
|
@ -319,6 +319,7 @@ static void findInterpreter(char * argv[]) {
|
||||
static int runString(char * argv[], int flags, char * string) {
|
||||
findInterpreter(argv);
|
||||
krk_initVM(flags);
|
||||
krk_startModule("__main__");
|
||||
krk_interpret(string, 1, "<stdin>","<stdin>");
|
||||
krk_freeVM();
|
||||
return 0;
|
||||
@ -698,6 +699,7 @@ _finishArgs:
|
||||
(void)blockWidth;
|
||||
}
|
||||
} else {
|
||||
krk_startModule("__main__");
|
||||
result = krk_runfile(argv[optind],1,"__main__",argv[optind]);
|
||||
}
|
||||
|
||||
|
17
src/vm.c
17
src/vm.c
@ -1440,7 +1440,12 @@ int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs) {
|
||||
|
||||
/* Compile and run the module in a new context and exit the VM when it
|
||||
* returns to the current call frame; modules should return objects. */
|
||||
KrkInstance * enclosing = krk_currentThread.module;
|
||||
krk_startModule(runAs->chars);
|
||||
krk_tableSet(&vm.modules, OBJECT_VAL(runAs), OBJECT_VAL(krk_currentThread.module));
|
||||
if (isPackage) krk_attachNamedValue(&krk_currentThread.module->fields,"__ispackage__",BOOLEAN_VAL(1));
|
||||
*moduleOut = krk_callfile(fileName,runAs->chars,fileName);
|
||||
krk_currentThread.module = enclosing;
|
||||
if (!IS_OBJECT(*moduleOut)) {
|
||||
if (!(krk_currentThread.flags & KRK_HAS_EXCEPTION)) {
|
||||
krk_runtimeError(vm.exceptions->importError,
|
||||
@ -1451,11 +1456,6 @@ int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs) {
|
||||
|
||||
krk_pop(); /* concatenated filename on stack */
|
||||
krk_push(*moduleOut);
|
||||
krk_tableSet(&vm.modules, OBJECT_VAL(runAs), *moduleOut);
|
||||
/* Was this a package? */
|
||||
if (isPackage) {
|
||||
krk_attachNamedValue(&AS_INSTANCE(*moduleOut)->fields,"__ispackage__",BOOLEAN_VAL(1));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -2340,9 +2340,6 @@ KrkInstance * krk_startModule(const char * name) {
|
||||
}
|
||||
|
||||
KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * fromFile) {
|
||||
KrkInstance * enclosing = krk_currentThread.module;
|
||||
if (newScope) krk_startModule(fromName);
|
||||
|
||||
KrkFunction * function = krk_compile(src, 0, fromFile);
|
||||
if (!function) {
|
||||
if (!krk_currentThread.frameCount) handleException();
|
||||
@ -2368,9 +2365,7 @@ KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * f
|
||||
KrkValue result = run();
|
||||
|
||||
if (newScope) {
|
||||
KrkValue out = OBJECT_VAL(krk_currentThread.module);
|
||||
krk_currentThread.module = enclosing;
|
||||
return out;
|
||||
return OBJECT_VAL(krk_currentThread.module);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user