added system.gorgeEx that includes the exitCode; refs #4874; fixes #1994

This commit is contained in:
Araq
2017-01-07 02:38:26 +01:00
parent a17d996be9
commit 9d488261df
8 changed files with 46 additions and 20 deletions

View File

@@ -879,7 +879,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
# it's a callback:
c.callbacks[-prc.offset-2].value(
VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[pointer](regs),
currentException: c.currentExceptionB))
currentException: c.currentExceptionB,
currentLineInfo: c.debug[pc]))
elif sfImportc in prc.flags:
if allowFFI notin c.features:
globalError(c.debug[pc], errGenerated, "VM not allowed to do FFI")
@@ -1246,7 +1247,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
createStr regs[ra]
regs[ra].node.strVal = opGorge(regs[rb].node.strVal,
regs[rc].node.strVal, regs[rd].node.strVal,
c.debug[pc])
c.debug[pc])[0]
of opcNError:
decodeB(rkNode)
let a = regs[ra].node

View File

@@ -181,6 +181,7 @@ type
ra*, rb*, rc*: Natural
slots*: pointer
currentException*: PNode
currentLineInfo*: TLineInfo
VmCallback* = proc (args: VmArgs) {.closure.}
PCtx* = ref TCtx

View File

@@ -9,24 +9,24 @@
import ast, types, msgs, os, osproc, streams, options, idents, securehash
proc readOutput(p: Process): string =
result = ""
proc readOutput(p: Process): (string, int) =
result[0] = ""
var output = p.outputStream
while not output.atEnd:
result.add(output.readLine)
result.add("\n")
if result.len > 0:
result.setLen(result.len - "\n".len)
discard p.waitForExit
result[0].add(output.readLine)
result[0].add("\n")
if result[0].len > 0:
result[0].setLen(result[0].len - "\n".len)
result[1] = p.waitForExit
proc opGorge*(cmd, input, cache: string, info: TLineInfo): string =
proc opGorge*(cmd, input, cache: string, info: TLineInfo): (string, int) =
let workingDir = parentDir(info.toFullPath)
if cache.len > 0:# and optForceFullMake notin gGlobalOptions:
let h = secureHash(cmd & "\t" & input & "\t" & cache)
let filename = options.toGeneratedFile("gorge_" & $h, "txt")
var f: File
if open(f, filename):
result = f.readAll
result = (f.readAll, 0)
f.close
return
var readSuccessful = false
@@ -38,9 +38,9 @@ proc opGorge*(cmd, input, cache: string, info: TLineInfo): string =
p.inputStream.close()
result = p.readOutput
readSuccessful = true
writeFile(filename, result)
writeFile(filename, result[0])
except IOError, OSError:
if not readSuccessful: result = ""
if not readSuccessful: result = ("", -1)
else:
try:
var p = startProcess(cmd, workingDir,
@@ -50,7 +50,7 @@ proc opGorge*(cmd, input, cache: string, info: TLineInfo): string =
p.inputStream.close()
result = p.readOutput
except IOError, OSError:
result = ""
result = ("", -1)
proc opSlurp*(file: string, info: TLineInfo, module: PSym): string =
try:

View File

@@ -59,6 +59,11 @@ proc staticWalkDirImpl(path: string, relative: bool): PNode =
result.add newTree(nkPar, newIntNode(nkIntLit, k.ord),
newStrNode(nkStrLit, f))
proc gorgeExWrapper(a: VmArgs) {.nimcall.} =
let (s, e) = opGorge(getString(a, 0), getString(a, 1), getString(a, 2),
a.currentLineInfo)
setResult a, newTree(nkPar, newStrNode(nkStrLit, s), newIntNode(nkIntLit, e))
proc registerAdditionalOps*(c: PCtx) =
wrap1f_math(sqrt)
wrap1f_math(ln)
@@ -92,3 +97,4 @@ proc registerAdditionalOps*(c: PCtx) =
systemop getCurrentExceptionMsg
registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
systemop gorgeEx

View File

@@ -3368,6 +3368,11 @@ proc staticExec*(command: string, input = "", cache = ""): string {.
## .. code-block:: nim
## const stateMachine = staticExec("dfaoptimizer", "input", "0.8.0")
proc gorgeEx*(command: string, input = "", cache = ""): tuple[output: string,
exitCode: int] =
## Same as `gorge` but also returns the precious exit code.
discard
proc `+=`*[T: SomeOrdinal|uint|uint64](x: var T, y: T) {.
magic: "Inc", noSideEffect.}
## Increments an ordinal

View File

@@ -3,10 +3,18 @@ import os
template getScriptDir(): string =
parentDir(instantiationInfo(-1, true).filename)
const
execName = when defined(windows): "tgorge.bat" else: "sh tgorge.sh"
relOutput = gorge(execName)
absOutput = gorge(getScriptDir() / execName)
block gorge:
const
execName = when defined(windows): "tgorge.bat" else: "./tgorge.sh"
relOutput = gorge(execName)
absOutput = gorge(getScriptDir() / execName)
doAssert relOutput == "gorge test"
doAssert absOutput == "gorge test"
doAssert relOutput == "gorge test"
doAssert absOutput == "gorge test"
block gorgeEx:
const
execName = when defined(windows): "tgorgeex.bat" else: "./tgorgeex.sh"
res = gorgeEx(execName)
doAssert res.output == "gorgeex test"
doAssert res.exitCode == 1

2
tests/vm/tgorgeex.bat Normal file
View File

@@ -0,0 +1,2 @@
@echo gorgeex test
@exit /b 1

3
tests/vm/tgorgeex.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
echo "gorgeex test"
exit 1