From e93fbb254ff48105c466c133354cbddc32012f94 Mon Sep 17 00:00:00 2001 From: lyon Date: Fri, 13 May 2022 09:27:59 +0800 Subject: [PATCH] run "main" module as default --- src/PikaObj.c | 22 +++++++++++++++++++--- src/PikaObj.h | 1 + src/pika_config_valid.h | 5 +++++ tools/pikaCompiler/src/class_info.rs | 4 ++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/PikaObj.c b/src/PikaObj.c index 8c69dea09..29d636183 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -942,20 +942,36 @@ PikaObj* obj_linkLibObj(PikaObj* self, LibObj* library) { return self; } -int obj_importModule(PikaObj* self, char* module_name) { +uint8_t* obj_getModuleByteCode(PikaObj* self, char* module_name) { /* exit when no found '__lib' */ if (!obj_isArgExist(self, "__lib")) { - return 1; + return NULL; } /* find module from the library */ LibObj* lib = obj_getPtr(self, "__lib"); PikaObj* module = obj_getObj(lib, module_name); /* exit when no module in '__lib' */ if (NULL == module) { + return NULL; + } + return obj_getPtr(module, "bytecode"); +} + +int obj_runModule(PikaObj* self, char* module_name) { + uint8_t* bytecode = obj_getModuleByteCode(self, module_name); + if (NULL == bytecode) { return 1; } + pikaVM_runByteCode(self, bytecode); + return 0; +} + +int obj_importModule(PikaObj* self, char* module_name) { /* import bytecode of the module */ - uint8_t* bytecode = obj_getPtr(module, "bytecode"); + uint8_t* bytecode = obj_getModuleByteCode(self, module_name); + if (NULL == bytecode) { + return 1; + } obj_importModuleWithByteCode(self, module_name, bytecode); return 0; } diff --git a/src/PikaObj.h b/src/PikaObj.h index 97b3f742a..72682cfcb 100644 --- a/src/PikaObj.h +++ b/src/PikaObj.h @@ -250,6 +250,7 @@ PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes); int obj_importModule(PikaObj* self, char* module_name); int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr); int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr); +int obj_runModule(PikaObj* self, char* module_name); #define PIKA_PYTHON_BEGIN #define PIKA_PYTHON(x) diff --git a/src/pika_config_valid.h b/src/pika_config_valid.h index 97182a95c..ce695250f 100644 --- a/src/pika_config_valid.h +++ b/src/pika_config_valid.h @@ -39,6 +39,7 @@ #define PIKA_BUILTIN_LIST_ENABLE 0 #define PIKA_BUILTIN_DICT_ENABLE 0 #define PIKA_READ_FILE_BUFF_SIZE 0x10000 + #define PIKA_INIT_STRING_ENABLE 0 /* optimize options */ #define PIKA_OPTIMIZE_SIZE 0 @@ -75,11 +76,15 @@ #define PIKA_BUILTIN_DICT_ENABLE 0 #undef PIKA_BUILTIN_LIST_ENABLE #define PIKA_BUILTIN_LIST_ENABLE 0 + #undef PIKA_INIT_STRING_ENABLE + #define PIKA_INIT_STRING_ENABLE 0 #elif PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MAXIMAL #undef PIKA_BUILTIN_DICT_ENABLE #define PIKA_BUILTIN_DICT_ENABLE 1 #undef PIKA_BUILTIN_LIST_ENABLE #define PIKA_BUILTIN_LIST_ENABLE 1 + #undef PIKA_INIT_STRING_ENABLE + #define PIKA_INIT_STRING_ENABLE 1 #endif /* configuration validation */ diff --git a/tools/pikaCompiler/src/class_info.rs b/tools/pikaCompiler/src/class_info.rs index 21db97d9e..b7317c670 100644 --- a/tools/pikaCompiler/src/class_info.rs +++ b/tools/pikaCompiler/src/class_info.rs @@ -148,6 +148,7 @@ impl ClassInfo { /* use obj_run to run the script in main.pyi */ script_fn.push_str(" extern unsigned char pikaModules_py_a[];\n"); script_fn.push_str(" obj_linkLibrary(__pikaMain, pikaModules_py_a);\n"); + script_fn.push_str("#if PIKA_INIT_STRING_ENABLE\n"); script_fn.push_str(" obj_run(__pikaMain,\n"); /* get the origin script content */ let script_content_origin = String::from(&self.script_list.content); @@ -164,6 +165,9 @@ impl ClassInfo { script_fn.push_str(&script_content); /* add the END of script string */ script_fn.push_str(" \"\\n\");\n"); + script_fn.push_str("#else \n"); + script_fn.push_str(" obj_runModule(__pikaMain, \"main\");\n"); + script_fn.push_str("#endif\n"); script_fn.push_str(" return __pikaMain;\r\n"); script_fn.push_str("}\r\n\r\n"); return script_fn;