summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md1
-rw-r--r--compiler/vmops.nim48
-rw-r--r--lib/core/macros.nim4
-rw-r--r--tests/vm/tvmmisc.nim24
4 files changed, 55 insertions, 22 deletions
diff --git a/changelog.md b/changelog.md
index d3ac27d6d..689c6d253 100644
--- a/changelog.md
+++ b/changelog.md
@@ -54,3 +54,4 @@
 
 - The VM's instruction count limit was raised to 1 billion instructions in order
   to support more complex computations at compile-time.
+- Added ``macros.getProjectPath`` and ``ospaths.putEnv`` procs to VM.
diff --git a/compiler/vmops.nim b/compiler/vmops.nim
index 2a00f207a..f7debe2df 100644
--- a/compiler/vmops.nim
+++ b/compiler/vmops.nim
@@ -13,7 +13,9 @@ from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
   arctan, arctan2, cos, cosh, hypot, sinh, sin, tan, tanh, pow, trunc,
   floor, ceil, fmod
 
-from os import getEnv, existsEnv, dirExists, fileExists, walkDir
+from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir
+
+from options import gProjectPath
 
 template mathop(op) {.dirty.} =
   registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`)
@@ -27,6 +29,9 @@ template ospathsop(op) {.dirty.} =
 template systemop(op) {.dirty.} =
   registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`)
 
