blueloveTH пре 1 недеља
родитељ
комит
6baa5c7f52
2 измењених фајлова са 11 додато и 3 уклоњено
  1. 4 1
      include/pocketpy/pocketpy.h
  2. 7 2
      src/public/ValueCreation.c

+ 4 - 1
include/pocketpy/pocketpy.h

@@ -209,7 +209,10 @@ PK_API bool py_smarteval(const char* source, py_Ref module, ...) PY_RAISE PY_RET
 
 
 /// Create an `int` object.
 /// Create an `int` object.
 PK_API void py_newint(py_OutRef, py_i64);
 PK_API void py_newint(py_OutRef, py_i64);
-/// Create a trivial value object.
+/// Create a trivial value object from the first `size` (<= 16) bytes of `data`.
+/// Every byte of `out` is set: what `data` does not fill is zero, `extra` included,
+/// so equal values are equal byte for byte, in memory and once pickled.
+/// To tag the value through `extra`, set it after this call.
 PK_API void py_newtrivial(py_OutRef out, py_Type type, void* data, int size);
 PK_API void py_newtrivial(py_OutRef out, py_Type type, void* data, int size);
 /// Create a `float` object.
 /// Create a `float` object.
 PK_API void py_newfloat(py_OutRef, py_f64);
 PK_API void py_newfloat(py_OutRef, py_f64);

+ 7 - 2
src/public/ValueCreation.c

@@ -14,9 +14,14 @@ void py_newint(py_OutRef out, py_i64 val) {
 }
 }
 
 
 void py_newtrivial(py_OutRef out, py_Type type, void* data, int size) {
 void py_newtrivial(py_OutRef out, py_Type type, void* data, int size) {
-    out->type = type;
-    out->is_ptr = false;
     assert(size <= 16);
     assert(size <= 16);
+    // A trivial value is kept, compared and pickled as its raw bytes, so all of
+    // them are set: zero first, which leaves `is_ptr` false and covers the padding
+    // after it, `extra` and whatever of the 16 bytes payload `data` does not fill.
+    // Only what is not zero is assigned after that. A caller that tags the value
+    // through `extra` sets it after this call.
+    memset(out, 0, sizeof(py_TValue));
+    out->type = type;
     memcpy(&out->_chars, data, size);
     memcpy(&out->_chars, data, size);
 }
 }