summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2020-05-13 14:29:37 +0200
committerAraq <rumpf_a@web.de>2020-05-13 14:29:53 +0200
commita3719df8b327ce33d4fdbfc7be06b058e9a2ea48 (patch)
tree08157b18fb0f409e6e957a3876f622fbc2bfa6ab /lib/pure
parent041ee92bba0ba3f361218eb20ceeeee6eec85f91 (diff)
downloadNim-a3719df8b327ce33d4fdbfc7be06b058e9a2ea48.tar.gz
fixes #14331
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/quitprocs.nim28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/pure/quitprocs.nim b/lib/pure/quitprocs.nim
new file mode 100644
index 000000000..76bbcdb2b
--- /dev/null
+++ b/lib/pure/quitprocs.nim
@@ -0,0 +1,28 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2016 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## ``system.addQuitProc`` is nice and very useful but due to its C-based
+## implementation it doesn't support closures which limits its usefulness.
+## This module fixes this. Later versions of this module will also
+## support the JavaScript backend.
+
+var
+  gClosures: seq[proc () {.closure.}]
+
+proc callClosures() {.noconv.} =
+  for i in countdown(gClosures.len-1, 0):
+    gClosures[i]()
+
+proc addQuitClosure*(cl: proc () {.closure.}) =
+  ## Like ``system.addQuitProc`` but it supports closures.
+  if gClosures.len == 0:
+    addQuitProc(callClosures)
+    gClosures = @[cl]
+  else:
+    gClosures.add(cl)