Skip to content

Commit db45bd9

Browse files
committed
str(obj) from __str__ is test ok
1 parent e860275 commit db45bd9

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed

package/PikaStdLib/PikaStdLib_SysObj.c

+18-13
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,26 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
9696
ArgType type = arg_getType(arg);
9797
Args buffs = {0};
9898
char* res = NULL;
99-
do {
100-
if (ARG_TYPE_INT == type) {
101-
int val = arg_getInt(arg);
102-
res = strsFormat(&buffs, 11, "%d", val);
103-
break;
104-
}
105-
if (ARG_TYPE_FLOAT == type) {
106-
float val = arg_getFloat(arg);
107-
res = strsFormat(&buffs, 11, "%f", val);
108-
break;
99+
if (ARG_TYPE_INT == type) {
100+
int val = arg_getInt(arg);
101+
res = strsFormat(&buffs, 11, "%d", val);
102+
goto exit;
103+
}
104+
if (ARG_TYPE_FLOAT == type) {
105+
float val = arg_getFloat(arg);
106+
res = strsFormat(&buffs, 11, "%f", val);
107+
goto exit;
108+
}
109+
if (ARG_TYPE_OBJECT == type) {
110+
res = obj_toStr(arg_getPtr(arg));
111+
if (NULL != res) {
112+
goto exit;
109113
}
110-
} while (0);
111-
obj_setStr(self, "__strtmp", res);
114+
}
115+
exit:
116+
obj_setStr(self, "__buf", res);
112117
strsDeinit(&buffs);
113-
return obj_getStr(self, "__strtmp");
118+
return obj_getStr(self, "__buf");
114119
}
115120

116121
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {

port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c

+18-13
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,26 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
9696
ArgType type = arg_getType(arg);
9797
Args buffs = {0};
9898
char* res = NULL;
99-
do {
100-
if (ARG_TYPE_INT == type) {
101-
int val = arg_getInt(arg);
102-
res = strsFormat(&buffs, 11, "%d", val);
103-
break;
104-
}
105-
if (ARG_TYPE_FLOAT == type) {
106-
float val = arg_getFloat(arg);
107-
res = strsFormat(&buffs, 11, "%f", val);
108-
break;
99+
if (ARG_TYPE_INT == type) {
100+
int val = arg_getInt(arg);
101+
res = strsFormat(&buffs, 11, "%d", val);
102+
goto exit;
103+
}
104+
if (ARG_TYPE_FLOAT == type) {
105+
float val = arg_getFloat(arg);
106+
res = strsFormat(&buffs, 11, "%f", val);
107+
goto exit;
108+
}
109+
if (ARG_TYPE_OBJECT == type) {
110+
res = obj_toStr(arg_getPtr(arg));
111+
if (NULL != res) {
112+
goto exit;
109113
}
110-
} while (0);
111-
obj_setStr(self, "__strtmp", res);
114+
}
115+
exit:
116+
obj_setStr(self, "__buf", res);
112117
strsDeinit(&buffs);
113-
return obj_getStr(self, "__strtmp");
118+
return obj_getStr(self, "__buf");
114119
}
115120

116121
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {

port/linux/test/pikaMain-test.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1773,10 +1773,12 @@ TEST(pikaMain, CModule__str__) {
17731773
__platform_printf("BEGIN\r\n");
17741774
obj_run(self,
17751775
"op = PikaMath.Operator()\n"
1776-
"print(op)\n");
1776+
"print(op)\n"
1777+
"print('obj: ' + str(op))\n");
17771778
/* assert */
1778-
EXPECT_STREQ(log_buff[0], "test\r\n");
1779-
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
1779+
EXPECT_STREQ(log_buff[0], "obj: test\r\n");
1780+
EXPECT_STREQ(log_buff[1], "test\r\n");
1781+
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
17801782
/* deinit */
17811783
obj_deinit(self);
17821784
EXPECT_EQ(pikaMemNow(), 0);

src/BaseObj.c

+3-21
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,9 @@ void Baseobj_print(PikaObj* self, Args* args) {
6060
}
6161
}
6262
if (ARG_TYPE_OBJECT == arg_type) {
63-
PikaObj* obj = arg_getPtr(arg);
64-
/* clang-format off */
65-
PIKA_PYTHON(
66-
__res = __str__()
67-
)
68-
/* clang-format on */
69-
70-
/* check method arg */
71-
Arg* method_arg = obj_getMethodArg(obj, "__str__");
72-
if (NULL != method_arg) {
73-
arg_deinit(method_arg);
74-
const uint8_t bytes[] = {
75-
0x08, 0x00, /* instruct array size */
76-
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
77-
array */
78-
0x0f, 0x00, /* const pool size */
79-
0x00, 0x5f, 0x5f, 0x73, 0x74, 0x72, 0x5f, 0x5f, 0x00,
80-
0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
81-
};
82-
pikaVM_runByteCode(obj, (uint8_t*)bytes);
83-
__platform_printf("%s\r\n", obj_getStr(obj, "__res"));
63+
char* to_str = obj_toStr(arg_getPtr(arg));
64+
if (NULL != to_str) {
65+
__platform_printf("%s\r\n", to_str);
8466
return;
8567
}
8668
}

src/PikaObj.c

+26
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,29 @@ int obj_importModule(PikaObj* self, char* module_name) {
974974
obj_importModuleWithByteCode(self, module_name, bytecode);
975975
return 0;
976976
}
977+
978+
char* obj_toStr(PikaObj* self) {
979+
/* clang-format off */
980+
PIKA_PYTHON(
981+
__res = __str__()
982+
)
983+
/* clang-format on */
984+
985+
/* check method arg */
986+
Arg* method_arg = obj_getMethodArg(self, "__str__");
987+
if (NULL != method_arg) {
988+
arg_deinit(method_arg);
989+
const uint8_t bytes[] = {
990+
0x08, 0x00, /* instruct array size */
991+
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
992+
array */
993+
0x0f, 0x00, /* const pool size */
994+
0x00, 0x5f, 0x5f, 0x73, 0x74, 0x72, 0x5f, 0x5f, 0x00,
995+
0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
996+
};
997+
pikaVM_runByteCode(self, (uint8_t*)bytes);
998+
char* str_res = obj_getStr(self, "__res");
999+
return str_res;
1000+
}
1001+
return NULL;
1002+
}

src/PikaObj.h

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ int obj_importModule(PikaObj* self, char* module_name);
242242
int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr);
243243
int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr);
244244
int obj_runModule(PikaObj* self, char* module_name);
245+
char* obj_toStr(PikaObj* self);
245246

246247
#define PIKA_PYTHON_BEGIN
247248
#define PIKA_PYTHON(x)

0 commit comments

Comments
 (0)