support mod1 import mod2 import mod1

This commit is contained in:
pikastech 2022-10-02 16:29:48 +08:00
parent 0d857f25a0
commit 6a9535470b
12 changed files with 92 additions and 51 deletions

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test", "program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [ "args": [
"--gtest_filter=vm.*" // "--gtest_filter=module.while_loop"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -1,2 +1,3 @@
import test_module1
def mytest(): def mytest():
print('test_module_2_hello') print('test_module_2_hello')

View File

@ -1,4 +1,2 @@
cp config/pika_config_void.h config/pika_config.h
sh only_make.sh
cd package/pikascript && \ cd package/pikascript && \
./pika $1 ./pika $1

View File

@ -335,8 +335,8 @@ int LibObj_loadLibraryFile(LibObj* self, char* lib_file_name) {
lib_file_name); lib_file_name);
return PIKA_RES_ERR_IO_ERROR; return PIKA_RES_ERR_IO_ERROR;
} }
/* save file_arg as __lib_buf to libObj */ /* save file_arg as @lib_buf to libObj */
obj_setArg_noCopy(self, "__lib_buf", file_arg); obj_setArg_noCopy(self, "@lib_buf", file_arg);
if (0 != LibObj_loadLibrary(self, arg_getBytes(file_arg))) { if (0 != LibObj_loadLibrary(self, arg_getBytes(file_arg))) {
__platform_printf("Error: Could not load library from '%s'\n", __platform_printf("Error: Could not load library from '%s'\n",
lib_file_name); lib_file_name);
@ -569,7 +569,7 @@ void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name) {
int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) {
Args buffs = {0}; Args buffs = {0};
if (argType_isObject(arg_getType(argEach))) { if (argType_isObject(arg_getType(argEach))) {
LibObj* lib = args_getPtr(context, "__lib"); LibObj* lib = args_getPtr(context, "@lib");
PikaMaker* maker = args_getPtr(context, "__maker"); PikaMaker* maker = args_getPtr(context, "__maker");
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
char* module_name = obj_getStr(module_obj, "name"); char* module_name = obj_getStr(module_obj, "name");
@ -592,7 +592,7 @@ void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path) {
LibObj* lib = New_LibObj(NULL); LibObj* lib = New_LibObj(NULL);
Args buffs = {0}; Args buffs = {0};
__platform_printf(" linking %s...\n", lib_path); __platform_printf(" linking %s...\n", lib_path);
args_setPtr(&context, "__lib", lib); args_setPtr(&context, "@lib", lib);
args_setPtr(&context, "__maker", self); args_setPtr(&context, "__maker", self);
args_foreach(self->list, __foreach_handler_linkCompiledModules, &context); args_foreach(self->list, __foreach_handler_linkCompiledModules, &context);
args_deinit_stack(&context); args_deinit_stack(&context);

View File

@ -1074,8 +1074,18 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self,
char* name, char* name,
uint8_t* byteCode) { uint8_t* byteCode) {
PikaObj* New_PikaStdLib_SysObj(Args * args); PikaObj* New_PikaStdLib_SysObj(Args * args);
obj_newDirectObj(self, name, New_PikaStdLib_SysObj); if (!obj_isArgExist(__pikaMain, name)) {
pikaVM_runByteCode(obj_getObj(self, name), (uint8_t*)byteCode); obj_newDirectObj(__pikaMain, name, New_PikaStdLib_SysObj);
pikaVM_runByteCode(obj_getObj(__pikaMain, name), (uint8_t*)byteCode);
}
if (self != __pikaMain) {
Arg* module_arg = obj_getArg(__pikaMain, name);
PikaObj* module_obj = arg_getPtr(module_arg);
obj_setArg(self, name, module_arg);
pika_assert(argType_isObject(arg_getType(module_arg)));
/* decrase refcnt to avoid circle reference */
obj_refcntDec(module_obj);
}
return self; return self;
} }
@ -1089,33 +1099,33 @@ PikaObj* obj_importModuleWithByteCodeFrame(PikaObj* self,
} }
PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name) { PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name) {
obj_newMetaObj(self, "__lib", New_LibObj); obj_newMetaObj(self, "@lib", New_LibObj);
LibObj* lib = obj_getObj(self, "__lib"); LibObj* lib = obj_getObj(self, "@lib");
LibObj_loadLibraryFile(lib, input_file_name); LibObj_loadLibraryFile(lib, input_file_name);
return self; return self;
} }
PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes) { PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes) {
obj_newMetaObj(self, "__lib", New_LibObj); obj_newMetaObj(self, "@lib", New_LibObj);
LibObj* lib = obj_getObj(self, "__lib"); LibObj* lib = obj_getObj(self, "@lib");
LibObj_loadLibrary(lib, library_bytes); LibObj_loadLibrary(lib, library_bytes);
return self; return self;
} }
PikaObj* obj_linkLibObj(PikaObj* self, LibObj* library) { PikaObj* obj_linkLibObj(PikaObj* self, LibObj* library) {
obj_setPtr(self, "__lib", library); obj_setPtr(self, "@lib", library);
return self; return self;
} }
uint8_t* obj_getByteCodeFromModule(PikaObj* self, char* module_name) { uint8_t* obj_getByteCodeFromModule(PikaObj* self, char* module_name) {
/* exit when no found '__lib' */ /* exit when no found '@lib' */
if (!obj_isArgExist(self, "__lib")) { if (!obj_isArgExist(self, "@lib")) {
return NULL; return NULL;
} }
/* 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 = obj_getObj(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

@ -1152,6 +1152,11 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
goto exit; goto exit;
} }
if (strEqu(run_path, "object")) {
return_arg = arg_newMetaObj(New_TinyObj);
goto exit;
}
/* get method host obj from reg */ /* get method host obj from reg */
if (NULL == method_host && _checkLReg(run_path)) { if (NULL == method_host && _checkLReg(run_path)) {
uint8_t reg_index = _getLRegIndex(run_path); uint8_t reg_index = _getLRegIndex(run_path);
@ -2321,7 +2326,7 @@ static Arg* VM_instruction_handler_IMP(PikaObj* self,
obj_setArg(self, data, obj_getArg(__pikaMain, data)); obj_setArg(self, data, obj_getArg(__pikaMain, data));
return NULL; return NULL;
} }
/* import module from '__lib' */ /* import module from '@lib' */
if (0 == obj_importModule(self, data)) { if (0 == obj_importModule(self, data)) {
return NULL; return NULL;
} }

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 11 #define PIKA_VERSION_MINOR 11
#define PIKA_VERSION_MICRO 2 #define PIKA_VERSION_MICRO 2
#define PIKA_EDIT_TIME "2022/10/02 00:00:51" #define PIKA_EDIT_TIME "2022/10/02 16:29:46"

View File

@ -368,4 +368,24 @@ TEST(issue, global) {
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);
} }
TEST(module, mod1_mod2_mod1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
extern unsigned char pikaModules_py_a[];
obj_linkLibrary(pikaMain, pikaModules_py_a);
/* run */
__platform_printf("BEGIN\r\n");
obj_run(pikaMain,
"import test_module1\n"
"test_module1.test_module2.test_module1.mytest()"
);
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "test_module_1_hello\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif #endif

View File

@ -1,6 +1,7 @@
import PikaStdLib import PikaStdLib
import test import test
import TemplateDevice import TemplateDevice
import module
from pika_cjson import cJSON from pika_cjson import cJSON
print('hello pikascript!') print('hello pikascript!')
mem = PikaStdLib.MemChecker() mem = PikaStdLib.MemChecker()

View File

@ -1 +1 @@
import module

View File

@ -199,36 +199,6 @@ impl Compiler {
let mut file_str = String::new(); let mut file_str = String::new();
file.read_to_string(&mut file_str).unwrap(); file.read_to_string(&mut file_str).unwrap();
/* return when compiled */
if !self.compiled_list.contains(&file_name) {
/* print info */
match suffix {
"py" => {
println!(" scaning {}{}.{}...", self.source_path, file_name, suffix);
}
"pyi" => {
println!(
" binding {}{}.{}...",
self.source_path, file_name, suffix
);
}
_ => {}
}
}
/* push to compiled list, only compile once */
self.compiled_list.push_back(String::clone(&file_name));
/* solve top package.
About what is top package:
Top package is the package imported by main.py directly,
and can be used to create objcet in runtime.
So the each classes of top package are loaded to flash.
The top package is solved as an static object, and each calsses
of the top package is solved as a function of the static
object. To implament this, a [package]-api.c file is created
to supply the class of static object.
*/
if is_top_c_pkg { if is_top_c_pkg {
/* /*
Push top C package to PikaMain. Push top C package to PikaMain.
@ -248,6 +218,42 @@ impl Compiler {
self.package_name_now = Some(package_name.clone()); self.package_name_now = Some(package_name.clone());
} }
/* return when compiled */
if !self.compiled_list.contains(&file_name) {
/* print info */
match suffix {
"py" => {
println!(" scaning {}{}.{}...", self.source_path, file_name, suffix);
}
"pyi" => {
println!(
" binding {}{}.{}...",
self.source_path, file_name, suffix
);
}
_ => {}
}
} else {
/* not scan *.py again */
match suffix {
"py" => return self,
_ => {}
}
}
/* push to compiled list, only compile once */
self.compiled_list.push_back(String::clone(&file_name));
/* solve top package.
About what is top package:
Top package is the package imported by main.py directly,
and can be used to create objcet in runtime.
So the each classes of top package are loaded to flash.
The top package is solved as an static object, and each calsses
of the top package is solved as a function of the static
object. To implament this, a [package]-api.c file is created
to supply the class of static object.
*/
let lines: Vec<&str> = file_str.split('\n').collect(); let lines: Vec<&str> = file_str.split('\n').collect();
let mut last_line = String::from(""); let mut last_line = String::from("");
/* analyse each line */ /* analyse each line */