diff --git a/package/PikaStdLib/PikaStdData.pyi b/package/PikaStdLib/PikaStdData.pyi index 208af718f..5b1e6def1 100644 --- a/package/PikaStdLib/PikaStdData.pyi +++ b/package/PikaStdLib/PikaStdData.pyi @@ -2,104 +2,60 @@ from PikaObj import * class List(TinyObj): - def __init__(self): - pass - + def __init__(self): ... # add an arg after the end of list - def append(self, arg: any): - pass - + def append(self, arg: any): ... # get an arg by the index - def get(self, i: int) -> any: - pass - + def get(self, i: int) -> any: ... # set an arg by the index - def set(self, i: int, arg: any): - pass - + def set(self, i: int, arg: any): ... # get the length of list - def len(self) -> int: - pass - + def len(self) -> int: ... # support for loop - def __iter__(self) -> any: - pass - + def __iter__(self) -> any: ... # support for loop - def __next__(self) -> any: - pass - + def __next__(self) -> any: ... # support list[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = list[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... class Dict(TinyObj): - def __init__(self): - pass - + def __init__(self): ... # get an arg by the key - def get(self, key: str) -> any: - pass - + def get(self, key: str) -> any: ... # set an arg by the key - def set(self, key: str, arg: any): - pass - + def set(self, key: str, arg: any): ... # remove an arg by the key - def remove(self, key: str): - pass - - def __iter__(self) -> any: - pass - - def __next__(self) -> any: - pass - + def remove(self, key: str): ... + def __iter__(self) -> any: ... + def __next__(self) -> any: ... # support dict[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = dict[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... class String(TinyObj): - def __init__(self, s: str): - pass - - def set(self, s: str): - pass - - def get(self) -> str: - pass - - def __iter__(self) -> any: - pass - - def __next__(self) -> any: - pass - + def __init__(self, s: str): ... + def set(self, s: str): ... + def get(self) -> str: ... + def __iter__(self) -> any: ... + def __next__(self) -> any: ... # support string[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = string[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... + # support str() + def __str__(self) -> str: ... class ByteArray(List): # convert a string to ByteArray - def fromString(self, s: str): - pass + def fromString(self, s: str): ... class Utils(TinyObj): - def int_to_bytes(self, val: int) -> bytes: - pass + # convert a int to bytes + def int_to_bytes(self, val: int) -> bytes: ... diff --git a/package/PikaStdLib/PikaStdData_String.c b/package/PikaStdLib/PikaStdData_String.c index d796e3ebe..8b1f8756c 100644 --- a/package/PikaStdLib/PikaStdData_String.c +++ b/package/PikaStdLib/PikaStdData_String.c @@ -1,4 +1,4 @@ -#include "PikaObj.h" +#include "PikaStdData_String.h" Arg* PikaStdData_String___iter__(PikaObj* self) { obj_setInt(self, "__iter_i", 0); @@ -33,8 +33,8 @@ Arg* PikaStdData_String___next__(PikaObj* self) { return res; } -Arg* PikaStdData_String___get__(PikaObj* self) { - int key_i = obj_getInt(self, "__key"); +Arg* PikaStdData_String___get__(PikaObj* self, Arg* __key) { + int key_i = arg_getInt(__key); char* str = obj_getStr(self, "str"); uint16_t len = strGetSize(str); char char_buff[] = " "; @@ -46,4 +46,19 @@ Arg* PikaStdData_String___get__(PikaObj* self) { } } -void PikaStdData_String___set__(PikaObj* self) {} +void PikaStdData_String___set__(PikaObj* self, Arg* __key, Arg* __val) { + int key_i = arg_getInt(__key); + char* str = obj_getStr(self, "str"); + char* val = arg_getStr(__val); + uint16_t len = strGetSize(str); + if (key_i >= len) { + obj_setErrorCode(self, 1); + __platform_printf("Error String Overflow\r\n"); + return; + } + str[key_i] = val[0]; +} + +char* PikaStdData_String___str__(PikaObj* self) { + return obj_getStr(self, "str"); +} diff --git a/package/PikaStdLib/PikaStdLib_SysObj.c b/package/PikaStdLib/PikaStdLib_SysObj.c index 967393884..a364dbf34 100644 --- a/package/PikaStdLib/PikaStdLib_SysObj.c +++ b/package/PikaStdLib/PikaStdLib_SysObj.c @@ -116,7 +116,7 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) { res = strsFormat(&buffs, 11, "%f", val); goto exit; } - if (ARG_TYPE_OBJECT == type) { + if (argType_isObject(type)) { res = obj_toStr(arg_getPtr(arg)); if (NULL != res) { goto exit; diff --git a/port/linux/package/pikascript/PikaStdData.pyi b/port/linux/package/pikascript/PikaStdData.pyi index 208af718f..5b1e6def1 100644 --- a/port/linux/package/pikascript/PikaStdData.pyi +++ b/port/linux/package/pikascript/PikaStdData.pyi @@ -2,104 +2,60 @@ from PikaObj import * class List(TinyObj): - def __init__(self): - pass - + def __init__(self): ... # add an arg after the end of list - def append(self, arg: any): - pass - + def append(self, arg: any): ... # get an arg by the index - def get(self, i: int) -> any: - pass - + def get(self, i: int) -> any: ... # set an arg by the index - def set(self, i: int, arg: any): - pass - + def set(self, i: int, arg: any): ... # get the length of list - def len(self) -> int: - pass - + def len(self) -> int: ... # support for loop - def __iter__(self) -> any: - pass - + def __iter__(self) -> any: ... # support for loop - def __next__(self) -> any: - pass - + def __next__(self) -> any: ... # support list[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = list[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... class Dict(TinyObj): - def __init__(self): - pass - + def __init__(self): ... # get an arg by the key - def get(self, key: str) -> any: - pass - + def get(self, key: str) -> any: ... # set an arg by the key - def set(self, key: str, arg: any): - pass - + def set(self, key: str, arg: any): ... # remove an arg by the key - def remove(self, key: str): - pass - - def __iter__(self) -> any: - pass - - def __next__(self) -> any: - pass - + def remove(self, key: str): ... + def __iter__(self) -> any: ... + def __next__(self) -> any: ... # support dict[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = dict[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... class String(TinyObj): - def __init__(self, s: str): - pass - - def set(self, s: str): - pass - - def get(self) -> str: - pass - - def __iter__(self) -> any: - pass - - def __next__(self) -> any: - pass - + def __init__(self, s: str): ... + def set(self, s: str): ... + def get(self) -> str: ... + def __iter__(self) -> any: ... + def __next__(self) -> any: ... # support string[] = val - def __set__(self, __key: any, __val: any): - pass - + def __set__(self, __key: any, __val: any): ... # support val = string[] - def __get__(self, __key: any) -> any: - pass + def __get__(self, __key: any) -> any: ... + # support str() + def __str__(self) -> str: ... class ByteArray(List): # convert a string to ByteArray - def fromString(self, s: str): - pass + def fromString(self, s: str): ... class Utils(TinyObj): - def int_to_bytes(self, val: int) -> bytes: - pass + # convert a int to bytes + def int_to_bytes(self, val: int) -> bytes: ... diff --git a/port/linux/package/pikascript/main.py b/port/linux/package/pikascript/main.py index 0127628f8..2e7c2ca48 100644 --- a/port/linux/package/pikascript/main.py +++ b/port/linux/package/pikascript/main.py @@ -9,7 +9,11 @@ import test_module1 import test_cmodule import TempDevTest as dev import TemplateDevice +from PikaStdData import String +mem = PikaStdLib.MemChecker() print('hello pikascript!') +print('mem used max:') +mem.max() dev.test() diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c index d796e3ebe..8b1f8756c 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c @@ -1,4 +1,4 @@ -#include "PikaObj.h" +#include "PikaStdData_String.h" Arg* PikaStdData_String___iter__(PikaObj* self) { obj_setInt(self, "__iter_i", 0); @@ -33,8 +33,8 @@ Arg* PikaStdData_String___next__(PikaObj* self) { return res; } -Arg* PikaStdData_String___get__(PikaObj* self) { - int key_i = obj_getInt(self, "__key"); +Arg* PikaStdData_String___get__(PikaObj* self, Arg* __key) { + int key_i = arg_getInt(__key); char* str = obj_getStr(self, "str"); uint16_t len = strGetSize(str); char char_buff[] = " "; @@ -46,4 +46,19 @@ Arg* PikaStdData_String___get__(PikaObj* self) { } } -void PikaStdData_String___set__(PikaObj* self) {} +void PikaStdData_String___set__(PikaObj* self, Arg* __key, Arg* __val) { + int key_i = arg_getInt(__key); + char* str = obj_getStr(self, "str"); + char* val = arg_getStr(__val); + uint16_t len = strGetSize(str); + if (key_i >= len) { + obj_setErrorCode(self, 1); + __platform_printf("Error String Overflow\r\n"); + return; + } + str[key_i] = val[0]; +} + +char* PikaStdData_String___str__(PikaObj* self) { + return obj_getStr(self, "str"); +} 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 967393884..a364dbf34 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -116,7 +116,7 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) { res = strsFormat(&buffs, 11, "%f", val); goto exit; } - if (ARG_TYPE_OBJECT == type) { + if (argType_isObject(type)) { res = obj_toStr(arg_getPtr(arg)); if (NULL != res) { goto exit; diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index a44667ae6..ec4265b5d 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -2090,4 +2090,45 @@ TEST(pikaMain, slice_a12) { obj_deinit(self); EXPECT_EQ(pikaMemNow(), 0); } -#endif \ No newline at end of file +#endif + +TEST(pikaMain, str_string) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + /* run */ + PikaObj* self = newRootObj("pikaMain", New_PikaMain); + __platform_printf("BEGIN\r\n"); + obj_run(self, + "a = PikaStdData.String('test')\n" + "print(a)\n" + ); + /* assert */ + EXPECT_STREQ(log_buff[0], "test\r\n"); + EXPECT_STREQ(log_buff[1], "BEGIN\r\n"); + /* deinit */ + obj_deinit(self); + EXPECT_EQ(pikaMemNow(), 0); +} + +#if PIKA_SYNTEX_ITEM_SLICE_ENABLE +TEST(pikaMain, string_index) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + /* run */ + PikaObj* self = newRootObj("pikaMain", New_PikaMain); + __platform_printf("BEGIN\r\n"); + obj_run(self, + "a = PikaStdData.String('test')\n" + "a[1]\n" + "a[1] = 'q'\n" + "print(a)\n" + ); + /* assert */ + EXPECT_STREQ(log_buff[0], "tqst\r\n"); + EXPECT_STREQ(log_buff[1], "e\r\n"); + EXPECT_STREQ(log_buff[2], "BEGIN\r\n"); + /* deinit */ + obj_deinit(self); + EXPECT_EQ(pikaMemNow(), 0); +} +#endif diff --git a/src/BaseObj.c b/src/BaseObj.c index 3410f0f81..e72a2507c 100644 --- a/src/BaseObj.c +++ b/src/BaseObj.c @@ -59,7 +59,7 @@ void Baseobj_print(PikaObj* self, Args* args) { return; } } - if (ARG_TYPE_OBJECT == arg_type) { + if (argType_isObject(arg_type)) { char* to_str = obj_toStr(arg_getPtr(arg)); if (NULL != to_str) { __platform_printf("%s\r\n", to_str); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index 98bd3b8cb..3813d9d95 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -178,7 +178,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name) { } static int32_t __foreach_handler_listModules(Arg* argEach, Args* context) { - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { PikaObj* module_obj = arg_getPtr(argEach); __platform_printf("%s\r\n", obj_getStr(module_obj, "name")); } @@ -191,7 +191,7 @@ void LibObj_listModules(LibObj* self) { static int32_t __foreach_handler_writeBytecode(Arg* argEach, Args* context) { FILE* out_file = args_getPtr(context, "out_file"); - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { PikaObj* module_obj = arg_getPtr(argEach); char* bytecode = obj_getPtr(module_obj, "bytecode"); size_t bytecode_size = obj_getBytesSize(module_obj, "buff"); @@ -206,7 +206,7 @@ static int32_t __foreach_handler_writeBytecode(Arg* argEach, Args* context) { static int32_t __foreach_handler_writeIndex(Arg* argEach, Args* context) { FILE* out_file = args_getPtr(context, "out_file"); - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { PikaObj* module_obj = arg_getPtr(argEach); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); char buff[LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)] = {0}; @@ -221,7 +221,7 @@ static int32_t __foreach_handler_writeIndex(Arg* argEach, Args* context) { } static int32_t __foreach_handler_getModuleNum(Arg* argEach, Args* context) { - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { args_setInt(context, "module_num", args_getInt(context, "module_num") + 1); } @@ -347,8 +347,7 @@ int Lib_loadLibraryFileToArray(char* origin_file_name, char* out_folder) { char* array_name = strsGetLastToken(&buffs, origin_file_name, '/'); array_name = strsReplace(&buffs, array_name, ".", "_"); __platform_printf(" loading %s[]...\n", array_name); - pika_fputs("#include \"PikaPlatform.h\"\n", - fp); + pika_fputs("#include \"PikaPlatform.h\"\n", fp); pika_fputs("/* warning: auto generated file, please do not modify */\n", fp); pika_fputs("PIKA_BYTECODE_ALIGN const unsigned char ", fp); @@ -485,7 +484,7 @@ exit: } int32_t __foreach_handler_printStates(Arg* argEach, Args* context) { - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { PikaObj* module_obj = arg_getPtr(argEach); __platform_printf("%s: %s\r\n", obj_getStr(module_obj, "name"), obj_getStr(module_obj, "state")); @@ -498,7 +497,7 @@ void pikaMaker_printStates(PikaMaker* self) { } int32_t __foreach_handler_getFirstNocompiled(Arg* argEach, Args* context) { - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { PikaObj* module_obj = arg_getPtr(argEach); char* state = obj_getStr(module_obj, "state"); if (args_isArgExist(context, "res")) { @@ -544,7 +543,7 @@ void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name) { int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { Args buffs = {0}; - if (arg_getType(argEach) == ARG_TYPE_OBJECT) { + if (argType_isObject(arg_getType(argEach))) { LibObj* lib = args_getPtr(context, "__lib"); PikaMaker* maker = args_getPtr(context, "__maker"); PikaObj* module_obj = arg_getPtr(argEach); diff --git a/src/PikaObj.c b/src/PikaObj.c index c564d4922..8104d8cf7 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -429,7 +429,7 @@ static PikaObj* __obj_getObjDirect(PikaObj* self, char* name) { return __obj_initSubObj(self, name); } /* found Objcet */ - if (type == ARG_TYPE_OBJECT || type == ARG_TYPE_OBJECT_NEW) { + if (argType_isObject(type)) { return args_getPtr(self->list, name); } return NULL; diff --git a/src/dataArg.c b/src/dataArg.c index c11aff2fc..9927836a5 100644 --- a/src/dataArg.c +++ b/src/dataArg.c @@ -385,3 +385,10 @@ uint8_t* arg_getContent(Arg* self) { void arg_setNext(Arg* self, Arg* next) { self->next = next; } + +uint8_t argType_isObject(ArgType type) { + if (ARG_TYPE_OBJECT == type || ARG_TYPE_OBJECT_NEW == type) { + return 1; + } + return 0; +} diff --git a/src/dataArg.h b/src/dataArg.h index 0a5796bd9..0cbb2da95 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -116,5 +116,6 @@ void arg_deinitHeap(Arg* self); Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size); void arg_printBytes(Arg* self); Arg* arg_loadFile(Arg* self, char* filename); +uint8_t argType_isObject(ArgType type); #endif diff --git a/src/dataArgs.c b/src/dataArgs.c index a51e006d4..4adc12d24 100644 --- a/src/dataArgs.c +++ b/src/dataArgs.c @@ -434,7 +434,7 @@ char* args_print(Args* self, char* name) { goto exit; } - if (type == ARG_TYPE_OBJECT) { + if (argType_isObject(type)) { void* val = args_getPtr(self, name); res = getPrintStringFromPtr(self, name, val); goto exit;