Compare commits

...

3 Commits

Author SHA1 Message Date
blueloveTH
9c2b96e572 add function wrapper for malloc/free 2025-07-12 21:42:18 +08:00
blueloveTH
39958a92d2 Update ceval.c 2025-07-12 21:21:29 +08:00
blueloveTH
81679b740e fix https://github.com/pocketpy/pocketpy/issues/382 2025-07-12 21:11:12 +08:00
7 changed files with 59 additions and 6 deletions

View File

@ -127,6 +127,13 @@ PK_API int py_gc_collect();
/// Setup the callbacks for the current VM. /// Setup the callbacks for the current VM.
PK_API py_Callbacks* py_callbacks(); PK_API py_Callbacks* py_callbacks();
/// Wrapper for `PK_MALLOC(size)`.
PK_API void* py_malloc(size_t size);
/// Wrapper for `PK_REALLOC(ptr, size)`.
PK_API void* py_realloc(void* ptr, size_t size);
/// Wrapper for `PK_FREE(ptr)`.
PK_API void py_free(void* ptr);
/// Begin the watchdog with `timeout` in milliseconds. /// Begin the watchdog with `timeout` in milliseconds.
/// `PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature. /// `PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature.
/// You need to call `py_watchdog_end()` later. /// You need to call `py_watchdog_end()` later.

View File

@ -14,7 +14,7 @@ public:
// get the python exception object // get the python exception object
object& exception() { return m_exception; } object& exception() { return m_exception; }
~python_error() { std::free(m_what); } ~python_error() { py_free(m_what); }
bool match(py_Type type) const { return py_isinstance(m_exception.ptr(), type); } bool match(py_Type type) const { return py_isinstance(m_exception.ptr(), type); }

View File

@ -1303,7 +1303,10 @@ bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop) {
py_newbool(py_retval(), !res); py_newbool(py_retval(), !res);
return true; return true;
} }
return TypeError("unsupported operand type(s) for '%s'", pk_op2str(op));
py_Type lhs_t = rop ? TOP()->type : SECOND()->type;
py_Type rhs_t = rop ? SECOND()->type : TOP()->type;
return TypeError("unsupported operand type(s) for '%s': '%t' and '%t'", pk_op2str(op), lhs_t, rhs_t);
} }
bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) { bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {

View File

@ -1,8 +1,4 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include "pocketpy/common/utils.h"
#include "pocketpy/objects/object.h"
#include "pocketpy/common/sstream.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
#include <math.h> #include <math.h>

View File

@ -47,6 +47,18 @@ void py_initialize() {
pk_initialized = true; pk_initialized = true;
} }
void* py_malloc(size_t size) {
return PK_MALLOC(size);
}
void* py_realloc(void* ptr, size_t size) {
return PK_REALLOC(ptr, size);
}
void py_free(void* ptr) {
PK_FREE(ptr);
}
py_GlobalRef py_True() { return &_True; } py_GlobalRef py_True() { return &_True; }
py_GlobalRef py_False() { return &_False; } py_GlobalRef py_False() { return &_False; }

View File

@ -175,6 +175,32 @@ static bool int__mod__(int argc, py_Ref argv) {
return true; return true;
} }
static bool float__mod__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2);
py_f64 lhs = py_tofloat(&argv[0]);
py_f64 rhs;
if(try_castfloat(&argv[1], &rhs)) {
if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
py_newfloat(py_retval(), fmod(lhs, rhs));
return true;
}
py_newnotimplemented(py_retval());
return true;
}
static bool float__rmod__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2);
py_f64 rhs = py_tofloat(&argv[0]);
py_f64 lhs;
if(try_castfloat(&argv[1], &lhs)) {
if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
py_newfloat(py_retval(), fmod(lhs, rhs));
return true;
}
py_newnotimplemented(py_retval());
return true;
}
static bool int__divmod__(int argc, py_Ref argv) { static bool int__divmod__(int argc, py_Ref argv) {
PY_CHECK_ARGC(2); PY_CHECK_ARGC(2);
PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(1, tp_int);
@ -508,6 +534,10 @@ void pk_number__register() {
py_bindmagic(tp_int, __mod__, int__mod__); py_bindmagic(tp_int, __mod__, int__mod__);
py_bindmagic(tp_int, __divmod__, int__divmod__); py_bindmagic(tp_int, __divmod__, int__divmod__);
// fmod
py_bindmagic(tp_float, __mod__, float__mod__);
py_bindmagic(tp_float, __rmod__, float__rmod__);
// int.__invert__ & int.<BITWISE OP> // int.__invert__ & int.<BITWISE OP>
py_bindmagic(tp_int, __invert__, int__invert__); py_bindmagic(tp_int, __invert__, int__invert__);

View File

@ -109,3 +109,8 @@ assert abs(0.0) == 0.0
# exit(1) # exit(1)
# except ValueError: # except ValueError:
# pass # pass
assert eq(10 % 4, 2)
assert eq(10.5 % 4, 2.5)
assert eq(10 % 4.5, 1.0)
assert eq(10.5 % 4.5, 1.5)