preparations for making 'closure' the default calling convention for proc types

This commit is contained in:
Araq 2012-07-16 23:00:57 +02:00
commit 8d99753d63
36 changed files with 117 additions and 102 deletions

View file

@ -56,7 +56,7 @@ const
onlySafeCode = true
proc merge[T](a, b: var openArray[T], lo, m, hi: int,
cmp: proc (x, y: T): int, order: TSortOrder) =
cmp: proc (x, y: T): int {.closure.}, order: TSortOrder) =
template `<-` (a, b: expr) =
when false:
a = b
@ -102,7 +102,7 @@ proc merge[T](a, b: var openArray[T], lo, m, hi: int,
if k < j: copyMem(addr(a[k]), addr(b[i]), sizeof(T)*(j-k))
proc sort*[T](a: var openArray[T],
cmp: proc (x, y: T): int,
cmp: proc (x, y: T): int {.closure.},
order = TSortOrder.Ascending) =
## Default Nimrod sort. The sorting is guaranteed to be stable and
## the worst case is guaranteed to be O(n log n).

View file

@ -33,14 +33,14 @@ type
TDelegate = object
deleVal*: PObject
handleRead*: proc (h: PObject)
handleWrite*: proc (h: PObject)
handleConnect*: proc (h: PObject)
handleRead*: proc (h: PObject) {.nimcall.}
handleWrite*: proc (h: PObject) {.nimcall.}
handleConnect*: proc (h: PObject) {.nimcall.}
handleAccept*: proc (h: PObject)
getSocket*: proc (h: PObject): tuple[info: TInfo, sock: TSocket]
handleAccept*: proc (h: PObject) {.nimcall.}
getSocket*: proc (h: PObject): tuple[info: TInfo, sock: TSocket] {.nimcall.}
task*: proc (h: PObject)
task*: proc (h: PObject) {.nimcall.}
mode*: TMode
PDelegate* = ref TDelegate
@ -56,10 +56,10 @@ type
userArg: PObject
handleRead*: proc (s: PAsyncSocket, arg: PObject)
handleConnect*: proc (s: PAsyncSocket, arg: PObject)
handleRead*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.}
handleConnect*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.}
handleAccept*: proc (s: PAsyncSocket, arg: PObject)
handleAccept*: proc (s: PAsyncSocket, arg: PObject) {.nimcall.}
lineBuffer: TaintedString ## Temporary storage for ``recvLine``

View file

@ -39,14 +39,14 @@ proc zip*[S, T](seq1: seq[S], seq2: seq[T]): seq[tuple[a: S, b: T]] =
newSeq(result, m)
for i in 0 .. m-1: result[i] = (seq1[i], seq2[i])
iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool): T =
iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): T =
## Iterates through a sequence and yields every item that fulfills the
## predicate.
for i in countup(0, len(seq1) -1):
var item = seq1[i]
if pred(item): yield seq1[i]
proc filter*[T](seq1: seq[T], pred: proc(item: T): bool): seq[T] =
proc filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): seq[T] =
## Returns all items in a sequence that fulfilled the predicate.
accumulateResult(filter(seq1, pred))

View file

@ -11,7 +11,8 @@
##
## This module implements an event system that is not dependant on external
## graphical toolkits. It was originally called ``NimEE`` because
## it was inspired by Python's PyEE module. There are two ways you can use events: one is a python-inspired way; the other is more of a C-style way.
## it was inspired by Python's PyEE module. There are two ways you can use
## events: one is a python-inspired way; the other is more of a C-style way.
##
## .. code-block:: Nimrod
## var ee = initEventEmitter()
@ -44,22 +45,21 @@ type
proc initEventHandler*(name: string): TEventHandler =
## Initializes an EventHandler with the specified name and returns it.
#new(result)
result.handlers = @[]
result.name = name
proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) =
proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) =
## Adds the callback to the specified event handler.
handler.handlers.add(func)
proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) =
proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) =
## Removes the callback from the specified event handler.
for i in countup(0, len(handler.handlers) -1):
if func == handler.handlers[i]:
handler.handlers.del(i)
break
proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)): bool =
proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}): bool =
## Checks if a callback is registered to this event handler.
return handler.handlers.contains(func)
@ -73,7 +73,7 @@ proc getEventhandler(emitter: var TEventEmitter, event: string): int =
if emitter.s[k].name == event: return k
return -1
proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) =
proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs) {.closure.}) =
## Assigns a event handler with the specified callback. If the event
## doesn't exist, it will be created.
var i = getEventHandler(emitter, event)
@ -99,5 +99,4 @@ proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) =
proc initEventEmitter*(): TEventEmitter =
## Creates and returns a new EventEmitter.
#new(result)
result.s = @[]

View file

@ -347,7 +347,8 @@ proc close*(s: TServer) =
## closes the server (and the socket the server uses).
close(s.socket)
proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
proc run*(handleRequest: proc (client: TSocket,
path, query: string): bool {.closure.},
port = TPort(80)) =
## encapsulates the server object and main loop
var s: TServer

