[backport] run nimpretty on async

This commit is contained in:
narimiran 2019-09-27 10:50:16 +02:00
commit aa513d78e7
8 changed files with 61 additions and 57 deletions

View file

@ -267,7 +267,7 @@ when defined(windows) or defined(nimdoc):
pcd: PostCallbackDataPtr pcd: PostCallbackDataPtr
AsyncEvent* = ptr AsyncEventImpl AsyncEvent* = ptr AsyncEventImpl
Callback = proc (fd: AsyncFD): bool {.closure,gcsafe.} Callback = proc (fd: AsyncFD): bool {.closure, gcsafe.}
proc hash(x: AsyncFD): Hash {.borrow.} proc hash(x: AsyncFD): Hash {.borrow.}
proc `==`*(x: AsyncFD, y: AsyncFD): bool {.borrow.} proc `==`*(x: AsyncFD, y: AsyncFD): bool {.borrow.}
@ -795,7 +795,7 @@ when defined(windows) or defined(nimdoc):
proc contains*(disp: PDispatcher, fd: AsyncFD): bool = proc contains*(disp: PDispatcher, fd: AsyncFD): bool =
return fd in disp.handles return fd in disp.handles
{.push stackTrace:off.} {.push stackTrace: off.}
proc waitableCallback(param: pointer, proc waitableCallback(param: pointer,
timerOrWaitFired: WINBOOL): void {.stdcall.} = timerOrWaitFired: WINBOOL): void {.stdcall.} =
var p = cast[PostCallbackDataPtr](param) var p = cast[PostCallbackDataPtr](param)
@ -1093,7 +1093,7 @@ else:
# queue. # queue.
type type
AsyncFD* = distinct cint AsyncFD* = distinct cint
Callback = proc (fd: AsyncFD): bool {.closure,gcsafe.} Callback = proc (fd: AsyncFD): bool {.closure, gcsafe.}
AsyncData = object AsyncData = object
readList: seq[Callback] readList: seq[Callback]

View file

@ -164,8 +164,8 @@ proc pasv(ftp: AsyncFtpClient) {.async.} =
assertReply(pasvMsg, "227") assertReply(pasvMsg, "227")
var betweenParens = captureBetween(pasvMsg.string, '(', ')') var betweenParens = captureBetween(pasvMsg.string, '(', ')')
var nums = betweenParens.split(',') var nums = betweenParens.split(',')
var ip = nums[0.. ^3] var ip = nums[0 .. ^3]
var port = nums[^2.. ^1] var port = nums[^2 .. ^1]
var properPort = port[0].parseInt()*256+port[1].parseInt() var properPort = port[0].parseInt()*256+port[1].parseInt()
await ftp.dsock.connect(ip.join("."), Port(properPort.toU16)) await ftp.dsock.connect(ip.join("."), Port(properPort.toU16))
ftp.dsockConnected = true ftp.dsockConnected = true

View file

