mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 12:37:46 +00:00
preparations to make the twidgets test work
This commit is contained in:
@@ -1918,14 +1918,14 @@ proc genSomeCast(p: BProc, e: PNode, d: var TLoc) =
|
||||
var a: TLoc
|
||||
initLocExpr(p, e.sons[1], a)
|
||||
let etyp = skipTypes(e.typ, abstractRange+{tyOwned})
|
||||
let srcTyp = skipTypes(e.sons[1].typ, abstractRange)
|
||||
if etyp.kind in ValueTypes and lfIndirect notin a.flags:
|
||||
putIntoDest(p, d, e, "(*($1*) ($2))" %
|
||||
[getTypeDesc(p.module, e.typ), addrLoc(p.config, a)], a.storage)
|
||||
elif etyp.kind == tyProc and etyp.callConv == ccClosure:
|
||||
elif etyp.kind == tyProc and etyp.callConv == ccClosure and srcTyp.callConv != ccClosure:
|
||||
putIntoDest(p, d, e, "(($1) ($2))" %
|
||||
[getClosureType(p.module, etyp, clHalfWithEnv), rdCharLoc(a)], a.storage)
|
||||
else:
|
||||
let srcTyp = skipTypes(e.sons[1].typ, abstractRange)
|
||||
# C++ does not like direct casts from pointer to shorter integral types
|
||||
if srcTyp.kind in {tyPtr, tyPointer} and etyp.kind in IntegralTypes:
|
||||
putIntoDest(p, d, e, "(($1) (ptrdiff_t) ($2))" %
|
||||
|
||||
@@ -701,6 +701,15 @@ proc injectDefaultCalls(n: PNode, c: var Con) =
|
||||
proc isCursor(n: PNode): bool {.inline.} =
|
||||
result = n.kind == nkSym and sfCursor in n.sym.flags
|
||||
|
||||
proc keepVar(n, it: PNode, c: var Con): PNode =
|
||||
# keep the var but transform 'ri':
|
||||
result = copyNode(n)
|
||||
var itCopy = copyNode(it)
|
||||
for j in 0..it.len-2:
|
||||
itCopy.add it[j]
|
||||
itCopy.add p(it[it.len-1], c)
|
||||
result.add itCopy
|
||||
|
||||
proc p(n: PNode; c: var Con): PNode =
|
||||
case n.kind
|
||||
of nkVarSection, nkLetSection:
|
||||
@@ -726,15 +735,10 @@ proc p(n: PNode; c: var Con): PNode =
|
||||
if ri.kind != nkEmpty:
|
||||
let r = moveOrCopy(v, ri, c)
|
||||
result.add r
|
||||
else:
|
||||
result.add keepVar(n, it, c)
|
||||
else:
|
||||
# keep it, but transform 'ri':
|
||||
var varSection = copyNode(n)
|
||||
var itCopy = copyNode(it)
|
||||
for j in 0..L-2:
|
||||
itCopy.add it[j]
|
||||
itCopy.add p(ri, c)
|
||||
varSection.add itCopy
|
||||
result.add varSection
|
||||
result.add keepVar(n, it, c)
|
||||
of nkCallKinds:
|
||||
let parameters = n[0].typ
|
||||
let L = if parameters != nil: parameters.len else: 0
|
||||
@@ -752,7 +756,7 @@ proc p(n: PNode; c: var Con): PNode =
|
||||
else:
|
||||
result = n
|
||||
of nkAsgn, nkFastAsgn:
|
||||
if hasDestructor(n[0].typ):
|
||||
if hasDestructor(n[0].typ) and n[1].kind notin {nkProcDef, nkDo, nkLambda, nkClosure}:
|
||||
result = moveOrCopy(n[0], n[1], c)
|
||||
else:
|
||||
result = copyNode(n)
|
||||
|
||||
74
tests/destructor/twidgets.nim
Normal file
74
tests/destructor/twidgets.nim
Normal file
@@ -0,0 +1,74 @@
|
||||
discard """
|
||||
cmd: '''nim c --newruntime $file'''
|
||||
output: '''button
|
||||
clicked!'''
|
||||
disabled: "true"
|
||||
"""
|
||||
|
||||
type
|
||||
Widget* = ref object of RootObj
|
||||
drawImpl: owned(proc (self: Widget))
|
||||
|
||||
Button* = ref object of Widget
|
||||
caption: string
|
||||
onclick: owned(proc())
|
||||
|
||||
Window* = ref object of Widget
|
||||
elements: seq[owned Widget]
|
||||
|
||||
|
||||
proc newButton(caption: string; onclick: owned(proc())): owned Button =
|
||||
proc draw(self: Widget) =
|
||||
let b = Button(self)
|
||||
echo b.caption
|
||||
|
||||
#result = Button(drawImpl: draw, caption: caption, onclick: onclick)
|
||||
new(result)
|
||||
result.drawImpl = draw
|
||||
result.caption = caption
|
||||
result.onclick = onclick
|
||||
|
||||
iterator unitems*[T](a: seq[owned T]): T {.inline.} =
|
||||
## Iterates over each item of `a`.
|
||||
var i = 0
|
||||
let L = len(a)
|
||||
while i < L:
|
||||
yield a[i]
|
||||
inc(i)
|
||||
assert(len(a) == L, "seq modified while iterating over it")
|
||||
|
||||
proc newWindow(): owned Window =
|
||||
proc draw(self: Widget) =
|
||||
let w = Window(self)
|
||||
for e in unitems(w.elements):
|
||||
let d = (proc(self: Widget))e.drawImpl
|
||||
if not d.isNil: d(e)
|
||||
|
||||
result = Window(drawImpl: draw, elements: @[])
|
||||
|
||||
proc draw(w: Widget) =
|
||||
let d = (proc(self: Widget))w.drawImpl
|
||||
if not d.isNil: d(w)
|
||||
|
||||
proc add*(w: Window; elem: owned Widget) =
|
||||
w.elements.add elem
|
||||
|
||||
proc main =
|
||||
var w = newWindow()
|
||||
|
||||
var b = newButton("button", nil)
|
||||
#let u: Button = b
|
||||
b.onclick = proc () =
|
||||
b.caption = "clicked!"
|
||||
w.add b
|
||||
|
||||
w.draw()
|
||||
# simulate button click:
|
||||
#u.onclick()
|
||||
|
||||
w.draw()
|
||||
|
||||
main()
|
||||
|
||||
let (a, d) = allocCounters()
|
||||
discard cprintf("%ld %ld new: %ld\n", a, d, allocs)
|
||||
Reference in New Issue
Block a user