module_gc.c
1 #include <kuroko/vm.h>
2 #include <kuroko/util.h>
3 
4 KRK_Function(collect) {
5  FUNCTION_TAKES_NONE();
6  if (&krk_currentThread != vm.threads) return krk_runtimeError(vm.exceptions->valueError, "only the main thread can do that");
7  return INTEGER_VAL(krk_collectGarbage());
8 }
9 
10 KRK_Function(pause) {
11  FUNCTION_TAKES_NONE();
12  vm.globalFlags |= (KRK_GLOBAL_GC_PAUSED);
13  return NONE_VAL();
14 }
15 
16 KRK_Function(resume) {
17  FUNCTION_TAKES_NONE();
18  vm.globalFlags &= ~(KRK_GLOBAL_GC_PAUSED);
19  return NONE_VAL();
20 }
21 
22 KRK_Module(gc) {
23  KRK_DOC(module, "@brief Namespace containing methods for controlling the garbage collector.");
24 
25  KRK_DOC(BIND_FUNC(module,collect),
26  "@brief Triggers one cycle of garbage collection.");
27  KRK_DOC(BIND_FUNC(module,pause),
28  "@brief Disables automatic garbage collection until @ref resume is called.");
29  KRK_DOC(BIND_FUNC(module,resume),
30  "@brief Re-enable automatic garbage collection after it was stopped by @ref pause ");
31 }
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Definition: exceptions.c:460
size_t krk_collectGarbage(void)
Run a cycle of the garbage collector.
Definition: memory.c:514
KRK_Module(socket)
Utilities for creating native bindings.
#define KRK_DOC(thing, text)
Attach documentation to a thing of various types.
Definition: util.h:304
Core API for the bytecode virtual machine.
krk_threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
#define vm
Convenience macro for namespacing.
Definition: vm.h:257