@ -260,7 +260,7 @@ proc clearCallbacks*(future: FutureBase) =
future.callbacks.function = nil future.callbacks.function = nil
future.callbacks.next = nil future.callbacks.next = nil
proc addCallback*(future: FutureBase, cb: proc() {.closure,gcsafe.}) = proc addCallback*(future: FutureBase, cb: proc() {.closure, gcsafe.}) =
## Adds the callbacks proc to be called when the future completes. ## Adds the callbacks proc to be called when the future completes.
## ##
## If future has already completed then ``cb`` will be called immediately. ## If future has already completed then ``cb`` will be called immediately.
@ -271,7 +271,7 @@ proc addCallback*(future: FutureBase, cb: proc() {.closure,gcsafe.}) =
future.callbacks.add cb future.callbacks.add cb
proc addCallback*[T](future: Future[T], proc addCallback*[T](future: Future[T],
cb: proc (future: Future[T]) {.closure,gcsafe.}) = cb: proc (future: Future[T]) {.closure, gcsafe.}) =
## Adds the callbacks proc to be called when the future completes. ## Adds the callbacks proc to be called when the future completes.
## ##
## If future has already completed then ``cb`` will be called immediately. ## If future has already completed then ``cb`` will be called immediately.
@ -280,7 +280,7 @@ proc addCallback*[T](future: Future[T],
cb(future) cb(future)
) )
proc `callback=`*(future: FutureBase, cb: proc () {.closure,gcsafe.}) = proc `callback=`*(future: FutureBase, cb: proc () {.closure, gcsafe.}) =
## Clears the list of callbacks and sets the callback proc to be called when the future completes. ## Clears the list of callbacks and sets the callback proc to be called when the future completes.
## ##
## If future has already completed then ``cb`` will be called immediately. ## If future has already completed then ``cb`` will be called immediately.
@ -290,7 +290,7 @@ proc `callback=`*(future: FutureBase, cb: proc () {.closure,gcsafe.}) =
future.addCallback cb future.addCallback cb
proc `callback=`*[T](future: Future[T], proc `callback=`*[T](future: Future[T],
cb: proc (future: Future[T]) {.closure,gcsafe.}) = cb: proc (future: Future[T]) {.closure, gcsafe.}) =
## Sets the callback proc to be called when the future completes. ## Sets the callback proc to be called when the future completes.
## ##
## If future has already completed then ``cb`` will be called immediately. ## If future has already completed then ``cb`` will be called immediately.

View file

