From 2616c8378b59d9ac281b2bbeda1a56fc919e2d29 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 1 Jun 2024 14:04:20 +0800 Subject: [PATCH] some fix --- include/pocketpy/obj.h | 4 ++-- include/pocketpy/tuplelist.h | 19 ++++++++++++------- src/ceval.cpp | 6 +----- src/compiler.cpp | 2 +- src/pocketpy.cpp | 8 ++++++-- src/tuplelist.cpp | 6 ------ src/vm.cpp | 4 ++-- 7 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index 92aba4a1..e179c5a2 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -113,8 +113,8 @@ struct PyObject final{ static constexpr int FIXED_SIZE = 16; bool gc_marked; // whether this object is marked - Type type; // we have a duplicated type here for saving memory - NameDict* _attr; + Type type; // we have a duplicated type here for convenience + NameDict* _attr; // gc will delete this on destruction bool is_attr_valid() const noexcept { return _attr != nullptr; } void* _value_ptr() noexcept { return (char*)this + FIXED_SIZE; } diff --git a/include/pocketpy/tuplelist.h b/include/pocketpy/tuplelist.h index 57df62b9..34b50b2b 100644 --- a/include/pocketpy/tuplelist.h +++ b/include/pocketpy/tuplelist.h @@ -7,13 +7,8 @@ namespace pkpy { -struct List: pod_vector{ - using pod_vector::pod_vector; - void _gc_mark(VM*) const; -}; - struct Tuple { - static const int INLINED_SIZE = 4; + static const int INLINED_SIZE = 3; PyVar* _args; PyVar _inlined[INLINED_SIZE]; @@ -22,7 +17,6 @@ struct Tuple { Tuple(int n); Tuple(const Tuple& other); Tuple(Tuple&& other) noexcept; - Tuple(List&& other) noexcept; ~Tuple(); Tuple(PyVar, PyVar); @@ -41,6 +35,17 @@ struct Tuple { void _gc_mark(VM*) const; }; +struct List: pod_vector{ + using pod_vector::pod_vector; + void _gc_mark(VM*) const; + + Tuple to_tuple() const{ + Tuple ret(size()); + for(int i=0; iparse_int_slice(s, self.size(), start, stop, step); List new_list; 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) + return VAR(std::move(new_list)); + else + return VAR(new_list.to_tuple()); } 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+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"); return vm->None; diff --git a/src/tuplelist.cpp b/src/tuplelist.cpp index 0fdc01dc..8c21091a 100644 --- a/src/tuplelist.cpp +++ b/src/tuplelist.cpp @@ -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){ _args[0] = _0; _args[1] = _1; diff --git a/src/vm.cpp b/src/vm.cpp index 249a032d..df5b2935 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -942,8 +942,8 @@ void VM::__init_builtin_types(){ this->_main = new_module("__main__"); } -// `heap.gc_scope_lock();` needed before calling this function void VM::__unpack_as_list(ArgsView args, List& list){ + auto _lock = heap.gc_scope_lock(); for(PyVar obj: args){ if(is_type(obj, tp_star_wrapper)){ 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){ + auto _lock = heap.gc_scope_lock(); for(PyVar obj: args){ if(is_type(obj, tp_star_wrapper)){ const StarWrapper& w = _CAST(StarWrapper&, obj);