2022-06-14 03:20:17 +00:00
|
|
|
#include "PikaStdLib_SysObj.h"
|
2022-07-15 02:06:23 +00:00
|
|
|
#include "PikaStdData_FILEIO.h"
|
2021-12-28 01:21:47 +08:00
|
|
|
#include "PikaStdLib_RangeObj.h"
|
2022-01-09 23:49:23 +08:00
|
|
|
#include "PikaStdLib_StringObj.h"
|
2022-06-14 03:20:17 +00:00
|
|
|
#include "PikaVM.h"
|
2021-10-01 20:00:15 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:10:08 +08:00
|
|
|
Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
2021-10-01 20:00:15 +08:00
|
|
|
if (NULL == arg) {
|
|
|
|
obj_setSysOut(self, "[error] type: arg no found.");
|
|
|
|
obj_setErrorCode(self, 1);
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newNull();
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
2021-11-15 09:58:54 +08:00
|
|
|
ArgType type = arg_getType(arg);
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_INT == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'int'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_FLOAT == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'float'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_STRING == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'str'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-06-30 16:10:08 +08:00
|
|
|
if (argType_isObject(type)) {
|
|
|
|
PikaObj* obj = arg_getPtr(arg);
|
|
|
|
NewFun clsptr = obj_getClass(obj);
|
|
|
|
PikaObj* New_PikaStdData_List(Args * args);
|
|
|
|
if (clsptr == New_PikaStdData_List) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'list'>");
|
2022-06-30 16:10:08 +08:00
|
|
|
}
|
|
|
|
/* dict */
|
|
|
|
PikaObj* New_PikaStdData_Dict(Args * args);
|
|
|
|
if (clsptr == New_PikaStdData_Dict) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'dict'>");
|
2022-06-30 16:10:08 +08:00
|
|
|
}
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'object'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-04-28 00:42:06 +08:00
|
|
|
if (ARG_TYPE_OBJECT_META == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'meta object'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-04-28 16:04:14 +08:00
|
|
|
if (ARG_TYPE_BYTES == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'bytes'>");
|
2022-04-28 16:04:14 +08:00
|
|
|
}
|
2022-04-27 23:38:00 +08:00
|
|
|
if (ARG_TYPE_METHOD_NATIVE == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'buitin_function_or_method'>");
|
2022-04-27 23:38:00 +08:00
|
|
|
}
|
2022-04-28 00:29:32 +08:00
|
|
|
if (ARG_TYPE_METHOD_OBJECT == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'method'>");
|
2022-04-27 23:38:00 +08:00
|
|
|
}
|
2022-04-28 00:29:32 +08:00
|
|
|
if (ARG_TYPE_METHOD_STATIC == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'function'>");
|
2021-11-15 09:58:54 +08:00
|
|
|
}
|
2022-07-06 15:13:23 +08:00
|
|
|
if (ARG_TYPE_NONE == type) {
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr("<class 'NoneType'>");
|
2022-07-06 15:13:23 +08:00
|
|
|
}
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newNull();
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
2021-11-24 11:15:50 +08:00
|
|
|
|
2022-09-28 11:23:39 +08:00
|
|
|
pika_float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) {
|
2021-11-24 11:15:50 +08:00
|
|
|
ArgType type = arg_getType(arg);
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_INT == type) {
|
2021-11-24 11:15:50 +08:00
|
|
|
return (float)arg_getInt(arg);
|
|
|
|
}
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_FLOAT == type) {
|
2021-11-24 11:15:50 +08:00
|
|
|
return (float)arg_getFloat(arg);
|
|
|
|
}
|
2022-11-08 22:07:06 +08:00
|
|
|
if (ARG_TYPE_STRING == type) {
|
|
|
|
return strtod(arg_getStr(arg), NULL);
|
|
|
|
}
|
2022-10-24 10:14:12 +08:00
|
|
|
obj_setSysOut(self, "[error] convert to pika_float type failed.");
|
2021-11-24 11:15:50 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
return -99999.99999;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
|
|
|
|
ArgType type = arg_getType(arg);
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_INT == type) {
|
2021-11-24 11:15:50 +08:00
|
|
|
return (int)arg_getInt(arg);
|
|
|
|
}
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_FLOAT == type) {
|
2021-11-24 11:15:50 +08:00
|
|
|
return (int)arg_getFloat(arg);
|
|
|
|
}
|
2022-04-09 01:15:21 +08:00
|
|
|
if (ARG_TYPE_STRING == type) {
|
|
|
|
return (int)fast_atoi(arg_getStr(arg));
|
|
|
|
}
|
2022-05-27 10:29:57 +08:00
|
|
|
if (ARG_TYPE_BYTES == type) {
|
|
|
|
size_t size = arg_getBytesSize(arg);
|
|
|
|
if (size != 1) {
|
|
|
|
obj_setSysOut(self, "ValueError: invalid literal for int()");
|
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
return -999999999;
|
|
|
|
}
|
|
|
|
uint8_t val = *arg_getBytes(arg);
|
|
|
|
return val;
|
|
|
|
}
|
2022-10-24 10:14:12 +08:00
|
|
|
obj_setSysOut(self, "[error] convert to int type failed.");
|
2021-11-24 11:15:50 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
return -999999999;
|
2021-12-28 01:21:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-09 00:13:02 +08:00
|
|
|
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
|
2022-07-06 15:13:23 +08:00
|
|
|
obj_removeArg(self, "__buf");
|
2022-01-09 00:13:02 +08:00
|
|
|
ArgType type = arg_getType(arg);
|
2022-03-19 09:44:53 +08:00
|
|
|
Args buffs = {0};
|
2022-07-29 23:40:16 +08:00
|
|
|
char* res = "";
|
2022-05-25 13:57:02 +08:00
|
|
|
if (ARG_TYPE_INT == type) {
|
|
|
|
int val = arg_getInt(arg);
|
|
|
|
res = strsFormat(&buffs, 11, "%d", val);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (ARG_TYPE_FLOAT == type) {
|
2022-09-28 11:23:39 +08:00
|
|
|
pika_float val = arg_getFloat(arg);
|
2022-05-25 13:57:02 +08:00
|
|
|
res = strsFormat(&buffs, 11, "%f", val);
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-06-10 11:34:46 +08:00
|
|
|
if (ARG_TYPE_BYTES == type) {
|
|
|
|
res = (char*)arg_getBytes(arg);
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-06-24 16:08:27 +08:00
|
|
|
if (ARG_TYPE_STRING == type) {
|
2022-06-24 03:30:56 +00:00
|
|
|
res = arg_getStr(arg);
|
|
|
|
}
|
2022-07-06 15:13:23 +08:00
|
|
|
if (ARG_TYPE_NONE == type) {
|
|
|
|
res = "None";
|
|
|
|
}
|
2022-06-01 11:46:59 +08:00
|
|
|
if (argType_isObject(type)) {
|
2022-05-25 13:57:02 +08:00
|
|
|
res = obj_toStr(arg_getPtr(arg));
|
|
|
|
if (NULL != res) {
|
|
|
|
goto exit;
|
2022-01-09 00:13:02 +08:00
|
|
|
}
|
2022-05-25 13:57:02 +08:00
|
|
|
}
|
|
|
|
exit:
|
|
|
|
obj_setStr(self, "__buf", res);
|
2022-03-17 16:47:03 +08:00
|
|
|
strsDeinit(&buffs);
|
2022-05-25 13:57:02 +08:00
|
|
|
return obj_getStr(self, "__buf");
|
2022-01-09 00:13:02 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 01:21:47 +08:00
|
|
|
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
|
2022-09-19 09:53:27 +08:00
|
|
|
/* object */
|
|
|
|
PIKA_BOOL is_temp = 0;
|
|
|
|
PikaObj* arg_obj = _arg_to_obj(arg, &is_temp);
|
|
|
|
NewFun _clsptr = (NewFun)arg_obj->constructor;
|
|
|
|
if (_clsptr == New_PikaStdLib_RangeObj) {
|
|
|
|
/* found RangeObj, return directly */
|
2021-12-28 01:21:47 +08:00
|
|
|
return arg_copy(arg);
|
|
|
|
}
|
2022-09-19 09:53:27 +08:00
|
|
|
// pikaVM_runAsm(arg_obj,
|
|
|
|
// "B0\n"
|
|
|
|
// "0 RUN __iter__\n"
|
|
|
|
// "0 OUT __res\n");
|
|
|
|
const uint8_t bytes[] = {
|
2022-11-08 22:07:06 +08:00
|
|
|
0x08, 0x00, 0x00, 0x00, /* instruct array size */
|
2022-09-19 09:53:27 +08:00
|
|
|
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0a, 0x00, /* instruct array */
|
2022-11-08 22:07:06 +08:00
|
|
|
0x10, 0x00, 0x00, 0x00, /* const pool size */
|
2022-09-19 09:53:27 +08:00
|
|
|
0x00, 0x5f, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x5f,
|
|
|
|
0x5f, 0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
|
|
|
|
};
|
|
|
|
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
|
|
|
Arg* res = arg_copy(args_getArg(arg_obj->list, "__res"));
|
|
|
|
obj_removeArg(arg_obj, "__res");
|
|
|
|
if (is_temp) {
|
|
|
|
obj_refcntDec(arg_obj);
|
2021-12-28 01:21:47 +08:00
|
|
|
}
|
2022-09-19 09:53:27 +08:00
|
|
|
return res;
|
2021-12-28 01:21:47 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:01:30 +08:00
|
|
|
Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) {
|
2021-12-28 01:21:47 +08:00
|
|
|
/* set template arg to create rangeObj */
|
2022-06-08 10:00:54 +08:00
|
|
|
Arg* obj_arg = arg_newDirectObj(New_PikaStdLib_RangeObj);
|
2022-09-12 20:01:30 +08:00
|
|
|
PikaObj* range_obj = arg_getPtr(obj_arg);
|
2022-10-13 01:24:58 +08:00
|
|
|
RangeData range_data = {0};
|
2022-09-12 20:01:30 +08:00
|
|
|
if (tuple_getSize(ax) == 1) {
|
|
|
|
int start = 0;
|
|
|
|
int end = arg_getInt(tuple_getArg(ax, 0));
|
2022-10-13 01:24:58 +08:00
|
|
|
range_data.start = start;
|
|
|
|
range_data.end = end;
|
|
|
|
range_data.step = 1;
|
2022-09-12 20:01:30 +08:00
|
|
|
} else if (tuple_getSize(ax) == 2) {
|
|
|
|
int start = arg_getInt(tuple_getArg(ax, 0));
|
|
|
|
int end = arg_getInt(tuple_getArg(ax, 1));
|
2022-10-13 01:24:58 +08:00
|
|
|
range_data.start = start;
|
|
|
|
range_data.end = end;
|
|
|
|
range_data.step = 1;
|
2022-09-12 20:01:30 +08:00
|
|
|
} else if (tuple_getSize(ax) == 3) {
|
|
|
|
int start = arg_getInt(tuple_getArg(ax, 0));
|
|
|
|
int end = arg_getInt(tuple_getArg(ax, 1));
|
|
|
|
int step = arg_getInt(tuple_getArg(ax, 2));
|
2022-10-13 01:24:58 +08:00
|
|
|
range_data.start = start;
|
|
|
|
range_data.end = end;
|
|
|
|
range_data.step = step;
|
2022-09-12 20:01:30 +08:00
|
|
|
}
|
2022-10-13 01:24:58 +08:00
|
|
|
range_data.i = range_data.start;
|
|
|
|
obj_setStruct(range_obj, "_", range_data);
|
2022-06-08 10:00:54 +08:00
|
|
|
return obj_arg;
|
2021-12-28 01:21:47 +08:00
|
|
|
}
|
2022-01-13 21:57:32 +08:00
|
|
|
|
2022-07-30 18:26:58 +08:00
|
|
|
Arg* PikaStdLib_SysObj___getitem__(PikaObj* self, Arg* obj, Arg* key) {
|
2022-11-14 15:00:33 +08:00
|
|
|
return __vm_get(NULL, self, key, obj);
|
2022-01-13 21:57:32 +08:00
|
|
|
}
|
2022-01-13 23:31:06 +08:00
|
|
|
|
2022-07-29 23:40:16 +08:00
|
|
|
Arg* PikaStdLib_SysObj___setitem__(PikaObj* self,
|
|
|
|
Arg* obj,
|
2022-07-30 18:26:58 +08:00
|
|
|
Arg* key,
|
2022-07-29 23:40:16 +08:00
|
|
|
Arg* val) {
|
2022-01-13 21:57:32 +08:00
|
|
|
ArgType obj_type = arg_getType(obj);
|
2022-03-02 15:27:19 +08:00
|
|
|
if (ARG_TYPE_STRING == obj_type) {
|
2022-01-18 23:52:20 +08:00
|
|
|
int index = arg_getInt(key);
|
|
|
|
char* str_val = arg_getStr(val);
|
|
|
|
char* str_pyload = arg_getStr(obj);
|
|
|
|
str_pyload[index] = str_val[0];
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newStr(str_pyload);
|
2022-01-18 23:52:20 +08:00
|
|
|
}
|
2022-05-21 19:23:34 +08:00
|
|
|
if (ARG_TYPE_BYTES == obj_type) {
|
|
|
|
int index = arg_getInt(key);
|
2022-06-24 16:08:27 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-05-21 19:23:34 +08:00
|
|
|
uint8_t* bytes_pyload = arg_getBytes(obj);
|
|
|
|
size_t bytes_len = arg_getBytesSize(obj);
|
2022-06-24 16:08:27 +08:00
|
|
|
bytes_pyload[index] = byte_val;
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newBytes(bytes_pyload, bytes_len);
|
2022-05-21 19:23:34 +08:00
|
|
|
}
|
2022-06-01 14:39:59 +08:00
|
|
|
if (argType_isObject(obj_type)) {
|
2022-01-13 21:57:32 +08:00
|
|
|
PikaObj* arg_obj = arg_getPtr(obj);
|
|
|
|
obj_setArg(arg_obj, "__key", key);
|
|
|
|
obj_setArg(arg_obj, "__val", val);
|
2022-07-29 23:40:16 +08:00
|
|
|
/* clang-format off */
|
|
|
|
PIKA_PYTHON(
|
|
|
|
__setitem__(__key, __val)
|
|
|
|
)
|
|
|
|
/* clang-format on */
|
2022-03-19 09:44:53 +08:00
|
|
|
const uint8_t bytes[] = {
|
2022-11-08 22:07:06 +08:00
|
|
|
0x0c, 0x00, 0x00, 0x00, /* instruct array size */
|
2022-03-19 09:44:53 +08:00
|
|
|
0x10, 0x81, 0x01, 0x00, 0x10, 0x01, 0x07, 0x00, 0x00, 0x02, 0x0d,
|
|
|
|
0x00,
|
|
|
|
/* instruct array */
|
2022-11-08 22:07:06 +08:00
|
|
|
0x19, 0x00, 0x00, 0x00, /* const pool size */
|
2022-03-19 09:44:53 +08:00
|
|
|
0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x76, 0x61,
|
2022-07-29 23:40:16 +08:00
|
|
|
0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d,
|
|
|
|
0x5f, 0x5f, 0x00,
|
|
|
|
/* const pool */
|
2022-03-19 09:44:53 +08:00
|
|
|
};
|
|
|
|
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newRef(arg_obj);
|
2022-01-13 21:57:32 +08:00
|
|
|
}
|
2022-06-24 16:08:27 +08:00
|
|
|
return NULL;
|
2022-01-13 21:57:32 +08:00
|
|
|
}
|
2022-04-09 01:15:21 +08:00
|
|
|
|
|
|
|
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
|
|
|
|
if (ARG_TYPE_STRING == arg_getType(arg)) {
|
|
|
|
return strGetSize(arg_getStr(arg));
|
|
|
|
}
|
2022-06-10 10:55:48 +08:00
|
|
|
if (ARG_TYPE_BYTES == arg_getType(arg)) {
|
|
|
|
return arg_getBytesSize(arg);
|
|
|
|
}
|
2022-06-24 18:18:06 +08:00
|
|
|
|
|
|
|
if (argType_isObject(arg_getType(arg))) {
|
|
|
|
PikaObj* arg_obj = arg_getPtr(arg);
|
|
|
|
Arg* method_arg = obj_getMethodArg(arg_obj, "__len__");
|
|
|
|
if (NULL != method_arg) {
|
|
|
|
arg_deinit(method_arg);
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
PIKA_PYTHON(
|
|
|
|
__res = __len__()
|
|
|
|
)
|
|
|
|
/* clang-format on */
|
|
|
|
const uint8_t bytes[] = {
|
2022-11-08 22:07:06 +08:00
|
|
|
0x08, 0x00, 0x00, 0x00, /* instruct array size */
|
2022-06-24 18:18:06 +08:00
|
|
|
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct
|
|
|
|
array */
|
2022-11-08 22:07:06 +08:00
|
|
|
0x0f, 0x00, 0x00, 0x00, /* const pool size */
|
2022-06-24 18:18:06 +08:00
|
|
|
0x00, 0x5f, 0x5f, 0x6c, 0x65, 0x6e, 0x5f, 0x5f, 0x00,
|
|
|
|
0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */
|
|
|
|
};
|
|
|
|
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
|
|
|
|
return obj_getInt(arg_obj, "__res");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 01:15:21 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
__platform_printf("[Error] len: arg type not support\r\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-04-13 13:23:45 +08:00
|
|
|
|
2022-09-19 09:53:27 +08:00
|
|
|
Arg* PikaStdLib_SysObj_list(PikaObj* self, PikaTuple* val) {
|
2022-07-14 08:36:02 +00:00
|
|
|
#if PIKA_BUILTIN_STRUCT_ENABLE
|
2022-09-19 09:53:27 +08:00
|
|
|
if (1 == tuple_getSize(val)) {
|
|
|
|
Arg* in = tuple_getArg(val, 0);
|
|
|
|
obj_setArg(self, "__list", in);
|
|
|
|
/* clang-format off */
|
|
|
|
PIKA_PYTHON(
|
|
|
|
__res = []
|
|
|
|
for __item in __list:
|
|
|
|
__res.append(__item)
|
|
|
|
del __item
|
|
|
|
del __list
|
|
|
|
)
|
|
|
|
/* clang-format on */
|
|
|
|
const uint8_t bytes[] = {
|
2022-11-08 22:07:06 +08:00
|
|
|
0x3c, 0x00, 0x00, 0x00, /* instruct array size */
|
2022-09-19 09:53:27 +08:00
|
|
|
0x00, 0x95, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x10, 0x81, 0x07,
|
|
|
|
0x00, 0x00, 0x02, 0x0e, 0x00, 0x00, 0x04, 0x13, 0x00, 0x00, 0x82,
|
|
|
|
0x17, 0x00, 0x00, 0x04, 0x24, 0x00, 0x00, 0x0d, 0x24, 0x00, 0x00,
|
|
|
|
0x07, 0x2b, 0x00, 0x11, 0x81, 0x24, 0x00, 0x01, 0x02, 0x2d, 0x00,
|
|
|
|
0x00, 0x86, 0x3a, 0x00, 0x00, 0x8c, 0x13, 0x00, 0x00, 0x8c, 0x24,
|
|
|
|
0x00, 0x00, 0x8c, 0x07, 0x00,
|
|
|
|
/* instruct array */
|
2022-11-08 22:07:06 +08:00
|
|
|
0x3d, 0x00, 0x00, 0x00, /* const pool size */
|
2022-09-19 09:53:27 +08:00
|
|
|
0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73, 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, 0x5f, 0x5f, 0x72, 0x65, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65,
|
|
|
|
0x6e, 0x64, 0x00, 0x2d, 0x31, 0x00,
|
|
|
|
/* const pool */
|
|
|
|
};
|
|
|
|
pikaVM_runByteCode(self, (uint8_t*)bytes);
|
|
|
|
return arg_copy(obj_getArg(self, "__res"));
|
|
|
|
}
|
2022-04-25 15:01:58 +08:00
|
|
|
PikaObj* New_PikaStdData_List(Args * args);
|
2022-06-24 03:30:56 +00:00
|
|
|
return arg_newDirectObj(New_PikaStdData_List);
|
2022-07-01 23:41:32 +08:00
|
|
|
#else
|
2022-04-14 15:58:24 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
2022-04-14 16:12:18 +08:00
|
|
|
__platform_printf("[Error] built-in list is not enabled.\r\n");
|
2022-07-01 23:41:32 +08:00
|
|
|
#endif
|
2022-09-19 09:53:27 +08:00
|
|
|
return arg_newNull();
|
2022-04-14 16:12:18 +08:00
|
|
|
}
|
|
|
|
|
2022-09-28 10:55:36 +08:00
|
|
|
Arg* PikaStdLib_SysObj_dict(PikaObj* self, PikaTuple* val) {
|
2022-07-14 08:36:02 +00:00
|
|
|
#if PIKA_BUILTIN_STRUCT_ENABLE
|
2022-04-25 15:01:58 +08:00
|
|
|
PikaObj* New_PikaStdData_Dict(Args * args);
|
2022-06-24 03:30:56 +00:00
|
|
|
return arg_newDirectObj(New_PikaStdData_Dict);
|
2022-07-01 23:41:32 +08:00
|
|
|
#else
|
2022-04-14 16:12:18 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
__platform_printf("[Error] built-in dist is not enabled.\r\n");
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newNull();
|
2022-07-01 23:41:32 +08:00
|
|
|
#endif
|
2022-04-13 13:23:45 +08:00
|
|
|
}
|
2022-05-25 15:42:25 +08:00
|
|
|
|
2022-05-25 15:53:39 +08:00
|
|
|
char* PikaStdLib_SysObj_hex(PikaObj* self, int val) {
|
2022-05-25 15:42:25 +08:00
|
|
|
char buff[PIKA_SPRINTF_BUFF_SIZE] = {0};
|
2022-07-15 10:47:24 +08:00
|
|
|
if (val >= 0) {
|
2022-05-25 15:42:25 +08:00
|
|
|
__platform_sprintf(buff, "0x%02x", val);
|
2022-05-25 15:53:39 +08:00
|
|
|
} else {
|
2022-05-25 15:42:25 +08:00
|
|
|
__platform_sprintf(buff, "-0x%02x", -val);
|
|
|
|
}
|
|
|
|
/* load the string from stack to heap */
|
|
|
|
obj_setStr(self, "__buf", buff);
|
|
|
|
return obj_getStr(self, "__buf");
|
|
|
|
}
|
2022-05-25 15:53:39 +08:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2022-05-26 15:46:34 +08:00
|
|
|
|
|
|
|
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' */
|
2022-07-20 10:32:01 +08:00
|
|
|
Arg* bytes = arg_newBytes(NULL, size);
|
2022-05-26 15:46:34 +08:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
if (ARG_TYPE_BYTES == type) {
|
|
|
|
return arg_copy(val);
|
|
|
|
}
|
|
|
|
if (ARG_TYPE_STRING == type) {
|
|
|
|
int size = strGetSize(arg_getStr(val));
|
2022-07-20 10:32:01 +08:00
|
|
|
Arg* bytes = arg_newBytes((uint8_t*)arg_getStr(val), size);
|
2022-05-26 15:46:34 +08:00
|
|
|
return bytes;
|
|
|
|
}
|
2022-09-19 10:01:16 +08:00
|
|
|
#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, list_getSize(list));
|
|
|
|
uint8_t* bytes_raw = arg_getBytes(bytes);
|
|
|
|
for (size_t i = 0; i < list_getSize(list); i++) {
|
|
|
|
bytes_raw[i] = (uint8_t)list_getInt(list, i);
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-05-26 15:46:34 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
__platform_printf("Error: input arg type not supported.\r\n");
|
2022-07-20 10:32:01 +08:00
|
|
|
return arg_newNull();
|
2022-05-26 15:46:34 +08:00
|
|
|
}
|
2022-05-26 17:03:58 +08:00
|
|
|
|
2022-09-04 21:19:41 +08:00
|
|
|
static char* __print_arg(PikaObj* self, Arg* val) {
|
2022-11-17 12:30:37 +08:00
|
|
|
Args buffs = {0};
|
|
|
|
char* res = NULL;
|
|
|
|
if (NULL == val) {
|
|
|
|
goto __exit;
|
|
|
|
}
|
2022-06-10 12:04:06 +08:00
|
|
|
ArgType arg_type = arg_getType(val);
|
2022-11-17 12:30:37 +08:00
|
|
|
if (arg_type == ARG_TYPE_BYTES) {
|
|
|
|
res = __printBytes(self, val);
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
if (arg_type == ARG_TYPE_STRING) {
|
|
|
|
res = arg_getStr(val);
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
if (arg_type == ARG_TYPE_NONE) {
|
|
|
|
res = "None";
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
if (arg_type == ARG_TYPE_INT) {
|
|
|
|
int64_t value = arg_getInt(val);
|
|
|
|
res = strsFormat(&buffs, 32, "%lld", value);
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
if (arg_type == ARG_TYPE_FLOAT) {
|
|
|
|
pika_float value = arg_getFloat(val);
|
|
|
|
res = strsFormat(&buffs, 32, "%f", value);
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
if (arg_type == ARG_TYPE_POINTER ||
|
|
|
|
arg_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
|
|
|
|
void* value = arg_getPtr(val);
|
|
|
|
res = strsFormat(&buffs, 32, "%p", value);
|
|
|
|
goto __exit;
|
2022-06-10 12:04:06 +08:00
|
|
|
}
|
|
|
|
if (argType_isObject(arg_type)) {
|
2022-11-17 12:30:37 +08:00
|
|
|
res = obj_toStr(arg_getPtr(val));
|
|
|
|
goto __exit;
|
2022-06-10 12:04:06 +08:00
|
|
|
}
|
2022-11-17 12:30:37 +08:00
|
|
|
__exit:
|
2022-06-10 12:04:06 +08:00
|
|
|
if (NULL == res) {
|
2022-08-30 14:47:32 +08:00
|
|
|
obj_setSysOut(self, "Error: can not print val");
|
2022-06-10 12:04:06 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
}
|
2022-11-17 12:30:37 +08:00
|
|
|
if (NULL != res) {
|
|
|
|
res = obj_cacheStr(self, res);
|
|
|
|
}
|
|
|
|
strsDeinit(&buffs);
|
2022-09-04 21:19:41 +08:00
|
|
|
return res;
|
2022-06-10 12:04:06 +08:00
|
|
|
}
|
|
|
|
|
2022-09-02 00:13:01 +08:00
|
|
|
void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) {
|
2022-06-11 14:56:51 +00:00
|
|
|
int arg_size = tuple_getSize(val);
|
2022-11-17 12:48:10 +08:00
|
|
|
char* end = dict_getStr(ops, "end");
|
|
|
|
if (NULL == end) {
|
|
|
|
/* default */
|
|
|
|
end = "\r\n";
|
|
|
|
}
|
|
|
|
if (arg_size == 1) {
|
|
|
|
arg_singlePrint(tuple_getArg(val, 0), PIKA_FALSE, end);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-17 12:30:37 +08:00
|
|
|
Arg* print_out_arg = NULL;
|
2022-06-11 14:56:51 +00:00
|
|
|
PIKA_BOOL is_get_print = PIKA_FALSE;
|
|
|
|
for (int i = 0; i < arg_size; i++) {
|
|
|
|
Arg* arg = tuple_getArg(val, i);
|
2022-09-04 21:19:41 +08:00
|
|
|
char* item = __print_arg(self, arg);
|
2022-06-11 14:56:51 +00:00
|
|
|
if (NULL != item) {
|
|
|
|
is_get_print = PIKA_TRUE;
|
2022-11-17 12:30:37 +08:00
|
|
|
if (NULL == print_out_arg) {
|
|
|
|
print_out_arg = arg_newStr("");
|
|
|
|
}
|
2022-06-11 14:56:51 +00:00
|
|
|
print_out_arg = arg_strAppend(print_out_arg, item);
|
|
|
|
if (i < arg_size - 1) {
|
|
|
|
print_out_arg = arg_strAppend(print_out_arg, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PIKA_TRUE == is_get_print) {
|
2022-11-17 12:48:10 +08:00
|
|
|
__platform_printf("%s%s", arg_getStr(print_out_arg), end);
|
2022-11-17 12:30:37 +08:00
|
|
|
}
|
|
|
|
if (NULL != print_out_arg) {
|
|
|
|
arg_deinit(print_out_arg);
|
2022-06-11 14:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 03:20:17 +00:00
|
|
|
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
|
2022-07-11 06:42:12 +00:00
|
|
|
#if PIKA_SYNTAX_FORMAT_ENABLE
|
2022-06-14 03:20:17 +00:00
|
|
|
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;
|
2022-07-06 15:13:23 +08:00
|
|
|
#else
|
2022-07-01 22:55:29 +08:00
|
|
|
obj_setErrorCode(self, 1);
|
2022-07-11 03:14:14 +00:00
|
|
|
__platform_printf("[Error] PIKA_SYNTAX_FORMAT_ENABLE is not enabled.\r\n");
|
2022-07-01 22:55:29 +08:00
|
|
|
return NULL;
|
2022-07-06 15:13:23 +08:00
|
|
|
#endif
|
2022-06-14 03:20:17 +00:00
|
|
|
}
|
2022-06-14 12:06:56 +08:00
|
|
|
|
|
|
|
int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
|
|
|
|
uintptr_t ptr = 0;
|
|
|
|
if (argType_isObject(arg_getType(obj))) {
|
|
|
|
ptr = (uintptr_t)arg_getPtr(obj);
|
|
|
|
} else {
|
|
|
|
ptr = (uintptr_t)obj;
|
|
|
|
}
|
|
|
|
return ptr & (0x7FFFFFFF);
|
|
|
|
}
|
2022-07-15 02:06:23 +00:00
|
|
|
|
2022-07-30 18:26:58 +08:00
|
|
|
PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* path, char* mode) {
|
2022-07-15 02:06:23 +00:00
|
|
|
#if PIKA_FILEIO_ENABLE
|
|
|
|
PikaObj* file = newNormalObj(New_PikaStdData_FILEIO);
|
2022-08-06 17:59:32 +08:00
|
|
|
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;
|
|
|
|
}
|
2022-07-15 02:06:23 +00:00
|
|
|
return file;
|
|
|
|
#else
|
|
|
|
obj_setErrorCode(self, 1);
|
|
|
|
__platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n");
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2022-08-10 11:49:54 +08:00
|
|
|
|
|
|
|
/* __dir_each */
|
|
|
|
int32_t __dir_each(Arg* argEach, Args* context) {
|
|
|
|
PikaObj* list = args_getPtr(context, "list");
|
|
|
|
if (argType_isCallable(arg_getType(argEach))) {
|
|
|
|
char name_buff[PIKA_LINE_BUFF_SIZE / 2] = {0};
|
|
|
|
char* method_name =
|
|
|
|
methodArg_getName(argEach, name_buff, sizeof(name_buff));
|
|
|
|
Arg* arg_str = arg_newStr(method_name);
|
|
|
|
__vm_List_append(list, arg_str);
|
|
|
|
arg_deinit(arg_str);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, PikaObj* obj) {
|
|
|
|
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;
|
|
|
|
}
|
2022-08-10 12:07:32 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-08-11 17:21:01 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-09-26 11:03:14 +08:00
|
|
|
Arg* arg = obj_getArg(obj, name);
|
|
|
|
if (NULL == arg) {
|
|
|
|
arg = obj_getMethodArg(obj, name);
|
2022-10-15 19:02:36 +08:00
|
|
|
return arg_copy(arg);
|
2022-09-26 11:03:14 +08:00
|
|
|
}
|
|
|
|
if (NULL != arg) {
|
|
|
|
res = arg_copy(arg);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return NULL;
|
2022-08-11 17:21:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-09-14 13:46:19 +08:00
|
|
|
|
2022-09-19 09:53:27 +08:00
|
|
|
void PikaStdLib_SysObj_exit(PikaObj* self) {
|
2022-09-14 13:46:19 +08:00
|
|
|
pks_vm_exit();
|
|
|
|
}
|
2022-09-26 10:14:01 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-09-26 11:03:14 +08:00
|
|
|
if (obj_isArgExist(obj, name)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Arg* method = obj_getMethodArg(obj, name);
|
|
|
|
if (NULL != method) {
|
|
|
|
arg_deinit(method);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-09-26 10:14:01 +08:00
|
|
|
}
|
2022-10-01 14:03:57 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-10-09 19:46:05 +08:00
|
|
|
|
2022-11-08 12:37:33 +08:00
|
|
|
static enum shellCTRL __obj_shellLineHandler_input(PikaObj* self,
|
2022-11-08 22:07:06 +08:00
|
|
|
char* input_line,
|
|
|
|
struct ShellConfig* cfg) {
|
2022-10-09 19:46:05 +08:00
|
|
|
cfg->context = arg_newStr(input_line);
|
2022-11-08 12:37:33 +08:00
|
|
|
return SHELL_CTRL_EXIT;
|
2022-10-09 19:46:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* PikaStdLib_SysObj_input(PikaObj* self, PikaTuple* info) {
|
2022-10-21 17:14:06 +08:00
|
|
|
struct ShellConfig cfg = {
|
|
|
|
.prefix = "",
|
|
|
|
.context = NULL,
|
|
|
|
.handler = __obj_shellLineHandler_input,
|
2022-10-25 14:32:43 +08:00
|
|
|
.fn_getchar = __platform_getchar,
|
2022-10-21 17:14:06 +08:00
|
|
|
};
|
2022-10-09 19:46:05 +08:00
|
|
|
if (tuple_getSize(info) > 0) {
|
2022-10-19 13:16:50 +08:00
|
|
|
__platform_printf("%s", tuple_getStr(info, 0));
|
2022-10-09 19:46:05 +08:00
|
|
|
}
|
2022-10-25 11:52:42 +08:00
|
|
|
_temp__do_pikaScriptShell(self, &cfg);
|
2022-10-09 19:46:05 +08:00
|
|
|
char* res = obj_cacheStr(self, arg_getStr(cfg.context));
|
|
|
|
arg_deinit(cfg.context);
|
|
|
|
return res;
|
|
|
|
}
|