blueloveTH 1 năm trước cách đây
mục cha
commit
d8f2808462

+ 1 - 0
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();

+ 3 - 3
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 *************/
 

+ 1 - 0
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 {

+ 19 - 0
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);
+}

+ 2 - 0
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]);