diff --git a/include/pocketpy/interpreter/modules.h b/include/pocketpy/interpreter/modules.h index a74f56ab..f94a2870 100644 --- a/include/pocketpy/interpreter/modules.h +++ b/include/pocketpy/interpreter/modules.h @@ -6,3 +6,4 @@ void pk__add_module_math(); void pk__add_module_dis(); void pk__add_module_random(); void pk__add_module_json(); +void pk__add_module_gc(); \ No newline at end of file diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 5e139a5f..db02f484 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -80,9 +80,9 @@ bool py_exec(const char* source, /// After the execution, the result will be set to `py_retval()`. /// The stack size will be reduced by 2. bool py_execdyn(const char* source, - const char* filename, - enum py_CompileMode mode, - py_Ref module) PY_RAISE; + const char* filename, + enum py_CompileMode mode, + py_Ref module) PY_RAISE; /************* Values Creation *************/ diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index a5658766..1d8049d7 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -198,6 +198,7 @@ void VM__ctor(VM* self) { pk__add_module_dis(); pk__add_module_random(); pk__add_module_json(); + pk__add_module_gc(); // add python builtins do { diff --git a/src/modules/gc.c b/src/modules/gc.c new file mode 100644 index 00000000..f00c7507 --- /dev/null +++ b/src/modules/gc.c @@ -0,0 +1,19 @@ +#include "pocketpy/pocketpy.h" + +#include "pocketpy/common/utils.h" +#include "pocketpy/objects/object.h" +#include "pocketpy/common/sstream.h" +#include "pocketpy/interpreter/vm.h" + +static bool gc_collect(int argc, py_Ref argv){ + ManagedHeap* heap = &pk_current_vm->heap; + int res = ManagedHeap__collect(heap); + py_newint(py_retval(), res); + return true; +} + +void pk__add_module_gc() { + py_Ref mod = py_newmodule("gc"); + + py_bindfunc(mod, "collect", gc_collect); +} \ No newline at end of file diff --git a/src/public/modules.c b/src/public/modules.c index 6eb1d9e0..f47e493b 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -387,6 +387,7 @@ static bool builtins_ord(int argc, py_Ref argv) { } static bool builtins_globals(int argc, py_Ref argv) { + PY_CHECK_ARGC(0); Frame* frame = pk_current_vm->top_frame; if(frame->is_dynamic) { py_assign(py_retval(), &frame->p0[0]); @@ -397,6 +398,7 @@ static bool builtins_globals(int argc, py_Ref argv) { } static bool builtins_locals(int argc, py_Ref argv) { + PY_CHECK_ARGC(0); Frame* frame = pk_current_vm->top_frame; if(frame->is_dynamic) { py_assign(py_retval(), &frame->p0[1]);