mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
use 24 bytes data
Update pocketpy.h Update pocketpy.h
This commit is contained in:
parent
9c2b96e572
commit
7016a8780c
@ -19,8 +19,10 @@ typedef struct py_TValue {
|
|||||||
PyObject* _obj;
|
PyObject* _obj;
|
||||||
c11_vec2 _vec2;
|
c11_vec2 _vec2;
|
||||||
c11_vec2i _vec2i;
|
c11_vec2i _vec2i;
|
||||||
|
c11_vec3 _vec3;
|
||||||
|
c11_vec3i _vec3i;
|
||||||
c11_color32 _color32;
|
c11_color32 _color32;
|
||||||
void* _ptr;
|
void* _ptr;
|
||||||
char _chars[8];
|
char _chars[16];
|
||||||
};
|
};
|
||||||
} py_TValue;
|
} py_TValue;
|
||||||
|
@ -33,8 +33,13 @@ typedef struct py_TValue {
|
|||||||
py_Type type;
|
py_Type type;
|
||||||
bool is_ptr;
|
bool is_ptr;
|
||||||
int extra;
|
int extra;
|
||||||
|
union {
|
||||||
int64_t _i64;
|
int64_t _i64;
|
||||||
|
char _chars[16];
|
||||||
|
};
|
||||||
} py_TValue;
|
} py_TValue;
|
||||||
|
|
||||||
|
static_assert(sizeof(py_TValue) == 24, "sizeof(py_TValue) != 24");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// A string view type. It is helpful for passing strings which are not null-terminated.
|
/// 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.
|
/// 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.
|
||||||
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.
|
/// Create a `float` object.
|
||||||
PK_API void py_newfloat(py_OutRef, py_f64);
|
PK_API void py_newfloat(py_OutRef, py_f64);
|
||||||
/// Create a `bool` object.
|
/// 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`.
|
/// Convert an `int` object in python to `int64_t`.
|
||||||
PK_API py_i64 py_toint(py_Ref);
|
PK_API py_i64 py_toint(py_Ref);
|
||||||
/// Convert a trivial value object in python to `int64_t`.
|
/// 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`.
|
/// Convert a `float` object in python to `double`.
|
||||||
PK_API py_f64 py_tofloat(py_Ref);
|
PK_API py_f64 py_tofloat(py_Ref);
|
||||||
/// Cast a `int` or `float` object in python to `double`.
|
/// Cast a `int` or `float` object in python to `double`.
|
||||||
|
@ -58,25 +58,23 @@ c11_vec2i py_tovec2i(py_Ref self) {
|
|||||||
void py_newvec3(py_OutRef out, c11_vec3 v) {
|
void py_newvec3(py_OutRef out, c11_vec3 v) {
|
||||||
out->type = tp_vec3;
|
out->type = tp_vec3;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
c11_vec3* data = (c11_vec3*)(&out->extra);
|
out->_vec3 = v;
|
||||||
*data = v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_vec3 py_tovec3(py_Ref self) {
|
c11_vec3 py_tovec3(py_Ref self) {
|
||||||
assert(self->type == tp_vec3);
|
assert(self->type == tp_vec3);
|
||||||
return *(c11_vec3*)(&self->extra);
|
return self->_vec3;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newvec3i(py_OutRef out, c11_vec3i v) {
|
void py_newvec3i(py_OutRef out, c11_vec3i v) {
|
||||||
out->type = tp_vec3i;
|
out->type = tp_vec3i;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
c11_vec3i* data = (c11_vec3i*)(&out->extra);
|
out->_vec3i = v;
|
||||||
*data = v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_vec3i py_tovec3i(py_Ref self) {
|
c11_vec3i py_tovec3i(py_Ref self) {
|
||||||
assert(self->type == tp_vec3i);
|
assert(self->type == tp_vec3i);
|
||||||
return *(c11_vec3i*)(&self->extra);
|
return self->_vec3i;
|
||||||
}
|
}
|
||||||
|
|
||||||
c11_mat3x3* py_newmat3x3(py_OutRef out) {
|
c11_mat3x3* py_newmat3x3(py_OutRef out) {
|
||||||
|
@ -9,7 +9,7 @@ int64_t py_toint(py_Ref self) {
|
|||||||
return self->_i64;
|
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) {
|
double py_tofloat(py_Ref self) {
|
||||||
assert(self->type == tp_float);
|
assert(self->type == tp_float);
|
||||||
|
@ -30,9 +30,7 @@ void py_initialize() {
|
|||||||
bool is_little_endian = *(char*)&x == 1;
|
bool is_little_endian = *(char*)&x == 1;
|
||||||
if(!is_little_endian) c11__abort("is_little_endian != true");
|
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_TValue) == 24, "sizeof(py_TValue) != 24");
|
||||||
static_assert(sizeof(py_CFunction) <= 8, "sizeof(py_CFunction) > 8");
|
|
||||||
static_assert(sizeof(py_TValue) == 16, "sizeof(py_TValue) != 16");
|
|
||||||
static_assert(offsetof(py_TValue, extra) == 4, "offsetof(py_TValue, extra) != 4");
|
static_assert(offsetof(py_TValue, extra) == 4, "offsetof(py_TValue, extra) != 4");
|
||||||
|
|
||||||
pk_current_vm = pk_all_vm[0] = &pk_default_vm;
|
pk_current_vm = pk_all_vm[0] = &pk_default_vm;
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
void py_newstr(py_OutRef out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
|
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) {
|
char* py_newstrn(py_OutRef out, int size) {
|
||||||
if(size < 8) {
|
if(size < 16) {
|
||||||
out->type = tp_str;
|
out->type = tp_str;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
c11_string* ud = (c11_string*)(&out->extra);
|
c11_string* ud = (c11_string*)(&out->extra);
|
||||||
|
@ -13,10 +13,11 @@ void py_newint(py_OutRef out, py_i64 val) {
|
|||||||
out->_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->type = type;
|
||||||
out->is_ptr = false;
|
out->is_ptr = false;
|
||||||
out->_i64 = data;
|
assert(size <= 16);
|
||||||
|
memcpy(&out->_chars, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newfloat(py_OutRef out, py_f64 val) {
|
void py_newfloat(py_OutRef out, py_f64 val) {
|
||||||
|
@ -231,3 +231,7 @@ assert f"{(1, 2, 3)}" == "(1, 2, 3)"
|
|||||||
|
|
||||||
# stack=[1,2,3,4]
|
# stack=[1,2,3,4]
|
||||||
# assert f"{stack[2:]}" == '[3, 4]'
|
# assert f"{stack[2:]}" == '[3, 4]'
|
||||||
|
|
||||||
|
|
||||||
|
assert id('1' * 16) is not None
|
||||||
|
assert id('1' * 15) is None
|
Loading…
x
Reference in New Issue
Block a user