Răsfoiți Sursa

add mem check

add big_dict_update test
pikastech 3 ani în urmă
părinte
comite
efd9abbf17

+ 44 - 26
package/PikaStdLib/PikaStdData.pyi

@@ -58,19 +58,26 @@ class List(Tuple):
 
 
 class Dict:
-    def __init__(self): ...
-    # get an arg by the key
+    def __init__(self):
+        """ get an arg by the key """
+
     def get(self, key: str) -> any: ...
-    # set an arg by the key
-    def set(self, key: str, arg: any): ...
-    # remove an arg by the key
-    def remove(self, key: str): ...
+
+    def set(self, key: str, arg: any):
+        """ set an arg by the key """
+
+    def remove(self, key: str):
+        """ remove an arg by the key """
+
     def __iter__(self) -> any: ...
     def __next__(self) -> any: ...
-    # support dict[]  = val
-    def __setitem__(self, __key: any, __val: any): ...
-    # support val = dict[]
-    def __getitem__(self, __key: any) -> any: ...
+
+    def __setitem__(self, __key: any, __val: any):
+        """ support dict[]  = val """
+
+    def __getitem__(self, __key: any) -> any:
+        """ support val = dict[] """
+
     def __del__(self): ...
     def __str__(self) -> str: ...
     def keys(self) -> dict_keys: ...
@@ -80,6 +87,9 @@ class Dict:
     def __contains__(self, val: any) -> int:
         """ support val in dict """
 
+    def update(self, other: Dict):
+        """ update dict """
+
 
 class dict_keys:
     def __iter__(self) -> any: ...
@@ -101,12 +111,16 @@ class String:
     def get(self) -> str: ...
     def __iter__(self) -> any: ...
     def __next__(self) -> any: ...
-    # support string[]  = val
-    def __setitem__(self, __key: any, __val: any): ...
-    # support val = string[]
-    def __getitem__(self, __key: any) -> any: ...
-    # support str()
-    def __str__(self) -> str: ...
+
+    def __setitem__(self, __key: any, __val: any):
+        """ support string[]  = val """
+
+    def __getitem__(self, __key: any) -> any:
+        """ support val = string[] """
+
+    def __str__(self) -> str:
+        """ support str() """
+
     def __len__(self) -> int: ...
     def encode(self, *encoding) -> bytes: ...
     def startswith(self, prefix: str) -> int: ...
@@ -122,14 +136,18 @@ class String:
 
 
 class ByteArray:
-    # convert a string to ByteArray
-    def __init__(self, bytes: any): ...
-    # support for loop
-    def __iter__(self) -> any: ...
-    # support for loop
-    def __next__(self) -> any: ...
-    # support [] index
-    def __getitem__(self, __key: int) -> int: ...
+    def __init__(self, bytes: any):
+        """ convert a bytes to ByteArray """
+
+    def __iter__(self) -> any:
+        """ support for loop """
+
+    def __next__(self) -> any:
+        """ support for loop """
+
+    def __getitem__(self, __key: int) -> int:
+        """ support [] index """
+
     def __setitem__(self, __key: int, __val: int): ...
     def __str__(self) -> str: ...
     def decode(self) -> str: ...
@@ -148,5 +166,5 @@ class FILEIO:
 
 
 class Utils:
-    # convert a int to bytes
-    def int_to_bytes(self, val: int) -> bytes: ...
+    def int_to_bytes(self, val: int) -> bytes:
+        """ convert a int to bytes """

+ 35 - 0
package/PikaStdLib/PikaStdData_Dict.c

@@ -254,3 +254,38 @@ char* PikaStdData_dict_items___str__(PikaObj* self) {
     arg_deinit(str_arg);
     return obj_getStr(self, "_buf");
 }
