Browse Source

add `py_newtrivial`

backup

add `py_totrivial`
blueloveTH 8 months ago
parent
commit
13e82c1913
3 changed files with 13 additions and 0 deletions
  1. 5 0
      include/pocketpy/pocketpy.h
  2. 2 0
      src/public/cast.c
  3. 6 0
      src/public/values.c

+ 5 - 0
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__`.

+ 2 - 0
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;

+ 6 - 0
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;