diff --git a/doc/site/core/system.markdown b/doc/site/core/system.markdown index 1826d42f..cfaf8cc3 100644 --- a/doc/site/core/system.markdown +++ b/doc/site/core/system.markdown @@ -6,6 +6,16 @@ use during development or debugging. ## Static Methods +### System.**clock** + +Returns the number of seconds (including fractional seconds) since the program +was started. This is usually used for benchmarking. + +### System.**gc**() + +Requests that the VM perform an immediate garbage collection to free unused +memory. + ### System.**print**() Prints a single newline to the console. @@ -36,8 +46,3 @@ afterwards. Converts the value to a string by calling `toString` on it. In the above example, the result of `4 + 5` is printed, and then the prompt is printed on the same line because no newline character was printed afterwards. - -### System.**clock** - -Returns the number of seconds (including fractional seconds) since the program -was started. This is usually used for benchmarking. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 1be29953..eeae093e 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -1008,6 +1008,12 @@ DEF_PRIMITIVE(system_clock) RETURN_NUM((double)clock() / CLOCKS_PER_SEC); } +DEF_PRIMITIVE(system_gc) +{ + wrenCollectGarbage(vm); + RETURN_NULL; +} + DEF_PRIMITIVE(system_writeString) { if (vm->config.writeFn != NULL) @@ -1254,6 +1260,7 @@ void wrenInitializeCore(WrenVM* vm) ObjClass* systemClass = AS_CLASS(wrenFindVariable(vm, coreModule, "System")); PRIMITIVE(systemClass->obj.classObj, "clock", system_clock); + PRIMITIVE(systemClass->obj.classObj, "gc()", system_gc); PRIMITIVE(systemClass->obj.classObj, "writeString_(_)", system_writeString); // While bootstrapping the core types and running the core module, a number diff --git a/test/api/foreign_class.c b/test/api/foreign_class.c index 6e1e7b85..afe7f00e 100644 --- a/test/api/foreign_class.c +++ b/test/api/foreign_class.c @@ -5,11 +5,6 @@ static int finalized = 0; -static void apiGC(WrenVM* vm) -{ - wrenCollectGarbage(vm); -} - static void apiFinalized(WrenVM* vm) { wrenReturnDouble(vm, finalized); @@ -84,7 +79,6 @@ static void resourceFinalize(WrenVM* vm) WrenForeignMethodFn foreignClassBindMethod(const char* signature) { - if (strcmp(signature, "static Api.gc()") == 0) return apiGC; if (strcmp(signature, "static Api.finalized") == 0) return apiFinalized; if (strcmp(signature, "Counter.increment(_)") == 0) return counterIncrement; if (strcmp(signature, "Counter.value") == 0) return counterValue; diff --git a/test/api/foreign_class.wren b/test/api/foreign_class.wren index b3affda0..46ff130a 100644 --- a/test/api/foreign_class.wren +++ b/test/api/foreign_class.wren @@ -1,5 +1,4 @@ class Api { - foreign static gc() foreign static finalized } @@ -64,15 +63,15 @@ var resources = [ Resource.new() ] -Api.gc() +System.gc() System.print(Api.finalized) // expect: 0 resources.removeAt(-1) -Api.gc() +System.gc() System.print(Api.finalized) // expect: 1 resources.clear() -Api.gc() +System.gc() System.print(Api.finalized) // expect: 3