@ -169,7 +169,7 @@ proc processRequest(
for i in 0..1: for i in 0..1:
lineFut.mget().setLen(0) lineFut.mget().setLen(0)
lineFut.clean() lineFut.clean()
await client.recvLineInto(lineFut, maxLength=maxLine) # TODO: Timeouts. await client.recvLineInto(lineFut, maxLength = maxLine) # TODO: Timeouts.
if lineFut.mget == "": if lineFut.mget == "":
client.close() client.close()
@ -214,7 +214,7 @@ proc processRequest(
i = 0 i = 0
lineFut.mget.setLen(0) lineFut.mget.setLen(0)
lineFut.clean() lineFut.clean()
await client.recvLineInto(lineFut, maxLength=maxLine) await client.recvLineInto(lineFut, maxLength = maxLine)
if lineFut.mget == "": if lineFut.mget == "":
client.close(); return false client.close(); return false
@ -242,7 +242,8 @@ proc processRequest(
# - Check for Content-length header # - Check for Content-length header
if request.headers.hasKey("Content-Length"): if request.headers.hasKey("Content-Length"):
var contentLength = 0 var contentLength = 0
if parseSaturatedNatural(request.headers["Content-Length"], contentLength) == 0: if parseSaturatedNatural(request.headers["Content-Length"],
contentLength) == 0:
await request.respond(Http400, "Bad Request. Invalid Content-Length.") await request.respond(Http400, "Bad Request. Invalid Content-Length.")
return true return true
else: else:

View file

@ -163,7 +163,8 @@ proc processBody(node, retFutureSym: NimNode,
if node[1][0].eqIdent("await"): if node[1][0].eqIdent("await"):
# x = await y # x = await y
var newAsgn = node var newAsgn = node
result.createVar("future" & $node[0].toStrLit, node[1][1], newAsgn[1], newAsgn, node) result.createVar("future" & $node[0].toStrLit, node[1][1], newAsgn[1],
newAsgn, node)
else: discard else: discard
of nnkDiscardStmt: of nnkDiscardStmt:
# discard await x # discard await x
@ -296,11 +297,12 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
var closureIterator = newProc(iteratorNameSym, [parseExpr("owned(FutureBase)")], var closureIterator = newProc(iteratorNameSym, [parseExpr("owned(FutureBase)")],
procBody, nnkIteratorDef) procBody, nnkIteratorDef)
closureIterator.pragma = newNimNode(nnkPragma, lineInfoFrom=prc.body) closureIterator.pragma = newNimNode(nnkPragma, lineInfoFrom = prc.body)
closureIterator.addPragma(newIdentNode("closure")) closureIterator.addPragma(newIdentNode("closure"))
# If proc has an explicit gcsafe pragma, we add it to iterator as well. # If proc has an explicit gcsafe pragma, we add it to iterator as well.
if prc.pragma.findChild(it.kind in {nnkSym, nnkIdent} and $it == "gcsafe") != nil: if prc.pragma.findChild(it.kind in {nnkSym, nnkIdent} and $it ==
"gcsafe") != nil:
closureIterator.addPragma(newIdentNode("gcsafe")) closureIterator.addPragma(newIdentNode("gcsafe"))
outerProcBody.add(closureIterator) outerProcBody.add(closureIterator)
@ -414,20 +416,20 @@ proc splitProc(prc: NimNode): (NimNode, NimNode) =
result[0] = prc.copyNimTree() result[0] = prc.copyNimTree()
# Retrieve the `T` inside `Future[T]`. # Retrieve the `T` inside `Future[T]`.
let returnType = stripReturnType(result[0][3][0]) let returnType = stripReturnType(result[0][3][0])
result[0][3][0] = splitParamType(returnType, async=false) result[0][3][0] = splitParamType(returnType, async = false)
for i in 1 ..< result[0][3].len: for i in 1 ..< result[0][3].len:
# Sync proc (0) -> FormalParams (3) -> IdentDefs, the parameter (i) -> # Sync proc (0) -> FormalParams (3) -> IdentDefs, the parameter (i) ->
# parameter type (1). # parameter type (1).
result[0][3][i][1] = splitParamType(result[0][3][i][1], async=false) result[0][3][i][1] = splitParamType(result[0][3][i][1], async = false)
result[0][6] = stripAwait(result[0][6]) result[0][6] = stripAwait(result[0][6])
result[1] = prc.copyNimTree() result[1] = prc.copyNimTree()
if result[1][3][0].kind == nnkBracketExpr: if result[1][3][0].kind == nnkBracketExpr:
result[1][3][0][1] = splitParamType(result[1][3][0][1], async=true) result[1][3][0][1] = splitParamType(result[1][3][0][1], async = true)
for i in 1 ..< result[1][3].len: for i in 1 ..< result[1][3].len:
# Async proc (1) -> FormalParams (3) -> IdentDefs, the parameter (i) -> # Async proc (1) -> FormalParams (3) -> IdentDefs, the parameter (i) ->
# parameter type (1). # parameter type (1).
result[1][3][i][1] = splitParamType(result[1][3][i][1], async=true) result[1][3][i][1] = splitParamType(result[1][3][i][1], async = true)
macro multisync*(prc: untyped): untyped = macro multisync*(prc: untyped): untyped =
## Macro which processes async procedures into both asynchronous and ## Macro which processes async procedures into both asynchronous and

View file

@ -603,7 +603,8 @@ proc recvLine*(socket: AsyncSocket,
await socket.recvLineInto(resString, flags, maxLength) await socket.recvLineInto(resString, flags, maxLength)
result = resString.mget() result = resString.mget()
proc listen*(socket: AsyncSocket, backlog = SOMAXCONN) {.tags: [ReadIOEffect].} = proc listen*(socket: AsyncSocket, backlog = SOMAXCONN) {.tags: [
ReadIOEffect].} =
## Marks ``socket`` as accepting connections. ## Marks ``socket`` as accepting connections.
## ``Backlog`` specifies the maximum length of the ## ``Backlog`` specifies the maximum length of the
## queue of pending connections. ## queue of pending connections.

View file

@ -45,7 +45,7 @@ proc complete*[T](future: FutureStream[T]) =
future.cb() future.cb()
proc `callback=`*[T](future: FutureStream[T], proc `callback=`*[T](future: FutureStream[T],
cb: proc (future: FutureStream[T]) {.closure,gcsafe.}) = cb: proc (future: FutureStream[T]) {.closure, gcsafe.}) =
## Sets the callback proc to be called when data was placed inside the ## Sets the callback proc to be called when data was placed inside the
## future stream. ## future stream.
## ##