View file

@ -23,13 +23,14 @@ type
## here shouldn't be used directly. They are
## accessible so that a stream implementation
## can override them.
closeImpl*: proc (s: PStream)
atEndImpl*: proc (s: PStream): bool
setPositionImpl*: proc (s: PStream, pos: int)
getPositionImpl*: proc (s: PStream): int
readDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int): int
writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int)
flushImpl*: proc (s: PStream)
closeImpl*: proc (s: PStream) {.nimcall.}
atEndImpl*: proc (s: PStream): bool {.nimcall.}
setPositionImpl*: proc (s: PStream, pos: int) {.nimcall.}
getPositionImpl*: proc (s: PStream): int {.nimcall.}
readDataImpl*: proc (s: PStream, buffer: pointer,
bufLen: int): int {.nimcall.}
writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int) {.nimcall.}
flushImpl*: proc (s: PStream) {.nimcall.}
proc flush*(s: PStream) =
## flushes the buffers that the stream `s` might use.

View file

@ -111,7 +111,7 @@ proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
## leaked implementation detail. Do not use.
proc new*[T](a: var ref T, finalizer: proc (x: ref T)) {.
proc new*[T](a: var ref T, finalizer: proc (x: ref T) {.nimcall.}) {.
magic: "NewFinalize", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``. When the garbage collector frees the object,
@ -1416,13 +1416,13 @@ proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} =
result = s[L]
setLen(s, L)
proc each*[T, S](data: openArray[T], op: proc (x: T): S): seq[S] =
proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] =
## The well-known ``map`` operation from functional programming. Applies
## `op` to every item in `data` and returns the result as a sequence.
newSeq(result, data.len)
for i in 0..data.len-1: result[i] = op(data[i])
proc each*[T](data: var openArray[T], op: proc (x: var T)) =
proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) =
## The well-known ``map`` operation from functional programming. Applies
## `op` to every item in `data`.
for i in 0..data.len-1: op(data[i])
@ -1567,11 +1567,11 @@ const nimrodStackTrace = compileOption("stacktrace")
# of the code
var
dbgLineHook*: proc ()
dbgLineHook*: proc () {.nimcall.}
## set this variable to provide a procedure that should be called before
## each executed instruction. This should only be used by debuggers!
## Only code compiled with the ``debugger:on`` switch calls this hook.
globalRaiseHook*: proc (e: ref E_Base): bool
globalRaiseHook*: proc (e: ref E_Base): bool {.nimcall.}
## with this hook you can influence exception handling on a global level.
## If not nil, every 'raise' statement ends up calling this hook. Ordinary
## application code should never set this hook! You better know what you
@ -1579,7 +1579,7 @@ var
## exception is caught and does not propagate further through the call
## stack.
localRaiseHook* {.threadvar.}: proc (e: ref E_Base): bool
localRaiseHook* {.threadvar.}: proc (e: ref E_Base): bool {.nimcall.}
## with this hook you can influence exception handling on a
## thread local level.
## If not nil, every 'raise' statement ends up calling this hook. Ordinary
@ -1587,7 +1587,7 @@ var
## do when setting this. If ``localRaiseHook`` returns false, the exception
## is caught and does not propagate further through the call stack.
outOfMemHook*: proc ()
outOfMemHook*: proc () {.nimcall.}
## set this variable to provide a procedure that should be called
## in case of an `out of memory`:idx: event. The standard handler
## writes an error message and terminates the program. `outOfMemHook` can

View file

@ -45,7 +45,7 @@ type
TWalkOp = enum
waZctDecRef, waPush, waCycleDecRef
TFinalizer {.compilerproc.} = proc (self: pointer)
TFinalizer {.compilerproc.} = proc (self: pointer) {.nimcall.}
# A ref type can have a finalizer that is called before the object's
# storage is freed.

View file

@ -62,7 +62,7 @@ type # This should be he same as ast.TTypeKind
base: ptr TNimType
node: ptr TNimNode # valid for tyRecord, tyObject, tyTuple, tyEnum
finalizer: pointer # the finalizer for the type
marker: proc (p: pointer, op: int) # marker proc for GC
marker: proc (p: pointer, op: int) {.nimcall.} # marker proc for GC
PNimType = ptr TNimType
# node.len may be the ``first`` element of a set

View file

@ -255,9 +255,9 @@ type
## that **must not** be part of a message! Use
## a ``TThreadId`` for that.
when TArg is void:
dataFn: proc ()
dataFn: proc () {.nimcall.}
else:
dataFn: proc (m: TArg)
dataFn: proc (m: TArg) {.nimcall.}
data: TArg
TThreadId*[TArg] = ptr TThread[TArg] ## the current implementation uses
## a pointer as a thread ID.

View file

