Add vtableAddr as a reusable helper for working around a Nim issue

This commit is contained in:
Zahary Karadjov 2020-04-09 22:56:15 +03:00
commit fa415774c3
No known key found for this signature in database
GPG key ID: C8936F8A3073D609

View file

@ -23,12 +23,15 @@ type
OutputStream* = ref OutputStreamObj OutputStream* = ref OutputStreamObj
OutputStreamVar = OutputStream OutputStreamVar = OutputStream
OutputStreamVTable* = object WritePageProc* = proc (s: OutputStream, page: openarray[byte])
writePage*: proc (s: OutputStream, page: openarray[byte]) {.nimcall, gcsafe, raises: [IOError, Defect].}
{.nimcall, gcsafe, raises: [IOError, Defect].}
flush*: proc (s: OutputStream) FlushProc* = proc (s: OutputStream)
{.nimcall, gcsafe, raises: [IOError, Defect].} {.nimcall, gcsafe, raises: [IOError, Defect].}
OutputStreamVTable* = object
writePage*: WritePageProc
flush*: FlushProc
WriteCursor* = object WriteCursor* = object
head, bufferEnd: ptr byte head, bufferEnd: ptr byte
@ -107,12 +110,19 @@ let FileStreamVTable = OutputStreamVTable(
flushFile output.file flushFile output.file
) )
template vtableAddr*(vtable: OutputStreamVTable): ptr OutputStreamVTable =
## This is a simple work-around for the somewhat broken side
## effects analysis of Nim - reading from global let variables
## is considered a side-effect.
{.noSideEffect.}:
unsafeAddr vtable
proc init*(T: type OutputStream, proc init*(T: type OutputStream,
filename: string, filename: string,
pageSize = defaultPageSize): OutputStream = pageSize = defaultPageSize): OutputStream =
implementOutputStream pageSize, implementOutputStream pageSize,
outputDevice = FileOutput(file: open(filename, fmWrite)), outputDevice = FileOutput(file: open(filename, fmWrite)),
vtable = unsafeAddr FileStreamVTable vtable = vtableAddr FileStreamVTable
proc init*(T: type OutputStream, proc init*(T: type OutputStream,
buffer: pointer, len: int): OutputStream = buffer: pointer, len: int): OutputStream =