first steps

This commit is contained in:
Araq 2014-07-20 01:06:03 +02:00
commit 0297c61378
7 changed files with 45 additions and 26 deletions

View file

@ -768,6 +768,8 @@ type
# it won't cause problems # it won't cause problems
TTypeSeq* = seq[PType] TTypeSeq* = seq[PType]
TTypeAttachedOp* = enum
attachedDestructor, attachedAsgn, attachedDeepCopy
TType* {.acyclic.} = object of TIdObj # \ TType* {.acyclic.} = object of TIdObj # \
# types are identical iff they have the # types are identical iff they have the
# same id; there may be multiple copies of a type # same id; there may be multiple copies of a type
@ -785,12 +787,13 @@ type
# the body of the user-defined type class # the body of the user-defined type class
# formal param list # formal param list
# else: unused # else: unused
destructor*: PSym # destructor. warning: nil here may not necessary
# mean that there is no destructor.
# see instantiateDestructor in types.nim
owner*: PSym # the 'owner' of the type owner*: PSym # the 'owner' of the type
sym*: PSym # types have the sym associated with them sym*: PSym # types have the sym associated with them
# it is used for converting types to strings # it is used for converting types to strings
attachedOp*: array[TTypeAttachedOp, PSym] # \
# destructor. warning: nil here may not necessary
# mean that there is no destructor.
# see instantiateDestructor in types.nim
size*: BiggestInt # the size of the type in bytes size*: BiggestInt # the size of the type in bytes
# -1 means that the size is unkwown # -1 means that the size is unkwown
align*: int # the type's alignment requirements align*: int # the type's alignment requirements
@ -1189,7 +1192,7 @@ proc assignType(dest, src: PType) =
dest.n = src.n dest.n = src.n
dest.size = src.size dest.size = src.size
dest.align = src.align dest.align = src.align
dest.destructor = src.destructor dest.attachedOps = src.attachedOps
# this fixes 'type TLock = TSysLock': # this fixes 'type TLock = TSysLock':
if src.sym != nil: if src.sym != nil:
if dest.sym != nil: if dest.sym != nil:

View file

