fix builtins namespace conflic with module

This commit is contained in:
pikastech 2023-05-22 09:53:50 +08:00
parent 87d66c758b
commit ad3b22f0f3
17 changed files with 205 additions and 1666 deletions

View File

@ -101,7 +101,7 @@ Arg* PikaStdData_dict_keys___next__(PikaObj* self) {
return res;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_dict_keys___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("dict_keys([");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
@ -116,7 +116,7 @@ char* PikaStdData_dict_keys___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
@ -151,13 +151,13 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* key_str = PikaStdLib_SysObj_str(self, item_key);
char* key_str = builtins_str(self, item_key);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, key_str);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, ": ");
char* val_str = PikaStdLib_SysObj_str(self, item_val);
char* val_str = builtins_str(self, item_val);
if (arg_getType(item_val) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
@ -248,7 +248,7 @@ char* PikaStdData_dict_items___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
str_arg = arg_strAppend(str_arg, item_str);
i++;
arg_deinit(item);

View File

@ -25,7 +25,7 @@ void PikaStdData_List___init__(PikaObj* self) {
__vm_List___init__(self);
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_List___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("[");
PikaList* list = obj_getPtr(self, "list");
@ -39,7 +39,7 @@ char* PikaStdData_List___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}

View File

@ -50,7 +50,7 @@ void PikaStdData_Tuple___del__(PikaObj* self) {
args_deinit(list);
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_Tuple___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("(");
PikaList* list = obj_getPtr(self, "list");
@ -68,7 +68,7 @@ char* PikaStdData_Tuple___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}

View File

@ -1,5 +1,6 @@
from PikaObj import *
class MemChecker:
def max(self): ...
def now(self): ...
@ -15,143 +16,7 @@ class MemChecker:
class SysObj:
@staticmethod
def int(arg: any, *base) -> int: ...
@staticmethod
def bool(arg: any) -> bool: ...
@staticmethod
def float(arg: any) -> float: ...
@staticmethod
def str(arg: any) -> str: ...
@staticmethod
def iter(arg: any) -> any: ...
@staticmethod
def range(*ax) -> any: ...
@staticmethod
def print(*val, **ops): ...
@staticmethod
def __setitem__(obj: any, key: any, val: any) -> any: ...
@staticmethod
def __getitem__(obj: any, key: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def type(arg: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def isinstance(object: any, classinfo: any) -> bool: ...
@staticmethod
def len(arg: any) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def list(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def dict(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def tuple(arg: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def hex(val: int) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def ord(val: str) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def chr(val: int) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def bytes(val: any) -> bytes: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_SYNTAX_FORMAT_ENABLE")
def cformat(fmt: str, *var) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def id(obj: any) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_FILEIO_ENABLE")
def open(path: str, mode: str) -> object: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def dir(obj: any) -> list: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE")
def exec(code: str): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE")
def eval(code: str) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def getattr(obj: object, name: str) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def setattr(obj: object, name: str, val: any): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def hasattr(obj: object, name: str) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def exit(): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def input(*info) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def abs(val: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def max(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def min(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def help(name: str): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def reboot(): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def clear(): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_GC_MARK_SWEEP_ENABLE")
def gcdump(): ...
pass
@PIKA_C_MACRO_IF("0")

View File

@ -4,649 +4,3 @@
#include "PikaStdLib_StringObj.h"
#include "PikaVM.h"
#include "dataStrs.h"
void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
obj_setErrorCode(self, 0);
int32_t res = obj_removeArg(self, argPath);
if (1 == res) {
obj_setSysOut(self, "[error] del: object no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] del: arg not match.");
obj_setErrorCode(self, 2);
return;
}
}
Arg* _type(PikaObj* self, Arg* arg);
Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
return _type(self, arg);
}
pika_float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
if (ARG_TYPE_INT == type) {
return (pika_float)arg_getInt(arg);
}
if (ARG_TYPE_FLOAT == type) {
return (pika_float)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return strtod(arg_getStr(arg), NULL);
}
if (ARG_TYPE_BOOL == type) {
return (pika_float)arg_getBool(arg);
}
obj_setSysOut(self, "[error] convert to pika_float type failed.");
obj_setErrorCode(self, 1);
return _PIKA_FLOAT_ERR;
}
PIKA_RES _transeInt(Arg* arg, int base, int64_t* res);
int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg, PikaTuple* base) {
int64_t res = 0;
int64_t iBase = 10;
if (pikaTuple_getSize(base) > 0) {
if (arg_getType(arg) != ARG_TYPE_STRING &&
arg_getType(arg) != ARG_TYPE_BYTES) {
obj_setSysOut(self,
"TypeError: int() can't convert non-string with "
"explicit base");
obj_setErrorCode(self, 1);
return _PIKA_INT_ERR;
}
iBase = (int64_t)pikaTuple_getInt(base, 0);
}
if (_transeInt(arg, iBase, &res) == PIKA_RES_OK) {
return res;
}
obj_setSysOut(self, "ValueError: invalid literal for int()");
obj_setErrorCode(self, 1);
return _PIKA_INT_ERR;
}
pika_bool PikaStdLib_SysObj_bool(PikaObj* self, Arg* arg) {
int64_t res = 0;
if (_transeInt(arg, 10, &res) == PIKA_RES_OK) {
return res ? PIKA_TRUE : pika_false;
}
obj_setSysOut(self, "ValueError: invalid literal for bool()");
obj_setErrorCode(self, 1);
return _PIKA_BOOL_ERR;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
// if (arg_getType(arg) == ARG_TYPE_BYTES) {
// return obj_cacheStr(self, (char*)arg_getBytes(arg));
// }
Arg* arg_str = arg_toStrArg(arg);
if (NULL == arg_str) {
obj_setSysOut(self, "Error: convert to str type failed.");
obj_setErrorCode(self, 1);
return NULL;
}
char* str = obj_cacheStr(self, arg_getStr(arg_str));
arg_deinit(arg_str);
return str;
}
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
/* object */
pika_bool bIsTemp = pika_false;
PikaObj* oArg = _arg_to_obj(arg, &bIsTemp);
NewFun _clsptr = (NewFun)oArg->constructor;
if (_clsptr == New_PikaStdLib_RangeObj) {
/* found RangeObj, return directly */
return arg_copy(arg);
}
/* clang-format off */
PIKA_PYTHON(
@res_iter = __iter__()
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0a, 0x00, /* instruct array */
0x14, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x5f, 0x5f, 0x00, 0x40,
0x72, 0x65, 0x73, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x00, /* const pool */
};
Arg* res = pikaVM_runByteCodeReturn(oArg, (uint8_t*)bytes, "@res_iter");
if (bIsTemp) {
obj_refcntDec(oArg);
}
return res;
}
Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) {
/* set template arg to create rangeObj */
Arg* aRangeObj = arg_newDirectObj(New_PikaStdLib_RangeObj);
PikaObj* oRangeObj = arg_getPtr(aRangeObj);
RangeData tRangeData = {0};
if (pikaTuple_getSize(ax) == 1) {
int start = 0;
int end = arg_getInt(pikaTuple_getArg(ax, 0));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = 1;
} else if (pikaTuple_getSize(ax) == 2) {
int start = arg_getInt(pikaTuple_getArg(ax, 0));
int end = arg_getInt(pikaTuple_getArg(ax, 1));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = 1;
} else if (pikaTuple_getSize(ax) == 3) {
int start = arg_getInt(pikaTuple_getArg(ax, 0));
int end = arg_getInt(pikaTuple_getArg(ax, 1));
int step = arg_getInt(pikaTuple_getArg(ax, 2));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = step;
}
tRangeData.i = tRangeData.start;
obj_setStruct(oRangeObj, "_", tRangeData);
return aRangeObj;
}
Arg* PikaStdLib_SysObj___getitem__(PikaObj* self, Arg* obj, Arg* key) {
return _vm_get(NULL, self, key, obj);
}
Arg* PikaStdLib_SysObj___setitem__(PikaObj* self,
Arg* obj,
Arg* key,
Arg* val) {
ArgType obj_type = arg_getType(obj);
if (ARG_TYPE_STRING == obj_type) {
int index = arg_getInt(key);
char* str_val = arg_getStr(val);
char* str_pyload = arg_getStr(obj);
str_pyload[index] = str_val[0];
return arg_newStr(str_pyload);
}
if (ARG_TYPE_BYTES == obj_type) {
int index = arg_getInt(key);
uint8_t byte_val = 0;
if (ARG_TYPE_BYTES == arg_getType(val)) {
uint8_t* bytes_val = arg_getBytes(val);
byte_val = bytes_val[0];
}
if (ARG_TYPE_INT == arg_getType(val)) {
byte_val = arg_getInt(val);
}
uint8_t* bytes_pyload = arg_getBytes(obj);
size_t bytes_len = arg_getBytesSize(obj);
bytes_pyload[index] = byte_val;
return arg_newBytes(bytes_pyload, bytes_len);
}
if (argType_isObject(obj_type)) {
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
obj_setArg(arg_obj, "__val", val);
/* clang-format off */
PIKA_PYTHON(
__setitem__(__key, __val)
)
/* clang-format on */
const uint8_t bytes[] = {
0x0c, 0x00, 0x00, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x10, 0x01, 0x07, 0x00, 0x00, 0x02, 0x0d,
0x00,
/* instruct array */
0x19, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x76, 0x61,
0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d,
0x5f, 0x5f, 0x00,
/* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
return arg_newRef(arg_obj);
}
return NULL;
}
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == arg_getType(arg)) {
return strGetSize(arg_getStr(arg));
}
if (ARG_TYPE_BYTES == arg_getType(arg)) {
return arg_getBytesSize(arg);
}
if (arg_isObject(arg)) {
PikaObj* arg_obj = arg_getPtr(arg);
Arg* method_arg = obj_getMethodArgWithFullPath(arg_obj, "__len__");
if (NULL != method_arg) {
arg_deinit(method_arg);
obj_removeArg(arg_obj, "@res_len");
/* clang-format off */
PIKA_PYTHON(
@res_len = __len__()
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
array */
0x12, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6c, 0x65, 0x6e, 0x5f, 0x5f, 0x00, 0x40,
0x72, 0x65, 0x73, 0x5f, 0x6c, 0x65, 0x6e, 0x00, /* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
return obj_getInt(arg_obj, "@res_len");
}
}
obj_setErrorCode(self, 1);
__platform_printf("[Error] len: arg type not support\r\n");
return -1;
}
Arg* PikaStdLib_SysObj_list(PikaObj* self, PikaTuple* val) {
#if PIKA_BUILTIN_STRUCT_ENABLE
if (1 == pikaTuple_getSize(val)) {
Arg* in = pikaTuple_getArg(val, 0);
obj_setArg(self, "__list", in);
/* clang-format off */
PIKA_PYTHON(
@res_list = []
for __item in __list:
@res_list.append(__item)
del __item
del __list
)
/* clang-format on */
const uint8_t bytes[] = {
0x3c, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x95, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x10, 0x81, 0x0b,
0x00, 0x00, 0x02, 0x12, 0x00, 0x00, 0x04, 0x17, 0x00, 0x00, 0x82,
0x1b, 0x00, 0x00, 0x04, 0x28, 0x00, 0x00, 0x0d, 0x28, 0x00, 0x00,
0x07, 0x2f, 0x00, 0x11, 0x81, 0x28, 0x00, 0x01, 0x02, 0x31, 0x00,
0x00, 0x86, 0x42, 0x00, 0x00, 0x8c, 0x17, 0x00, 0x00, 0x8c, 0x28,
0x00, 0x00, 0x8c, 0x0b, 0x00,
/* instruct array */
0x45, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x40, 0x72, 0x65, 0x73, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x00,
0x5f, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x00, 0x69, 0x74, 0x65, 0x72,
0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30, 0x2e, 0x5f, 0x5f,
0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x69, 0x74,
0x65, 0x6d, 0x00, 0x32, 0x00, 0x40, 0x72, 0x65, 0x73, 0x5f, 0x6c,
0x69, 0x73, 0x74, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00,
0x2d, 0x31, 0x00, /* const pool */
};
return pikaVM_runByteCodeReturn(self, (uint8_t*)bytes, "@res_list");
}
PikaObj* New_PikaStdData_List(Args * args);
return arg_newDirectObj(New_PikaStdData_List);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in list is not enabled.\r\n");
return arg_newNull();
#endif
}
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);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in dist is not enabled.\r\n");
return arg_newNull();
#endif
}
Arg* PikaStdLib_SysObj_tuple(PikaObj* self, Arg* val) {
#if PIKA_BUILTIN_STRUCT_ENABLE
obj_setErrorCode(self, 1);
__platform_printf("Error: tuple() is not supported.\r\n");
return arg_newNone();
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in tuple is not enabled.\r\n");
return arg_newNull();
#endif
}
char* PikaStdLib_SysObj_hex(PikaObj* self, int val) {
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
if (val >= 0) {
__platform_sprintf(buff, "0x%02x", val);
} else {
__platform_sprintf(buff, "-0x%02x", -val);
}
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
int PikaStdLib_SysObj_ord(PikaObj* self, char* val) {
return (int)val[0];
}
char* PikaStdLib_SysObj_chr(PikaObj* self, int val) {
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
char to_str[] = "0";
to_str[0] = val;
__platform_sprintf(buff, "%s", to_str);
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
Arg* PikaStdLib_SysObj_bytes(PikaObj* self, Arg* val) {
ArgType type = arg_getType(val);
if (ARG_TYPE_INT == type) {
int size = arg_getInt(val);
/* src is NULL so the bytes are all '\0' */
Arg* bytes = arg_newBytes(NULL, size);
return bytes;
}
if (ARG_TYPE_BYTES == type) {
return arg_copy(val);
}
if (ARG_TYPE_STRING == type) {
int size = strGetSize(arg_getStr(val));
Arg* bytes = arg_newBytes((uint8_t*)arg_getStr(val), size);
return bytes;
}
#if !PIKA_NANO_ENABLE
if (argType_isObject(type)) {
PikaObj* obj = arg_getPtr(val);
PikaObj* New_PikaStdData_List(Args * args);
PikaObj* New_PikaStdData_Tuple(Args * args);
if (obj->constructor == New_PikaStdData_List ||
obj->constructor == New_PikaStdData_Tuple) {
PikaList* list = obj_getPtr(obj, "list");
Arg* bytes = arg_newBytes(NULL, pikaList_getSize(list));
uint8_t* bytes_raw = arg_getBytes(bytes);
for (size_t i = 0; i < pikaList_getSize(list); i++) {
bytes_raw[i] = (uint8_t)pikaList_getInt(list, i);
}
return bytes;
}
}
#endif
obj_setErrorCode(self, 1);
__platform_printf("Error: input arg type not supported.\r\n");
return arg_newNone();
}
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
int arg_size = pikaTuple_getSize(val);
char* end = pikaDict_getStr(ops, "end");
if (NULL == end) {
/* default */
end = "\r\n";
}
if (arg_size == 1) {
arg_print(pikaTuple_getArg(val, 0), pika_false, end);
return;
}
Arg* print_out_arg = NULL;
pika_bool is_get_print = pika_false;
for (int i = 0; i < arg_size; i++) {
Arg* arg = pikaTuple_getArg(val, i);
Arg* item_arg_str = arg_toStrArg(arg);
if (NULL != item_arg_str) {
is_get_print = PIKA_TRUE;
if (NULL == print_out_arg) {
print_out_arg = arg_newStr("");
}
print_out_arg =
arg_strAppend(print_out_arg, arg_getStr(item_arg_str));
if (i < arg_size - 1) {
print_out_arg = arg_strAppend(print_out_arg, " ");
}
arg_deinit(item_arg_str);
}
}
if (PIKA_TRUE == is_get_print) {
__platform_printf("%s%s", arg_getStr(print_out_arg), end);
}
if (NULL != print_out_arg) {
arg_deinit(print_out_arg);
}
}
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
#if PIKA_SYNTAX_FORMAT_ENABLE
Args buffs = {0};
pikaMemMaxReset();
char* res = strsFormatList(&buffs, fmt, &var->super);
obj_setStr(self, "_buf", res);
res = obj_getStr(self, "_buf");
strsDeinit(&buffs);
return res;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_SYNTAX_FORMAT_ENABLE is not enabled.\r\n");
return NULL;
#endif
}
int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
uintptr_t ptr = 0;
if (arg_isObject(obj)) {
ptr = (uintptr_t)arg_getPtr(obj);
} else {
ptr = (uintptr_t)obj;
}
return ptr & (0x7FFFFFFF);
}
PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* path, char* mode) {
#if PIKA_FILEIO_ENABLE
PikaObj* file = newNormalObj(New_PikaStdData_FILEIO);
if (0 != PikaStdData_FILEIO_init(file, path, mode)) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] open: can not open file.\r\n");
obj_deinit(file);
return NULL;
}
return file;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n");
return NULL;
#endif
}
/* __dir_each */
int32_t __dir_each(Arg* argEach, void* context);
PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) {
if (!arg_isObject(arg)) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] dir: not support type.\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);
Args* context = New_args(NULL);
args_setPtr(context, "list", list);
args_foreach(obj->list, __dir_each, context);
args_deinit(context);
return list;
}
void PikaStdLib_SysObj_exec(PikaObj* self, char* code) {
#if PIKA_EXEC_ENABLE
obj_run(self, code);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_EXEC_ENABLE is not enabled.\r\n");
#endif
}
Arg* PikaStdLib_SysObj_getattr(PikaObj* self, PikaObj* obj, char* name) {
Arg* res = NULL;
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] getattr: can not get attr of NULL.\r\n");
return NULL;
}
Arg* arg = obj_getArg(obj, name);
if (NULL == arg) {
arg = obj_getMethodArgWithFullPath(obj, name);
}
if (NULL != arg) {
res = arg_copy(arg);
methodArg_setHostObj(res, obj);
}
return res;
}
void PikaStdLib_SysObj_setattr(PikaObj* self,
PikaObj* obj,
char* name,
Arg* val) {
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] setattr: obj is null.\r\n");
goto exit;
}
obj_setArg(obj, name, val);
exit:
return;
}
void PikaStdLib_SysObj_exit(PikaObj* self) {
pks_vm_exit();
}
int PikaStdLib_SysObj_hasattr(PikaObj* self, PikaObj* obj, char* name) {
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] hasattr: obj is null.\r\n");
return 0;
}
if (obj_isArgExist(obj, name)) {
return 1;
}
Arg* method = obj_getMethodArgWithFullPath(obj, name);
if (NULL != method) {
arg_deinit(method);
return 1;
}
return 0;
}
Arg* PikaStdLib_SysObj_eval(PikaObj* self, char* code) {
Args buffs = {0};
char* cmd = strsAppend(&buffs, "@res = ", code);
obj_run(self, cmd);
Arg* res = arg_copy(obj_getArg(self, "@res"));
strsDeinit(&buffs);
obj_removeArg(self, "@res");
return res;
}
static enum shellCTRL __obj_shellLineHandler_input(PikaObj* self,
char* input_line,
struct ShellConfig* cfg) {
cfg->context = arg_newStr(input_line);
return SHELL_CTRL_EXIT;
}
char* PikaStdLib_SysObj_input(PikaObj* self, PikaTuple* info) {
struct ShellConfig cfg = {
.prefix = "",
.context = NULL,
.handler = __obj_shellLineHandler_input,
.fn_getchar = __platform_getchar,
};
if (pikaTuple_getSize(info) > 0) {
__platform_printf("%s", pikaTuple_getStr(info, 0));
}
_temp__do_pikaScriptShell(self, &cfg);
char* res = obj_cacheStr(self, arg_getStr(cfg.context));
arg_deinit(cfg.context);
return res;
}
extern volatile PikaObj* __pikaMain;
void PikaStdLib_SysObj_help(PikaObj* self, char* name) {
if (strEqu(name, "modules")) {
obj_printModules((PikaObj*)__pikaMain);
}
}
void PikaStdLib_SysObj_reboot(PikaObj* self) {
pika_platform_reboot();
}
void PikaStdLib_SysObj_clear(PikaObj* self) {
pika_platform_clear();
}
void PikaStdLib_SysObj_gcdump(PikaObj* self) {
pikaGC_markDump();
}
Arg* PikaStdLib_SysObj_abs(PikaObj* self, Arg* val) {
ArgType type = arg_getType(val);
if (type == ARG_TYPE_INT) {
int64_t v = arg_getInt(val);
if (v < 0) {
v = -v;
}
return arg_newInt(v);
}
if (type == ARG_TYPE_FLOAT) {
pika_float v = arg_getFloat(val);
if (v < 0) {
v = -v;
}
return arg_newFloat(v);
}
obj_setSysOut(self, "TypeError: bad operand type for abs()");
obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
return NULL;
}
PikaObj* New_PikaStdData_Tuple(Args* args);
/* clang-format off */
PIKA_PYTHON(
@res_max = @list[0]
for @item in @list:
if @item > @res_max:
@res_max = @item
)
/* clang-format on */
extern const uint8_t bc_max[];
/* clang-format off */
PIKA_PYTHON(
@res_max = @list[0]
for @item in @list:
if @item < @res_max:
@res_max = @item
)
/* clang-format on */
extern const uint8_t bc_min[];
Arg* _max_min(PikaObj* self, PikaTuple* val, uint8_t* bc);
Arg* PikaStdLib_SysObj_max(PikaObj* self, PikaTuple* val) {
return _max_min(self, val, (uint8_t*)bc_max);
}
Arg* PikaStdLib_SysObj_min(PikaObj* self, PikaTuple* val) {
return _max_min(self, val, (uint8_t*)bc_min);
}
pika_bool _isinstance(PikaObj* self, Arg* object, Arg* classinfo);
pika_bool PikaStdLib_SysObj_isinstance(PikaObj* self,
Arg* object,
Arg* classinfo) {
return _isinstance(self, object, classinfo);
}

