diff --git a/port/linux/package/pikascript/PikaStdLib.py b/port/linux/package/pikascript/PikaStdLib.py index 0f87c7745..0a2ec2f96 100644 --- a/port/linux/package/pikascript/PikaStdLib.py +++ b/port/linux/package/pikascript/PikaStdLib.py @@ -31,6 +31,9 @@ class SysObj(BaseObj): def float(arg: any) -> float: pass + def str(arg: any) -> str: + pass + def iter(arg: any) -> any: pass diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c index fe049881e..ed31ea0e7 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -76,6 +76,27 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) { return -999999999; } +char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) { + ArgType type = arg_getType(arg); + Args* buffs = New_strBuff(); + char* res = NULL; + do { + if (TYPE_INT == type) { + int val = arg_getInt(arg); + res = strsFormat(buffs, 11, "%d", val); + break; + } + if (TYPE_FLOAT == type) { + float val = arg_getFloat(arg); + res = strsFormat(buffs, 11, "%f", val); + break; + } + } while (0); + obj_setStr(self, "__strtmp", res); + args_deinit(buffs); + return obj_getStr(self, "__strtmp"); +} + Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) { /* a MATE object */ if (TYPE_MATE_OBJECT == arg_getType(arg)) { diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index 3ffc3596c..a42b8b6fe 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -573,4 +573,25 @@ TEST(pikaMain, str_add_print) { /* deinit */ obj_deinit(pikaMain); EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(pikaMain, int_float_to_str) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain); + /* run */ + obj_run(pikaMain, (char*) + "a = str(1)\n" + "b = str(1.2)\n" + "\n" + ); + /* collect */ + char* a = obj_getStr(pikaMain, (char*)"a"); + char* b = obj_getStr(pikaMain, (char*)"b"); + /* assert */ + EXPECT_STREQ(a, "1"); + EXPECT_STREQ(b, "1.200000"); + /* deinit */ + obj_deinit(pikaMain); + EXPECT_EQ(pikaMemNow(), 0); } \ No newline at end of file