not use 'index' in LibObj

This commit is contained in:
lyon 2022-05-01 21:07:41 +08:00
parent c94139da3d
commit 3fd45abe68
6 changed files with 31 additions and 30 deletions

View File

@ -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}",

View File

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

View File

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

View File

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

View File

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

View File

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