This commit is contained in:
blueloveTH 2023-05-20 18:45:46 +08:00
parent ca0aa5e1ab
commit bc6d233f8b
7 changed files with 55 additions and 28 deletions

View File

@ -16,6 +16,6 @@ Encode a python object into a JSON string.
It is supported by the compiler with `JSON_MODE` enabled. It is supported by the compiler with `JSON_MODE` enabled.
!!! !!!
There is a special method `__json__()` for object. There is a special method `__json__`.
If it is defined, it will be called when `json.dumps()` is called. If defined, it will be called when `json.dumps()` is called.
!!! !!!

View File

@ -23,8 +23,8 @@ class vec2:
def cross(self, other: vec2) -> float: ... def cross(self, other: vec2) -> float: ...
def length(self) -> float: ... def length(self) -> float: ...
def length_squared(self) -> float: ... def length_squared(self) -> float: ...
def normalize(self) -> None: ... def normalize(self) -> vec2: ...
def normalized(self) -> vec2: ... def rotate(self, radians: float) -> vec2: ...
class vec3: class vec3:
x: float x: float
@ -41,8 +41,7 @@ class vec3:
def cross(self, other: vec3) -> float: ... def cross(self, other: vec3) -> float: ...
def length(self) -> float: ... def length(self) -> float: ...
def length_squared(self) -> float: ... def length_squared(self) -> float: ...
def normalize(self) -> None: ... def normalize(self) -> vec3: ...
def normalized(self) -> vec3: ...
class mat3x3: class mat3x3:
_11: float _11: float
@ -94,12 +93,6 @@ class mat3x3:
# affine transformations # affine transformations
@staticmethod @staticmethod
def translate(v: vec2) -> mat3x3: ...
@staticmethod
def rotate(rad: float) -> mat3x3: ...
@staticmethod
def scale(v: vec2) -> mat3x3: ...
@staticmethod
def trs(t: vec2, r: float, s: vec2) -> mat3x3: ... def trs(t: vec2, r: float, s: vec2) -> mat3x3: ...
def is_affine(self) -> bool: ... def is_affine(self) -> bool: ...

View File