View File

@ -137,3 +137,12 @@ def clear(): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_GC_MARK_SWEEP_ENABLE")
def gcdump(): ...
@PIKA_C_MACRO_IF("0")
class RangeObj:
def __next__(self) -> any: ...
@PIKA_C_MACRO_IF("0")
class StringObj:
def __next__(self) -> any: ...

View File

@ -24,7 +24,7 @@
// "--gtest_filter=lua.eval"
// "--gtest_filter=eventloop.once1"
// "--gtest_filter=parser.fn_fn"
"--gtest_filter=builtin.isinstance"
// "--gtest_filter=builtin.isinstance"
// "--gtest_filter=VM.run_def_add"
// "--gtest_filter=parser.slice_fn"
],

View File

@ -1,5 +1,6 @@
from PikaObj import *
class MemChecker:
def max(self): ...
def now(self): ...
@ -15,143 +16,7 @@ class MemChecker:
class SysObj:
@staticmethod
def int(arg: any, *base) -> int: ...
@staticmethod
def bool(arg: any) -> bool: ...
@staticmethod
def float(arg: any) -> float: ...
@staticmethod
def str(arg: any) -> str: ...
@staticmethod
def iter(arg: any) -> any: ...
@staticmethod
def range(*ax) -> any: ...
@staticmethod
def print(*val, **ops): ...
@staticmethod
def __setitem__(obj: any, key: any, val: any) -> any: ...
@staticmethod
def __getitem__(obj: any, key: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def type(arg: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def isinstance(object: any, classinfo: any) -> bool: ...
@staticmethod
def len(arg: any) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def list(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def dict(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE")
def tuple(arg: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def hex(val: int) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def ord(val: str) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def chr(val: int) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def bytes(val: any) -> bytes: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_SYNTAX_FORMAT_ENABLE")
def cformat(fmt: str, *var) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def id(obj: any) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_FILEIO_ENABLE")
def open(path: str, mode: str) -> object: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def dir(obj: any) -> list: ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE")
def exec(code: str): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_EXEC_ENABLE")
def eval(code: str) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def getattr(obj: object, name: str) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def setattr(obj: object, name: str, val: any): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def hasattr(obj: object, name: str) -> int: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def exit(): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def input(*info) -> str: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def abs(val: any) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def max(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def min(*val) -> any: ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def help(name: str): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def reboot(): ...
@staticmethod
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
def clear(): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_GC_MARK_SWEEP_ENABLE")
def gcdump(): ...
pass
@PIKA_C_MACRO_IF("0")

View File

@ -137,3 +137,12 @@ def clear(): ...
@staticmethod
@PIKA_C_MACRO_IF("PIKA_GC_MARK_SWEEP_ENABLE")
def gcdump(): ...
@PIKA_C_MACRO_IF("0")
class RangeObj:
def __next__(self) -> any: ...
@PIKA_C_MACRO_IF("0")
class StringObj:
def __next__(self) -> any: ...

View File

@ -101,7 +101,7 @@ Arg* PikaStdData_dict_keys___next__(PikaObj* self) {
return res;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_dict_keys___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("dict_keys([");
PikaObj* dictptr = obj_getPtr(self, "dictptr");
@ -116,7 +116,7 @@ char* PikaStdData_dict_keys___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
@ -151,13 +151,13 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* key_str = PikaStdLib_SysObj_str(self, item_key);
char* key_str = builtins_str(self, item_key);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, key_str);
str_arg = arg_strAppend(str_arg, "'");
str_arg = arg_strAppend(str_arg, ": ");
char* val_str = PikaStdLib_SysObj_str(self, item_val);
char* val_str = builtins_str(self, item_val);
if (arg_getType(item_val) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}
@ -248,7 +248,7 @@ char* PikaStdData_dict_items___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
str_arg = arg_strAppend(str_arg, item_str);
i++;
arg_deinit(item);

View File

@ -25,7 +25,7 @@ void PikaStdData_List___init__(PikaObj* self) {
__vm_List___init__(self);
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_List___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("[");
PikaList* list = obj_getPtr(self, "list");
@ -39,7 +39,7 @@ char* PikaStdData_List___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}

View File

@ -50,7 +50,7 @@ void PikaStdData_Tuple___del__(PikaObj* self) {
args_deinit(list);
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg);
char* builtins_str(PikaObj* self, Arg* arg);
char* PikaStdData_Tuple___str__(PikaObj* self) {
Arg* str_arg = arg_newStr("(");
PikaList* list = obj_getPtr(self, "list");
@ -68,7 +68,7 @@ char* PikaStdData_Tuple___str__(PikaObj* self) {
if (i != 0) {
str_arg = arg_strAppend(str_arg, ", ");
}
char* item_str = PikaStdLib_SysObj_str(self, item);
char* item_str = builtins_str(self, item);
if (arg_getType(item) == ARG_TYPE_STRING) {
str_arg = arg_strAppend(str_arg, "'");
}

View File

@ -4,649 +4,3 @@
#include "PikaStdLib_StringObj.h"
#include "PikaVM.h"
#include "dataStrs.h"
void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) {
obj_setErrorCode(self, 0);
int32_t res = obj_removeArg(self, argPath);
if (1 == res) {
obj_setSysOut(self, "[error] del: object no found.");
obj_setErrorCode(self, 1);
return;
}
if (2 == res) {
obj_setSysOut(self, "[error] del: arg not match.");
obj_setErrorCode(self, 2);
return;
}
}
Arg* _type(PikaObj* self, Arg* arg);
Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
return _type(self, arg);
}
pika_float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
if (ARG_TYPE_INT == type) {
return (pika_float)arg_getInt(arg);
}
if (ARG_TYPE_FLOAT == type) {
return (pika_float)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return strtod(arg_getStr(arg), NULL);
}
if (ARG_TYPE_BOOL == type) {
return (pika_float)arg_getBool(arg);
}
obj_setSysOut(self, "[error] convert to pika_float type failed.");
obj_setErrorCode(self, 1);
return _PIKA_FLOAT_ERR;
}
PIKA_RES _transeInt(Arg* arg, int base, int64_t* res);
int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg, PikaTuple* base) {
int64_t res = 0;
int64_t iBase = 10;
if (pikaTuple_getSize(base) > 0) {
if (arg_getType(arg) != ARG_TYPE_STRING &&
arg_getType(arg) != ARG_TYPE_BYTES) {
obj_setSysOut(self,
"TypeError: int() can't convert non-string with "
"explicit base");
obj_setErrorCode(self, 1);
return _PIKA_INT_ERR;
}
iBase = (int64_t)pikaTuple_getInt(base, 0);
}
if (_transeInt(arg, iBase, &res) == PIKA_RES_OK) {
return res;
}
obj_setSysOut(self, "ValueError: invalid literal for int()");
obj_setErrorCode(self, 1);
return _PIKA_INT_ERR;
}
pika_bool PikaStdLib_SysObj_bool(PikaObj* self, Arg* arg) {
int64_t res = 0;
if (_transeInt(arg, 10, &res) == PIKA_RES_OK) {
return res ? PIKA_TRUE : pika_false;
}
obj_setSysOut(self, "ValueError: invalid literal for bool()");
obj_setErrorCode(self, 1);
return _PIKA_BOOL_ERR;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
// if (arg_getType(arg) == ARG_TYPE_BYTES) {
// return obj_cacheStr(self, (char*)arg_getBytes(arg));
// }
Arg* arg_str = arg_toStrArg(arg);
if (NULL == arg_str) {
obj_setSysOut(self, "Error: convert to str type failed.");
obj_setErrorCode(self, 1);
return NULL;
}
char* str = obj_cacheStr(self, arg_getStr(arg_str));
arg_deinit(arg_str);
return str;
}
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
/* object */
pika_bool bIsTemp = pika_false;
PikaObj* oArg = _arg_to_obj(arg, &bIsTemp);
NewFun _clsptr = (NewFun)oArg->constructor;
if (_clsptr == New_PikaStdLib_RangeObj) {
/* found RangeObj, return directly */
return arg_copy(arg);
}
/* clang-format off */
PIKA_PYTHON(
@res_iter = __iter__()
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0a, 0x00, /* instruct array */
0x14, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x5f, 0x5f, 0x00, 0x40,
0x72, 0x65, 0x73, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x00, /* const pool */
};
Arg* res = pikaVM_runByteCodeReturn(oArg, (uint8_t*)bytes, "@res_iter");
if (bIsTemp) {
obj_refcntDec(oArg);
}
return res;
}
Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) {
/* set template arg to create rangeObj */
Arg* aRangeObj = arg_newDirectObj(New_PikaStdLib_RangeObj);
PikaObj* oRangeObj = arg_getPtr(aRangeObj);
RangeData tRangeData = {0};
if (pikaTuple_getSize(ax) == 1) {
int start = 0;
int end = arg_getInt(pikaTuple_getArg(ax, 0));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = 1;
} else if (pikaTuple_getSize(ax) == 2) {
int start = arg_getInt(pikaTuple_getArg(ax, 0));
int end = arg_getInt(pikaTuple_getArg(ax, 1));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = 1;
} else if (pikaTuple_getSize(ax) == 3) {
int start = arg_getInt(pikaTuple_getArg(ax, 0));
int end = arg_getInt(pikaTuple_getArg(ax, 1));
int step = arg_getInt(pikaTuple_getArg(ax, 2));
tRangeData.start = start;
tRangeData.end = end;
tRangeData.step = step;
}
tRangeData.i = tRangeData.start;
obj_setStruct(oRangeObj, "_", tRangeData);
return aRangeObj;
}
Arg* PikaStdLib_SysObj___getitem__(PikaObj* self, Arg* obj, Arg* key) {
return _vm_get(NULL, self, key, obj);
}
Arg* PikaStdLib_SysObj___setitem__(PikaObj* self,
Arg* obj,
Arg* key,
Arg* val) {
ArgType obj_type = arg_getType(obj);
if (ARG_TYPE_STRING == obj_type) {
int index = arg_getInt(key);
char* str_val = arg_getStr(val);
char* str_pyload = arg_getStr(obj);
str_pyload[index] = str_val[0];
return arg_newStr(str_pyload);
}
if (ARG_TYPE_BYTES == obj_type) {
int index = arg_getInt(key);
uint8_t byte_val = 0;
if (ARG_TYPE_BYTES == arg_getType(val)) {
uint8_t* bytes_val = arg_getBytes(val);
byte_val = bytes_val[0];
}
if (ARG_TYPE_INT == arg_getType(val)) {
byte_val = arg_getInt(val);
}
uint8_t* bytes_pyload = arg_getBytes(obj);
size_t bytes_len = arg_getBytesSize(obj);
bytes_pyload[index] = byte_val;
return arg_newBytes(bytes_pyload, bytes_len);
}
if (argType_isObject(obj_type)) {
PikaObj* arg_obj = arg_getPtr(obj);
obj_setArg(arg_obj, "__key", key);
obj_setArg(arg_obj, "__val", val);
/* clang-format off */
PIKA_PYTHON(
__setitem__(__key, __val)
)
/* clang-format on */
const uint8_t bytes[] = {
0x0c, 0x00, 0x00, 0x00, /* instruct array size */
0x10, 0x81, 0x01, 0x00, 0x10, 0x01, 0x07, 0x00, 0x00, 0x02, 0x0d,
0x00,
/* instruct array */
0x19, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x76, 0x61,
0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d,
0x5f, 0x5f, 0x00,
/* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
return arg_newRef(arg_obj);
}
return NULL;
}
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == arg_getType(arg)) {
return strGetSize(arg_getStr(arg));
}
if (ARG_TYPE_BYTES == arg_getType(arg)) {
return arg_getBytesSize(arg);
}
if (arg_isObject(arg)) {
PikaObj* arg_obj = arg_getPtr(arg);
Arg* method_arg = obj_getMethodArgWithFullPath(arg_obj, "__len__");
if (NULL != method_arg) {
arg_deinit(method_arg);
obj_removeArg(arg_obj, "@res_len");
/* clang-format off */
PIKA_PYTHON(
@res_len = __len__()
)
/* clang-format on */
const uint8_t bytes[] = {
0x08, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
array */
0x12, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x5f, 0x5f, 0x6c, 0x65, 0x6e, 0x5f, 0x5f, 0x00, 0x40,
0x72, 0x65, 0x73, 0x5f, 0x6c, 0x65, 0x6e, 0x00, /* const pool */
};
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
return obj_getInt(arg_obj, "@res_len");
}
}
obj_setErrorCode(self, 1);
__platform_printf("[Error] len: arg type not support\r\n");
return -1;
}
Arg* PikaStdLib_SysObj_list(PikaObj* self, PikaTuple* val) {
#if PIKA_BUILTIN_STRUCT_ENABLE
if (1 == pikaTuple_getSize(val)) {
Arg* in = pikaTuple_getArg(val, 0);
obj_setArg(self, "__list", in);
/* clang-format off */
PIKA_PYTHON(
@res_list = []
for __item in __list:
@res_list.append(__item)
del __item
del __list
)
/* clang-format on */
const uint8_t bytes[] = {
0x3c, 0x00, 0x00, 0x00, /* instruct array size */
0x00, 0x95, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x10, 0x81, 0x0b,
0x00, 0x00, 0x02, 0x12, 0x00, 0x00, 0x04, 0x17, 0x00, 0x00, 0x82,
0x1b, 0x00, 0x00, 0x04, 0x28, 0x00, 0x00, 0x0d, 0x28, 0x00, 0x00,
0x07, 0x2f, 0x00, 0x11, 0x81, 0x28, 0x00, 0x01, 0x02, 0x31, 0x00,
0x00, 0x86, 0x42, 0x00, 0x00, 0x8c, 0x17, 0x00, 0x00, 0x8c, 0x28,
0x00, 0x00, 0x8c, 0x0b, 0x00,
/* instruct array */
0x45, 0x00, 0x00, 0x00, /* const pool size */
0x00, 0x40, 0x72, 0x65, 0x73, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x00,
0x5f, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x00, 0x69, 0x74, 0x65, 0x72,
0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30, 0x2e, 0x5f, 0x5f,
0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x69, 0x74,
0x65, 0x6d, 0x00, 0x32, 0x00, 0x40, 0x72, 0x65, 0x73, 0x5f, 0x6c,
0x69, 0x73, 0x74, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00,
0x2d, 0x31, 0x00, /* const pool */
};
return pikaVM_runByteCodeReturn(self, (uint8_t*)bytes, "@res_list");
}
PikaObj* New_PikaStdData_List(Args * args);
return arg_newDirectObj(New_PikaStdData_List);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in list is not enabled.\r\n");
return arg_newNull();
#endif
}
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);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in dist is not enabled.\r\n");
return arg_newNull();
#endif
}
Arg* PikaStdLib_SysObj_tuple(PikaObj* self, Arg* val) {
#if PIKA_BUILTIN_STRUCT_ENABLE
obj_setErrorCode(self, 1);
__platform_printf("Error: tuple() is not supported.\r\n");
return arg_newNone();
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] built-in tuple is not enabled.\r\n");
return arg_newNull();
#endif
}
char* PikaStdLib_SysObj_hex(PikaObj* self, int val) {
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
if (val >= 0) {
__platform_sprintf(buff, "0x%02x", val);
} else {
__platform_sprintf(buff, "-0x%02x", -val);
}
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
int PikaStdLib_SysObj_ord(PikaObj* self, char* val) {
return (int)val[0];
}
char* PikaStdLib_SysObj_chr(PikaObj* self, int val) {
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
char to_str[] = "0";
to_str[0] = val;
__platform_sprintf(buff, "%s", to_str);
/* load the string from stack to heap */
obj_setStr(self, "__buf", buff);
return obj_getStr(self, "__buf");
}
Arg* PikaStdLib_SysObj_bytes(PikaObj* self, Arg* val) {
ArgType type = arg_getType(val);
if (ARG_TYPE_INT == type) {
int size = arg_getInt(val);
/* src is NULL so the bytes are all '\0' */
Arg* bytes = arg_newBytes(NULL, size);
return bytes;
}
if (ARG_TYPE_BYTES == type) {
return arg_copy(val);
}
if (ARG_TYPE_STRING == type) {
int size = strGetSize(arg_getStr(val));
Arg* bytes = arg_newBytes((uint8_t*)arg_getStr(val), size);
return bytes;
}
#if !PIKA_NANO_ENABLE
if (argType_isObject(type)) {
PikaObj* obj = arg_getPtr(val);
PikaObj* New_PikaStdData_List(Args * args);
PikaObj* New_PikaStdData_Tuple(Args * args);
if (obj->constructor == New_PikaStdData_List ||
obj->constructor == New_PikaStdData_Tuple) {
PikaList* list = obj_getPtr(obj, "list");
Arg* bytes = arg_newBytes(NULL, pikaList_getSize(list));
uint8_t* bytes_raw = arg_getBytes(bytes);
for (size_t i = 0; i < pikaList_getSize(list); i++) {
bytes_raw[i] = (uint8_t)pikaList_getInt(list, i);
}
return bytes;
}
}
#endif
obj_setErrorCode(self, 1);
__platform_printf("Error: input arg type not supported.\r\n");
return arg_newNone();
}
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
int arg_size = pikaTuple_getSize(val);
char* end = pikaDict_getStr(ops, "end");
if (NULL == end) {
/* default */
end = "\r\n";
}
if (arg_size == 1) {
arg_print(pikaTuple_getArg(val, 0), pika_false, end);
return;
}
Arg* print_out_arg = NULL;
pika_bool is_get_print = pika_false;
for (int i = 0; i < arg_size; i++) {
Arg* arg = pikaTuple_getArg(val, i);
Arg* item_arg_str = arg_toStrArg(arg);
if (NULL != item_arg_str) {
is_get_print = PIKA_TRUE;
if (NULL == print_out_arg) {
print_out_arg = arg_newStr("");
}
print_out_arg =
arg_strAppend(print_out_arg, arg_getStr(item_arg_str));
if (i < arg_size - 1) {
print_out_arg = arg_strAppend(print_out_arg, " ");
}
arg_deinit(item_arg_str);
}
}
if (PIKA_TRUE == is_get_print) {
__platform_printf("%s%s", arg_getStr(print_out_arg), end);
}
if (NULL != print_out_arg) {
arg_deinit(print_out_arg);
}
}
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
#if PIKA_SYNTAX_FORMAT_ENABLE
Args buffs = {0};
pikaMemMaxReset();
char* res = strsFormatList(&buffs, fmt, &var->super);
obj_setStr(self, "_buf", res);
res = obj_getStr(self, "_buf");
strsDeinit(&buffs);
return res;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_SYNTAX_FORMAT_ENABLE is not enabled.\r\n");
return NULL;
#endif
}
int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
uintptr_t ptr = 0;
if (arg_isObject(obj)) {
ptr = (uintptr_t)arg_getPtr(obj);
} else {
ptr = (uintptr_t)obj;
}
return ptr & (0x7FFFFFFF);
}
PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* path, char* mode) {
#if PIKA_FILEIO_ENABLE
PikaObj* file = newNormalObj(New_PikaStdData_FILEIO);
if (0 != PikaStdData_FILEIO_init(file, path, mode)) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] open: can not open file.\r\n");
obj_deinit(file);
return NULL;
}
return file;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n");
return NULL;
#endif
}
/* __dir_each */
int32_t __dir_each(Arg* argEach, void* context);
PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) {
if (!arg_isObject(arg)) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] dir: not support type.\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);
Args* context = New_args(NULL);
args_setPtr(context, "list", list);
args_foreach(obj->list, __dir_each, context);
args_deinit(context);
return list;
}
void PikaStdLib_SysObj_exec(PikaObj* self, char* code) {
#if PIKA_EXEC_ENABLE
obj_run(self, code);
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_EXEC_ENABLE is not enabled.\r\n");
#endif
}
Arg* PikaStdLib_SysObj_getattr(PikaObj* self, PikaObj* obj, char* name) {
Arg* res = NULL;
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] getattr: can not get attr of NULL.\r\n");
return NULL;
}
Arg* arg = obj_getArg(obj, name);
if (NULL == arg) {
arg = obj_getMethodArgWithFullPath(obj, name);
}
if (NULL != arg) {
res = arg_copy(arg);
methodArg_setHostObj(res, obj);
}
return res;
}
void PikaStdLib_SysObj_setattr(PikaObj* self,
PikaObj* obj,
char* name,
Arg* val) {
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] setattr: obj is null.\r\n");
goto exit;
}
obj_setArg(obj, name, val);
exit:
return;
}
void PikaStdLib_SysObj_exit(PikaObj* self) {
pks_vm_exit();
}
int PikaStdLib_SysObj_hasattr(PikaObj* self, PikaObj* obj, char* name) {
if (NULL == obj) {
obj_setErrorCode(self, 1);
__platform_printf("[Error] hasattr: obj is null.\r\n");
return 0;
}
if (obj_isArgExist(obj, name)) {
return 1;
}
Arg* method = obj_getMethodArgWithFullPath(obj, name);
if (NULL != method) {
arg_deinit(method);
return 1;
}
return 0;
}
Arg* PikaStdLib_SysObj_eval(PikaObj* self, char* code) {
Args buffs = {0};
char* cmd = strsAppend(&buffs, "@res = ", code);
obj_run(self, cmd);
Arg* res = arg_copy(obj_getArg(self, "@res"));
strsDeinit(&buffs);
obj_removeArg(self, "@res");
return res;
}
static enum shellCTRL __obj_shellLineHandler_input(PikaObj* self,
char* input_line,
struct ShellConfig* cfg) {
cfg->context = arg_newStr(input_line);
return SHELL_CTRL_EXIT;
}
char* PikaStdLib_SysObj_input(PikaObj* self, PikaTuple* info) {
struct ShellConfig cfg = {
.prefix = "",
.context = NULL,
.handler = __obj_shellLineHandler_input,
.fn_getchar = __platform_getchar,
};
if (pikaTuple_getSize(info) > 0) {
__platform_printf("%s", pikaTuple_getStr(info, 0));
}
_temp__do_pikaScriptShell(self, &cfg);
char* res = obj_cacheStr(self, arg_getStr(cfg.context));
arg_deinit(cfg.context);
return res;
}
extern volatile PikaObj* __pikaMain;
void PikaStdLib_SysObj_help(PikaObj* self, char* name) {
if (strEqu(name, "modules")) {
obj_printModules((PikaObj*)__pikaMain);
}
}
void PikaStdLib_SysObj_reboot(PikaObj* self) {
pika_platform_reboot();
}
void PikaStdLib_SysObj_clear(PikaObj* self) {
pika_platform_clear();
}
void PikaStdLib_SysObj_gcdump(PikaObj* self) {
pikaGC_markDump();
}
Arg* PikaStdLib_SysObj_abs(PikaObj* self, Arg* val) {
ArgType type = arg_getType(val);
if (type == ARG_TYPE_INT) {
int64_t v = arg_getInt(val);
if (v < 0) {
v = -v;
}
return arg_newInt(v);
}
if (type == ARG_TYPE_FLOAT) {
pika_float v = arg_getFloat(val);
if (v < 0) {
v = -v;
}
return arg_newFloat(v);
}
obj_setSysOut(self, "TypeError: bad operand type for abs()");
obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
return NULL;
}
PikaObj* New_PikaStdData_Tuple(Args* args);
/* clang-format off */
PIKA_PYTHON(
@res_max = @list[0]
for @item in @list:
if @item > @res_max:
@res_max = @item
)
/* clang-format on */
extern const uint8_t bc_max[];
/* clang-format off */
PIKA_PYTHON(
@res_max = @list[0]
for @item in @list:
if @item < @res_max:
@res_max = @item
)
/* clang-format on */
extern const uint8_t bc_min[];
Arg* _max_min(PikaObj* self, PikaTuple* val, uint8_t* bc);
Arg* PikaStdLib_SysObj_max(PikaObj* self, PikaTuple* val) {
return _max_min(self, val, (uint8_t*)bc_max);
}
Arg* PikaStdLib_SysObj_min(PikaObj* self, PikaTuple* val) {
return _max_min(self, val, (uint8_t*)bc_min);
}
pika_bool _isinstance(PikaObj* self, Arg* object, Arg* classinfo);
pika_bool PikaStdLib_SysObj_isinstance(PikaObj* self,
Arg* object,
Arg* classinfo) {
return _isinstance(self, object, classinfo);
}

