support obj_newTuple/List helper function

This commit is contained in:
pikastech 2023-03-30 16:28:42 +08:00
parent d2ae781a18
commit fe3dff9b3d
6 changed files with 67 additions and 41 deletions

View File

@ -25,7 +25,7 @@ assert p.dirname('/home/user/dir/file.txt') == '/home/user/dir'
# Test exists method
assert p.exists('config/pika_config_void') == False
assert p.exists('/root/go') == True
assert p.exists('/usr/bin') == True
# Test isdir method
assert p.isdir('config/pika_config_void.h') == False
@ -40,7 +40,7 @@ assert p.isabs('dir/file.txt') == False
assert p.isabs('/home/user/file.txt') == True
# Test abspath method
assert p.abspath('config/pika_config_void.h') == "/root/pikascript/port/linux/config/pika_config_void.h"
assert p.abspath('/root/go') == "/root/go"
# assert p.abspath('config/pika_config_void.h') == "/root/pikascript/port/linux/config/pika_config_void.h"
assert p.abspath('/usr/bin') == "/usr/bin"
print("PASS")

View File

@ -304,35 +304,17 @@ PikaObj* os_path_split(PikaObj* self, char* path) {
char* folder = NULL;
char* file = NULL;
PikaObj* tuple = NULL;
Arg* aFolder = NULL;
Arg* aFile = NULL;
if (0 != _os_path_split(path, &folder, &file)) {
goto __exit; // 发生错误,跳转到 __exit 处做资源回收
}
tuple = newNormalObj(New_PikaStdData_Tuple);
PikaStdData_Tuple___init__(tuple);
aFolder = arg_newStr(folder);
aFile = arg_newStr(file);
PikaStdData_List_append(tuple, aFolder);
PikaStdData_List_append(tuple, aFile);
arg_deinit(aFolder);
arg_deinit(aFile);
tuple = obj_newTuple(arg_newStr(folder), arg_newStr(file));
free(folder);
free(file);
return tuple;
__exit:
if (aFolder) {
arg_deinit(aFolder);
}
if (aFile) {
arg_deinit(aFile);
}
if (tuple) {
obj_deinit(tuple);
}

View File

@ -304,35 +304,17 @@ PikaObj* os_path_split(PikaObj* self, char* path) {
char* folder = NULL;
char* file = NULL;
PikaObj* tuple = NULL;
Arg* aFolder = NULL;
Arg* aFile = NULL;
if (0 != _os_path_split(path, &folder, &file)) {
goto __exit; // 发生错误,跳转到 __exit 处做资源回收
}
tuple = newNormalObj(New_PikaStdData_Tuple);
PikaStdData_Tuple___init__(tuple);
aFolder = arg_newStr(folder);
aFile = arg_newStr(file);
PikaStdData_List_append(tuple, aFolder);
PikaStdData_List_append(tuple, aFile);
arg_deinit(aFolder);
arg_deinit(aFile);
tuple = obj_newTuple(arg_newStr(folder), arg_newStr(file));
free(folder);
free(file);
return tuple;
__exit:
if (aFolder) {
arg_deinit(aFolder);
}
if (aFile) {
arg_deinit(aFile);
}
if (tuple) {
obj_deinit(tuple);
}

View File

@ -55,6 +55,9 @@ PikaObj* New_PikaStdData_Dict(Args* args);
PikaObj* New_PikaStdData_dict_keys(Args* args);
PikaObj* New_PikaStdData_List(Args* args);
PikaObj* New_PikaStdData_Tuple(Args* args);
void PikaStdData_Tuple___init__(PikaObj *self);
void PikaStdData_List___init__(PikaObj *self);
void PikaStdData_List_append(PikaObj *self, Arg* arg);
void _mem_cache_deinit(void);
void _VMEvent_deinit(void);
void pikaGC_markObj(PikaGC* gc, PikaObj* self);
@ -448,6 +451,44 @@ PikaObj* obj_newObjFromConstructor(PikaObj* context,
return self;
}
static PikaObj* _pika_new_obj_with_args(PikaObj* (*constructor)(),
void (*init_func)(PikaObj*),
int num_args,
va_list args) {
PikaObj* obj = newNormalObj(constructor);
init_func(obj);
for (int i = 0; i < num_args; i++) {
Arg* arg = va_arg(args, Arg*);
PikaStdData_List_append(obj, arg);
arg_deinit(arg);
}
return obj;
}
PikaObj* _pika_tuple_new(int num_args, ...) {
va_list args;
va_start(args, num_args);
PikaObj* tuple = _pika_new_obj_with_args(
New_PikaStdData_Tuple, PikaStdData_Tuple___init__, num_args, args);
va_end(args);
return tuple;
}
PikaObj* _pika_list_new(int num_args, ...) {
va_list args;
va_start(args, num_args);
PikaObj* list = _pika_new_obj_with_args(
New_PikaStdData_List, PikaStdData_List___init__, num_args, args);
va_end(args);
return list;
}
Arg* _obj_getProp(PikaObj* obj, char* name) {
NativeProperty* prop = obj_getPtr(obj, "@p");
PikaObj* class_obj = NULL;

View File

@ -395,6 +395,24 @@ Method obj_getNativeMethod(PikaObj* self, char* method_name);
PIKA_RES obj_runNativeMethod(PikaObj* self, char* method_name, Args* args);
Arg* obj_newObjInPackage(NewFun newObjFun);
/* A helper function to create a new tuple PikaObj and append the given
* arguments (of type Arg*) to it. */
PikaObj* _pika_tuple_new(int num_args, ...);
/* A helper function to create a new list PikaObj and append the given arguments
* (of type Arg*) to it. */
PikaObj* _pika_list_new(int num_args, ...);
/* Macro to create a new tuple PikaObj with the given arguments (of type Arg*).
*/
#define obj_newTuple(...) \
_pika_tuple_new(sizeof((Arg*[]){__VA_ARGS__}) / sizeof(Arg*), __VA_ARGS__)
/* Macro to create a new list PikaObj with the given arguments (of type Arg*).
*/
#define obj_newList(...) \
_pika_list_new(sizeof((Arg*[]){__VA_ARGS__}) / sizeof(Arg*), __VA_ARGS__)
PikaObj* newNormalObj(NewFun newObjFun);
Arg* arg_setRef(Arg* self, char* name, PikaObj* obj);
Arg* arg_setObj(Arg* self, char* name, PikaObj* obj);

View File

@ -37,6 +37,9 @@ void New_PikaStdData_Tuple(void) {}
void New_PikaStdData_String(void) {}
void New_PikaStdData_ByteArray(void) {}
int strGetSizeUtf8(char* str){return 0;}
void PikaStdData_Tuple___init__(PikaObj *self){}
void PikaStdData_List___init__(PikaObj *self){}
void PikaStdData_List_append(PikaObj *self, Arg* arg){}
char* string_slice(Args* outBuffs, char* str, int start, int end) {
return NULL;
}