Skip to content

Commit

Permalink
[nodejs backend] paramStr, paramCount (nim-lang#17082)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored and ardek66 committed Mar 26, 2021
1 parent 3ca8d77 commit 9837916
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
42 changes: 33 additions & 9 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2796,12 +2796,12 @@ when defined(nimdoc):
## Returns the `i`-th `command line argument`:idx: given to the application.
##
## `i` should be in the range `1..paramCount()`, the `IndexDefect`
## exception will be raised for invalid values. Instead of iterating over
## `paramCount() <#paramCount>`_ with this proc you can call the
## convenience `commandLineParams() <#commandLineParams>`_.
## exception will be raised for invalid values. Instead of iterating
## over `paramCount() <#paramCount>`_ with this proc you can
## call the convenience `commandLineParams() <#commandLineParams>`_.
##
## Similarly to `argv`:idx: in C,
## it is possible to call ``paramStr(0)`` but this will return OS specific
## it is possible to call `paramStr(0)` but this will return OS specific
## contents (usually the name of the invoked executable). You should avoid
## this and call `getAppFilename() <#getAppFilename>`_ instead.
##
Expand All @@ -2825,7 +2825,22 @@ when defined(nimdoc):
## # Do something else!

elif defined(nimscript): discard
elif defined(nintendoswitch) or weirdTarget:
elif defined(nodejs):
type Argv = object of JSRoot
let argv {.importjs: "process.argv".} : Argv
proc len(argv: Argv): int {.importjs: "#.length".}
proc `[]`(argv: Argv, i: int): cstring {.importjs: "#[#]".}

proc paramCount*(): int {.tags: [ReadDirEffect].} =
result = argv.len - 2

proc paramStr*(i: int): string {.tags: [ReadIOEffect].} =
let i = i + 1
if i < argv.len and i >= 0:
result = $argv[i]
else:
raise newException(IndexDefect, formatErrorIndexBound(i - 1, argv.len - 2))
elif defined(nintendoswitch):
proc paramStr*(i: int): string {.tags: [ReadIOEffect].} =
raise newException(OSError, "paramStr is not implemented on Nintendo Switch")

Expand Down Expand Up @@ -2855,16 +2870,23 @@ elif defined(windows):
if not ownParsedArgv:
ownArgv = parseCmdLine($getCommandLine())
ownParsedArgv = true
if i < ownArgv.len and i >= 0: return ownArgv[i]
raise newException(IndexDefect, formatErrorIndexBound(i, ownArgv.len-1))
if i < ownArgv.len and i >= 0:
result = ownArgv[i]
else:
raise newException(IndexDefect, formatErrorIndexBound(i, ownArgv.len-1))

elif defined(genode):
proc paramStr*(i: int): string =
raise newException(OSError, "paramStr is not implemented on Genode")

proc paramCount*(): int =
raise newException(OSError, "paramCount is not implemented on Genode")
elif weirdTarget:
proc paramStr*(i: int): string {.tags: [ReadIOEffect].} =
raise newException(OSError, "paramStr is not implemented on current platform")

proc paramCount*(): int {.tags: [ReadIOEffect].} =
raise newException(OSError, "paramCount is not implemented on current platform")
elif not defined(createNimRtl) and
not(defined(posix) and appType == "lib"):
# On Posix, there is no portable way to get the command line from a DLL.
Expand All @@ -2874,8 +2896,10 @@ elif not defined(createNimRtl) and

proc paramStr*(i: int): string {.tags: [ReadIOEffect].} =
# Docstring in nimdoc block.
if i < cmdCount and i >= 0: return $cmdLine[i]
raise newException(IndexDefect, formatErrorIndexBound(i, cmdCount-1))
if i < cmdCount and i >= 0:
result = $cmdLine[i]
else:
raise newException(IndexDefect, formatErrorIndexBound(i, cmdCount-1))

proc paramCount*(): int {.tags: [ReadIOEffect].} =
# Docstring in nimdoc block.
Expand Down
11 changes: 11 additions & 0 deletions tests/stdlib/tcmdline.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
discard """
targets: "c js"
joinable: false
"""

import std/os

var params = paramCount()
doAssert params == 0
doAssert paramStr(0).len > 0
doAssert commandLineParams().len == 0

0 comments on commit 9837916

Please sign in to comment.