Skip to content

Commit

Permalink
sys/[handles, sockets]: workaround nim bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
alaviss committed Nov 13, 2021
1 parent 44bf145 commit 6929450
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/sys/handles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ when false:
type
Handle*[T] {.requiresInit.} = object
## An object used to associate a handle with a lifetime.
# Walkaround for nim-lang/Nim#16607
# XXX: compiler bug workaround, see: https://github.com/nim-lang/Nim/issues/19139
when true or not defined(release):
initialized: bool
fd: T
Expand All @@ -136,6 +136,14 @@ proc close*[T](h: var Handle[T]) {.inline.} =
## Close the handle owned by `h` and invalidating it.
##
## If `h` is invalid, `ClosedHandleDefect` will be raised.
{.warning: "compiler bug workaround; see: https://github.com/nim-lang/Nim/issues/19139".}
when false:
# TODO: Once nim-lang/Nim#16607 is fixed, make this into a debug check
assert h.initialized, "Handle was not initialized before close, this is a compiler bug."
else:
if not h.initialized:
return

try:
close h.fd
finally:
Expand All @@ -144,11 +152,11 @@ proc close*[T](h: var Handle[T]) {.inline.} =

proc `=destroy`[T](h: var Handle[T]) =
## Destroy the file handle.
{.warning: "compiler bug workaround; see: https://github.com/nim-lang/Nim/issues/19139".}
when false:
# TODO: Once nim-lang/Nim#16607 is fixed, make this into a debug check
assert h.initialized, "Handle was not initialized before destruction, this is a compiler bug."
else:
# Walkaround for nim-lang/Nim#16607
if not h.initialized:
return

Expand Down
23 changes: 19 additions & 4 deletions src/sys/sockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ proc close*(s: AsyncSocket) {.inline.} =
## The FD associated with `s` will be deregistered from `ioqueue`.
close s[]

# These are used to ensure correct destructor binding
{.warning: "compiler bug workaround; see: https://github.com/nim-lang/Nim/issues/19138".}
template initSocketObj(fd: SocketFD): SocketObj =
## Create a socket object from `fd`.
SocketObj(handle: initHandle(fd))

template initSocketObj(handle: sink Handle[SocketFD]): SocketObj =
## Create a socket object from `handle`.
SocketObj(handle: handle)

func newSocket*(fd: SocketFD): Socket {.inline.} =
## Creates a new `Socket` from an opened socket handle.
##
Expand All @@ -99,7 +109,9 @@ func newSocket*(fd: SocketFD): Socket {.inline.} =
## - On Windows, sockets created via Winsock `socket()` function are opened
## in overlapped mode and should be passed to `newAsyncSocket
## <#newAsyncSocket.SocketFD>` instead.
Socket(handle: initHandle(fd))
result = new SocketObj
wasMoved(result[])
result[] = initSocketObj(fd)

func newAsyncSocket*(fd: SocketFD): AsyncSocket {.inline.} =
## Creates a new `AsyncSocket` from an opened socket handle.
Expand All @@ -114,7 +126,8 @@ func newAsyncSocket*(fd: SocketFD): AsyncSocket {.inline.} =
## - On POSIX, it is assumed that `fd` is opened with `O_NONBLOCK` set.
##
## - On Windows, it is assumed that `fd` is opened in overlapped mode.
AsyncSocket newSocket(fd)
result = new AsyncSocketObj
result[] = AsyncSocketObj initSocketObj(fd)

func newSocket*(handle: sink Handle[SocketFD]): Socket {.inline.} =
## Creates a new `Socket` from an opened socket handle.
Expand All @@ -129,7 +142,8 @@ func newSocket*(handle: sink Handle[SocketFD]): Socket {.inline.} =
## - On Windows, sockets created via Winsock `socket()` function are opened
## in overlapped mode and should be passed to `newAsyncSocket
## <#newAsyncSocket.SocketFD>` instead.
Socket(handle: handle)
result = new SocketObj
result[] = initSocketObj(handle)

func newAsyncSocket*(handle: sink Handle[SocketFD]): AsyncSocket {.inline.} =
## Creates a new `AsyncSocket` from an opened socket handle.
Expand All @@ -144,7 +158,8 @@ func newAsyncSocket*(handle: sink Handle[SocketFD]): AsyncSocket {.inline.} =
## - On POSIX, it is assumed that `fd` is opened with `O_NONBLOCK` set.
##
## - On Windows, it is assumed that `fd` is opened in overlapped mode.
AsyncSocket newSocket(handle)
result = new AsyncSocketObj
result[] = AsyncSocketObj initSocketObj(handle)

template derive(T, Base: typedesc): untyped =
## Template to mass-borrow socket operations.
Expand Down

0 comments on commit 6929450

Please sign in to comment.