From bc6d233f8b1a83cb7ad06a80fea5d746eeed93a4 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 20 May 2023 18:45:46 +0800 Subject: [PATCH] ... --- docs/modules/json.md | 4 ++-- docs/modules/linalg.md | 15 ++++----------- src/ceval.h | 11 +++++++---- src/dict.h | 34 ++++++++++++++++++++++++++++++++-- src/pocketpy.h | 15 +++++++-------- tests/07_dict.py | 2 ++ tests/47_reflection.py | 2 +- 7 files changed, 55 insertions(+), 28 deletions(-) diff --git a/docs/modules/json.md b/docs/modules/json.md index 55bb0515..e3f721c8 100644 --- a/docs/modules/json.md +++ b/docs/modules/json.md @@ -16,6 +16,6 @@ Encode a python object into a JSON string. It is supported by the compiler with `JSON_MODE` enabled. !!! -There is a special method `__json__()` for object. -If it is defined, it will be called when `json.dumps()` is called. +There is a special method `__json__`. +If defined, it will be called when `json.dumps()` is called. !!! \ No newline at end of file diff --git a/docs/modules/linalg.md b/docs/modules/linalg.md index 84ba2baf..22208e6a 100644 --- a/docs/modules/linalg.md +++ b/docs/modules/linalg.md @@ -23,8 +23,8 @@ class vec2: def cross(self, other: vec2) -> float: ... def length(self) -> float: ... def length_squared(self) -> float: ... - def normalize(self) -> None: ... - def normalized(self) -> vec2: ... + def normalize(self) -> vec2: ... + def rotate(self, radians: float) -> vec2: ... class vec3: x: float @@ -41,8 +41,7 @@ class vec3: def cross(self, other: vec3) -> float: ... def length(self) -> float: ... def length_squared(self) -> float: ... - def normalize(self) -> None: ... - def normalized(self) -> vec3: ... + def normalize(self) -> vec3: ... class mat3x3: _11: float @@ -92,13 +91,7 @@ class mat3x3: @staticmethod def identity() -> mat3x3: ... - # affine transformations - @staticmethod - def translate(v: vec2) -> mat3x3: ... - @staticmethod - def rotate(rad: float) -> mat3x3: ... - @staticmethod - def scale(v: vec2) -> mat3x3: ... + # affine transformations @staticmethod def trs(t: vec2, r: float, s: vec2) -> mat3x3: ... diff --git a/src/ceval.h b/src/ceval.h index a1c3dfb1..98656040 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -8,7 +8,6 @@ namespace pkpy{ inline PyObject* VM::_run_top_frame(){ DEF_SNAME(add); - DEF_SNAME(dict); DEF_SNAME(set); DEF_SNAME(__enter__); DEF_SNAME(__exit__); @@ -241,13 +240,17 @@ __NEXT_STEP:; PUSH(_0); DISPATCH(); TARGET(BUILD_DICT) - _0 = VAR(STACK_VIEW(byte.arg).to_tuple()); - _0 = call(builtins->attr(dict), _0); + if(byte.arg == 0){ + PUSH(VAR(Dict(this))); + DISPATCH(); + } + _0 = VAR(STACK_VIEW(byte.arg).to_list()); + _0 = call(_t(tp_dict), _0); STACK_SHRINK(byte.arg); PUSH(_0); DISPATCH(); 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); STACK_SHRINK(byte.arg); PUSH(_0); diff --git a/src/dict.h b/src/dict.h index f0c39eb0..645ef9c0 100644 --- a/src/dict.h +++ b/src/dict.h @@ -29,6 +29,29 @@ struct Dict{ 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 set(PyObject* key, PyObject* val){ @@ -84,9 +107,16 @@ struct Dict{ _size--; } + void update(const Dict& other){ + for(int i=0; i items() const { std::vector v; - for(uint16_t i=0; i<_capacity; i++){ + for(int i=0; i<_capacity; i++){ if(_items[i].first == nullptr) continue; v.push_back(_items[i]); } @@ -98,7 +128,7 @@ struct Dict{ _size = 0; } - ~Dict(){ pool128.dealloc(_items); } + ~Dict(){ if(_items!=nullptr) pool128.dealloc(_items); } }; } // namespace pkpy \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 58be1cb5..2a2d6471 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -774,7 +774,7 @@ inline void init_builtins(VM* _vm) { if(args.size() == 1+1){ auto _lock = vm->heap.gc_scope_lock(); 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){ Tuple& t = CAST(Tuple&, item); if(t.size() != 2){ @@ -783,6 +783,7 @@ inline void init_builtins(VM* _vm) { } self.set(t[0], t[1]); } + return vm->None; } vm->TypeError("dict() takes at most 1 argument"); return vm->None; @@ -854,18 +855,16 @@ inline void init_builtins(VM* _vm) { 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& other = CAST(Dict&, args[1]); - for(auto& item : other.items()) self.set(item.first, item.second); + const Dict& other = CAST(Dict&, args[1]); + self.update(other); return vm->None; }); _vm->bind_method<0>("dict", "copy", [](VM* vm, ArgsView args) { - Dict& self = _CAST(Dict&, args[0]); - Dict copy(vm); - for(auto& item : self.items()) copy.set(item.first, item.second); - return VAR(std::move(copy)); + const Dict& self = _CAST(Dict&, args[0]); + return VAR(self); }); _vm->bind_method<0>("dict", "clear", [](VM* vm, ArgsView args) { diff --git a/tests/07_dict.py b/tests/07_dict.py index 50f21cff..e95c1953 100644 --- a/tests/07_dict.py +++ b/tests/07_dict.py @@ -16,6 +16,8 @@ for k,v in dict1.items(): tinydict = {'Name': 'circle', 'Age': 7} tinydict2 = {'Sex': 'female' } +assert len(tinydict) == 2 +assert len(tinydict2) == 1 tinydict.update(tinydict2) updated_dict = {'Name': 'circle', 'Age': 7, 'Sex': 'female'} for k,v in tinydict.items(): diff --git a/tests/47_reflection.py b/tests/47_reflection.py index 9361f6d6..a48c364f 100644 --- a/tests/47_reflection.py +++ b/tests/47_reflection.py @@ -10,7 +10,7 @@ assert hasattr(int, '__add__') assert type(1).__add__(1, 2) == 3 assert getattr(1, '__add__')(2) == 3 -a = {} +a = object() setattr(a, 'b', 1) assert a.b == 1 assert getattr(a, 'b') == 1 \ No newline at end of file