+template macrosop(op) {.dirty.} =
+  registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`)
+
 template wrap1f_math(op) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
     setResult(a, op(getFloat(a, 0)))
@@ -37,30 +42,30 @@ template wrap2f_math(op) {.dirty.} =
     setResult(a, op(getFloat(a, 0), getFloat(a, 1)))
   mathop op
 
-template wrap1s_os(op) {.dirty.} =
+template wrap0(op, modop) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
-    setResult(a, op(getString(a, 0)))
-  osop op
+    setResult(a, op())
+  modop op
 
-template wrap1s_ospaths(op) {.dirty.} =
+template wrap1s(op, modop) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
     setResult(a, op(getString(a, 0)))
-  ospathsop op
+  modop op
 
-template wrap2s_ospaths(op) {.dirty.} =
+template wrap2s(op, modop) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
     setResult(a, op(getString(a, 0), getString(a, 1)))
-  ospathsop op
+  modop op
 
-template wrap1s_system(op) {.dirty.} =
+template wrap1svoid(op, modop) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
-    setResult(a, op(getString(a, 0)))
-  systemop op
+    op(getString(a, 0))
+  modop op
 
-template wrap2svoid_system(op) {.dirty.} =
+template wrap2svoid(op, modop) {.dirty.} =
   proc `op Wrapper`(a: VmArgs) {.nimcall.} =
     op(getString(a, 0), getString(a, 1))
-  systemop op
+  modop op
 
 proc getCurrentExceptionMsgWrapper(a: VmArgs) {.nimcall.} =
   setResult(a, if a.currentException.isNil: ""
@@ -77,6 +82,9 @@ proc gorgeExWrapper(a: VmArgs) {.nimcall.} =
                        a.currentLineInfo)
   setResult a, newTree(nkPar, newStrNode(nkStrLit, s), newIntNode(nkIntLit, e))
 
+proc getProjectPathWrapper(a: VmArgs) {.nimcall.} =
+  setResult a, gProjectPath
+
 proc registerAdditionalOps*(c: PCtx) =
   wrap1f_math(sqrt)
   wrap1f_math(ln)
@@ -101,13 +109,15 @@ proc registerAdditionalOps*(c: PCtx) =
   wrap1f_math(ceil)
   wrap2f_math(fmod)
 
-  wrap2s_ospaths(getEnv)
-  wrap1s_ospaths(existsEnv)
-  wrap1s_os(dirExists)
-  wrap1s_os(fileExists)
-  wrap2svoid_system(writeFile)
-  wrap1s_system(readFile)
+  wrap2s(getEnv, ospathsop)
+  wrap1s(existsEnv, ospathsop)
+  wrap2svoid(putEnv, ospathsop)
+  wrap1s(dirExists, osop)
+  wrap1s(fileExists, osop)
+  wrap2svoid(writeFile, systemop)
+  wrap1s(readFile, systemop)
   systemop getCurrentExceptionMsg
   registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
     setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
   systemop gorgeEx
+  macrosop getProjectPath
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 9edaab237..3614cf71a 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -1293,3 +1293,7 @@ macro unpackVarargs*(callee: untyped; args: varargs[untyped]): untyped =
   result = newCall(callee)
   for i in 0 ..< args.len:
     result.add args[i]
+
+proc getProjectPath*(): string = discard
+  ## Returns the path to the currently compiling project
+
diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim
index 42a58fa8f..472660bc2 100644
--- a/tests/vm/tvmmisc.nim
+++ b/tests/vm/tvmmisc.nim
@@ -1,5 +1,8 @@
 # bug #4462
 import macros
+import os
+import ospaths
+import strutils
 
 block:
   proc foo(t: typedesc) {.compileTime.} =
@@ -18,7 +21,7 @@ block:
 # #6379
 static:
   import algorithm
-  
+
   var numArray = [1, 2, 3, 4, -1]
   numArray.sort(cmp)
   assert numArray == [-1, 1, 2, 3, 4]
@@ -57,10 +60,25 @@ block:
     result = @[0]
     result.setLen(2)
     var tmp: int
-      
-    for i in 0 .. <2:
+
+    for i in 0 ..< 2:
       inc tmp
       result[i] = tmp
 
   const fact1000 = abc()
   assert fact1000 == @[1, 2]
+
+# Tests for VM ops
+block:
+  static:
+    assert "vm" in getProjectPath()
+
+    let b = getEnv("UNSETENVVAR")
+    assert b == ""
+    assert existsEnv("UNSERENVVAR") == false
+    putEnv("UNSETENVVAR", "VALUE")
+    assert getEnv("UNSETENVVAR") == "VALUE"
+    assert existsEnv("UNSETENVVAR") == true
+
+    assert fileExists("MISSINGFILE") == false
+    assert dirExists("MISSINGDIR") == false
d1bacd3538e94c05cf3d69'>8a5373291 ^
90119066a ^


0f4567d59 ^
90119066a ^
9c47bb9cc ^
90119066a ^



8ce705f68 ^
90119066a ^

33c2d7bfc ^



1b54be777 ^
1a0725f02 ^

90119066a ^

6e6b90278 ^
ef6029d3a ^
33c2d7bfc ^
bbe49a14a ^
d2b95cb47 ^
c9b86ebce ^
b7dd8e7df ^

47886978c ^

1cf5c280e ^

cb4d81065 ^


c9b86ebce ^

bbe49a14a ^
b1b58b88c ^
442841037 ^
c9b86ebce ^



bbe49a14a ^
e839c01f5 ^

c9b86ebce ^
4f6b386f3 ^


90119066a ^
55c407464 ^
bbe49a14a ^
ece5887ab ^
d2b95cb47 ^



54fecdb8d ^


ef6029d3a ^
b3b2203ab ^
ef6029d3a ^



e33544ad1 ^
ef6029d3a ^
04a4ff567 ^
8507e506c ^

3b00d9cc7 ^
90119066a ^


dc8d8ebb7 ^
dc5a40f3f ^
dc8d8ebb7 ^
ecdc478da ^



dc8d8ebb7 ^


f8c8e319b ^
dc8d8ebb7 ^
86d64b2f5 ^
00b749c41 ^
93cd98dd1 ^
86d64b2f5 ^
3b00d9cc7 ^
41f32f5fb ^


90119066a ^
5be5bf022 ^
93cd98dd1 ^
5be5bf022 ^

e33544ad1 ^
5be5bf022 ^
93cd98dd1 ^
3919f0aa5 ^

2eb14bdd4 ^
5be5bf022 ^
3919f0aa5 ^
9605a0f87 ^
5be5bf022 ^

a424075b5 ^
d8430f5a7 ^
5be5bf022 ^
9ac2ee7b8 ^






d862d2272 ^
2909e4137 ^



5be5bf022 ^

2eb14bdd4 ^
5be5bf022 ^


82d5e773e ^
5b5bd3811 ^
7d1a96151 ^
5be5bf022 ^

e6ff6dd9c ^
2e477979a ^
5be5bf022 ^

2909e4137 ^
5be5bf022 ^
41f32f5fb ^

06b986dbd ^
8a5373291 ^
13b72ed0c ^
1fa23e347 ^
2990a5ea5 ^




1fa23e347 ^
2e477979a ^
1fa23e347 ^
41f32f5fb ^

ba2aa474a ^


13b72ed0c ^
41f32f5fb ^
dfc17e5f8 ^
765366c1f ^
5be5bf022 ^




2909e4137 ^
28a481401 ^
cb9496693 ^





089e741ce ^






41f32f5fb ^
2e477979a ^
5be5bf022 ^


5f8ab1653 ^
8ed3e295a ^
a4f8a89c8 ^
3b00d9cc7 ^
3905cfeac ^

13b72ed0c ^
813a4f1d8 ^
90119066a ^
6655537c6 ^
41f32f5fb ^

6655537c6 ^
08d82e4b1 ^



b1b58b88c ^




90119066a ^
b1b58b88c ^


3ee048eaa ^
b1b58b88c ^

dfecc3489 ^
76c3b314d ^
90119066a ^

3b00d9cc7 ^
90119066a ^
e64f1c7ee ^
0f4567d59 ^

0f4567d59 ^
e64f1c7ee ^

0e3ea1655 ^
2169fd63b ^
dc5a40f3f ^
0f4567d59 ^
cd292568d ^

dc5a40f3f ^
0f4567d59 ^


90119066a ^

3b00d9cc7 ^
0f4567d59 ^
3b00d9cc7 ^
330b3c445 ^

90119066a ^
8630ebbbf ^
dce0b3b00 ^
8630ebbbf ^




0f4567d59 ^

f3756d2cc ^
330b3c445 ^
721534119 ^
38bdf1cd7 ^
3b00d9cc7 ^
dfecc3489 ^
42bac5242 ^
b3176b881 ^




2039dad27 ^
5b789f2da ^
42bac5242 ^
1d6622290 ^
42bac5242 ^
1d6622290 ^
55c1953f6 ^
6870345cd ^
e0afacb9f ^
42bac5242 ^


dce0b3b00 ^
b8ec07f19 ^
1d6622290 ^
3d88d06b3 ^


f0cd50f9c ^



3d88d06b3 ^
5b789f2da ^





142e849b9 ^
90119066a ^





76235348f ^
ae6dac6b6 ^
90119066a ^

0f4567d59 ^
90119066a ^

3b00d9cc7 ^
90119066a ^


d6074a7c7 ^
90119066a ^





3b00d9cc7 ^
90119066a ^

346443d1b ^
73c355176 ^
346443d1b ^
90119066a ^
3b00d9cc7 ^

90119066a ^


3b00d9cc7 ^
90119066a ^


6ff8752be ^
3b00d9cc7 ^
4d5c3ebd4 ^
9c3751a37 ^
6ff8752be ^
8b93e4132 ^

2d3385c22 ^




ecdc478da ^
5492190bc ^
2d3385c22 ^


ecdc478da ^
2d3385c22 ^



07553034d ^


e7471ceba ^
ecdc478da ^
2d3385c22 ^

a5b19ba86 ^


2d3385c22 ^

07553034d ^

2d3385c22 ^
07553034d ^

ecdc478da ^

2d3385c22 ^
07553034d ^
442841037 ^
723d83855 ^
2d3385c22 ^
723d83855 ^
2d3385c22 ^
8b93e4132 ^
740527813 ^

d6074a7c7 ^
37229df7f ^
6ff8752be ^
82d5e773e ^
9a76398ec ^



411e602d1 ^

90119066a ^
b4e25a637 ^
885f250f1 ^













eb6225ad8 ^
e3e974214 ^

511b6ae27 ^
7a39eb13a ^
6d125da93 ^

94c8d53a0 ^
da83b992e ^
dfecc3489 ^
6d125da93 ^
b4e25a637 ^
04a4ff567 ^
885f250f1 ^
b4e25a637 ^
82effc581 ^




c6471b8f7 ^
82effc581 ^




e3e974214 ^








4780b08b9 ^
e3e974214 ^





9ffec7930 ^






















11db28bc6 ^



93cd98dd1 ^










c60916a2a ^

a6682de00 ^
11db28bc6 ^
adbabf145 ^
d0485e326 ^


d2b95cb47 ^
42c6eec4e ^



d2b95cb47 ^
54fecdb8d ^
c60916a2a ^
c6f9c3889 ^
5a9b3c27c ^
c6f9c3889 ^
dee9a9427 ^
808863325 ^
dee9a9427 ^
5d95137ce ^





dee9a9427 ^


a424075b5 ^
dee9a9427 ^

c6f9c3889 ^

a424075b5 ^
90808877c ^
a424075b5 ^
e70294dff ^

a424075b5 ^
e70294dff ^
330b3c445 ^

e909486e5 ^
dee9a9427 ^
a424075b5 ^


dee9a9427 ^
afc0259b1 ^




a424075b5 ^
dee9a9427 ^
664dbb6bf ^

39fbf3c84 ^




7ee6774fb ^
442841037 ^
2e477979a ^
7ee6774fb ^

















e7471ceba ^
7ee6774fb ^
34f0b9110 ^
a424075b5 ^
7ee6774fb ^




15a8996d5 ^
7ba565258 ^

















15a8996d5 ^
7ba565258 ^
15a8996d5 ^
8be5344b3 ^
3b00d9cc7 ^
8be5344b3 ^
90119066a ^
ab57e7f24 ^






a5b19ba86 ^

1a0725f02 ^



2e477979a ^




8be5344b3 ^
2e477979a ^
923086253 ^
1b54be777 ^
1a0725f02 ^



8be5344b3 ^
2e477979a ^



1a0725f02 ^
2e477979a ^











93cd98dd1 ^
c60916a2a ^
2e477979a ^


5be5bf022 ^
3919f0aa5 ^
5be5bf022 ^
33c2d7bfc ^

5be5bf022 ^
b2b9af6a5 ^
5be5bf022 ^
3919f0aa5 ^
dce0b3b00 ^

2e477979a ^
5be5bf022 ^
9ffec7930 ^
b7dd8e7df ^


e3e974214 ^
ab57e7f24 ^
8be5344b3 ^
2e477979a ^
2ec52faae ^

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736