support return val from type()

This commit is contained in:
pikastech 2022-06-30 16:10:08 +08:00
parent 60bab82f60
commit 944503cd23
13 changed files with 85 additions and 53 deletions

3
examples/BuiltIn/type.py Normal file
View File

@ -0,0 +1,3 @@
list = [1, 2, 3]
if str(type(list)) == "<class 'list'>":
print('OK')

View File

@ -10,7 +10,7 @@ class MemChecker(TinyObj):
class SysObj(BaseObj):
def type(self, arg: any): ...
def type(self, arg: any) -> any: ...
def remove(self, argPath: str): ...
def int(self, arg: any) -> int: ...
def float(self, arg: any) -> float: ...

View File

@ -19,49 +19,51 @@ void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
}
}
void PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
return arg_setNull(NULL);
}
ArgType type = arg_getType(arg);
if (ARG_TYPE_INT == type) {
obj_setSysOut(self, "<class 'int'>");
return;
return arg_setStr(NULL, "", "<class 'int'>");
}
if (ARG_TYPE_FLOAT == type) {
obj_setSysOut(self, "<class 'float'>");
return;
return arg_setStr(NULL, "", "<class 'float'>");
}
if (ARG_TYPE_STRING == type) {
obj_setSysOut(self, "<class 'str'>");
return;
return arg_setStr(NULL, "", "<class 'str'>");
}
if (ARG_TYPE_OBJECT == type) {
obj_setSysOut(self, "<class 'object'>");
return;
if (argType_isObject(type)) {
PikaObj* obj = arg_getPtr(arg);
NewFun clsptr = obj_getClass(obj);
PikaObj* New_PikaStdData_List(Args * args);
if (clsptr == New_PikaStdData_List) {
return arg_setStr(NULL, "", "<class 'list'>");
}
/* dict */
PikaObj* New_PikaStdData_Dict(Args * args);
if (clsptr == New_PikaStdData_Dict) {
return arg_setStr(NULL, "", "<class 'dict'>");
}
}
if (ARG_TYPE_OBJECT_META == type) {
obj_setSysOut(self, "<class 'meta object'>");
return;
return arg_setStr(NULL, "", "<class 'meta object'>");
}
if (ARG_TYPE_BYTES == type) {
obj_setSysOut(self, "<class 'bytes'>");
return;
return arg_setStr(NULL, "", "<class 'bytes'>");
}
if (ARG_TYPE_METHOD_NATIVE == type) {
obj_setSysOut(self, "<class 'buitin_function_or_method'>");
return;
return arg_setStr(NULL, "", "<class 'buitin_function_or_method'>");
}
if (ARG_TYPE_METHOD_OBJECT == type) {
obj_setSysOut(self, "<class 'method'>");
return;
return arg_setStr(NULL, "", "<class 'method'>");
}
if (ARG_TYPE_METHOD_STATIC == type) {
obj_setSysOut(self, "<class 'function'>");
return;
return arg_setStr(NULL, "", "<class 'function'>");
}
return arg_setNull(NULL);
}
double PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=parser.dict_literal1"
"--gtest_filter=builtin.type1"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -10,7 +10,7 @@ class MemChecker(TinyObj):
class SysObj(BaseObj):
def type(self, arg: any): ...
def type(self, arg: any) -> any: ...
def remove(self, argPath: str): ...
def int(self, arg: any) -> int: ...
def float(self, arg: any) -> float: ...

View File

