Separate 'gc' into shared module

This commit is contained in:
K. Lange 2023-12-14 13:47:23 +09:00
parent 874882d926
commit 02c6f58f3c
3 changed files with 41 additions and 40 deletions

View File

@ -562,42 +562,3 @@ size_t krk_collectGarbage(void) {
return out;
}
#ifndef KRK_NO_SYSTEM_MODULES
KRK_Function(collect) {
FUNCTION_TAKES_NONE();
if (&krk_currentThread != vm.threads) return krk_runtimeError(vm.exceptions->valueError, "only the main thread can do that");
return INTEGER_VAL(krk_collectGarbage());
}
KRK_Function(pause) {
FUNCTION_TAKES_NONE();
vm.globalFlags |= (KRK_GLOBAL_GC_PAUSED);
return NONE_VAL();
}
KRK_Function(resume) {
FUNCTION_TAKES_NONE();
vm.globalFlags &= ~(KRK_GLOBAL_GC_PAUSED);
return NONE_VAL();
}
void krk_module_init_gc(void) {
/**
* gc = module()
*
* Namespace for methods for controlling the garbage collector.
*/
KrkInstance * gcModule = krk_newInstance(vm.baseClasses->moduleClass);
krk_attachNamedObject(&vm.modules, "gc", (KrkObj*)gcModule);
krk_attachNamedObject(&gcModule->fields, "__name__", (KrkObj*)S("gc"));
krk_attachNamedValue(&gcModule->fields, "__file__", NONE_VAL());
KRK_DOC(gcModule, "@brief Namespace containing methods for controlling the garbage collector.");
KRK_DOC(BIND_FUNC(gcModule,collect),
"@brief Triggers one cycle of garbage collection.");
KRK_DOC(BIND_FUNC(gcModule,pause),
"@brief Disables automatic garbage collection until @ref resume is called.");
KRK_DOC(BIND_FUNC(gcModule,resume),
"@brief Re-enable automatic garbage collection after it was stopped by @ref pause ");
}
#endif

41
src/modules/module_gc.c Normal file
View File

@ -0,0 +1,41 @@
#include <kuroko/vm.h>
#include <kuroko/util.h>
KRK_Function(collect) {
FUNCTION_TAKES_NONE();
if (&krk_currentThread != vm.threads) return krk_runtimeError(vm.exceptions->valueError, "only the main thread can do that");
return INTEGER_VAL(krk_collectGarbage());
}
KRK_Function(pause) {
FUNCTION_TAKES_NONE();
vm.globalFlags |= (KRK_GLOBAL_GC_PAUSED);
return NONE_VAL();
}
KRK_Function(resume) {
FUNCTION_TAKES_NONE();
vm.globalFlags &= ~(KRK_GLOBAL_GC_PAUSED);
return NONE_VAL();
}
KrkValue krk_module_onload_gc(KrkString * runAs) {
/**
* gc = module()
*
* Namespace for methods for controlling the garbage collector.
*/
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_DOC(module, "@brief Namespace containing methods for controlling the garbage collector.");
KRK_DOC(BIND_FUNC(module,collect),
"@brief Triggers one cycle of garbage collection.");
KRK_DOC(BIND_FUNC(module,pause),
"@brief Disables automatic garbage collection until @ref resume is called.");
KRK_DOC(BIND_FUNC(module,resume),
"@brief Re-enable automatic garbage collection after it was stopped by @ref pause ");
return krk_pop();
}

View File

@ -968,7 +968,6 @@ void krk_initVM(int flags) {
if (!(vm.globalFlags & KRK_GLOBAL_NO_DEFAULT_MODULES)) {
#ifndef KRK_NO_SYSTEM_MODULES
krk_module_init_kuroko();
krk_module_init_gc();
krk_module_init_fileio();
#endif
#ifndef KRK_DISABLE_THREADS