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..a372ff4ead76 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,16 @@ 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) + +var x2 = 2 +reset(x2) + +block: + let (a, d) = allocCounters() + discard cprintf("%ld %ld\n", a, d)