diff --git a/include/pocketpy/objects/codeobject.h b/include/pocketpy/objects/codeobject.h index 220cb8cc..b66b1eb7 100644 --- a/include/pocketpy/objects/codeobject.h +++ b/include/pocketpy/objects/codeobject.h @@ -120,15 +120,16 @@ void FuncDecl__add_arg(FuncDecl* self, py_Name name); void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value); void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name); void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name); +void FuncDecl__gc_mark(const FuncDecl* self); // runtime function typedef struct Function { FuncDecl_ decl; py_TValue module; // weak ref PyObject* clazz; // weak ref - NameDict* closure; // strong ref + NameDict* closure; // strong ref py_CFunction cfunc; // wrapped C function } Function; void Function__ctor(Function* self, FuncDecl_ decl, py_TValue* module); -void Function__dtor(Function* self); \ No newline at end of file +void Function__dtor(Function* self); diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 6b6108f6..fed282c8 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -602,6 +602,14 @@ static void mark_object(PyObject* obj) { if(ti->gc_mark) ti->gc_mark(PyObject__userdata(obj)); } +void FuncDecl__gc_mark(const FuncDecl* self) { + CodeObject__gc_mark(&self->code); + for(int j = 0; j < self->kwargs.length; j++) { + FuncDeclKwArg* kw = c11__at(FuncDeclKwArg, &self->kwargs, j); + pk__mark_value(&kw->value); + } +} + void CodeObject__gc_mark(const CodeObject* self) { for(int i = 0; i < self->consts.length; i++) { py_TValue* p = c11__at(py_TValue, &self->consts, i); @@ -609,12 +617,7 @@ void CodeObject__gc_mark(const CodeObject* self) { } for(int i = 0; i < self->func_decls.length; i++) { FuncDecl_ decl = c11__getitem(FuncDecl_, &self->func_decls, i); - CodeObject__gc_mark(&decl->code); - - for(int j = 0; j < decl->kwargs.length; j++) { - FuncDeclKwArg* kw = c11__at(FuncDeclKwArg, &decl->kwargs, j); - pk__mark_value(&kw->value); - } + FuncDecl__gc_mark(decl); } } diff --git a/src/public/modules.c b/src/public/modules.c index e2fda4a7..d5d82ec1 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -685,7 +685,7 @@ py_TValue pk_builtins__register() { static void function__gc_mark(void* ud) { Function* func = ud; if(func->closure) pk__mark_namedict(func->closure); - CodeObject__gc_mark(&func->decl->code); + FuncDecl__gc_mark(func->decl); } static bool function__doc__(int argc, py_Ref argv) {