diff options
author | Araq <rumpf_a@web.de> | 2011-12-04 20:14:50 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-12-04 20:14:50 +0100 |
commit | 70cf34cbdcff55771b6936b898fdb87c22af403e (patch) | |
tree | 84a1569f0d89e9f0a4b630ef3710675fdc1282d6 /lib | |
parent | 728328eec2064ed2086b230c09db063268d064fd (diff) | |
download | Nim-70cf34cbdcff55771b6936b898fdb87c22af403e.tar.gz |
'assert' is now implemented without compiler magic
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/system.nim | 25 | ||||
-rwxr-xr-x | lib/system/channels.nim | 6 | ||||
-rwxr-xr-x | lib/system/ecmasys.nim | 2 |
3 files changed, 22 insertions, 11 deletions
diff --git a/lib/system.nim b/lib/system.nim index 145d1552a..c7e26230a 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1050,13 +1050,6 @@ proc deallocShared*(p: Pointer) {.noconv, rtl.} ## memory (or just freeing it twice!) a core dump may happen ## or other memory may be corrupted. -proc assert*(cond: bool) {.magic: "Assert", noSideEffect.} - ## provides a means to implement `programming by contracts`:idx: in Nimrod. - ## ``assert`` evaluates expression ``cond`` and if ``cond`` is false, it - ## raises an ``EAssertionFailure`` exception. However, the compiler may - ## not generate any code at all for ``assert`` if it is advised to do so. - ## Use ``assert`` for debugging purposes only. - proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.} ## swaps the values `a` and `b`. This is often more efficient than ## ``tmp = a; a = b; b = tmp``. Particularly useful for sorting algorithms. @@ -2071,3 +2064,21 @@ proc `/=` *(x: var float, y:float) {.inline, noSideEffect.} = proc `&=`* (x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.} +proc rand*(max: int): int {.magic: "Rand", sideEffect.} + ## compile-time `random` function. Useful for debugging. + +proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.} + ## converts the AST of `x` into a string representation. This is very useful + ## for debugging. + +template assert*(cond: expr, msg = "") = + ## provides a means to implement `programming by contracts`:idx: in Nimrod. + ## ``assert`` evaluates expression ``cond`` and if ``cond`` is false, it + ## raises an ``EAssertionFailure`` exception. However, the compiler may + ## not generate any code at all for ``assert`` if it is advised to do so. + ## Use ``assert`` for debugging purposes only. + when compileOption("assertions"): + if not cond: + raise newException(EAssertionFailed, astToStr(cond) & ' ' & msg) + + diff --git a/lib/system/channels.nim b/lib/system/channels.nim index 4a37bcc14..fe93d6840 100755 --- a/lib/system/channels.nim +++ b/lib/system/channels.nim @@ -38,7 +38,7 @@ proc initRawChannel(p: pointer) = proc deinitRawChannel(p: pointer) = var c = cast[PRawChannel](p) - # we need to grab the lock to be save against sending threads! + # we need to grab the lock to be safe against sending threads! acquireSys(c.lock) c.mask = ChannelDeadMask deallocOsPages(c.region) @@ -154,7 +154,7 @@ proc rawSend(q: PRawChannel, data: pointer, typ: PNimType) = ## adds an `item` to the end of the queue `q`. var cap = q.mask+1 if q.count >= cap: - # start with capicity for 2 entries in the queue: + # start with capacity for 2 entries in the queue: if cap == 0: cap = 1 var n = cast[pbytes](Alloc0(q.region, cap*2*typ.size)) var z = 0 @@ -175,7 +175,7 @@ proc rawSend(q: PRawChannel, data: pointer, typ: PNimType) = q.wr = (q.wr + 1) and q.mask proc rawRecv(q: PRawChannel, data: pointer, typ: PNimType) = - assert q.count > 0 + sysAssert q.count > 0, "rawRecv" dec q.count storeAux(data, addr(q.data[q.rd * typ.size]), typ, q, mLoad) q.rd = (q.rd + 1) and q.mask diff --git a/lib/system/ecmasys.nim b/lib/system/ecmasys.nim index 9951811d1..bf8a01ecf 100755 --- a/lib/system/ecmasys.nim +++ b/lib/system/ecmasys.nim @@ -410,7 +410,7 @@ proc NimCopy(x: pointer, ti: PNimType): pointer {.compilerproc.} proc NimCopyAux(dest, src: Pointer, n: ptr TNimNode) {.exportc.} = case n.kind - of nkNone: sysAssert(false) + of nkNone: sysAssert(false, "NimCopyAux") of nkSlot: asm "`dest`[`n`.offset] = NimCopy(`src`[`n`.offset], `n`.typ);" of nkList: |