From ea467f9c1954458a1561ca334b83935686806a5c Mon Sep 17 00:00:00 2001 From: Andrii Riabushenko Date: Wed, 8 May 2019 22:38:14 +0100 Subject: [PATCH 1/2] reset for newruntime --- lib/system.nim | 17 +++++++++++++---- tests/destructor/tnewruntime_strutils.nim | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index ce6ed4c7fae6..b14bbd8e707e 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -264,10 +264,11 @@ proc new*[T](a: var ref T, finalizer: proc (x: ref T) {.nimcall.}) {. ## **Note**: The `finalizer` refers to the type `T`, not to the object! ## This means that for each object of type `T` the finalizer will be called! -proc reset*[T](obj: var T) {.magic: "Reset", noSideEffect.} - ## Resets an object `obj` to its initial (binary zero) value. - ## - ## This needs to be called before any possible `object branch transition`:idx:. +when not defined(nimV2): + proc reset*[T](obj: var T) {.magic: "Reset", noSideEffect.} + ## Resets an object `obj` to its initial (binary zero) value. + ## + ## This needs to be called before any possible `object branch transition`:idx:. proc wasMoved*[T](obj: var T) {.magic: "WasMoved", noSideEffect.} = ## Resets an object `obj` to its initial (binary zero) value to signify @@ -4441,6 +4442,14 @@ when defined(genode): # and return to thread entrypoint. +when defined(nimV2): + proc reset*[T](obj: var T) = + ## Resets an object `obj` to its initial (binary zero) value. + ## Destructor is invoked if T type has destructor. + mixin `=destroy` + `=destroy`(obj) + zeroMem(obj.addr, sizeof(obj)) + import system/widestrs export widestrs diff --git a/tests/destructor/tnewruntime_strutils.nim b/tests/destructor/tnewruntime_strutils.nim index 5b8684354ac6..dbbe920a19ee 100644 --- a/tests/destructor/tnewruntime_strutils.nim +++ b/tests/destructor/tnewruntime_strutils.nim @@ -1,6 +1,8 @@ discard """ cmd: '''nim c --newruntime $file''' - output: '''442 442''' + output: '''442 442 +443 443 +''' """ import strutils, os @@ -187,5 +189,13 @@ proc staticTests = nonStaticTests() staticTests() -let (a, d) = allocCounters() -discard cprintf("%ld %ld\n", a, d) +block: + let (a, d) = allocCounters() + discard cprintf("%ld %ld\n", a, d) + +var x = newSeq[string](2) +reset(x) + +block: + let (a, d) = allocCounters() + discard cprintf("%ld %ld\n", a, d) From 3ca57ae8f47b2bdf7e8960df0fbb94d3cd838e63 Mon Sep 17 00:00:00 2001 From: Andrii Riabushenko Date: Thu, 9 May 2019 20:56:31 +0100 Subject: [PATCH 2/2] improve test --- tests/destructor/tnewruntime_strutils.nim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/destructor/tnewruntime_strutils.nim b/tests/destructor/tnewruntime_strutils.nim index dbbe920a19ee..a372ff4ead76 100644 --- a/tests/destructor/tnewruntime_strutils.nim +++ b/tests/destructor/tnewruntime_strutils.nim @@ -196,6 +196,9 @@ block: var x = newSeq[string](2) reset(x) +var x2 = 2 +reset(x2) + block: let (a, d) = allocCounters() discard cprintf("%ld %ld\n", a, d)