View File

@ -2815,10 +2815,14 @@ void builtins_remove(PikaObj* self, char* argPath) {
}
}
Arg* _type(PikaObj* self, Arg* arg);
Arg* _type(Arg* arg);
Arg* builtins_type(PikaObj* self, Arg* arg) {
return _type(self, arg);
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return NULL;
}
return _type(arg);
}
pika_float builtins_float(PikaObj* self, Arg* arg) {
@ -2917,13 +2921,13 @@ char* builtins_str(PikaObj* self, Arg* arg) {
return str;
}
PikaObj* New_PikaStdLib_RangeObj(Args* args);
PikaObj* New_builtins_RangeObj(Args* args);
Arg* builtins_iter(PikaObj* self, Arg* arg) {
/* object */
pika_bool bIsTemp = pika_false;
PikaObj* oArg = _arg_to_obj(arg, &bIsTemp);
NewFun _clsptr = (NewFun)oArg->constructor;
if (_clsptr == New_PikaStdLib_RangeObj) {
if (_clsptr == New_builtins_RangeObj) {
/* found RangeObj, return directly */
return arg_copy(arg);
}
@ -2948,7 +2952,7 @@ Arg* builtins_iter(PikaObj* self, Arg* arg) {
Arg* builtins_range(PikaObj* self, PikaTuple* ax) {
/* set template arg to create rangeObj */
Arg* aRangeObj = arg_newDirectObj(New_PikaStdLib_RangeObj);
Arg* aRangeObj = arg_newDirectObj(New_builtins_RangeObj);
PikaObj* oRangeObj = arg_getPtr(aRangeObj);
RangeData tRangeData = {0};
if (pikaTuple_getSize(ax) == 1) {
@ -3387,80 +3391,107 @@ static enum shellCTRL __obj_shellLineHandler_input(PikaObj* self,
return SHELL_CTRL_EXIT;
}
Arg* _type(PikaObj* self, Arg* arg) {
if (NULL == arg) {
obj_setSysOut(self, "[error] type: arg no found.");
obj_setErrorCode(self, 1);
return arg_newNone();
}
Arg* _type(Arg* arg) {
Arg* result;
PikaObj* oBuiltins = NULL;
ArgType type = arg_getType(arg);
oBuiltins = obj_getBuiltins();
if (ARG_TYPE_INT == type) {
return arg_copy(obj_getMethodArgWithFullPath(self, "int"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "int"));
goto __exit;
}
if (ARG_TYPE_FLOAT == type) {
return arg_copy(obj_getMethodArgWithFullPath(self, "float"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "float"));
goto __exit;
}
if (ARG_TYPE_STRING == type) {
return arg_copy(obj_getMethodArgWithFullPath(self, "str"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "str"));
goto __exit;
}
if (ARG_TYPE_BOOL == type) {
return arg_copy(obj_getMethodArgWithFullPath(self, "bool"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "bool"));
goto __exit;
}
if (ARG_TYPE_BYTES == type) {
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "bytes"));
goto __exit;
}
if (argType_isObject(type)) {
PikaObj* obj = arg_getPtr(arg);
NewFun clsptr = obj_getClass(obj);
PikaObj* New_PikaStdData_List(Args * args);
/* list */
if (clsptr == New_PikaStdData_List) {
return arg_copy(obj_getMethodArgWithFullPath(self, "list"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "list"));
goto __exit;
}
/* dict */
PikaObj* New_PikaStdData_Dict(Args * args);
if (clsptr == New_PikaStdData_Dict) {
return arg_copy(obj_getMethodArgWithFullPath(self, "dict"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "dict"));
goto __exit;
}
/* tuple */
PikaObj* New_PikaStdData_Tuple(Args * args);
if (clsptr == New_PikaStdData_Tuple) {
return arg_copy(obj_getMethodArgWithFullPath(self, "tuple"));
result = arg_copy(obj_getMethodArgWithFullPath(oBuiltins, "tuple"));
goto __exit;
}
#if PIKA_TYPE_FULL_FEATURE_ENABLE
Arg* aMethod = obj_getArg(obj, "__class__");
if (NULL != aMethod) {
return arg_copy(aMethod);
result = arg_copy(aMethod);
goto __exit;
}
#endif
return arg_newStr("<class 'object'>");
}
if (ARG_TYPE_OBJECT_META == type) {
return arg_newStr("<class 'meta object'>");
}
if (ARG_TYPE_BYTES == type) {
return arg_newStr("<class 'bytes'>");
}
if (ARG_TYPE_METHOD_OBJECT == type) {
return arg_newStr("<class 'method'>");
}
if (ARG_TYPE_METHOD_STATIC == type) {
return arg_newStr("<class 'function'>");
}
if (ARG_TYPE_NONE == type) {
return arg_newStr("<class 'NoneType'>");
}
return arg_newStr("<class 'buitin_function_or_method'>");
}
pika_bool _isinstance(PikaObj* self, Arg* object, Arg* classinfo) {
pika_bool res = pika_false;
Arg* aObjType = NULL;
if (!argType_isConstructor(arg_getType(classinfo)) &&
!argType_isCallable(arg_getType(classinfo))) {
obj_setErrorCode(self, 1);
__platform_printf("TypeError: isinstance() arg 2 must be a type\r\n");
res = pika_false;
result = arg_newStr("<class 'object'>");
goto __exit;
}
aObjType = _type(self, object);
if (ARG_TYPE_OBJECT_META == type) {
result = arg_newStr("<class 'meta object'>");
goto __exit;
}
if (ARG_TYPE_METHOD_OBJECT == type) {
result = arg_newStr("<class 'method'>");
goto __exit;
}
if (ARG_TYPE_METHOD_STATIC == type) {
result = arg_newStr("<class 'function'>");
goto __exit;
}
if (ARG_TYPE_NONE == type) {
result = arg_newStr("<class 'NoneType'>");
goto __exit;
}
result = arg_newStr("<class 'buitin_function_or_method'>");
__exit:
if (NULL != oBuiltins) {
obj_deinit(oBuiltins);
}
return result;
}
pika_bool _isinstance(Arg* object, Arg* classinfo) {
pika_bool res = pika_false;
Arg* aObjType = NULL;
aObjType = _type(object);
while (1) {
if (arg_getPtr(aObjType) == arg_getPtr(classinfo)) {
res = pika_true;
@ -3624,5 +3655,33 @@ Arg* builtins_min(PikaObj* self, PikaTuple* val) {
}
pika_bool builtins_isinstance(PikaObj* self, Arg* object, Arg* classinfo) {
return _isinstance(self, object, classinfo);
if (!argType_isConstructor(arg_getType(classinfo)) &&
!argType_isCallable(arg_getType(classinfo))) {
obj_setErrorCode(self, 1);
__platform_printf("TypeError: isinstance() arg 2 must be a type\r\n");
return pika_false;
}
return _isinstance(object, classinfo);
}
Arg* builtins_StringObj___next__(PikaObj* self) {
return arg_newNone();
}
Arg* builtins_RangeObj___next__(PikaObj* self) {
RangeData* _ = (RangeData*)args_getStruct(self->list, "_");
int end = _->end;
int step = _->step;
/* exit */
if (_->i >= end) {
return arg_newNone();
}
Arg* res = arg_newInt(_->i);
_->i += step;
return res;
}
PikaObj* New_builtins(Args* args);
PikaObj* obj_getBuiltins(void) {
return newNormalObj(New_builtins);
}

View File

@ -224,6 +224,7 @@ PIKA_RES obj_setArg_noCopy(PikaObj* self, char* argPath, Arg* arg);
PIKA_RES obj_setBytes(PikaObj* self, char* argPath, uint8_t* src, size_t size);
void* obj_getPtr(PikaObj* self, char* argPath);
PikaObj* obj_getBuiltins(void);
pika_float obj_getFloat(PikaObj* self, char* argPath);
char* obj_getStr(PikaObj* self, char* argPath);
int64_t obj_getInt(PikaObj* self, char* argPath);
@ -576,7 +577,7 @@ Arg* arg_toStrArg(Arg* arg);
/* [example]
const MethodProp floatMethod = {
.ptr = (void*)PikaStdLib_SysObj_floatMethod,
.ptr = (void*)builtins_floatMethod,
.bytecode_frame = NULL,
.def_context = NULL,
.declareation = "float(arg)",

View File

@ -931,6 +931,7 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
char* arg_path = data;
char* arg_name = strPointToLastToken(arg_path, '.');
pika_bool is_temp = pika_false;
PikaObj* oBuiltins = NULL;
switch (data[0]) {
case 'T':
@ -960,14 +961,14 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
/* find host from stack */
Arg* host_arg = stack_popArg_alloc(&(vm->stack));
if (NULL == host_arg) {
goto exit;
goto __exit;
}
if (arg_isObject(host_arg)) {
oHost = arg_getPtr(host_arg);
aRes = arg_copy_noalloc(obj_getArg(oHost, arg_path + 1), aRetReg);
}
arg_deinit(host_arg);
goto exit;
goto __exit;
}
/* find in local list first */
@ -982,7 +983,7 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
/* error cannot found host_object */
if (NULL == oHost) {
goto exit;
goto __exit;
}
/* proxy */
@ -1010,13 +1011,26 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
if (NULL == aRes) {
aRes = _obj_getProp(vm->globals, arg_name);
}
/* find res in builtins */
if (NULL == aRes) {
oBuiltins = obj_getBuiltins();
aRes = args_getArg(oBuiltins->list, arg_name);
}
if (NULL == aRes) {
aRes = _obj_getProp(oBuiltins, arg_name);
}
}
/* proxy */
if (NULL == aRes) {
aRes = _proxy_getattr(oHost, arg_name);
}
exit:
__exit:
if (NULL != oBuiltins) {
obj_deinit(oBuiltins);
}
if (NULL == aRes) {
VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("NameError: name '%s' is not defined\r\n",
@ -1845,6 +1859,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
pika_bool bSkipInit = pika_false;
char* sSysOut;
int iNumUsed = 0;
PikaObj* oBuiltin = NULL;
arg_newReg(arg_reg1, 32);
RunState tSubRunState = {.try_state = vm->run_state->try_state,
.try_result = TRY_RESULT_NONE};
@ -1867,11 +1882,11 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
if (VMState_getInputArgNum(vm) == 1) {
/* return arg directly */
aReturn = stack_popArg(&(vm->stack), aReturnRegistor);
goto exit;
goto __exit;
}
/* create a tuple */
aReturn = _vm_create_list_or_tuple(self, vm, pika_false);
goto exit;
goto __exit;
}
#if !PIKA_NANO_ENABLE
@ -1889,7 +1904,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
aReturn = _builtin_class(sRunPath);
if (NULL != aReturn) {
goto exit;
goto __exit;
}
/* get method host obj from reg */
@ -1953,7 +1968,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
/* error, not found object */
VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("Error: method '%s' no found.\r\n", sRunPath);
goto exit;
goto __exit;
}
pika_assert(obj_checkAlive(oMethodHost));
@ -1977,11 +1992,11 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
}
if (sArgName == sRunPath) {
/* get method in locals */
/* find method in locals */
if (NULL == aMethod) {
aMethod = obj_getMethodArg_noalloc(vm->locals, sArgName, &arg_reg1);
}
/* get method in global */
/* find method in global */
if (NULL == aMethod) {
aMethod =
obj_getMethodArg_noalloc(vm->globals, sArgName, &arg_reg1);
@ -1989,6 +2004,14 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
oThis = vm->globals;
}
}
/* find method in builtin */
if (NULL == aMethod) {
oBuiltin = obj_getBuiltins();
aMethod = obj_getMethodArg_noalloc(oBuiltin, sArgName, &arg_reg1);
if (aMethod != NULL) {
oThis = oBuiltin;
}
}
}
if (NULL == aMethod) {
@ -2010,7 +2033,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("NameError: name '%s' is not defined\r\n",
sRunPath);
goto exit;
goto __exit;
}
/* assert methodd type */
@ -2019,7 +2042,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND);
pika_platform_printf("TypeError: '%s' object is not callable\r\n",
sRunPath);
goto exit;
goto __exit;
}
/* create sub local scope */
@ -2031,7 +2054,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
/* load args failed */
if (vm->error_code != 0) {
goto exit;
goto __exit;
}
/* run method arg */
@ -2096,8 +2119,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
VMState_setErrorCode(vm, PIKA_RES_ERR_RUNTIME_ERROR);
}
goto exit;
exit:
goto __exit;
__exit:
if (NULL != aMethod) {
arg_deinit(aMethod);
}
@ -2107,6 +2130,9 @@ exit:
#endif
obj_deinit(oSublocals);
}
if (NULL != oBuiltin) {
obj_deinit(oBuiltin);
}
if (NULL != aStack && aMethod != aStack) {
arg_deinit(aStack);
}

