From d77e7f3f2d40fe74a462655f3730439fdf95ccde Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 22 Jan 2023 23:16:38 +0800 Subject: [PATCH] add filter for dir() --- package/PikaStdLib/PikaStdLib.pyi | 2 +- package/PikaStdLib/PikaStdLib_SysObj.c | 10 ++++++++-- port/linux/.vscode/launch.json | 2 +- port/linux/package/pikascript/PikaStdLib.pyi | 2 +- .../PikaStdLib/PikaStdLib_SysObj.c | 10 ++++++++-- test/VM-test.cpp | 18 ++++++++++++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/package/PikaStdLib/PikaStdLib.pyi b/package/PikaStdLib/PikaStdLib.pyi index 47e34c514..303e04aa0 100644 --- a/package/PikaStdLib/PikaStdLib.pyi +++ b/package/PikaStdLib/PikaStdLib.pyi @@ -85,7 +85,7 @@ class SysObj: @staticmethod @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") - def dir(obj: object) -> list: ... + def dir(obj: any) -> list: ... @staticmethod @PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE") diff --git a/package/PikaStdLib/PikaStdLib_SysObj.c b/package/PikaStdLib/PikaStdLib_SysObj.c index 629c4ac80..46f0e7119 100644 --- a/package/PikaStdLib/PikaStdLib_SysObj.c +++ b/package/PikaStdLib/PikaStdLib_SysObj.c @@ -569,7 +569,13 @@ int32_t __dir_each(Arg* argEach, Args* context) { return 0; } -PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, PikaObj* obj) { +PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { + if (!argType_isObject(arg_getType(arg))) { + obj_setErrorCode(self, 1); + __platform_printf("[Error] dir: arg is not object.\r\n"); + return NULL; + } + PikaObj* obj = arg_getPtr(arg); PikaObj* New_PikaStdData_List(Args * args); PikaObj* list = newNormalObj(New_PikaStdData_List); __vm_List___init__(list); @@ -683,6 +689,6 @@ void PikaStdLib_SysObj_help(PikaObj* self, char* name) { } } -void PikaStdLib_SysObj_reboot(PikaObj *self){ +void PikaStdLib_SysObj_reboot(PikaObj* self) { pika_platform_reboot(); } diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 0a0453847..8978f17aa 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - "--gtest_filter=VM._3_3" + "--gtest_filter=vm.dir_issue" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/package/pikascript/PikaStdLib.pyi b/port/linux/package/pikascript/PikaStdLib.pyi index 47e34c514..303e04aa0 100644 --- a/port/linux/package/pikascript/PikaStdLib.pyi +++ b/port/linux/package/pikascript/PikaStdLib.pyi @@ -85,7 +85,7 @@ class SysObj: @staticmethod @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") - def dir(obj: object) -> list: ... + def dir(obj: any) -> list: ... @staticmethod @PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE") 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 629c4ac80..46f0e7119 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -569,7 +569,13 @@ int32_t __dir_each(Arg* argEach, Args* context) { return 0; } -PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, PikaObj* obj) { +PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { + if (!argType_isObject(arg_getType(arg))) { + obj_setErrorCode(self, 1); + __platform_printf("[Error] dir: arg is not object.\r\n"); + return NULL; + } + PikaObj* obj = arg_getPtr(arg); PikaObj* New_PikaStdData_List(Args * args); PikaObj* list = newNormalObj(New_PikaStdData_List); __vm_List___init__(list); @@ -683,6 +689,6 @@ void PikaStdLib_SysObj_help(PikaObj* self, char* name) { } } -void PikaStdLib_SysObj_reboot(PikaObj *self){ +void PikaStdLib_SysObj_reboot(PikaObj* self) { pika_platform_reboot(); } diff --git a/test/VM-test.cpp b/test/VM-test.cpp index 1d39fcdd0..18fc0d505 100644 --- a/test/VM-test.cpp +++ b/test/VM-test.cpp @@ -2491,6 +2491,24 @@ TEST(vm, num_issue_lakj) { EXPECT_EQ(pikaMemNow(), 0); } +TEST(vm, dir_issue) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain); + /* run */ + __platform_printf("BEGIN\r\n"); + obj_run(pikaMain, + "class test:\n" + " pass\n" + "dir(test)\n" + ); + /* collect */ + /* assert */ + /* deinit */ + obj_deinit(pikaMain); + EXPECT_EQ(pikaMemNow(), 0); +} + #endif TEST_END \ No newline at end of file