Skip to content

Commit

Permalink
Add staticFileExists and staticDirExists
Browse files Browse the repository at this point in the history
  • Loading branch information
demotomohiro committed Jul 13, 2023
1 parent 9471b5f commit a16b40d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ proc registerAdditionalOps*(c: PCtx) =
systemop getCurrentException
registerCallback c, "stdlib.osdirs.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
registerCallback c, "stdlib.staticos.staticDirExists", proc (a: VmArgs) {.nimcall.} =
setResult(a, dirExists(getString(a, 0)))
registerCallback c, "stdlib.staticos.staticFileExists", proc (a: VmArgs) {.nimcall.} =
setResult(a, fileExists(getString(a, 0)))
registerCallback c, "stdlib.compilesettings.querySetting", proc (a: VmArgs) =
setResult(a, querySettingImpl(c.config, getInt(a, 0)))
registerCallback c, "stdlib.compilesettings.querySettingSeq", proc (a: VmArgs) =
Expand Down
13 changes: 13 additions & 0 deletions lib/std/staticos.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## This module implements path handling like os module but works at only compile-time.
## This module works even when cross compiling to OS that is not supported by os module.

proc staticFileExists*(filename: string): bool {.compileTime.} =
## Returns true if `filename` exists and is a regular file or symlink.
##
## Directories, device files, named pipes and sockets return false.
discard

proc staticDirExists*(dir: string): bool {.compileTime.} =
## Returns true if the directory `dir` exists. If `dir` is a file, false
## is returned. Follows symlinks.
discard
8 changes: 8 additions & 0 deletions tests/stdlib/tstaticos.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import std/[assertions, staticos, os]

block:
static:
doAssert staticDirExists("MISSINGFILE") == false
doAssert staticFileExists("MISSINGDIR") == false
doAssert staticDirExists(currentSourcePath().parentDir)
doAssert staticFileExists(currentSourcePath())
2 changes: 2 additions & 0 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ block:

doAssert fileExists("MISSINGFILE") == false
doAssert dirExists("MISSINGDIR") == false
doAssert fileExists(currentSourcePath())
doAssert dirExists(currentSourcePath().parentDir)

# bug #7210
block:
Expand Down

0 comments on commit a16b40d

Please sign in to comment.