summary refs log tree commit diff stats
path: root/tests/pragmas
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-06-20 00:51:07 -0700
committerGitHub <noreply@github.com>2021-06-20 09:51:07 +0200
commit6030e139b5c630b5508dbe12d7b58c56264212b1 (patch)
treebf96b32677b329c25bea1f0fc8017c2f393a6d6f /tests/pragmas
parent590d457631290beacf97d945c5d3a625cc671645 (diff)
downloadNim-6030e139b5c630b5508dbe12d7b58c56264212b1.tar.gz
move {.injectStmt.} to experimental; add a test (#18300)
* move {.injectStmt.} to experimental; add a test

* undocument and deprecat `.injectStmt` but keep its implementation until we have a replacement
Diffstat (limited to 'tests/pragmas')
-rw-r--r--tests/pragmas/tinjectstmt.nim48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/pragmas/tinjectstmt.nim b/tests/pragmas/tinjectstmt.nim
new file mode 100644
index 000000000..bca041e46
--- /dev/null
+++ b/tests/pragmas/tinjectstmt.nim
@@ -0,0 +1,48 @@
+discard """
+  joinable: false
+  output:'''
+onInject: 1
+onInject: 2
+ok0
+ok1
+onInject: 3
+onInject: 4
+0
+onInject: 5
+onInject: 6
+1
+onInject: 7
+onInject: 8
+2
+ok2
+onInject: 9
+'''
+"""
+
+# test {.injectStmt.}
+
+#[
+{.injectStmt.} pragma can be used to inject a statement before every
+other statement in the current module. It's now undocumented and may be removed
+in the future and replaced with something more general and without its limitations.
+e.g. (e.g. doesn't work in VM or js backends).
+]#
+
+from system/ansi_c import c_printf
+
+var count = 0
+proc onInject*() =
+  count.inc
+  # echo count # xxx would fail, probably infinite recursion
+  c_printf("onInject: %d\n", cast[int](count))
+
+{.injectStmt: onInject().}
+echo "ok0"
+proc main()=
+  echo "ok1"
+  for a in 0..<3:
+    echo a
+  echo "ok2"
+
+static: main() # xxx injectStmt not honred in VM
+main()