Skip to content

Commit

Permalink
Merge pull request #17 from nim-lang/devel
Browse files Browse the repository at this point in the history
Sync Fork from Upstream Repo
  • Loading branch information
sthagen authored Jan 4, 2020
2 parents 581ef3e + 47e7b87 commit efbee36
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
6 changes: 3 additions & 3 deletions compiler/extccomp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ compiler tcc:
linkLibCmd: "", # XXX: not supported yet
debug: " -g ",
pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n",
asmStmtFrmt: "asm($1);$n",
structStmtFmt: "$1 $2",
produceAsm: "",
props: {hasSwitchRange, hasComputedGoto})
produceAsm: gnuAsmListing,
props: {hasSwitchRange, hasComputedGoto, hasGnuAsm})

# Pelles C Compiler
compiler pcc:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =
for i in 1..<params.len:
let param = params[i].sym
if isSinkTypeForParam(param.typ):
createTypeBoundOps(t.graph, t.c, param.typ, param.info)
createTypeBoundOps(t, param.typ, param.info)

if not isEmptyType(s.typ[0]) and
({tfNeedsInit, tfNotNil} * s.typ[0].flags != {} or
Expand Down
84 changes: 83 additions & 1 deletion tests/destructor/ttuple.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

discard """
output: '''5.0 10.0'''
output: '''5.0 10.0
=destroy
=destroy
'''
"""

type
Expand Down Expand Up @@ -46,3 +49,82 @@ let h = MyOpt[float](has: true, val: 5.0)
myproc(h)


#-------------------------------------------------------------
type
MyObject* = object
len*: int
amount: UncheckedArray[float]

MyObjPtr* = ptr MyObject

MyObjContainer* {.byref.} = object
size1: int
size2: int
data: ptr UncheckedArray[MyObjPtr]


proc size1*(m: MyObjContainer): int {.inline.} = m.size1
proc size2*(m: MyObjContainer): int {.inline.} = m.size2

proc allocateMyObjPtr(size2: int): MyObjPtr =
cast[MyObjPtr](allocShared(sizeof(MyObject) + sizeof(float) * size2.int))

proc `=destroy`*(m: var MyObjContainer) {.inline.} =
if m.data != nil:
for i in 0..<m.size1:
if m.data[i] != nil:
deallocShared(m.data[i])
m.data[i] = nil
deallocShared(m.data)
echo "=destroy"
m.data = nil

proc `=sink`*(m: var MyObjContainer, m2: MyObjContainer) {.inline.} =
if m.data != m2.data:
`=destroy`(m)
m.size1 = m2.size1
m.size2 = m2.size2
m.data = m2.data


proc `=`*(m: var MyObjContainer, m2: MyObjContainer) {.error.}
## non copyable

func newMyObjContainer*(size2: Natural): MyObjContainer =
result.size2 = size2

proc push(m: var MyObjContainer, cf: MyObjPtr) =
## Add MyObjPtr to MyObjContainer, shallow copy
m.size1.inc
m.data = cast[ptr UncheckedArray[MyObjPtr]](reallocShared(m.data, m.size1 * sizeof(MyObjPtr)))
m.data[m.size1 - 1] = cf


proc add*(m: var MyObjContainer, amount: float) =
assert m.size2 > 0, "MyObjContainer is not initialized, use newMyObjContainer() to initialize object before use"
let cf = allocateMyObjPtr(m.size2)
for i in 0..<m.size2:
cf.amount[i.int] = amount

m.push(cf)

proc add*(dest: var MyObjContainer, src: sink MyObjContainer) =
# merge containers

for i in 0..<src.size1:
dest.push src.data[i]
src.data[i] = nil


proc test =
var cf1 = newMyObjContainer(100)
cf1.add(1)
cf1.add(2)

var cf3 = newMyObjContainer(100)
cf3.add(2)
cf3.add(3)

cf1.add(cf3)

test()

0 comments on commit efbee36

Please sign in to comment.