@ -214,9 +214,9 @@ proc optAsgnLoc(a: TLoc, t: PType, field: PRope): TLoc =
proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
let newflags = let newflags =
if src.k == locData: if src.k == locData:
flags + { needToCopy } flags + {needToCopy}
elif tfShallow in dest.t.flags: elif tfShallow in dest.t.flags:
flags - { needToCopy } flags - {needToCopy}
else: else:
flags flags
for i in 0 .. <dest.t.len: for i in 0 .. <dest.t.len:
@ -230,9 +230,9 @@ proc genOptAsgnObject(p: BProc, dest, src: TLoc, flags: TAssignmentFlags,
if t == nil: return if t == nil: return
let newflags = let newflags =
if src.k == locData: if src.k == locData:
flags + { needToCopy } flags + {needToCopy}
elif tfShallow in dest.t.flags: elif tfShallow in dest.t.flags:
flags - { needToCopy } flags - {needToCopy}
else: else:
flags flags
case t.kind case t.kind

View file

@ -332,7 +332,7 @@ const
errUsingNoSymbol: "'$1' is not a variable, constant or a proc name", errUsingNoSymbol: "'$1' is not a variable, constant or a proc name",
errMacroBodyDependsOnGenericTypes: "the macro body cannot be compiled, " & errMacroBodyDependsOnGenericTypes: "the macro body cannot be compiled, " &
"because the parameter '$1' has a generic type", "because the parameter '$1' has a generic type",
errDestructorNotGenericEnough: "Destructor signarue is too specific. " & errDestructorNotGenericEnough: "Destructor signature is too specific. " &
"A destructor must be associated will all instantiations of a generic type", "A destructor must be associated will all instantiations of a generic type",
errInlineIteratorsAsProcParams: "inline iterators can be used as parameters only for " & errInlineIteratorsAsProcParams: "inline iterators can be used as parameters only for " &
"templates, macros and other inline iterators", "templates, macros and other inline iterators",

View file

@ -42,7 +42,7 @@ proc doDestructorStuff(c: PContext, s: PSym, n: PNode) =
localError(n.info, errDestructorNotGenericEnough) localError(n.info, errDestructorNotGenericEnough)
return return
t.destructor = s t.attachedOp[attachedDestructor] = s
# automatically insert calls to base classes' destructors # automatically insert calls to base classes' destructors
if n.sons[bodyPos].kind != nkEmpty: if n.sons[bodyPos].kind != nkEmpty:
for i in countup(0, t.sonsLen - 1): for i in countup(0, t.sonsLen - 1):
@ -52,7 +52,7 @@ proc doDestructorStuff(c: PContext, s: PSym, n: PNode) =
let destructableT = instantiateDestructor(c, t.sons[i]) let destructableT = instantiateDestructor(c, t.sons[i])
if destructableT != nil: if destructableT != nil:
n.sons[bodyPos].addSon(newNode(nkCall, t.sym.info, @[ n.sons[bodyPos].addSon(newNode(nkCall, t.sym.info, @[
useSym(destructableT.destructor), useSym(destructableT.attachedOp[attachedDestructor]),
n.sons[paramsPos][1][0]])) n.sons[paramsPos][1][0]]))
proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode
@ -61,7 +61,7 @@ proc destroySym(c: PContext, field: PSym, holder: PNode): PNode =
let destructableT = instantiateDestructor(c, field.typ) let destructableT = instantiateDestructor(c, field.typ)
if destructableT != nil: if destructableT != nil:
result = newNode(nkCall, field.info, @[ result = newNode(nkCall, field.info, @[
useSym(destructableT.destructor), useSym(destructableT.attachedOp[attachedDestructor]),
newNode(nkDotExpr, field.info, @[holder, useSym(field)])]) newNode(nkDotExpr, field.info, @[holder, useSym(field)])])
proc destroyCase(c: PContext, n: PNode, holder: PNode): PNode = proc destroyCase(c: PContext, n: PNode, holder: PNode): PNode =
@ -128,10 +128,10 @@ proc instantiateDestructor(c: PContext, typ: PType): PType =
let typeHoldingUserDefinition = if t.kind == tyGenericInst: t.base let typeHoldingUserDefinition = if t.kind == tyGenericInst: t.base
else: t else: t
if typeHoldingUserDefinition.destructor != nil: if typeHoldingUserDefinition.attachedOp[attachedDestructor] != nil:
# XXX: This is not entirely correct for recursive types, but we need # XXX: This is not entirely correct for recursive types, but we need
# it temporarily to hide the "destroy is already defined" problem # it temporarily to hide the "destroy is already defined" problem
if typeHoldingUserDefinition.destructor notin if typeHoldingUserDefinition.attachedOp[attachedDestructor] notin
[analyzingDestructor, destructorIsTrivial]: [analyzingDestructor, destructorIsTrivial]:
return typeHoldingUserDefinition return typeHoldingUserDefinition
else: else:
@ -143,12 +143,12 @@ proc instantiateDestructor(c: PContext, typ: PType): PType =
if instantiateDestructor(c, t.sons[0]) != nil: if instantiateDestructor(c, t.sons[0]) != nil:
if rangeDestructorProc == nil: if rangeDestructorProc == nil:
rangeDestructorProc = searchInScopes(c, getIdent"nimDestroyRange") rangeDestructorProc = searchInScopes(c, getIdent"nimDestroyRange")
t.destructor = rangeDestructorProc t.attachedOp[attachedDestructor] = rangeDestructorProc
return t return t
else: else:
return nil return nil
of tyTuple, tyObject: of tyTuple, tyObject:
t.destructor = analyzingDestructor t.attachedOp[attachedDestructor] = analyzingDestructor
let generated = generateDestructor(c, t) let generated = generateDestructor(c, t)
if generated != nil: if generated != nil:
internalAssert t.sym != nil internalAssert t.sym != nil
@ -169,10 +169,10 @@ proc instantiateDestructor(c: PContext, typ: PType): PType =
generated generated
]) ])
let semantizedDef = semProc(c, fullDef) let semantizedDef = semProc(c, fullDef)
t.destructor = semantizedDef[namePos].sym t.attachedOp[attachedDestructor] = semantizedDef[namePos].sym
return t return t
else: else:
t.destructor = destructorIsTrivial t.attachedOp[attachedDestructor] = destructorIsTrivial
return nil return nil
else: else:
return nil return nil
@ -226,7 +226,7 @@ proc insertDestructors(c: PContext,
tryStmt.addSon( tryStmt.addSon(
newNode(nkFinally, info, @[ newNode(nkFinally, info, @[
semStmt(c, newNode(nkCall, info, @[ semStmt(c, newNode(nkCall, info, @[
useSym(destructableT.destructor), useSym(destructableT.attachedOp[attachedDestructor]),
useSym(varId.sym)]))])) useSym(varId.sym)]))]))
result.outer = newNodeI(nkStmtList, info) result.outer = newNodeI(nkStmtList, info)

View file

@ -325,7 +325,7 @@ proc suggestSym*(info: TLineInfo; s: PSym) {.inline.} =
if isServing: if isServing:
addToSourceMap(s, info) addToSourceMap(s, info)
proc markUsed(info: TLineInfo; s: PSym) = proc markUsed*(info: TLineInfo; s: PSym) =
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
if {sfDeprecated, sfError} * s.flags != {}: if {sfDeprecated, sfError} * s.flags != {}:
if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s) if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s)

