From 02c6f58f3c148a3e763c277ab3ac965cfff9058e Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 14 Dec 2023 13:47:23 +0900 Subject: [PATCH] Separate 'gc' into shared module --- src/memory.c | 39 --------------------------------------- src/modules/module_gc.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/vm.c | 1 - 3 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 src/modules/module_gc.c diff --git a/src/memory.c b/src/memory.c index f5ce35b..e43693b 100644 --- a/src/memory.c +++ b/src/memory.c @@ -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 diff --git a/src/modules/module_gc.c b/src/modules/module_gc.c new file mode 100644 index 0000000..7ee6a10 --- /dev/null +++ b/src/modules/module_gc.c @@ -0,0 +1,41 @@ +#include +#include + +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(); +} diff --git a/src/vm.c b/src/vm.c index a3731ae..8a228df 100644 --- a/src/vm.c +++ b/src/vm.c @@ -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