+
+void PikaStdData_Dict_update(PikaObj* self, PikaObj* other) {
+    PikaObj* context = newNormalObj(New_PikaStdLib_SysObj);
+    obj_setRef(context, "@other", other);
+    obj_setRef(context, "@self", self);
+    /* clang-format off */
+    PIKA_PYTHON(
+    for @item in @other:
+        @self[@item] = @other[@item]
+    
+    )
+    /* clang-format on */
+    const uint8_t
+        bytes[] =
+            {
+                0x40, 0x00, /* instruct array size */
+                0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x04,
+                0x0d, 0x00, 0x00, 0x82, 0x11, 0x00, 0x00, 0x04, 0x1e, 0x00,
+                0x00, 0x0d, 0x1e, 0x00, 0x00, 0x07, 0x24, 0x00, 0x11, 0x81,
+                0x26, 0x00, 0x11, 0x01, 0x1e, 0x00, 0x21, 0x01, 0x01, 0x00,
+                0x21, 0x01, 0x1e, 0x00, 0x11, 0x1d, 0x00, 0x00, 0x01, 0x02,
+                0x2c, 0x00, 0x01, 0x04, 0x26, 0x00, 0x00, 0x86, 0x38, 0x00,
+                0x00, 0x8c, 0x0d, 0x00, /* instruct array */
+                0x3b, 0x00,             /* const pool size */
+                0x00, 0x40, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x00, 0x69, 0x74,
+                0x65, 0x72, 0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30,
+                0x2e, 0x5f, 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00,
+                0x40, 0x69, 0x74, 0x65, 0x6d, 0x00, 0x32, 0x00, 0x40, 0x73,
+                0x65, 0x6c, 0x66, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69,
+                0x74, 0x65, 0x6d, 0x5f, 0x5f, 0x00, 0x2d, 0x31, 0x00, /* const
+                                                                         pool */
+            };
+    pikaVM_runByteCode(context, (uint8_t*)bytes);
+    obj_deinit(context);
+}

+ 1 - 1
package/PikaStdLib/PikaStdLib.pyi

@@ -57,7 +57,7 @@ class SysObj:
 
     @staticmethod
     @PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
-    def dict() -> any: ...
+    def dict(*val) -> any: ...
 
     @staticmethod
     @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")

+ 1 - 1
package/PikaStdLib/PikaStdLib_SysObj.c

@@ -341,7 +341,7 @@ Arg* PikaStdLib_SysObj_list(PikaObj* self, PikaTuple* val) {
     return arg_newNull();
 }
 