@ -419,7 +419,7 @@ type
pad32*: TFunction
TEventListener* = proc (para1: PObject){.cdecl.}
TEventListenerInitProc* = proc ()
TEventListenerInitProc* = proc (){.cdecl.}
TEventListenerInit* = proc (para1: TEventListenerInitProc){.cdecl.}
PKeyEventStruct* = ptr TKeyEventStruct
TKeyEventStruct*{.final, pure.} = object

View file

@ -291,7 +291,7 @@ type
PEvent* = ptr TEvent
TEventFunc* = proc (event: PEvent, data: gpointer){.cdecl.}
PXEvent* = ptr TXEvent
TXEvent* = proc ()
TXEvent* = proc () {.cdecl.}
PFilterReturn* = ptr TFilterReturn
TFilterReturn* = enum
FILTER_CONTINUE, FILTER_TRANSLATE, FILTER_REMOVE
@ -775,7 +775,7 @@ type
PWindowObjectClass* = ptr TWindowObjectClass
TWindowObjectClass* = object of TDrawableClass
window_invalidate_maybe_recurse_child_func* = proc (para1: PWindow,
para2: gpointer): gboolean
para2: gpointer): gboolean {.cdecl.}
proc TYPE_COLORMAP*(): GType
proc COLORMAP*(anObject: pointer): PColormap

View file

@ -2676,7 +2676,7 @@ when false:
proc g_critical*(format: cstring){.varargs.}
proc g_warning*(format: cstring){.varargs.}
type
TGPrintFunc* = proc (str: cstring)
TGPrintFunc* = proc (str: cstring){.cdecl, varargs.}
proc g_set_print_handler*(func: TGPrintFunc): TGPrintFunc{.cdecl,
dynlib: gliblib, importc: "g_set_print_handler".}

View file

@ -104,7 +104,7 @@ type
Tfree_callback* = proc (p: pointer){.cdecl.}
Trealloc_callback* = proc (p: pointer, size: int): pointer{.cdecl.}
Tstrdup_callback* = proc (str: cstring): cstring{.cdecl.}
Tcalloc_callback* = proc (nmemb: int, size: int): pointer
Tcalloc_callback* = proc (nmemb: int, size: int): pointer{.noconv.}
Tinfotype* = enum
INFO_TEXT = 0, INFO_HEADER_IN, INFO_HEADER_OUT, INFO_DATA_IN, INFO_DATA_OUT,
INFO_SSL_DATA_IN, INFO_SSL_DATA_OUT, INFO_END

View file

@ -8,6 +8,8 @@
#
{.deadCodeElim: on.}
{.push, callconv: cdecl.}
when defined(Unix):
const
lib = "libmysqlclient.so.15"
@ -1071,3 +1073,5 @@ proc INTERNAL_NUM_FIELD(f: Pst_mysql_field): bool =
proc reload(mysql: PMySQL): cint =
result = refresh(mysql, REFRESH_GRANT)
{.pop.}

View file

@ -11,6 +11,11 @@
import
GL
when defined(windows):
{.push, callconv: stdcall.}
else:
{.push, callconv: cdecl.}
when defined(windows):
const
dllname = "glu32.dll"
@ -326,4 +331,5 @@ const # Contours types -- obsolete!
GLU_ERROR* = GLU_TESS_ERROR
GLU_EDGE_FLAG* = GLU_TESS_EDGE_FLAG
{.pop.}
# implementation

View file

@ -1156,7 +1156,7 @@ proc PyWeakref_NewProxy*(ob, callback: PPyObject): PPyObject{.cdecl, importc, dy
proc PyWeakref_NewRef*(ob, callback: PPyObject): PPyObject{.cdecl, importc, dynlib: dllname.}
proc PyWrapper_New*(ob1, ob2: PPyObject): PPyObject{.cdecl, importc, dynlib: dllname.}
proc PyBool_FromLong*(ok: int): PPyObject{.cdecl, importc, dynlib: dllname.} #-
proc Py_AtExit*(prc: proc ()): int{.cdecl, importc, dynlib: dllname.} #-
proc Py_AtExit*(prc: proc () {.cdecl.}): int{.cdecl, importc, dynlib: dllname.} #-
#Py_Cleanup:procedure; cdecl, importc, dynlib: dllname;
#-
proc Py_CompileString*(s1, s2: cstring, i: int): PPyObject{.cdecl, importc, dynlib: dllname.} #-

View file

@ -1279,7 +1279,6 @@ type # This is the system-independent thread info struc
TByteArray* = array[0..32767, int8]
PWordArray* = ptr TWordArray
TWordArray* = array[0..16383, int16] # Generic procedure pointer
TProcedure* = proc ()
type TEventSeq = set[TEventKind]
template evconv(procName: expr, ptrName: typeDesc, assertions: TEventSeq): stmt {.immediate.} =

View file

@ -137,7 +137,7 @@ const
XrmEnumOneLevel* = 1
type
funcbool* = proc (): TBool
funcbool* = proc (): TBool {.cdecl.}
proc XrmEnumerateDatabase*(para1: TXrmDatabase, para2: TXrmNameList,
para3: TXrmClassList, para4: int32, para5: funcbool,