View file

@ -61,7 +61,9 @@ proc checkFinished[T](future: PFuture[T]) =
echo("Contents: ", future.value.repr) echo("Contents: ", future.value.repr)
echo("<----->") echo("<----->")
echo("Future already finished, cannot finish twice.") echo("Future already finished, cannot finish twice.")
assert false echo getStackTrace()
quit()
#assert false
proc complete*[T](future: PFuture[T], val: T) = proc complete*[T](future: PFuture[T], val: T) =
## Completes ``future`` with value ``val``. ## Completes ``future`` with value ``val``.
@ -836,16 +838,22 @@ template createCb*(retFutureSym, iteratorNameSym,
var nameIterVar = iteratorNameSym var nameIterVar = iteratorNameSym
#{.push stackTrace: off.} #{.push stackTrace: off.}
proc cb {.closure,gcsafe.} = proc cb {.closure,gcsafe.} =
echo("cb")
try: try:
if not nameIterVar.finished: if not nameIterVar.finished:
echo("Executing iter")
var next = nameIterVar() var next = nameIterVar()
echo(next.isNil, " ", next == nil, " ", nameIterVar.finished)
if next == nil: if next == nil:
assert retFutureSym.finished, "Async procedure's (" & assert retFutureSym.finished, "Async procedure's (" &
name & ") return Future was not finished." name & ") return Future was not finished."
else: else:
next.callback = cb next.callback = cb
except: except:
echo(getCurrentExceptionMsg())
echo(retFutureSym.stackTrace)
retFutureSym.fail(getCurrentException()) retFutureSym.fail(getCurrentException())
echo("hello")
cb() cb()
#{.pop.} #{.pop.}
proc generateExceptionCheck(futSym, proc generateExceptionCheck(futSym,
@ -1068,8 +1076,8 @@ macro async*(prc: stmt): stmt {.immediate.} =
result[6] = outerProcBody result[6] = outerProcBody
#echo(treeRepr(result)) #echo(treeRepr(result))
#if prc[0].getName == "routeReq": if prc[0].getName == "processClient":
#echo(toStrLit(result)) echo(toStrLit(result))
proc recvLine*(socket: TAsyncFD): PFuture[string] {.async.} = proc recvLine*(socket: TAsyncFD): PFuture[string] {.async.} =
## Reads a line of data from ``socket``. Returned future will complete once ## Reads a line of data from ``socket``. Returned future will complete once

View file

@ -114,8 +114,10 @@ proc processClient(client: PAsyncSocket, address: string,
return return
let lineParts = line.split(' ') let lineParts = line.split(' ')
if lineParts.len != 3: if lineParts.len != 3:
await request.respond(Http400, "Invalid request. Got: " & line) echo("About to raise")
continue raise newException(EBase, "FUCK YA")
#await request.respond(Http400, "Invalid request. Got: " & line)
#continue
let reqMethod = lineParts[0] let reqMethod = lineParts[0]
let path = lineParts[1] let path = lineParts[1]
@ -198,7 +200,13 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
# TODO: Causes compiler crash. # TODO: Causes compiler crash.
#var (address, client) = await server.socket.acceptAddr() #var (address, client) = await server.socket.acceptAddr()
var fut = await server.socket.acceptAddr() var fut = await server.socket.acceptAddr()
asyncCheck processClient(fut.client, fut.address, callback) var f = processClient(fut.client, fut.address, callback)
#echo(f.isNil)
#echo(f.repr)
f.callback =
proc () =
echo("processClient end")
echo(f.failed)
proc close*(server: PAsyncHttpServer) = proc close*(server: PAsyncHttpServer) =
## Terminates the async http server instance. ## Terminates the async http server instance.