-Arg* PikaStdLib_SysObj_dict(PikaObj* self) {
+Arg* PikaStdLib_SysObj_dict(PikaObj *self, PikaTuple* val){
 #if PIKA_BUILTIN_STRUCT_ENABLE
     PikaObj* New_PikaStdData_Dict(Args * args);
     return arg_newDirectObj(New_PikaStdData_Dict);

+ 1 - 1
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=proxy.test1"
+                // "--gtest_filter=stddata.dict_update"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 1 - 1
port/linux/config/pika_config_pool.h

@@ -1,4 +1,4 @@
 #define PIKA_STACK_BUFF_SIZE 1024 * 10
 #define PIKA_POOL_ENABLE 1
-#define PIKA_POOL_SIZE 1024 * 1024
+#define PIKA_POOL_SIZE 1024 * 1024 * 10
 #define PIKA_ASSERT_ENABLE 1

+ 44 - 26
port/linux/package/pikascript/PikaStdData.pyi

@@ -58,19 +58,26 @@ class List(Tuple):
 
 
 class Dict:
-    def __init__(self): ...
-    # get an arg by the key
+    def __init__(self):
+        """ get an arg by the key """
+
     def get(self, key: str) -> any: ...
-    # set an arg by the key
-    def set(self, key: str, arg: any): ...
-    # remove an arg by the key
-    def remove(self, key: str): ...
+
+    def set(self, key: str, arg: any):
+        """ set an arg by the key """
+
+    def remove(self, key: str):
+        """ remove an arg by the key """
+
     def __iter__(self) -> any: ...
     def __next__(self) -> any: ...
-    # support dict[]  = val
-    def __setitem__(self, __key: any, __val: any): ...
-    # support val = dict[]
-    def __getitem__(self, __key: any) -> any: ...
+
+    def __setitem__(self, __key: any, __val: any):
+        """ support dict[]  = val """
+
+    def __getitem__(self, __key: any) -> any:
+        """ support val = dict[] """
+
     def __del__(self): ...
     def __str__(self) -> str: ...
     def keys(self) -> dict_keys: ...
@@ -80,6 +87,9 @@ class Dict:
     def __contains__(self, val: any) -> int:
         """ support val in dict """
 
+    def update(self, other: Dict):
+        """ update dict """
+
 
 class dict_keys:
     def __iter__(self) -> any: ...
@@ -101,12 +111,16 @@ class String:
     def get(self) -> str: ...
     def __iter__(self) -> any: ...
     def __next__(self) -> any: ...
-    # support string[]  = val
-    def __setitem__(self, __key: any, __val: any): ...
-    # support val = string[]
-    def __getitem__(self, __key: any) -> any: ...
-    # support str()
-    def __str__(self) -> str: ...
+
+    def __setitem__(self, __key: any, __val: any):
+        """ support string[]  = val """
+
+    def __getitem__(self, __key: any) -> any:
+        """ support val = string[] """
+
+    def __str__(self) -> str:
+        """ support str() """
+
     def __len__(self) -> int: ...
     def encode(self, *encoding) -> bytes: ...
     def startswith(self, prefix: str) -> int: ...
@@ -122,14 +136,18 @@ class String:
 
 
 class ByteArray:
-    # convert a string to ByteArray
-    def __init__(self, bytes: any): ...
-    # support for loop
-    def __iter__(self) -> any: ...
-    # support for loop
-    def __next__(self) -> any: ...
-    # support [] index
-    def __getitem__(self, __key: int) -> int: ...
+    def __init__(self, bytes: any):
+        """ convert a bytes to ByteArray """
+
+    def __iter__(self) -> any:
+        """ support for loop """
+
+    def __next__(self) -> any:
+        """ support for loop """
+
+    def __getitem__(self, __key: int) -> int:
+        """ support [] index """
+
     def __setitem__(self, __key: int, __val: int): ...
     def __str__(self) -> str: ...
     def decode(self) -> str: ...
@@ -148,5 +166,5 @@ class FILEIO:
 
 
 class Utils:
-    # convert a int to bytes
-    def int_to_bytes(self, val: int) -> bytes: ...
+    def int_to_bytes(self, val: int) -> bytes:
+        """ convert a int to bytes """

+ 1 - 1
port/linux/package/pikascript/PikaStdLib.pyi

@@ -57,7 +57,7 @@ class SysObj:
 
     @staticmethod
     @PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
-    def dict() -> any: ...
+    def dict(*val) -> any: ...
 
     @staticmethod
     @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")

+ 35 - 0
port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c

@@ -254,3 +254,38 @@ char* PikaStdData_dict_items___str__(PikaObj* self) {
     arg_deinit(str_arg);
     return obj_getStr(self, "_buf");
 }
+
+void PikaStdData_Dict_update(PikaObj* self, PikaObj* other) {
+    PikaObj* context = newNormalObj(New_PikaStdLib_SysObj);
+    obj_setRef(context, "@other", other);
+    obj_setRef(context, "@self", self);
+    /* clang-format off */
+    PIKA_PYTHON(
+    for @item in @other:
+        @self[@item] = @other[@item]
+    
+    )
+    /* clang-format on */
+    const uint8_t
+        bytes[] =
+            {
+                0x40, 0x00, /* instruct array size */
+                0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00, 0x04,
+                0x0d, 0x00, 0x00, 0x82, 0x11, 0x00, 0x00, 0x04, 0x1e, 0x00,
+                0x00, 0x0d, 0x1e, 0x00, 0x00, 0x07, 0x24, 0x00, 0x11, 0x81,
+                0x26, 0x00, 0x11, 0x01, 0x1e, 0x00, 0x21, 0x01, 0x01, 0x00,
+                0x21, 0x01, 0x1e, 0x00, 0x11, 0x1d, 0x00, 0x00, 0x01, 0x02,
+                0x2c, 0x00, 0x01, 0x04, 0x26, 0x00, 0x00, 0x86, 0x38, 0x00,
+                0x00, 0x8c, 0x0d, 0x00, /* instruct array */
+                0x3b, 0x00,             /* const pool size */
+                0x00, 0x40, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x00, 0x69, 0x74,
+                0x65, 0x72, 0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30,
+                0x2e, 0x5f, 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00,
+                0x40, 0x69, 0x74, 0x65, 0x6d, 0x00, 0x32, 0x00, 0x40, 0x73,
+                0x65, 0x6c, 0x66, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69,
+                0x74, 0x65, 0x6d, 0x5f, 0x5f, 0x00, 0x2d, 0x31, 0x00, /* const
+                                                                         pool */
+            };
+    pikaVM_runByteCode(context, (uint8_t*)bytes);
+    obj_deinit(context);
+}

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

@@ -341,7 +341,7 @@ Arg* PikaStdLib_SysObj_list(PikaObj* self, PikaTuple* val) {
     return arg_newNull();
 }
 
-Arg* PikaStdLib_SysObj_dict(PikaObj* self) {
+Arg* PikaStdLib_SysObj_dict(PikaObj *self, PikaTuple* val){
 #if PIKA_BUILTIN_STRUCT_ENABLE
     PikaObj* New_PikaStdData_Dict(Args * args);
     return arg_newDirectObj(New_PikaStdData_Dict);

+ 1 - 1
port/linux/push-core.sh

@@ -5,7 +5,7 @@ FLAG_NOTE="\033[35m[Note]\033[0m"
 
 cp package/pikascript/pikascript-core/* ../../src -r
 cp package/pikascript/PikaObj.pyi ../../src
-git add ../../test/python/*.py
+git add $(find ../../test/python -name '*.py')
 sh std_push.sh PikaStdLib
 sh std_push.sh PikaStdData
 sh std_push.sh PikaDebug

+ 17 - 0
test/VM-test.cpp

@@ -1938,4 +1938,21 @@ TEST(vm, issue_dict_update) {
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
+TEST(vm, issue_big_dict_update) {
+    /* init */
+    pikaMemInfo.heapUsedMax = 0;
+    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
+    extern unsigned char pikaModules_py_a[];
+    obj_linkLibrary(pikaMain, pikaModules_py_a);
+    /* run */
+    __platform_printf("BEGIN\r\n");
+    pikaVM_runSingleFile(pikaMain,
+                         "test/python/issue/issue_big_dict_update.py");
+    /* collect */
+    /* assert */
+    /* deinit */
+    obj_deinit(pikaMain);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
 #endif

+ 9 - 0
test/compile-test.cpp

@@ -640,3 +640,12 @@ TEST(compiler, setattr) {
     Parser_linesToArray(lines);
     EXPECT_EQ(pikaMemNow(), 0);
 }
+
+TEST(compiler, dict_update) {
+    char* lines = 
+    "for @item in @other:\n"
+    "    @self[@item] = @other[@item]\n"
+    ;
+    Parser_linesToArray(lines);
+    EXPECT_EQ(pikaMemNow(), 0);
+}

Fișier diff suprimat deoarece este prea mare
+ 1 - 0
test/python/issue/issue_big_dict_update.py


+ 2 - 0
test/python/issue/issue_dict_update.py

@@ -1,4 +1,6 @@
+import PikaStdLib
 nodes = {"type": "div", "attributes": {"class": "container", "style": {"width": 240, "height": 240, "margin": 0, "padding": 0, "border-radius": 0, "border-width": 0, "border-color": "red", "background-color": 37864}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "result-text", "style": {"top": 5, "left": 5, "width": 220, "text-align": "right", "height": 30, "color": "white", "text-overflow": "longbreak", "border-width": 0, "border-color": "red", "background-color": "transparent"}}, "nodes": [], "bindings":[{"attrName": "text", "key": "result", "isText": True}], "events": [], "value":"0", "widgetName":"widget1"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 10}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"1", "widgetName":"widget3"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget2"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 60}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {
     "left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"2", "widgetName":"widget5"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget4"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 110}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"3", "widgetName":"widget7"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget6"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 160}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"+", "widgetName":"widget9"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget8"}], "bindings": [], "events": [], "widgetName": "widget0"}
 print(nodes)
 inner = nodes['nodes'][2]['nodes'][0]['widgetName']
+PikaStdLib.MemChecker().max()

+ 21 - 0
test/stddata-test.cpp

@@ -373,4 +373,25 @@ TEST(stddata, list_insert_) {
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
+TEST(stddata, dict_update) {
+    /* init */
+    pikaMemInfo.heapUsedMax = 0;
+    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
+    /* run */
+    __platform_printf("BEGIN\r\n");
+    obj_run(pikaMain,
+            "d = {'a':1, 'b':2, 'c':3}\n"
+            "d.update({'b':4, 'd':5})\n"
+            "res1 = d['b']\n"
+            "res2 = d['a']\n"
+            "print(d)\n");
+    /* collect */
+    /* assert */
+    EXPECT_EQ(obj_getInt(pikaMain, "res1"), 4);
+    EXPECT_EQ(obj_getInt(pikaMain, "res2"), 1);
+    /* deinit */
+    obj_deinit(pikaMain);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
 #endif

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff