mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
some fix
This commit is contained in:
parent
6ec4353aef
commit
2616c8378b
@ -113,8 +113,8 @@ struct PyObject final{
|
|||||||
static constexpr int FIXED_SIZE = 16;
|
static constexpr int FIXED_SIZE = 16;
|
||||||
|
|
||||||
bool gc_marked; // whether this object is marked
|
bool gc_marked; // whether this object is marked
|
||||||
Type type; // we have a duplicated type here for saving memory
|
Type type; // we have a duplicated type here for convenience
|
||||||
NameDict* _attr;
|
NameDict* _attr; // gc will delete this on destruction
|
||||||
|
|
||||||
bool is_attr_valid() const noexcept { return _attr != nullptr; }
|
bool is_attr_valid() const noexcept { return _attr != nullptr; }
|
||||||
void* _value_ptr() noexcept { return (char*)this + FIXED_SIZE; }
|
void* _value_ptr() noexcept { return (char*)this + FIXED_SIZE; }
|
||||||
|
@ -7,13 +7,8 @@
|
|||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
struct List: pod_vector<PyVar>{
|
|
||||||
using pod_vector<PyVar>::pod_vector;
|
|
||||||
void _gc_mark(VM*) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Tuple {
|
struct Tuple {
|
||||||
static const int INLINED_SIZE = 4;
|
static const int INLINED_SIZE = 3;
|
||||||
|
|
||||||
PyVar* _args;
|
PyVar* _args;
|
||||||
PyVar _inlined[INLINED_SIZE];
|
PyVar _inlined[INLINED_SIZE];
|
||||||
@ -22,7 +17,6 @@ struct Tuple {
|
|||||||
Tuple(int n);
|
Tuple(int n);
|
||||||
Tuple(const Tuple& other);
|
Tuple(const Tuple& other);
|
||||||
Tuple(Tuple&& other) noexcept;
|
Tuple(Tuple&& other) noexcept;
|
||||||
Tuple(List&& other) noexcept;
|
|
||||||
~Tuple();
|
~Tuple();
|
||||||
|
|
||||||
Tuple(PyVar, PyVar);
|
Tuple(PyVar, PyVar);
|
||||||
@ -41,6 +35,17 @@ struct Tuple {
|
|||||||
void _gc_mark(VM*) const;
|
void _gc_mark(VM*) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct List: pod_vector<PyVar>{
|
||||||
|
using pod_vector<PyVar>::pod_vector;
|
||||||
|
void _gc_mark(VM*) const;
|
||||||
|
|
||||||
|
Tuple to_tuple() const{
|
||||||
|
Tuple ret(size());
|
||||||
|
for(int i=0; i<size(); i++) ret[i] = (*this)[i];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// a lightweight view for function args, it does not own the memory
|
// a lightweight view for function args, it does not own the memory
|
||||||
struct ArgsView{
|
struct ArgsView{
|
||||||
PyVar* _begin;
|
PyVar* _begin;
|
||||||
|
@ -428,15 +428,13 @@ __NEXT_STEP:
|
|||||||
} DISPATCH()
|
} DISPATCH()
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
case OP_BUILD_TUPLE_UNPACK: {
|
case OP_BUILD_TUPLE_UNPACK: {
|
||||||
auto _lock = heap.gc_scope_lock();
|
|
||||||
List list;
|
List list;
|
||||||
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
||||||
STACK_SHRINK(byte.arg);
|
STACK_SHRINK(byte.arg);
|
||||||
PyVar _0 = VAR(Tuple(std::move(list)));
|
PyVar _0 = VAR(list.to_tuple());
|
||||||
PUSH(_0);
|
PUSH(_0);
|
||||||
} DISPATCH()
|
} DISPATCH()
|
||||||
case OP_BUILD_LIST_UNPACK: {
|
case OP_BUILD_LIST_UNPACK: {
|
||||||
auto _lock = heap.gc_scope_lock();
|
|
||||||
List list;
|
List list;
|
||||||
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
||||||
STACK_SHRINK(byte.arg);
|
STACK_SHRINK(byte.arg);
|
||||||
@ -444,7 +442,6 @@ __NEXT_STEP:
|
|||||||
PUSH(_0);
|
PUSH(_0);
|
||||||
} DISPATCH()
|
} DISPATCH()
|
||||||
case OP_BUILD_DICT_UNPACK: {
|
case OP_BUILD_DICT_UNPACK: {
|
||||||
auto _lock = heap.gc_scope_lock();
|
|
||||||
Dict dict;
|
Dict dict;
|
||||||
__unpack_as_dict(STACK_VIEW(byte.arg), dict);
|
__unpack_as_dict(STACK_VIEW(byte.arg), dict);
|
||||||
STACK_SHRINK(byte.arg);
|
STACK_SHRINK(byte.arg);
|
||||||
@ -452,7 +449,6 @@ __NEXT_STEP:
|
|||||||
PUSH(_0);
|
PUSH(_0);
|
||||||
} DISPATCH()
|
} DISPATCH()
|
||||||
case OP_BUILD_SET_UNPACK: {
|
case OP_BUILD_SET_UNPACK: {
|
||||||
auto _lock = heap.gc_scope_lock();
|
|
||||||
List list;
|
List list;
|
||||||
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
__unpack_as_list(STACK_VIEW(byte.arg), list);
|
||||||
STACK_SHRINK(byte.arg);
|
STACK_SHRINK(byte.arg);
|
||||||
|
@ -1212,7 +1212,7 @@ __EAT_DOTS_END:
|
|||||||
if(curr().type == TK(")")) break;
|
if(curr().type == TK(")")) break;
|
||||||
}
|
}
|
||||||
consume(TK(")"));
|
consume(TK(")"));
|
||||||
return VAR(Tuple(std::move(cpnts)));
|
return VAR(cpnts.to_tuple());
|
||||||
}
|
}
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,11 @@ PyVar PyArrayGetItem(VM* vm, PyVar _0, PyVar _1){
|
|||||||
vm->parse_int_slice(s, self.size(), start, stop, step);
|
vm->parse_int_slice(s, self.size(), start, stop, step);
|
||||||
List new_list;
|
List new_list;
|
||||||
PK_SLICE_LOOP(i, start, stop, step) new_list.push_back(self[i]);
|
PK_SLICE_LOOP(i, start, stop, step) new_list.push_back(self[i]);
|
||||||
return VAR(T(std::move(new_list)));
|
|
||||||
|
if constexpr(std::is_same_v<T, List>)
|
||||||
|
return VAR(std::move(new_list));
|
||||||
|
else
|
||||||
|
return VAR(new_list.to_tuple());
|
||||||
}
|
}
|
||||||
vm->TypeError("indices must be integers or slices");
|
vm->TypeError("indices must be integers or slices");
|
||||||
}
|
}
|
||||||
@ -1024,7 +1028,7 @@ void __init_builtins(VM* _vm) {
|
|||||||
if(args.size() == 1+0) return VAR(Tuple(0));
|
if(args.size() == 1+0) return VAR(Tuple(0));
|
||||||
if(args.size() == 1+1){
|
if(args.size() == 1+1){
|
||||||
List list = vm->py_list(args[1]);
|
List list = vm->py_list(args[1]);
|
||||||
return VAR(Tuple(std::move(list)));
|
return VAR(list.to_tuple());
|
||||||
}
|
}
|
||||||
vm->TypeError("tuple() takes at most 1 argument");
|
vm->TypeError("tuple() takes at most 1 argument");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
|
@ -27,12 +27,6 @@ Tuple::Tuple(Tuple&& other) noexcept {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Tuple::Tuple(List&& other) noexcept {
|
|
||||||
_size = other.size();
|
|
||||||
_args = other._data;
|
|
||||||
other._data = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tuple::Tuple(PyVar _0, PyVar _1): Tuple(2){
|
Tuple::Tuple(PyVar _0, PyVar _1): Tuple(2){
|
||||||
_args[0] = _0;
|
_args[0] = _0;
|
||||||
_args[1] = _1;
|
_args[1] = _1;
|
||||||
|
@ -942,8 +942,8 @@ void VM::__init_builtin_types(){
|
|||||||
this->_main = new_module("__main__");
|
this->_main = new_module("__main__");
|
||||||
}
|
}
|
||||||
|
|
||||||
// `heap.gc_scope_lock();` needed before calling this function
|
|
||||||
void VM::__unpack_as_list(ArgsView args, List& list){
|
void VM::__unpack_as_list(ArgsView args, List& list){
|
||||||
|
auto _lock = heap.gc_scope_lock();
|
||||||
for(PyVar obj: args){
|
for(PyVar obj: args){
|
||||||
if(is_type(obj, tp_star_wrapper)){
|
if(is_type(obj, tp_star_wrapper)){
|
||||||
const StarWrapper& w = _CAST(StarWrapper&, obj);
|
const StarWrapper& w = _CAST(StarWrapper&, obj);
|
||||||
@ -962,8 +962,8 @@ void VM::__unpack_as_list(ArgsView args, List& list){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// `heap.gc_scope_lock();` needed before calling this function
|
|
||||||
void VM::__unpack_as_dict(ArgsView args, Dict& dict){
|
void VM::__unpack_as_dict(ArgsView args, Dict& dict){
|
||||||
|
auto _lock = heap.gc_scope_lock();
|
||||||
for(PyVar obj: args){
|
for(PyVar obj: args){
|
||||||
if(is_type(obj, tp_star_wrapper)){
|
if(is_type(obj, tp_star_wrapper)){
|
||||||
const StarWrapper& w = _CAST(StarWrapper&, obj);
|
const StarWrapper& w = _CAST(StarWrapper&, obj);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user