@ -8,7 +8,6 @@ namespace pkpy{
inline PyObject* VM::_run_top_frame(){ inline PyObject* VM::_run_top_frame(){
DEF_SNAME(add); DEF_SNAME(add);
DEF_SNAME(dict);
DEF_SNAME(set); DEF_SNAME(set);
DEF_SNAME(__enter__); DEF_SNAME(__enter__);
DEF_SNAME(__exit__); DEF_SNAME(__exit__);
@ -241,13 +240,17 @@ __NEXT_STEP:;
PUSH(_0); PUSH(_0);
DISPATCH(); DISPATCH();
TARGET(BUILD_DICT) TARGET(BUILD_DICT)
_0 = VAR(STACK_VIEW(byte.arg).to_tuple()); if(byte.arg == 0){
_0 = call(builtins->attr(dict), _0); PUSH(VAR(Dict(this)));
DISPATCH();
}
_0 = VAR(STACK_VIEW(byte.arg).to_list());
_0 = call(_t(tp_dict), _0);
STACK_SHRINK(byte.arg); STACK_SHRINK(byte.arg);
PUSH(_0); PUSH(_0);
DISPATCH(); DISPATCH();
TARGET(BUILD_SET) TARGET(BUILD_SET)
_0 = VAR(STACK_VIEW(byte.arg).to_tuple()); _0 = VAR(STACK_VIEW(byte.arg).to_list());
_0 = call(builtins->attr(set), _0); _0 = call(builtins->attr(set), _0);
STACK_SHRINK(byte.arg); STACK_SHRINK(byte.arg);
PUSH(_0); PUSH(_0);

View File

@ -29,6 +29,29 @@ struct Dict{
int size() const { return _size; } int size() const { return _size; }
Dict(Dict&& other){
vm = other.vm;
_capacity = other._capacity;
_mask = other._mask;
_size = other._size;
_critical_size = other._critical_size;
_items = other._items;
other._items = nullptr;
}
Dict(const Dict& other){
vm = other.vm;
_capacity = other._capacity;
_mask = other._mask;
_size = other._size;
_critical_size = other._critical_size;
_items = (Item*)pool128.alloc(_capacity * sizeof(Item));
memcpy(_items, other._items, _capacity * sizeof(Item));
}
Dict& operator=(const Dict&) = delete;
Dict& operator=(Dict&&) = delete;
void _probe(PyObject* key, bool& ok, int& i) const; void _probe(PyObject* key, bool& ok, int& i) const;
void set(PyObject* key, PyObject* val){ void set(PyObject* key, PyObject* val){
@ -84,9 +107,16 @@ struct Dict{
_size--; _size--;
} }
void update(const Dict& other){
for(int i=0; i<other._capacity; i++){
if(other._items[i].first == nullptr) continue;
set(other._items[i].first, other._items[i].second);
}
}
std::vector<Item> items() const { std::vector<Item> items() const {
std::vector<Item> v; std::vector<Item> v;
for(uint16_t i=0; i<_capacity; i++){ for(int i=0; i<_capacity; i++){
if(_items[i].first == nullptr) continue; if(_items[i].first == nullptr) continue;
v.push_back(_items[i]); v.push_back(_items[i]);
} }
@ -98,7 +128,7 @@ struct Dict{
_size = 0; _size = 0;
} }
~Dict(){ pool128.dealloc(_items); } ~Dict(){ if(_items!=nullptr) pool128.dealloc(_items); }
}; };
} // namespace pkpy } // namespace pkpy

View File

@ -774,7 +774,7 @@ inline void init_builtins(VM* _vm) {
if(args.size() == 1+1){ if(args.size() == 1+1){
auto _lock = vm->heap.gc_scope_lock(); auto _lock = vm->heap.gc_scope_lock();
Dict& self = _CAST(Dict&, args[0]); Dict& self = _CAST(Dict&, args[0]);
List& list = CAST(List&, vm->py_list(args[1])); List& list = CAST(List&, args[1]);
for(PyObject* item : list){ for(PyObject* item : list){
Tuple& t = CAST(Tuple&, item); Tuple& t = CAST(Tuple&, item);
if(t.size() != 2){ if(t.size() != 2){
@ -783,6 +783,7 @@ inline void init_builtins(VM* _vm) {
} }
self.set(t[0], t[1]); self.set(t[0], t[1]);
} }
return vm->None;
} }
vm->TypeError("dict() takes at most 1 argument"); vm->TypeError("dict() takes at most 1 argument");
return vm->None; return vm->None;
@ -854,18 +855,16 @@ inline void init_builtins(VM* _vm) {
return VAR(std::move(items)); return VAR(std::move(items));
}); });
_vm->bind_method<-1>("dict", "update", [](VM* vm, ArgsView args) { _vm->bind_method<1>("dict", "update", [](VM* vm, ArgsView args) {
Dict& self = _CAST(Dict&, args[0]); Dict& self = _CAST(Dict&, args[0]);
Dict& other = CAST(Dict&, args[1]); const Dict& other = CAST(Dict&, args[1]);
for(auto& item : other.items()) self.set(item.first, item.second); self.update(other);
return vm->None; return vm->None;
}); });
_vm->bind_method<0>("dict", "copy", [](VM* vm, ArgsView args) { _vm->bind_method<0>("dict", "copy", [](VM* vm, ArgsView args) {
Dict& self = _CAST(Dict&, args[0]); const Dict& self = _CAST(Dict&, args[0]);
Dict copy(vm); return VAR(self);
for(auto& item : self.items()) copy.set(item.first, item.second);
return VAR(std::move(copy));
}); });
_vm->bind_method<0>("dict", "clear", [](VM* vm, ArgsView args) { _vm->bind_method<0>("dict", "clear", [](VM* vm, ArgsView args) {

View File

@ -16,6 +16,8 @@ for k,v in dict1.items():
tinydict = {'Name': 'circle', 'Age': 7} tinydict = {'Name': 'circle', 'Age': 7}
tinydict2 = {'Sex': 'female' } tinydict2 = {'Sex': 'female' }
assert len(tinydict) == 2
assert len(tinydict2) == 1
tinydict.update(tinydict2) tinydict.update(tinydict2)
updated_dict = {'Name': 'circle', 'Age': 7, 'Sex': 'female'} updated_dict = {'Name': 'circle', 'Age': 7, 'Sex': 'female'}
for k,v in tinydict.items(): for k,v in tinydict.items():

View File

@ -10,7 +10,7 @@ assert hasattr(int, '__add__')
assert type(1).__add__(1, 2) == 3 assert type(1).__add__(1, 2) == 3
assert getattr(1, '__add__')(2) == 3 assert getattr(1, '__add__')(2) == 3
a = {} a = object()
setattr(a, 'b', 1) setattr(a, 'b', 1)
assert a.b == 1 assert a.b == 1
assert getattr(a, 'b') == 1 assert getattr(a, 'b') == 1