Преглед изворни кода

use 24 bytes data

Update pocketpy.h

Update pocketpy.h
blueloveTH пре 7 месеци
родитељ
комит
7016a8780c

+ 3 - 1
include/pocketpy/objects/base.h

@@ -19,8 +19,10 @@ typedef struct py_TValue {
         PyObject* _obj;
         c11_vec2 _vec2;
         c11_vec2i _vec2i;
+        c11_vec3 _vec3;
+        c11_vec3i _vec3i;
         c11_color32 _color32;
         void* _ptr;
-        char _chars[8];
+        char _chars[16];
     };
 } py_TValue;

+ 8 - 3
include/pocketpy/pocketpy.h

@@ -33,8 +33,13 @@ typedef struct py_TValue {
     py_Type type;
     bool is_ptr;
     int extra;
-    int64_t _i64;
+    union {
+        int64_t _i64;
+        char _chars[16];
+    };
 } py_TValue;
+
+static_assert(sizeof(py_TValue) == 24, "sizeof(py_TValue) != 24");
 #endif
 
 /// A string view type. It is helpful for passing strings which are not null-terminated.
@@ -212,7 +217,7 @@ 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);
+PK_API void py_newtrivial(py_OutRef out, py_Type type, void* data, int size);
 /// Create a `float` object.
 PK_API void py_newfloat(py_OutRef, py_f64);
 /// Create a `bool` object.
@@ -295,7 +300,7 @@ 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);
+PK_API void* 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`.

+ 4 - 6
src/modules/vmath.c

@@ -58,25 +58,23 @@ c11_vec2i py_tovec2i(py_Ref self) {
 void py_newvec3(py_OutRef out, c11_vec3 v) {
     out->type = tp_vec3;
     out->is_ptr = false;
-    c11_vec3* data = (c11_vec3*)(&out->extra);
-    *data = v;
+    out->_vec3 = v;
 }
 
 c11_vec3 py_tovec3(py_Ref self) {
     assert(self->type == tp_vec3);
-    return *(c11_vec3*)(&self->extra);
+    return self->_vec3;
 }
 
 void py_newvec3i(py_OutRef out, c11_vec3i v) {
     out->type = tp_vec3i;
     out->is_ptr = false;
-    c11_vec3i* data = (c11_vec3i*)(&out->extra);
-    *data = v;
+    out->_vec3i = v;
 }
 
 c11_vec3i py_tovec3i(py_Ref self) {
     assert(self->type == tp_vec3i);
-    return *(c11_vec3i*)(&self->extra);
+    return self->_vec3i;
 }
 
 c11_mat3x3* py_newmat3x3(py_OutRef out) {

+ 1 - 1
src/public/cast.c

@@ -9,7 +9,7 @@ int64_t py_toint(py_Ref self) {
     return self->_i64;
 }
 
-py_i64 py_totrivial(py_Ref self) { return self->_i64; }
+void* py_totrivial(py_Ref self) { return &self->_chars; }
 
 double py_tofloat(py_Ref self) {
     assert(self->type == tp_float);

+ 1 - 3
src/public/internal.c

@@ -30,9 +30,7 @@ void py_initialize() {
     bool is_little_endian = *(char*)&x == 1;
     if(!is_little_endian) c11__abort("is_little_endian != true");
 
-    // check py_TValue; 16 bytes to make py_arg() macro work
-    static_assert(sizeof(py_CFunction) <= 8, "sizeof(py_CFunction) > 8");
-    static_assert(sizeof(py_TValue) == 16, "sizeof(py_TValue) != 16");
+    static_assert(sizeof(py_TValue) == 24, "sizeof(py_TValue) != 24");
     static_assert(offsetof(py_TValue, extra) == 4, "offsetof(py_TValue, extra) != 4");
 
     pk_current_vm = pk_all_vm[0] = &pk_default_vm;

+ 1 - 1
src/public/py_str.c

@@ -9,7 +9,7 @@
 void py_newstr(py_OutRef out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
 
 char* py_newstrn(py_OutRef out, int size) {
-    if(size < 8) {
+    if(size < 16) {
         out->type = tp_str;
         out->is_ptr = false;
         c11_string* ud = (c11_string*)(&out->extra);

+ 3 - 2
src/public/values.c

@@ -13,10 +13,11 @@ void py_newint(py_OutRef out, py_i64 val) {
     out->_i64 = val;
 }
 
-void py_newtrivial(py_OutRef out, py_Type type, py_i64 data) {
+void py_newtrivial(py_OutRef out, py_Type type, void* data, int size) {
     out->type = type;
     out->is_ptr = false;
-    out->_i64 = data;
+    assert(size <= 16);
+    memcpy(&out->_chars, data, size);
 }
 
 void py_newfloat(py_OutRef out, py_f64 val) {

+ 4 - 0
tests/04_str.py

@@ -231,3 +231,7 @@ assert f"{(1, 2, 3)}" == "(1, 2, 3)"
 
 # stack=[1,2,3,4]
 # assert f"{stack[2:]}" == '[3, 4]'
+
+
+assert id('1' * 16) is not None
+assert id('1' * 15) is None