update gc

Update ref.h

up
This commit is contained in:
blueloveTH 2023-03-28 22:11:58 +08:00
parent 6ea82f01fd
commit 16ef631bfd
5 changed files with 28 additions and 16 deletions

View File

@ -47,7 +47,7 @@ jobs:
build_dir: web build_dir: web
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name == 'push' if: github.event_name == 'push' && github.ref == 'refs/heads/main'
- uses: actions/upload-artifact@v3 - uses: actions/upload-artifact@v3
with: with:
path: output path: output

View File

@ -87,6 +87,9 @@ public:
virtual ~BaseIter() = default; virtual ~BaseIter() = default;
}; };
template <typename, typename=void> struct is_container_gc : std::false_type {};
template <typename T> struct is_container_gc<T, std::void_t<decltype(T::_mark)>> : std::true_type {};
struct GCHeader { struct GCHeader {
bool enabled; // whether this object is managed by GC bool enabled; // whether this object is managed by GC
bool marked; // whether this object is marked bool marked; // whether this object is marked
@ -135,7 +138,7 @@ struct Py_ : PyObject {
void mark() override { void mark() override {
PyObject::mark(); PyObject::mark();
// extra mark for `T` if constexpr (is_container_gc<T>::value) _value._mark();
} }
}; };
@ -174,19 +177,13 @@ union BitsCvt {
BitsCvt(f64 val) : _float(val) {} BitsCvt(f64 val) : _float(val) {}
}; };
template <typename, typename = void> struct is_py_class : std::false_type {}; template <typename, typename=void> struct is_py_class : std::false_type {};
template <typename T> struct is_py_class<T, std::void_t<decltype(T::_type)>> : std::true_type {}; template <typename T> struct is_py_class<T, std::void_t<decltype(T::_type)>> : std::true_type {};
template<typename T> template<typename T> void _check_py_class(VM*, PyObject*);
void _check_py_class(VM* vm, PyObject* var); template<typename T> T py_pointer_cast(VM*, PyObject*);
template<typename T> T py_value_cast(VM*, PyObject*);
template<typename T> struct Discarded { };
T py_pointer_cast(VM* vm, PyObject* var);
template<typename T>
T py_value_cast(VM* vm, PyObject* var);
struct Discarded {};
template<typename __T> template<typename __T>
__T py_cast(VM* vm, PyObject* obj) { __T py_cast(VM* vm, PyObject* obj) {

View File

@ -755,6 +755,11 @@ inline void add_module_random(VM* vm){
vm->_exec(code, mod); vm->_exec(code, mod);
} }
inline void add_module_gc(VM* vm){
PyObject* mod = vm->new_module("gc");
vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->gc_collect())));
}
inline void VM::post_init(){ inline void VM::post_init(){
init_builtins(this); init_builtins(this);
add_module_sys(this); add_module_sys(this);
@ -767,6 +772,7 @@ inline void VM::post_init(){
add_module_io(this); add_module_io(this);
add_module_os(this); add_module_os(this);
add_module_c(this); add_module_c(this);
add_module_gc(this);
for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){ for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){
_lazy_modules[name] = kPythonLibs[name]; _lazy_modules[name] = kPythonLibs[name];

View File

@ -99,9 +99,7 @@ struct IndexRef : BaseRef {
} }
void set(VM* vm, Frame* frame, PyObject* val) const{ void set(VM* vm, Frame* frame, PyObject* val) const{
Args args(3); vm->fast_call(__setitem__, Args{obj, index, val});
args[0] = obj; args[1] = index; args[2] = std::move(val);
vm->fast_call(__setitem__, std::move(args));
} }
void del(VM* vm, Frame* frame) const{ void del(VM* vm, Frame* frame) const{

View File

@ -144,6 +144,11 @@ public:
return nullptr; return nullptr;
} }
i64 gc_collect(){
heap.collect(this);
return 0;
}
template<typename T> template<typename T>
PyObject* gcnew(Type type, T&& val){ PyObject* gcnew(Type type, T&& val){
PyObject* obj = new Py_<std::decay_t<T>>(type, std::forward<T>(val)); PyObject* obj = new Py_<std::decay_t<T>>(type, std::forward<T>(val));
@ -931,4 +936,10 @@ inline PyObject* VM::_exec(){
} }
} }
inline std::vector<PyObject*> ManagedHeap::get_roots(VM *vm) {
std::vector<PyObject*> roots;
// ...
return roots;
}
} // namespace pkpy } // namespace pkpy