We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e860275 commit db45bd9Copy full SHA for db45bd9
package/PikaStdLib/PikaStdLib_SysObj.c
@@ -96,21 +96,26 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
96
ArgType type = arg_getType(arg);
97
Args buffs = {0};
98
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
+ if (ARG_TYPE_INT == type) {
+ int val = arg_getInt(arg);
+ res = strsFormat(&buffs, 11, "%d", val);
+ goto exit;
+ }
+ if (ARG_TYPE_FLOAT == type) {
+ float val = arg_getFloat(arg);
+ res = strsFormat(&buffs, 11, "%f", val);
109
+ if (ARG_TYPE_OBJECT == type) {
110
+ res = obj_toStr(arg_getPtr(arg));
111
+ if (NULL != res) {
112
113
}
- } while (0);
- obj_setStr(self, "__strtmp", res);
114
115
+exit:
116
+ obj_setStr(self, "__buf", res);
117
strsDeinit(&buffs);
- return obj_getStr(self, "__strtmp");
118
+ return obj_getStr(self, "__buf");
119
120
121
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c
port/linux/test/pikaMain-test.cpp
@@ -1773,10 +1773,12 @@ TEST(pikaMain, CModule__str__) {
1773
__platform_printf("BEGIN\r\n");
1774
obj_run(self,
1775
"op = PikaMath.Operator()\n"
1776
- "print(op)\n");
+ "print(op)\n"
1777
+ "print('obj: ' + str(op))\n");
1778
/* assert */
- EXPECT_STREQ(log_buff[0], "test\r\n");
1779
- EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
+ 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");
1782
/* deinit */
1783
obj_deinit(self);
1784
EXPECT_EQ(pikaMemNow(), 0);
src/BaseObj.c
@@ -60,27 +60,9 @@ void Baseobj_print(PikaObj* self, Args* args) {
60
61
62
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"));
+ char* to_str = obj_toStr(arg_getPtr(arg));
+ if (NULL != to_str) {
+ __platform_printf("%s\r\n", to_str);
84
return;
85
86
src/PikaObj.c
@@ -974,3 +974,29 @@ int obj_importModule(PikaObj* self, char* module_name) {
974
obj_importModuleWithByteCode(self, module_name, bytecode);
975
return 0;
976
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
@@ -242,6 +242,7 @@ int obj_importModule(PikaObj* self, char* module_name);
242
int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr);
243
int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr);
244
int obj_runModule(PikaObj* self, char* module_name);
245
+char* obj_toStr(PikaObj* self);
246
247
#define PIKA_PYTHON_BEGIN
248
#define PIKA_PYTHON(x)
0 commit comments