From 3fd45abe688b3017f7799ce9acee96239e01a2c1 Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 1 May 2022 21:07:41 +0800 Subject: [PATCH] not use 'index' in LibObj --- port/linux/.vscode/launch.json | 2 +- port/linux/test/compile-test.cpp | 4 ++-- src/PikaCompiler.c | 18 +++++++----------- src/PikaObj.c | 18 ++++++++++++++++++ src/PikaObj.h | 1 + src/PikaVM.c | 18 ++---------------- 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 8ca72b543..00054a1c1 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - "--gtest_filter=lib.compile_link_import" + // "--gtest_filter=lib.compile_link_import" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/test/compile-test.cpp b/port/linux/test/compile-test.cpp index ffe27ec42..eb751ae90 100644 --- a/port/linux/test/compile-test.cpp +++ b/port/linux/test/compile-test.cpp @@ -339,8 +339,8 @@ TEST(lib, lib_link_bytecode) { LibObj_dynamicLink(lib, "module3", (uint8_t*)0x33433); LibObj_dynamicLink(lib, "module4", (uint8_t*)0x33433); LibObj_dynamicLink(lib, "module5", (uint8_t*)0x33433); - EXPECT_STREQ(obj_getStr(lib, "index.module1.name"), "module1"); - EXPECT_EQ((uintptr_t)obj_getPtr(lib, "index.module1.bytecode"), 0x3344); + EXPECT_STREQ(obj_getStr(lib, "module1.name"), "module1"); + EXPECT_EQ((uintptr_t)obj_getPtr(lib, "module1.bytecode"), 0x3344); /* deinit */ LibObj_deinit(lib); EXPECT_EQ(pikaMemNow(), 0); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index 611a30d5d..4125ee33f 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -123,7 +123,6 @@ int pikaCompileFile(char* input_file_name) { LibObj* New_LibObj(void) { LibObj* self = New_TinyObj(NULL); - obj_newObj(self, "index", "", New_TinyObj); return self; } @@ -133,11 +132,10 @@ void LibObj_deinit(LibObj* self) { /* add bytecode to lib, not copy the bytecode */ void LibObj_dynamicLink(LibObj* self, char* module_name, uint8_t* bytecode) { - PikaObj* index_obj = obj_getObj(self, "index"); - if (!obj_isArgExist(index_obj, module_name)) { - obj_newObj(index_obj, module_name, "", New_TinyObj); + if (!obj_isArgExist(self, module_name)) { + obj_newObj(self, module_name, "", New_TinyObj); } - PikaObj* module_obj = obj_getObj(index_obj, module_name); + PikaObj* module_obj = obj_getObj(self, module_name); obj_setStr(module_obj, "name", module_name); obj_setPtr(module_obj, "bytecode", bytecode); } @@ -147,11 +145,10 @@ int LibObj_staticLink(LibObj* self, char* module_name, uint8_t* bytecode, size_t size) { - PikaObj* index_obj = obj_getObj(self, "index"); - if (!obj_isArgExist(index_obj, module_name)) { - obj_newObj(index_obj, module_name, "", New_TinyObj); + if (!obj_isArgExist(self, module_name)) { + obj_newObj(self, module_name, "", New_TinyObj); } - PikaObj* module_obj = obj_getObj(index_obj, module_name); + PikaObj* module_obj = obj_getObj(self, module_name); /* copy bytecode to buff */ obj_setBytes(module_obj, "buff", bytecode, size); /* link to buff */ @@ -191,6 +188,5 @@ static int32_t __foreach_handler_listModules(Arg* argEach, Args* handleArgs) { } void LibObj_listModules(LibObj* self) { - PikaObj* index_obj = obj_getObj(self, "index"); - args_foreach(index_obj->list, __foreach_handler_listModules, NULL); + args_foreach(self->list, __foreach_handler_listModules, NULL); } diff --git a/src/PikaObj.c b/src/PikaObj.c index 1e97bf2a5..a990a7e6f 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -934,3 +934,21 @@ PikaObj* obj_linkLibrary(PikaObj* self, LibObj* library) { obj_setPtr(self, "__lib", library); return self; } + +int obj_importModule(PikaObj* self, char* module_name) { + /* exit when no found '__lib' */ + if (!obj_isArgExist(self, "__lib")) { + return 1; + } + /* 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 1; + } + /* import bytecode of the module */ + uint8_t* bytecode = obj_getPtr(module, "bytecode"); + obj_importModuleWithByteCode(self, module_name, bytecode); + return 0; +} diff --git a/src/PikaObj.h b/src/PikaObj.h index 00a62a3f7..9ee6b46b6 100644 --- a/src/PikaObj.h +++ b/src/PikaObj.h @@ -245,6 +245,7 @@ int32_t obj_newObj(PikaObj* self, Arg* arg_newMetaObj(NewFun objPtr); PikaObj* obj_linkLibrary(PikaObj* self, LibObj* library); +int obj_importModule(PikaObj* self, char* module_name); #define PIKA_PYTHON_BEGIN #define PIKA_PYTHON(x) diff --git a/src/PikaVM.c b/src/PikaVM.c index e7200fa4a..8e6a7d92a 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -913,27 +913,13 @@ static Arg* VM_instruction_handler_IMP(PikaObj* self, VMState* vs, char* data) { if (obj_isArgExist(self, data)) { return NULL; } - /* exit when no found '__lib' */ - if (!obj_isArgExist(self, "__lib")) { + /* import module from '__lib' */ + if (0 != obj_importModule(self, data)) { VMState_setErrorCode(vs, 3); __platform_printf("ModuleNotFoundError: No module named '%s'\r\n", data); return NULL; } - /* find module from the library */ - LibObj* lib = obj_getPtr(self, "__lib"); - PikaObj* index = obj_getObj(lib, "index"); - PikaObj* module = obj_getObj(index, data); - /* exit when no module in '__lib' */ - if (NULL == module) { - VMState_setErrorCode(vs, 3); - __platform_printf("ModuleNotFoundError: No module named '%s'\r\n", - data); - return NULL; - } - /* import bytecode of the module */ - uint8_t* bytecode = obj_getPtr(module, "bytecode"); - obj_importModuleWithByteCode(self, data, bytecode); return NULL; }