support import subsrc.mod

This commit is contained in:
Lyon 2023-10-27 22:18:38 +08:00
parent 8ab5aab04b
commit d3a2ef5fab
14 changed files with 100 additions and 34 deletions

View File

@ -14,7 +14,7 @@
// "--gtest_filter=vm.keyword_2" // "--gtest_filter=vm.keyword_2"
// "--gtest_filter=compiler.find_break_point" // "--gtest_filter=compiler.find_break_point"
// "--gtest_filter=pikaMain.REPL_pdb_set_break" // "--gtest_filter=pikaMain.REPL_pdb_set_break"
"--gtest_filter=make.compile_depend" "--gtest_filter=vm.subsrc_import"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -1,8 +1,6 @@
{ {
"C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}", "C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, IndentWidth: 4}",
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json", "C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
"editor.defaultFormatter": "xaver.clang-format",
"C_Cpp.intelliSenseEngine": "disabled",
"files.associations": { "files.associations": {
"*.rs": "rust", "*.rs": "rust",
"pikastdlib_sysobj.h": "c", "pikastdlib_sysobj.h": "c",
@ -131,9 +129,6 @@
"[python]": { "[python]": {
"editor.defaultFormatter": "ms-python.python" "editor.defaultFormatter": "ms-python.python"
}, },
"[c]": {
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
},
"[jsonc]": { "[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features" "editor.defaultFormatter": "vscode.json-language-features"
} }

View File

@ -4,7 +4,6 @@ import this
import os import os
import time import time
import struct import struct
import flashdb import flashdb
print('hello pikapython!') print('hello pikapython!')

View File

@ -39,4 +39,5 @@ _thread
weakref weakref
eventloop eventloop
this this
fsm fsm
subsrc.mod1

View File

@ -0,0 +1,2 @@
print('in mod1')
import subsrc.mod2

View File

@ -0,0 +1 @@
print('in mod2')

View File

@ -0,0 +1 @@
print('in mod3')

View File

@ -0,0 +1 @@
print('in mod4')

View File

@ -224,16 +224,15 @@ void LibObj_deinit(LibObj* self) {
/* add bytecode to lib, not copy the bytecode */ /* add bytecode to lib, not copy the bytecode */
void LibObj_dynamicLink(LibObj* self, char* module_name, uint8_t* bytecode) { void LibObj_dynamicLink(LibObj* self, char* module_name, uint8_t* bytecode) {
if (strIsContain(module_name, '.')) { Args buffs = {0};
/* skip file */ char* module_obj_name = strsReplace(&buffs, module_name, ".", "|");
return; if (!obj_isArgExist(self, module_obj_name)) {
obj_newObj(self, module_obj_name, "", New_TinyObj);
} }
if (!obj_isArgExist(self, module_name)) { PikaObj* module_obj = obj_getObj(self, module_obj_name);
obj_newObj(self, module_name, "", New_TinyObj);
}
PikaObj* module_obj = obj_getObj(self, module_name);
obj_setStr(module_obj, "name", module_name); obj_setStr(module_obj, "name", module_name);
obj_setPtr(module_obj, "bytecode", bytecode); obj_setPtr(module_obj, "bytecode", bytecode);
strsDeinit(&buffs);
} }
/* /*
@ -248,10 +247,12 @@ int LibObj_staticLink(LibObj* self,
char* module_name, char* module_name,
uint8_t* bytecode, uint8_t* bytecode,
size_t size) { size_t size) {
if (!obj_isArgExist(self, module_name)) { Args buffs = {0};
obj_newObj(self, module_name, "", New_TinyObj); char* module_obj_name = strsReplace(&buffs, module_name, ".", "|");
if (!obj_isArgExist(self, module_obj_name)) {
obj_newObj(self, module_obj_name, "", New_TinyObj);
} }
PikaObj* module_obj = obj_getObj(self, module_name); PikaObj* module_obj = obj_getObj(self, module_obj_name);
uint16_t name_len = strGetSize(module_name); uint16_t name_len = strGetSize(module_name);
/* copy bytecode to buff */ /* copy bytecode to buff */
@ -261,6 +262,7 @@ int LibObj_staticLink(LibObj* self,
/* link to buff */ /* link to buff */
LibObj_dynamicLink(self, module_name, obj_getBytes(module_obj, "buff")); LibObj_dynamicLink(self, module_name, obj_getBytes(module_obj, "buff"));
strsDeinit(&buffs);
return 0; return 0;
} }
@ -387,7 +389,7 @@ static int32_t __foreach_handler_libWriteBytecode(Arg* argEach,
return 0; return 0;
} }
//#define NAME_BUFF_SIZE LIB_INFO_BLOCK_SIZE - sizeof(uint32_t) // #define NAME_BUFF_SIZE LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)
static int32_t __foreach_handler_libWriteIndex(Arg* argEach, static int32_t __foreach_handler_libWriteIndex(Arg* argEach,
PikaLinker* linker) { PikaLinker* linker) {
Args buffs = {0}; Args buffs = {0};
@ -649,6 +651,33 @@ Arg* _getPack_libraryBytes(char* pack_name) {
return f_arg; return f_arg;
} }
typedef struct {
char* module_name;
PikaObj* module;
} Context_LibObj_getModule;
int32_t _handler_LibObj_getModule(Arg* argEach, void* context) {
Context_LibObj_getModule* ctx = context;
if (NULL != ctx->module) {
return 0;
}
if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach);
if (strEqu(obj_getStr(module_obj, "name"), ctx->module_name)) {
ctx->module = module_obj;
return 0;
}
}
return 0;
}
PikaObj* LibObj_getModule(LibObj* self, char* module_name) {
Context_LibObj_getModule context = {0};
context.module_name = module_name;
args_foreach(self->list, _handler_LibObj_getModule, &context);
return context.module;
}
int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes) { int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes) {
int module_num = _getModuleNum(library_bytes); int module_num = _getModuleNum(library_bytes);
if (module_num < 0) { if (module_num < 0) {
@ -816,7 +845,8 @@ __exit:
static PIKA_RES __Maker_compileModuleWithInfo(PikaMaker* self, static PIKA_RES __Maker_compileModuleWithInfo(PikaMaker* self,
char* module_name) { char* module_name) {
Args buffs = {0}; Args buffs = {0};
char* input_file_name = strsAppend(&buffs, module_name, ".py"); char* input_file_name = strsReplace(&buffs, module_name, ".", "/");
input_file_name = strsAppend(&buffs, input_file_name, ".py");
char* input_file_path = char* input_file_path =
strsPathJoin(&buffs, obj_getStr(self, "pwd"), input_file_name); strsPathJoin(&buffs, obj_getStr(self, "pwd"), input_file_name);
pika_platform_printf(" compiling %s...\r\n", input_file_name); pika_platform_printf(" compiling %s...\r\n", input_file_name);
@ -869,10 +899,13 @@ void pikaMaker_setPWD(PikaMaker* self, char* pwd) {
* @return: void * @return: void
*/ */
void pikaMaker_setState(PikaMaker* self, char* module_name, char* state) { void pikaMaker_setState(PikaMaker* self, char* module_name, char* state) {
obj_newMetaObj(self, module_name, New_TinyObj); Args buffs = {0};
PikaObj* module_obj = obj_getObj(self, module_name); char* module_obj_name = strsReplace(&buffs, module_name, ".", "|");
obj_newMetaObj(self, module_obj_name, New_TinyObj);
PikaObj* module_obj = obj_getObj(self, module_obj_name);
obj_setStr(module_obj, "name", module_name); obj_setStr(module_obj, "name", module_name);
obj_setStr(module_obj, "state", state); obj_setStr(module_obj, "state", state);
strsDeinit(&buffs);
} }
char* pikaMaker_getState(PikaMaker* self, char* module_name) { char* pikaMaker_getState(PikaMaker* self, char* module_name) {
@ -1031,10 +1064,12 @@ int pikaMaker_getDependencies(PikaMaker* self, char* module_name) {
instructUnit_getInstructIndex(ins_unit) == INH) { instructUnit_getInstructIndex(ins_unit) == INH) {
char* imp_module_name = char* imp_module_name =
constPool_getByOffset(const_pool, ins_unit->const_pool_index); constPool_getByOffset(const_pool, ins_unit->const_pool_index);
char* imp_module_path = char* imp_module_name_fs =
strsPathJoin(&buffs, obj_getStr(self, "pwd"), imp_module_name); strsReplace(&buffs, imp_module_name, ".", "/");
char* imp_module_path = strsPathJoin(
&buffs, obj_getStr(self, "pwd"), imp_module_name_fs);
/* check if compiled the module */ /* check if compiled the module */
if (obj_isArgExist(self, imp_module_name)) { if (args_isArgExist(self->list, imp_module_name)) {
/* module info is exist, do nothing */ /* module info is exist, do nothing */
} else { } else {
/* module info is not exist */ /* module info is not exist */
@ -1053,7 +1088,8 @@ int pikaMaker_getDependencies(PikaMaker* self, char* module_name) {
pika_platform_printf( pika_platform_printf(
" [warning]: file: '%s.pyi', '%s.py' or '%s.py.o' " " [warning]: file: '%s.pyi', '%s.py' or '%s.py.o' "
"no found\n", "no found\n",
imp_module_name, imp_module_name, imp_module_name); imp_module_name_fs, imp_module_name_fs,
imp_module_name_fs);
} }
} }
} }
@ -1173,7 +1209,8 @@ PIKA_RES pikaMaker_compileModuleWithList(PikaMaker* self, char* list_content) {
break; break;
} }
module_name = strsSubStr(&buffs, module_name_start, module_name_end); module_name = strsSubStr(&buffs, module_name_start, module_name_end);
enum PIKA_MODULE_TYPE module_type = _checkModuleType(module_name); char* module_name_fs = strsReplace(&buffs, module_name, ".", "/");
enum PIKA_MODULE_TYPE module_type = _checkModuleType(module_name_fs);
if (module_type == PIKA_MODULE_TYPE_PY) { if (module_type == PIKA_MODULE_TYPE_PY) {
res = pikaMaker_compileModuleWithDepends(self, module_name); res = pikaMaker_compileModuleWithDepends(self, module_name);
if (PIKA_RES_OK != res) { if (PIKA_RES_OK != res) {
@ -1184,6 +1221,12 @@ PIKA_RES pikaMaker_compileModuleWithList(PikaMaker* self, char* list_content) {
if (module_type == PIKA_MODULE_TYPE_PYO) { if (module_type == PIKA_MODULE_TYPE_PYO) {
pikaMaker_linkByteocdeFile(self, module_name); pikaMaker_linkByteocdeFile(self, module_name);
} }
if (module_type == PIKA_MODULE_TYPE_UNKNOWN) {
pika_platform_printf(
" [warning]: file: '%s.pyi', '%s.py' or '%s.py.o' "
"no found\n",
module_name, module_name, module_name);
}
module_name_start = module_name_end + 1; module_name_start = module_name_end + 1;
} }
__exit: __exit:

View File

@ -23,6 +23,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name);
void LibObj_listModules(LibObj* self); void LibObj_listModules(LibObj* self);
int LibObj_linkFile(LibObj* self, char* output_file_name); int LibObj_linkFile(LibObj* self, char* output_file_name);
int LibObj_loadLibraryFile(LibObj* self, char* input_file_name); int LibObj_loadLibraryFile(LibObj* self, char* input_file_name);
PikaObj* LibObj_getModule(LibObj* self, char* module_name);
int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root); int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root);
PikaMaker* New_PikaMaker(void); PikaMaker* New_PikaMaker(void);
void pikaMaker_setPWD(PikaMaker* self, char* pwd); void pikaMaker_setPWD(PikaMaker* self, char* pwd);

View File

@ -1270,6 +1270,9 @@ pika_bool obj_isArgExist(PikaObj* self, char* argPath) {
return 0; return 0;
} }
PikaObj* obj_host = obj_getHostObj(self, argPath); PikaObj* obj_host = obj_getHostObj(self, argPath);
if (obj_host == NULL) {
return pika_false;
}
int32_t res = 0; int32_t res = 0;
char* argName; char* argName;
Arg* arg; Arg* arg;
@ -2505,7 +2508,26 @@ int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr) {
obj_setName(arg_getPtr(aNewObj), objName); obj_setName(arg_getPtr(aNewObj), objName);
arg_setType(aNewObj, ARG_TYPE_OBJECT); arg_setType(aNewObj, ARG_TYPE_OBJECT);
// pikaGC_enable(arg_getPtr(aNewObj)); // pikaGC_enable(arg_getPtr(aNewObj));
args_setArg(self->list, aNewObj); obj_setArg_noCopy(self, objName, aNewObj);
return 0;
}
int32_t obj_newHostObj(PikaObj* self, char* objName) {
Args buffs = {0};
size_t tokenCnt = strCountSign(objName, '.');
if (0 == tokenCnt) {
return 0;
}
PikaObj* this = self;
objName = strsCopy(&buffs, objName);
for (int i = 0; i < tokenCnt; i++) {
char* name = strsPopToken(&buffs, &objName, '.');
if (!obj_isArgExist(this, name)) {
obj_newDirectObj(this, name, New_TinyObj);
this = obj_getObj(this, name);
}
}
strsDeinit(&buffs);
return 0; return 0;
} }
@ -2543,12 +2565,9 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self,
uint8_t* byteCode) { uint8_t* byteCode) {
if (!obj_isArgExist((PikaObj*)__pikaMain, name)) { if (!obj_isArgExist((PikaObj*)__pikaMain, name)) {
/* import to main module context */ /* import to main module context */
obj_newHostObj((PikaObj*)__pikaMain, name);
obj_newDirectObj((PikaObj*)__pikaMain, name, New_TinyObj); obj_newDirectObj((PikaObj*)__pikaMain, name, New_TinyObj);
pikaVM_runByteCode(obj_getObj((PikaObj*)__pikaMain, name),
(uint8_t*)byteCode);
PikaObj* module_obj = obj_getObj((PikaObj*)__pikaMain, name); PikaObj* module_obj = obj_getObj((PikaObj*)__pikaMain, name);
PikaVMThread vm_thread = {.try_state = TRY_STATE_NONE, PikaVMThread vm_thread = {.try_state = TRY_STATE_NONE,
.try_result = TRY_RESULT_NONE}; .try_result = TRY_RESULT_NONE};
pikaVM_runBytecode_ex_cfg cfg = {0}; pikaVM_runBytecode_ex_cfg cfg = {0};
@ -2563,6 +2582,7 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self,
/* import to other module context */ /* import to other module context */
Arg* aModule = obj_getArg((PikaObj*)__pikaMain, name); Arg* aModule = obj_getArg((PikaObj*)__pikaMain, name);
PikaObj* oModule = arg_getPtr(aModule); PikaObj* oModule = arg_getPtr(aModule);
obj_newHostObj(self, name);
obj_setArg(self, name, aModule); obj_setArg(self, name, aModule);
arg_setIsWeakRef(obj_getArg(self, name), pika_true); arg_setIsWeakRef(obj_getArg(self, name), pika_true);
pika_assert(arg_isObject(aModule)); pika_assert(arg_isObject(aModule));
@ -2620,7 +2640,7 @@ uint8_t* pika_getByteCodeFromModule(char* module_name) {
} }
/* find module from the library */ /* find module from the library */
LibObj* lib = obj_getPtr(self, "@lib"); LibObj* lib = obj_getPtr(self, "@lib");
PikaObj* module = obj_getObj(lib, module_name); PikaObj* module = LibObj_getModule(lib, module_name);
/* exit when no module in '@lib' */ /* exit when no module in '@lib' */
if (NULL == module) { if (NULL == module) {
return NULL; return NULL;

View File

@ -569,6 +569,7 @@ PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes);
int obj_importModule(PikaObj* self, char* module_name); int obj_importModule(PikaObj* self, char* module_name);
int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr); int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr);
int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr); int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr);
int32_t obj_newHostObj(PikaObj* self, char* objName);
int obj_runModule(PikaObj* self, char* module_name); int obj_runModule(PikaObj* self, char* module_name);
PikaObj* obj_runFile(PikaObj* self, char* file_name); PikaObj* obj_runFile(PikaObj* self, char* file_name);
PikaObj* obj_runSingleFile(PikaObj* self, char* file_name); PikaObj* obj_runSingleFile(PikaObj* self, char* file_name);

View File

@ -3964,6 +3964,7 @@ VMParameters* pikaVM_run(PikaObj* self, char* py_lines) {
} }
VMParameters* pikaVM_runByteCode(PikaObj* self, const uint8_t* bytecode) { VMParameters* pikaVM_runByteCode(PikaObj* self, const uint8_t* bytecode) {
pika_assert(NULL != self);
PikaVMThread vm_thread = {.try_state = TRY_STATE_NONE, PikaVMThread vm_thread = {.try_state = TRY_STATE_NONE,
.try_result = TRY_RESULT_NONE}; .try_result = TRY_RESULT_NONE};
pikaVM_runBytecode_ex_cfg cfg = {0}; pikaVM_runBytecode_ex_cfg cfg = {0};

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 12 #define PIKA_VERSION_MINOR 12
#define PIKA_VERSION_MICRO 7 #define PIKA_VERSION_MICRO 7
#define PIKA_EDIT_TIME "2023/10/26 21:36:47" #define PIKA_EDIT_TIME "2023/10/27 22:18:28"