Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nodejs backend] paramStr, paramCount #17082

Merged
merged 28 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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