This commit is contained in:
blueloveTH 2024-08-09 12:54:29 +08:00
parent 3e99f46273
commit d8f2808462
5 changed files with 26 additions and 3 deletions

View File

@ -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();

View File

@ -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 *************/

View File

@ -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
src/modules/gc.c Normal file
View File

@ -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);
}

View File

@ -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]);