Skip to content

Commit

Permalink
support int() for bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed May 27, 2022
1 parent e4ac0a8 commit 3c7b765
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions package/PikaStdLib/PikaStdLib_SysObj.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 87,16 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
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;
}
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 87,16 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
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;
}
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
Expand Down
15 changes: 15 additions & 0 deletions port/linux/test/pikaMain-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1876,3 1876,18 @@ TEST(pikaMain, REPL_push_mode) {
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}

TEST(pikaMain, int_from_bytes) {
/* init */
pikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
__platform_printf("BEGIN\r\n");
obj_run(self, "int(b'test'[0])\n");
/* assert */
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
EXPECT_STREQ(log_buff[0], "116\r\n");
/* deinit */
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}

0 comments on commit 3c7b765

Please sign in to comment.