Separate 'gc' into shared module
This commit is contained in:
parent
874882d926
commit
02c6f58f3c
39
src/memory.c
39
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
|
||||
|
41
src/modules/module_gc.c
Normal file
41
src/modules/module_gc.c
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user