mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
ca0aa5e1ab
commit
bc6d233f8b
@ -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.
|
||||
!!!
|
@ -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
|
||||
@ -94,12 +93,6 @@ class mat3x3:
|
||||
|
||||
# affine transformations
|
||||
@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 is_affine(self) -> bool: ...
|
||||
|
11
src/ceval.h
11
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);
|
||||
|
34
src/dict.h
34
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<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> 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
|
@ -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) {
|
||||
|
@ -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():
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user