Эх сурвалжийг харах

str(obj) from __str__ is test ok

lyon 3 жил өмнө
parent
commit
db45bd9964

+ 18 - 13
package/PikaStdLib/PikaStdLib_SysObj.c

@@ -96,21 +96,26 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
     ArgType type = arg_getType(arg);
     Args buffs = {0};
     char* res = NULL;
-    do {
-        if (ARG_TYPE_INT == type) {
-            int val = arg_getInt(arg);
-            res = strsFormat(&buffs, 11, "%d", val);
-            break;
-        }
-        if (ARG_TYPE_FLOAT == type) {
-            float val = arg_getFloat(arg);
-            res = strsFormat(&buffs, 11, "%f", val);
-            break;
+    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);
+        goto exit;
+    }
+    if (ARG_TYPE_OBJECT == type) {
+        res = obj_toStr(arg_getPtr(arg));
+        if (NULL != res) {
+            goto exit;
         }
-    } while (0);
-    obj_setStr(self, "__strtmp", res);
+    }
+exit:
+    obj_setStr(self, "__buf", res);
     strsDeinit(&buffs);
-    return obj_getStr(self, "__strtmp");
+    return obj_getStr(self, "__buf");
 }
 
 Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {

+ 18 - 13
port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c

@@ -96,21 +96,26 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
     ArgType type = arg_getType(arg);
     Args buffs = {0};
     char* res = NULL;
-    do {
-        if (ARG_TYPE_INT == type) {
-            int val = arg_getInt(arg);
-            res = strsFormat(&buffs, 11, "%d", val);
-            break;
-        }
-        if (ARG_TYPE_FLOAT == type) {
-            float val = arg_getFloat(arg);
-            res = strsFormat(&buffs, 11, "%f", val);
-            break;
+    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);
+        goto exit;
+    }
+    if (ARG_TYPE_OBJECT == type) {
+        res = obj_toStr(arg_getPtr(arg));
+        if (NULL != res) {
+            goto exit;
         }
-    } while (0);
-    obj_setStr(self, "__strtmp", res);
+    }
+exit:
+    obj_setStr(self, "__buf", res);
     strsDeinit(&buffs);
-    return obj_getStr(self, "__strtmp");
+    return obj_getStr(self, "__buf");
 }
 
 Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {

+ 5 - 3
port/linux/test/pikaMain-test.cpp

@@ -1773,10 +1773,12 @@ TEST(pikaMain, CModule__str__) {
     __platform_printf("BEGIN\r\n");
     obj_run(self,
             "op = PikaMath.Operator()\n"
-            "print(op)\n");
+            "print(op)\n"
+            "print('obj: ' + str(op))\n");
     /* assert */
-    EXPECT_STREQ(log_buff[0], "test\r\n");
-    EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
+    EXPECT_STREQ(log_buff[0], "obj: test\r\n");
+    EXPECT_STREQ(log_buff[1], "test\r\n");
+    EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
     /* deinit */
     obj_deinit(self);
     EXPECT_EQ(pikaMemNow(), 0);

+ 3 - 21
src/BaseObj.c

@@ -60,27 +60,9 @@ void Baseobj_print(PikaObj* self, Args* args) {
         }
     }
     if (ARG_TYPE_OBJECT == arg_type) {
-        PikaObj* obj = arg_getPtr(arg);
-        /* clang-format off */
-        PIKA_PYTHON(
-            __res = __str__()
-        )
-        /* clang-format on */
-
-        /* check method arg */
-        Arg* method_arg = obj_getMethodArg(obj, "__str__");
-        if (NULL != method_arg) {
-            arg_deinit(method_arg);
-            const uint8_t bytes[] = {
-                0x08, 0x00, /* instruct array size */
-                0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
-                                                                   array */
-                0x0f, 0x00, /* const pool size */
-                0x00, 0x5f, 0x5f, 0x73, 0x74, 0x72, 0x5f, 0x5f, 0x00,
-                0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
-            };
-            pikaVM_runByteCode(obj, (uint8_t*)bytes);
-            __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);
             return;
         }
     }

+ 26 - 0
src/PikaObj.c

@@ -974,3 +974,29 @@ int obj_importModule(PikaObj* self, char* module_name) {
     obj_importModuleWithByteCode(self, module_name, bytecode);
     return 0;
 }
+
+char* obj_toStr(PikaObj* self) {
+    /* clang-format off */
+    PIKA_PYTHON(
+        __res = __str__()
+    )
+    /* clang-format on */
+
+    /* check method arg */
+    Arg* method_arg = obj_getMethodArg(self, "__str__");
+    if (NULL != method_arg) {
+        arg_deinit(method_arg);
+        const uint8_t bytes[] = {
+            0x08, 0x00, /* instruct array size */
+            0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
+                                                               array */
+            0x0f, 0x00, /* const pool size */
+            0x00, 0x5f, 0x5f, 0x73, 0x74, 0x72, 0x5f, 0x5f, 0x00,
+            0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
+        };
+        pikaVM_runByteCode(self, (uint8_t*)bytes);
+        char* str_res = obj_getStr(self, "__res");
+        return str_res;
+    }
+    return NULL;
+}

+ 1 - 0
src/PikaObj.h

@@ -242,6 +242,7 @@ int obj_importModule(PikaObj* self, char* module_name);
 int32_t obj_newMetaObj(PikaObj* self, char* objName, NewFun newFunPtr);
 int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr);
 int obj_runModule(PikaObj* self, char* module_name);
+char* obj_toStr(PikaObj* self);
 
 #define PIKA_PYTHON_BEGIN
 #define PIKA_PYTHON(x)