mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
support __str__ for String(), find mem leak when print(String('test'))
This commit is contained in:
parent
9edb478e46
commit
a97d6f3f76
@ -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: ...
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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: ...
|
||||
|
@ -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()
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -2090,4 +2090,45 @@ TEST(pikaMain, slice_a12) {
|
||||
obj_deinit(self);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
#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
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user