View File

@ -80,19 +80,19 @@ TEST(class, dir_) {
extern "C" {
void PikaStdLib_SysObj_intMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_int, "int", "arg");
void builtins_intMethod(PikaObj* self, Args* args);
method_typedef(builtins_int, "int", "arg");
void PikaStdLib_SysObj_floatMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_float, "float", "arg");
void builtins_floatMethod(PikaObj* self, Args* args);
method_typedef(builtins_float, "float", "arg");
void PikaStdLib_SysObj_printMethod(PikaObj* self, Args* args);
method_typedef(PikaStdLib_SysObj_print, "print", "*val,**ops");
void builtins_printMethod(PikaObj* self, Args* args);
method_typedef(builtins_print, "print", "*val,**ops");
class_def(PikaStdLib_SysObj){
method_def(PikaStdLib_SysObj_int, hash_time33("int")),
method_def(PikaStdLib_SysObj_float, hash_time33("float")),
method_def(PikaStdLib_SysObj_print, hash_time33("print")),
method_def(builtins_int, hash_time33("int")),
method_def(builtins_float, hash_time33("float")),
method_def(builtins_print, hash_time33("print")),
};
class_inhert(PikaStdLib_SysObj, TinyObj);
@ -101,9 +101,6 @@ PikaObj* New_NativeMethodBase(Args* args) {
obj_setClass(self, PikaStdLib_SysObj);
return self;
}
}
TEST(class, native_class1) {