summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-06-16 02:43:48 -0700
committerGitHub <noreply@github.com>2020-06-16 11:43:48 +0200
commitdfe51d10a1f204d0c3ab1a8be1e109029cc54f9b (patch)
tree17eb11f3545d9f690329e02dadd7e015460838e6 /lib
parent45cac4afda2272182ea1eb7572493d6c71e2da4e (diff)
downloadNim-dfe51d10a1f204d0c3ab1a8be1e109029cc54f9b.tar.gz
`addQuitProc` now works with closures, and c, js(node/browser) backend; fix some bugs in testament (#14342)
* make addQuitProc great again

* fix bugs in testament

* fix test

* change 2016 => 2020

* addQuitProc => addExitProc + locks

* move to std/exitprocs
Diffstat (limited to 'lib')
-rw-r--r--lib/core/locks.nim13
-rw-r--r--lib/pure/quitprocs.nim28
-rw-r--r--lib/std/exitprocs.nim65
-rw-r--r--lib/system.nim9
4 files changed, 79 insertions, 36 deletions
diff --git a/lib/core/locks.nim b/lib/core/locks.nim
index 7e2f4ad34..bddd6d864 100644
--- a/lib/core/locks.nim
+++ b/lib/core/locks.nim
@@ -9,6 +9,10 @@
 
 ## This module contains Nim's support for locks and condition vars.
 
+#[
+for js, for now we treat locks as noop's to avoid pushing `when defined(js)`
+in client code that uses locks.
+]#
 
 when not compileOption("threads") and not defined(nimdoc):
   when false: # fix #12330
@@ -26,7 +30,8 @@ type
 
 proc initLock*(lock: var Lock) {.inline.} =
   ## Initializes the given lock.
-  initSysLock(lock)
+  when not defined(js):
+    initSysLock(lock)
 
 proc deinitLock*(lock: var Lock) {.inline.} =
   ## Frees the resources associated with the lock.
@@ -38,11 +43,13 @@ proc tryAcquire*(lock: var Lock): bool =
 
 proc acquire*(lock: var Lock) =
   ## Acquires the given lock.
-  acquireSys(lock)
+  when not defined(js):
+    acquireSys(lock)
 
 proc release*(lock: var Lock) =
   ## Releases the given lock.
-  releaseSys(lock)
+  when not defined(js):
+    releaseSys(lock)
 
 
 proc initCond*(cond: var Cond) {.inline.} =
diff --git a/lib/pure/quitprocs.nim b/lib/pure/quitprocs.nim
deleted file mode 100644
index 76bbcdb2b..000000000
--- a/lib/pure/quitprocs.nim
+++ /dev/null
@@ -1,28 +0,0 @@
-#
-#
-#            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)
diff --git a/lib/std/exitprocs.nim b/lib/std/exitprocs.nim
new file mode 100644
index 000000000..b2811735c
--- /dev/null
+++ b/lib/std/exitprocs.nim
@@ -0,0 +1,65 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2020 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+import locks
+
+type
+  FunKind = enum kClosure, kNoconv # extend as needed
+  Fun = object
+    case kind: FunKind
+    of kClosure: fun1: proc () {.closure.}
+    of kNoconv: fun2: proc () {.noconv.}
+
+var
+  gFunsLock: Lock
+  gFuns: seq[Fun]
+
+initLock(gFunsLock)
+
+when defined(js):
+  proc addAtExit(quitProc: proc() {.noconv.}) =
+    when defined(nodejs):
+      asm """
+        process.on('exit', `quitProc`);
+      """
+    elif defined(js):
+      asm """
+        window.onbeforeunload = `quitProc`;
+      """
+else:
+  proc addAtExit(quitProc: proc() {.noconv.}) {.
+    importc: "atexit", header: "<stdlib.h>".}
+
+proc callClosures() {.noconv.} =
+  withLock gFunsLock:
+    for i in countdown(gFuns.len-1, 0):
+      let fun = gFuns[i]
+      case fun.kind
+      of kClosure: fun.fun1()
+      of kNoconv: fun.fun2()
+
+template fun() =
+  if gFuns.len == 0:
+    addAtExit(callClosures)
+
+proc addExitProc*(cl: proc () {.closure.}) =
+  ## Adds/registers a quit procedure. Each call to `addExitProc` registers
+  ## another quit procedure. They are executed on a last-in, first-out basis.
+  # Support for `addExitProc` is done by Ansi C's facilities here.
+  # In case of an unhandled exception the exit handlers should
+  # not be called explicitly! The user may decide to do this manually though.
+  withLock gFunsLock:
+    fun()
+    gFuns.add Fun(kind: kClosure, fun1: cl)
+
+proc addExitProc*(cl: proc() {.noconv.}) =
+  ## overload for `noconv` procs.
+  withLock gFunsLock:
+    fun()
+    gFuns.add Fun(kind: kNoconv, fun2: cl)
diff --git a/lib/system.nim b/lib/system.nim
index 85be43cba..2235b8b70 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1147,8 +1147,8 @@ when defined(nimdoc):
   proc quit*(errorcode: int = QuitSuccess) {.magic: "Exit", noreturn.}
     ## Stops the program immediately with an exit code.
     ##
-    ## Before stopping the program the "quit procedures" are called in the
-    ## opposite order they were added with `addQuitProc <#addQuitProc,proc>`_.
+    ## Before stopping the program the "exit procedures" are called in the
+    ## opposite order they were added with `addExitProc <exitprocs.html#addExitProc,proc>`_.
     ## ``quit`` never returns and ignores any exception that may have been raised
     ## by the quit procedures.  It does *not* call the garbage collector to free
     ## all the memory, unless a quit procedure calls `GC_fullCollect
@@ -1426,8 +1426,8 @@ proc toBiggestInt*(f: BiggestFloat): BiggestInt {.noSideEffect.} =
   ## Same as `toInt <#toInt,float>`_ but for ``BiggestFloat`` to ``BiggestInt``.
   if f >= 0: BiggestInt(f+0.5) else: BiggestInt(f-0.5)
 
-proc addQuitProc*(quitProc: proc() {.noconv.}) {.
-  importc: "atexit", header: "<stdlib.h>".}
+proc addQuitProc*(quitProc: proc() {.noconv.}) {. 
+  importc: "atexit", header: "<stdlib.h>", deprecated: "use exitprocs.addExitProc".} 
   ## Adds/registers a quit procedure.
   ##
   ## Each call to ``addQuitProc`` registers another quit procedure. Up to 30
@@ -1440,7 +1440,6 @@ proc addQuitProc*(quitProc: proc() {.noconv.}) {.
 # In case of an unhandled exception the exit handlers should
 # not be called explicitly! The user may decide to do this manually though.
 
-
 proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.}
   ## Swaps the values `a` and `b`.
   ##