mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
add magic_code and version_num to lib_file info_block
This commit is contained in:
parent
ad9e97ee98
commit
b1b45b8784
@ -477,3 +477,23 @@ TEST(lib, load_file) {
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(lib, load_no_file) {
|
||||
/* compile */
|
||||
LibObj* lib = New_LibObj();
|
||||
int res = LibObj_loadLibraryFile(lib, "test/python/mian.py.o");
|
||||
EXPECT_EQ(res, 1);
|
||||
/* deinit */
|
||||
LibObj_deinit(lib);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(lib, load_err_file_type) {
|
||||
/* compile */
|
||||
LibObj* lib = New_LibObj();
|
||||
int res = LibObj_loadLibraryFile(lib, "test/python/main.py.o");
|
||||
EXPECT_EQ(res, 2);
|
||||
/* deinit */
|
||||
LibObj_deinit(lib);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
@ -228,9 +228,21 @@ int LibObj_saveLibraryFile(LibObj* self, char* output_file_name) {
|
||||
/* write meta information */
|
||||
char buff[LIB_INFO_BLOCK_SIZE] = {0};
|
||||
args_foreach(self->list, __foreach_handler_getModuleNum, &handleArgs);
|
||||
|
||||
/* meta info */
|
||||
char magic_code[] = {0x7f, 'p', 'y', 'a'};
|
||||
uint32_t version_num = 1;
|
||||
uint32_t module_num = args_getInt(&handleArgs, "module_num");
|
||||
|
||||
/* write meta info */
|
||||
const uint32_t magic_code_offset = sizeof(uint32_t) * 0;
|
||||
const uint32_t version_offset = sizeof(uint32_t) * 1;
|
||||
const uint32_t module_num_offset = sizeof(uint32_t) * 2;
|
||||
|
||||
__platform_memcpy(buff + magic_code_offset, &magic_code, sizeof(uint32_t));
|
||||
__platform_memcpy(buff + version_offset, &version_num, sizeof(uint32_t));
|
||||
/* write module_num to the file */
|
||||
__platform_memcpy(buff, &module_num, sizeof(uint32_t));
|
||||
__platform_memcpy(buff + module_num_offset, &module_num, sizeof(uint32_t));
|
||||
/* aline to 32 bytes */
|
||||
__platform_fwrite(buff, 1, LIB_INFO_BLOCK_SIZE, out_file);
|
||||
/* write module index to file */
|
||||
@ -245,7 +257,25 @@ int LibObj_saveLibraryFile(LibObj* self, char* output_file_name) {
|
||||
}
|
||||
|
||||
int LibObj_loadLibrary(LibObj* self, uint8_t* library) {
|
||||
uint32_t module_num = library[0];
|
||||
char* magic_code = (char*)library;
|
||||
|
||||
uint32_t* library_info = (uint32_t*)library;
|
||||
uint32_t version_num = library_info[1];
|
||||
uint32_t module_num = library_info[2];
|
||||
|
||||
/* check magic_code */
|
||||
if (!((magic_code[0] == 0x7f) && (magic_code[1] == 'p') &&
|
||||
(magic_code[2] == 'y') && (magic_code[3] == 'a'))) {
|
||||
__platform_printf("Error: invalid magic code.\r\n");
|
||||
return 1;
|
||||
}
|
||||
/* check version num */
|
||||
if (version_num != LIB_VERSION_NUMBER) {
|
||||
__platform_printf(
|
||||
"Error: invalid version number. Expected %, got %\r\n",
|
||||
LIB_VERSION_NUMBER, version_num);
|
||||
return 2;
|
||||
}
|
||||
uint8_t* bytecode_addr = library + LIB_INFO_BLOCK_SIZE * (module_num + 1);
|
||||
for (uint32_t i = 0; i < module_num; i++) {
|
||||
char* module_name = (char*)(library + LIB_INFO_BLOCK_SIZE * (i + 1));
|
||||
@ -259,8 +289,17 @@ int LibObj_loadLibrary(LibObj* self, uint8_t* library) {
|
||||
|
||||
int LibObj_loadLibraryFile(LibObj* self, char* input_file_name) {
|
||||
Arg* file_arg = arg_loadFile(NULL, input_file_name);
|
||||
if (NULL == file_arg) {
|
||||
__platform_printf("Error: Could not load library file '%s'\n",
|
||||
input_file_name);
|
||||
return 1;
|
||||
}
|
||||
/* save file_arg as __lib_buf to libObj */
|
||||
obj_setArg_noCopy(self, "__lib_buf", file_arg);
|
||||
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",
|
||||
input_file_name);
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ void LibObj_listModules(LibObj* self);
|
||||
int LibObj_saveLibraryFile(LibObj* self, char* output_file_name);
|
||||
int LibObj_loadLibraryFile(LibObj* self, char* input_file_name);
|
||||
|
||||
#define LIB_VERSION_NUMBER 1
|
||||
#define LIB_INFO_BLOCK_SIZE 32
|
||||
|
||||
#endif
|
@ -366,12 +366,16 @@ Arg* arg_loadFile(Arg* self, char* filename) {
|
||||
char* file_buff = __platform_malloc(PIKA_READ_FILE_BUFF_SIZE);
|
||||
__platform_memset(file_buff, 0, PIKA_READ_FILE_BUFF_SIZE);
|
||||
FILE* input_file = __platform_fopen(filename, "r");
|
||||
if (NULL == input_file) {
|
||||
__platform_printf("Error: Couldn't open file '%s'\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
Arg* res = New_arg(NULL);
|
||||
size_t file_size =
|
||||
__platform_fread(file_buff, 1, PIKA_READ_FILE_BUFF_SIZE, input_file);
|
||||
|
||||
if (file_size >= PIKA_READ_FILE_BUFF_SIZE) {
|
||||
__platform_printf("error: not enough buff for input file.\r\n");
|
||||
__platform_printf("Error: Not enough buff for input file.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
/* add '\0' to the end of the string */
|
||||
|
Loading…
x
Reference in New Issue
Block a user