run "main" module as default

This commit is contained in:
lyon 2022-05-13 09:27:59 +08:00
parent 6ef8d41d44
commit e93fbb254f
4 changed files with 29 additions and 3 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -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 */

View File

@ -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;