mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
f010ca07a2
commit
7ca82df6ca
@ -125,18 +125,19 @@ namespace pkpy{
|
|||||||
|
|
||||||
void C99Struct::_register(VM* vm, PyObject* mod, PyObject* type){
|
void C99Struct::_register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
|
vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
|
||||||
|
Type cls = PK_OBJ_GET(Type, args[0]);
|
||||||
if(args.size() == 1+1){
|
if(args.size() == 1+1){
|
||||||
if(is_int(args[1])){
|
if(is_int(args[1])){
|
||||||
int size = _CAST(int, args[1]);
|
int size = _CAST(int, args[1]);
|
||||||
return VAR_T(C99Struct, size);
|
return vm->heap.gcnew<C99Struct>(cls, size);
|
||||||
}
|
}
|
||||||
if(is_non_tagged_type(args[1], vm->tp_str)){
|
if(is_non_tagged_type(args[1], vm->tp_str)){
|
||||||
const Str& s = _CAST(Str&, args[1]);
|
const Str& s = _CAST(Str&, args[1]);
|
||||||
return VAR_T(C99Struct, (void*)s.data, s.size);
|
return vm->heap.gcnew<C99Struct>(cls, (void*)s.data, s.size);
|
||||||
}
|
}
|
||||||
if(is_non_tagged_type(args[1], vm->tp_bytes)){
|
if(is_non_tagged_type(args[1], vm->tp_bytes)){
|
||||||
const Bytes& b = _CAST(Bytes&, args[1]);
|
const Bytes& b = _CAST(Bytes&, args[1]);
|
||||||
return VAR_T(C99Struct, (void*)b.data(), b.size());
|
return vm->heap.gcnew<C99Struct>(cls, (void*)b.data(), b.size());
|
||||||
}
|
}
|
||||||
vm->TypeError("expected int, str or bytes");
|
vm->TypeError("expected int, str or bytes");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
@ -144,7 +145,7 @@ namespace pkpy{
|
|||||||
if(args.size() == 1+2){
|
if(args.size() == 1+2){
|
||||||
void* p = CAST(void*, args[1]);
|
void* p = CAST(void*, args[1]);
|
||||||
int size = CAST(int, args[2]);
|
int size = CAST(int, args[2]);
|
||||||
return VAR_T(C99Struct, p, size);
|
return vm->heap.gcnew<C99Struct>(cls, p, size);
|
||||||
}
|
}
|
||||||
vm->TypeError("expected 1 or 2 arguments");
|
vm->TypeError("expected 1 or 2 arguments");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
|
@ -33,3 +33,26 @@ for i in range(10):
|
|||||||
|
|
||||||
c.free(array)
|
c.free(array)
|
||||||
c.free(array2)
|
c.free(array2)
|
||||||
|
|
||||||
|
class Vec2(c.struct):
|
||||||
|
def __new__(cls, x: float, y: float):
|
||||||
|
obj = c.struct.__new__(cls, 8)
|
||||||
|
obj.write_float(x, 0)
|
||||||
|
obj.write_float(y, 4)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
@property
|
||||||
|
def x(self) -> float:
|
||||||
|
return self.read_float(0)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def y(self) -> float:
|
||||||
|
return self.read_float(4)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"Vec2({self.x}, {self.y})"
|
||||||
|
|
||||||
|
a = Vec2(1, 2)
|
||||||
|
assert isinstance(a, c.struct)
|
||||||
|
assert type(a) is Vec2
|
||||||
|
assert repr(a) == "Vec2(1.0, 2.0)"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user