@ -19,49 +19,51 @@ void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
}
}
void PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return;
return arg_setNull(NULL);
}
ArgType type = arg_getType(arg);
if (ARG_TYPE_INT == type) {
obj_setSysOut(self, "<class 'int'>");
return;
return arg_setStr(NULL, "", "<class 'int'>");
}
if (ARG_TYPE_FLOAT == type) {
obj_setSysOut(self, "<class 'float'>");
return;
return arg_setStr(NULL, "", "<class 'float'>");
}
if (ARG_TYPE_STRING == type) {
obj_setSysOut(self, "<class 'str'>");
return;
return arg_setStr(NULL, "", "<class 'str'>");
}
if (ARG_TYPE_OBJECT == type) {
obj_setSysOut(self, "<class 'object'>");
return;
if (argType_isObject(type)) {
PikaObj* obj = arg_getPtr(arg);
NewFun clsptr = obj_getClass(obj);
PikaObj* New_PikaStdData_List(Args * args);
if (clsptr == New_PikaStdData_List) {
return arg_setStr(NULL, "", "<class 'list'>");
}
/* dict */
PikaObj* New_PikaStdData_Dict(Args * args);
if (clsptr == New_PikaStdData_Dict) {
return arg_setStr(NULL, "", "<class 'dict'>");
}
}
if (ARG_TYPE_OBJECT_META == type) {
obj_setSysOut(self, "<class 'meta object'>");
return;
return arg_setStr(NULL, "", "<class 'meta object'>");
}
if (ARG_TYPE_BYTES == type) {
obj_setSysOut(self, "<class 'bytes'>");
return;
return arg_setStr(NULL, "", "<class 'bytes'>");
}
if (ARG_TYPE_METHOD_NATIVE == type) {
obj_setSysOut(self, "<class 'buitin_function_or_method'>");
return;
return arg_setStr(NULL, "", "<class 'buitin_function_or_method'>");
}
if (ARG_TYPE_METHOD_OBJECT == type) {
obj_setSysOut(self, "<class 'method'>");
return;
return arg_setStr(NULL, "", "<class 'method'>");
}
if (ARG_TYPE_METHOD_STATIC == type) {
obj_setSysOut(self, "<class 'function'>");
return;
return arg_setStr(NULL, "", "<class 'function'>");
}
return arg_setNull(NULL);
}
double PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {

View File

@ -17,3 +17,21 @@ TEST(builtin, bytes) {
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
#if PIKA_BUILTIN_LIST_ENABLE
TEST(builtin, type1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/type.py");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
EXPECT_STREQ(log_buff[0], "OK\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif

View File

@ -22,8 +22,7 @@ TEST(sysObj, noMethod) {
// int errCode = args_getErrorCode(globals->list);
// printf("sysout = %s\r\n", sysOut);
// ASSERT_EQ(1, strEqu("[error] runner: method no found.", sysOut));
EXPECT_STREQ(log_buff[4],
"NameError: name 'printttt' is not defined\r\n");
EXPECT_STREQ(log_buff[4], "NameError: name 'printttt' is not defined\r\n");
// ASSERT_EQ(2, errCode);
// obj_deinit(globals);
obj_deinit(obj);

View File

@ -331,8 +331,12 @@ exit:
return method;
}
NewFun obj_getClass(PikaObj* obj) {
return (NewFun)obj_getPtr(obj, "_clsptr");
}
PikaObj* obj_getClassObj(PikaObj* obj) {
NewFun classPtr = (NewFun)obj_getPtr(obj, "_clsptr");
NewFun classPtr = obj_getClass(obj);
if (NULL == classPtr) {
return NULL;
}
@ -1137,7 +1141,6 @@ void pks_eventLisener_sendSignal(PikaEventListener* self,
/* print major version info */
void pks_printVersion(void) {
__platform_printf("pikascript-core==v%d.%d.%d (%s)\r\n",
PIKA_VERSION_MAJOR, PIKA_VERSION_MINOR,
PIKA_VERSION_MICRO, PIKA_EDIT_TIME);
__platform_printf("pikascript-core==v%d.%d.%d (%s)\r\n", PIKA_VERSION_MAJOR,
PIKA_VERSION_MINOR, PIKA_VERSION_MICRO, PIKA_EDIT_TIME);
}

View File

@ -273,6 +273,7 @@ void pks_eventLisener_init(PikaEventListener** p_self);
void pks_eventLisener_deinit(PikaEventListener** p_self);
PikaObj* methodArg_getDefContext(Arg* method_arg);
PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name);
NewFun obj_getClass(PikaObj* obj);
void pks_printVersion(void);

View File

@ -1630,6 +1630,9 @@ void VMState_solveUnusedStack(VMState* vs) {
__platform_printf("%s\r\n", arg_getStr(arg));
} else if (type == ARG_TYPE_BYTES) {
arg_printBytes(arg);
} else if (argType_isObject(type) || ARG_TYPE_POINTER == type ||
ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
__platform_printf("0x%llx\r\n", arg_getPtr(arg));
}
arg_deinit(arg);
}

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 8
#define PIKA_VERSION_MICRO 8
#define PIKA_EDIT_TIME "2022/06/30 13:21:33"
#define PIKA_EDIT_TIME "2022/06/30 16:10:06"

View File

@ -435,7 +435,8 @@ char* args_print(Args* self, char* name) {
goto exit;
}
if (argType_isObject(type)) {
if (argType_isObject(type) || ARG_TYPE_POINTER == type ||
ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
void* val = args_getPtr(self, name);
res = getPrintStringFromPtr(self, name, val);
goto exit;