summary refs log tree commit diff stats
path: root/lib/pure/quitprocs.nim
blob: 76bbcdb2b1fea509dc4b4b92068bd541752b5e9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)