From 16ef631bfd0b8e12c086435dc555f855a357a57f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 28 Mar 2023 22:11:58 +0800 Subject: [PATCH] update gc Update ref.h up --- .github/workflows/main.yml | 2 +- src/obj.h | 21 +++++++++------------ src/pocketpy.h | 6 ++++++ src/ref.h | 4 +--- src/vm.h | 11 +++++++++++ 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c01fbb29..033b90a5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -47,7 +47,7 @@ jobs: build_dir: web env: 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 with: path: output diff --git a/src/obj.h b/src/obj.h index 8abcea95..3a776cf6 100644 --- a/src/obj.h +++ b/src/obj.h @@ -87,6 +87,9 @@ public: virtual ~BaseIter() = default; }; +template struct is_container_gc : std::false_type {}; +template struct is_container_gc> : std::true_type {}; + struct GCHeader { bool enabled; // whether this object is managed by GC bool marked; // whether this object is marked @@ -135,7 +138,7 @@ struct Py_ : PyObject { void mark() override { PyObject::mark(); - // extra mark for `T` + if constexpr (is_container_gc::value) _value._mark(); } }; @@ -174,19 +177,13 @@ union BitsCvt { BitsCvt(f64 val) : _float(val) {} }; -template struct is_py_class : std::false_type {}; +template struct is_py_class : std::false_type {}; template struct is_py_class> : std::true_type {}; -template -void _check_py_class(VM* vm, PyObject* var); - -template -T py_pointer_cast(VM* vm, PyObject* var); - -template -T py_value_cast(VM* vm, PyObject* var); - -struct Discarded {}; +template void _check_py_class(VM*, PyObject*); +template T py_pointer_cast(VM*, PyObject*); +template T py_value_cast(VM*, PyObject*); +struct Discarded { }; template __T py_cast(VM* vm, PyObject* obj) { diff --git a/src/pocketpy.h b/src/pocketpy.h index a2e3d8fa..3f92734e 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -755,6 +755,11 @@ inline void add_module_random(VM* vm){ 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(){ init_builtins(this); add_module_sys(this); @@ -767,6 +772,7 @@ inline void VM::post_init(){ add_module_io(this); add_module_os(this); add_module_c(this); + add_module_gc(this); for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){ _lazy_modules[name] = kPythonLibs[name]; diff --git a/src/ref.h b/src/ref.h index 4bcb3b3e..7a6f0310 100644 --- a/src/ref.h +++ b/src/ref.h @@ -99,9 +99,7 @@ struct IndexRef : BaseRef { } void set(VM* vm, Frame* frame, PyObject* val) const{ - Args args(3); - args[0] = obj; args[1] = index; args[2] = std::move(val); - vm->fast_call(__setitem__, std::move(args)); + vm->fast_call(__setitem__, Args{obj, index, val}); } void del(VM* vm, Frame* frame) const{ diff --git a/src/vm.h b/src/vm.h index 686d9056..01bd7289 100644 --- a/src/vm.h +++ b/src/vm.h @@ -144,6 +144,11 @@ public: return nullptr; } + i64 gc_collect(){ + heap.collect(this); + return 0; + } + template PyObject* gcnew(Type type, T&& val){ PyObject* obj = new Py_>(type, std::forward(val)); @@ -931,4 +936,10 @@ inline PyObject* VM::_exec(){ } } +inline std::vector ManagedHeap::get_roots(VM *vm) { + std::vector roots; + // ... + return roots; +} + } // namespace pkpy \ No newline at end of file