Compare commits

...

5 Commits

Author SHA1 Message Date
Tanisha Vasudeva
b2133c5f4d
Merge 6cc3d8023350163e72b83d49981ad2c4b08d39b4 into 2600eb535296ff1063e9e674ce61be9a90001798 2026-04-04 13:43:58 +00:00
blueloveTH
2600eb5352 Update lz4.c 2026-04-04 16:09:06 +08:00
blueloveTH
2a2e9ae1f2 Update lz4.c 2026-04-04 15:41:15 +08:00
blueloveTH
6e3ed84a9e fix stdc.memcpy 2026-04-04 13:24:25 +08:00
tanisha
6cc3d80233 Added readline() 2026-02-28 20:28:04 +05:30
4 changed files with 51 additions and 17 deletions

View File

@ -5,7 +5,7 @@ intptr = int
def malloc(size: int) -> intptr: ...
def free(ptr: intptr) -> None: ...
def memcpy(dst: intptr, src: intptr, n: int) -> None: ...
def memcpy(dst: intptr, src: intptr | bytes, n: int) -> None: ...
def memset(s: intptr, c: int, n: int) -> None: ...
def memcmp(s1: intptr, s2: intptr, n: int) -> int: ...

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <assert.h>
#include "pocketpy/common/utils.h"
#include "pocketpy/pocketpy.h"
#include "lz4/lib/lz4.h"
@ -10,13 +11,13 @@ static bool lz4_compress(int argc, py_Ref argv) {
PY_CHECK_ARG_TYPE(0, tp_bytes);
int src_size;
const void* src = py_tobytes(argv, &src_size);
int dst_capacity = LZ4_compressBound(src_size);
char* p = (char*)py_newbytes(py_retval(), sizeof(int) + dst_capacity);
memcpy(p, &src_size, sizeof(int));
char* dst = p + sizeof(int);
uint32_t dst_capacity = LZ4_compressBound(src_size);
char* p = (char*)py_newbytes(py_retval(), sizeof(uint32_t) + dst_capacity);
memcpy(p, &src_size, sizeof(uint32_t));
char* dst = p + sizeof(uint32_t);
int dst_size = LZ4_compress_default(src, dst, src_size, dst_capacity);
if(dst_size <= 0) return ValueError("LZ4 compression failed");
py_bytes_resize(py_retval(), sizeof(int) + dst_size);
py_bytes_resize(py_retval(), sizeof(uint32_t) + dst_size);
return true;
}
@ -24,15 +25,16 @@ static bool lz4_decompress(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
PY_CHECK_ARG_TYPE(0, tp_bytes);
int total_size;
const int* p = (int*)py_tobytes(argv, &total_size);
const uint32_t* p = (uint32_t*)py_tobytes(argv, &total_size);
const char* src = (const char*)(p + 1);
if(total_size < sizeof(int)) return ValueError("invalid LZ4 data");
int uncompressed_size = *p;
if(uncompressed_size < 0) return ValueError("invalid LZ4 data");
if(total_size < sizeof(uint32_t)) return ValueError("invalid LZ4 data");
uint32_t uncompressed_size;
memcpy(&uncompressed_size, p, sizeof(uint32_t));
if(uncompressed_size >= INT32_MAX) return ValueError("invalid LZ4 data");
char* dst = (char*)py_newbytes(py_retval(), uncompressed_size);
int dst_size = LZ4_decompress_safe(src, dst, total_size - sizeof(int), uncompressed_size);
int dst_size = LZ4_decompress_safe(src, dst, total_size - sizeof(uint32_t), uncompressed_size);
if(dst_size < 0) return ValueError("LZ4 decompression failed");
assert(dst_size == uncompressed_size);
c11__rtassert(dst_size == uncompressed_size);
return true;
}

View File

@ -215,7 +215,31 @@ static bool io_FileIO_flush(int argc, py_Ref argv) {
py_newnone(py_retval());
return true;
}
static bool io_FileIO_readlines(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
io_FileIO* ud = py_touserdata(py_arg(0));
py_newlist(py_retval());
char* buf = NULL;
size_t buf_size = 0;
size_t len = 0;
int c;
while(true) {
len = 0;
while((c = fgetc(ud->file)) != EOF) {
if(len + 1 >= buf_size) {
buf_size = (buf_size == 0) ? 64 : buf_size * 2;
buf = PK_REALLOC(buf, buf_size);
}
buf[len++] = (char)c;
if(c == '\n') break;
}
if(len == 0) break;
py_newstrv(py_getreg(0), (c11_sv){buf, len});
py_list_append(py_retval(), py_getreg(0));
}
if(buf) PK_FREE(buf);
return true;
}
void pk__add_module_io() {
py_Ref mod = py_newmodule("io");
@ -230,6 +254,7 @@ void pk__add_module_io() {
py_bindmethod(FileIO, "tell", io_FileIO_tell);
py_bindmethod(FileIO, "seek", io_FileIO_seek);
py_bindmethod(FileIO, "flush", io_FileIO_flush);
py_bindmethod(FileIO, "readlines", io_FileIO_readlines);
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);

View File

@ -124,12 +124,19 @@ static bool stdc_free(int argc, py_Ref argv) {
static bool stdc_memcpy(int argc, py_Ref argv) {
PY_CHECK_ARGC(3);
PY_CHECK_ARG_TYPE(0, tp_int);
PY_CHECK_ARG_TYPE(1, tp_int);
PY_CHECK_ARG_TYPE(2, tp_int);
PY_CHECK_ARG_TYPE(0, tp_int); // dst
void* dst = (void*)(intptr_t)py_toint(&argv[0]);
void* src = (void*)(intptr_t)py_toint(&argv[1]);
PY_CHECK_ARG_TYPE(2, tp_int); // n
py_i64 n = py_toint(&argv[2]);
void* src;
if(py_istype(&argv[1], tp_bytes)) {
int size;
src = py_tobytes(&argv[1], &size);
if(size < n) n = size;
} else {
PY_CHECK_ARG_TYPE(1, tp_int); // src
src = (void*)(intptr_t)py_toint(&argv[1]);
}
memcpy(dst, src, (size_t)n);
py_newnone(py_retval());
return true;