fix stdc.memcpy

This commit is contained in:
blueloveTH 2026-04-04 13:24:25 +08:00
parent 9a57b281e8
commit 6e3ed84a9e
2 changed files with 12 additions and 5 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

@ -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;