From 13e82c1913557d18ef1156c7b2b2f4c69d611de3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 26 Jun 2025 00:01:38 +0800 Subject: [PATCH] add `py_newtrivial` backup add `py_totrivial` --- include/pocketpy/pocketpy.h | 5 +++++ src/public/cast.c | 2 ++ src/public/values.c | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 33e5f5db..9480eb57 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -201,6 +201,8 @@ PK_API py_GlobalRef py_NIL(); /// Create an `int` object. PK_API void py_newint(py_OutRef, py_i64); +/// Create a trivial value object. +PK_API void py_newtrivial(py_OutRef out, py_Type type, py_i64 data); /// Create a `float` object. PK_API void py_newfloat(py_OutRef, py_f64); /// Create a `bool` object. @@ -282,6 +284,8 @@ PK_API void* py_newobject(py_OutRef out, py_Type type, int slots, int udsize); /// Convert an `int` object in python to `int64_t`. PK_API py_i64 py_toint(py_Ref); +/// Convert a trivial value object in python to `int64_t`. +PK_API py_i64 py_totrivial(py_Ref); /// Convert a `float` object in python to `double`. PK_API py_f64 py_tofloat(py_Ref); /// Cast a `int` or `float` object in python to `double`. @@ -389,6 +393,7 @@ PK_API void py_setglobal(py_Name name, py_Ref val); PK_API py_ItemRef py_getbuiltin(py_Name name); /// Get the last return value. +/// Please note that `py_retval()` cannot be used as input argument. PK_API py_GlobalRef py_retval(); /// Get an item from the object's `__dict__`. diff --git a/src/public/cast.c b/src/public/cast.c index 4deabef1..c97d0893 100644 --- a/src/public/cast.c +++ b/src/public/cast.c @@ -9,6 +9,8 @@ int64_t py_toint(py_Ref self) { return self->_i64; } +py_i64 py_totrivial(py_Ref self) { return self->_i64; } + double py_tofloat(py_Ref self) { assert(self->type == tp_float); return self->_f64; diff --git a/src/public/values.c b/src/public/values.c index 590a29ac..d408ca94 100644 --- a/src/public/values.c +++ b/src/public/values.c @@ -13,6 +13,12 @@ void py_newint(py_OutRef out, py_i64 val) { out->_i64 = val; } +void py_newtrivial(py_OutRef out, py_Type type, py_i64 data) { + out->type = type; + out->is_ptr = false; + out->_i64 = data; +} + void py_newfloat(py_OutRef out, py_f64 val) { out->type = tp_float; out->is_ptr = false;