Skip to content

Commit

Permalink
refs nim-lang#16521 import fatal; add tstandalone
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Dec 31, 2020
1 parent 876fa3e commit f7af90d
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 81 deletions.
67 changes: 67 additions & 0 deletions lib/std/private/fatal.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
#
# Nim's Runtime Library
# (c) Copyright 2019 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#

{.push profiler: off.}

# xxx move to excpt.nim where it's used
when defined(nimHasExceptionsQuery):
const gotoBasedExceptions* = compileOption("exceptions", "goto")
else:
const gotoBasedExceptions* = false

when hostOS == "standalone":
proc name(t: typedesc): string {.magic: "TypeTrait".}

type PanicCallback* = proc(exceptionName: string, message: string, arg: string)
# xxx: {.noconv.} ?

var panicCallback: PanicCallback

when not declared(setPanicCallback):
# because fatal.nim is included twice; xxx: replace include with import
proc setPanicCallback*(a: PanicCallback) =
## this must be called at least once and is used by `sysFatal`; not thread safe.
panicCallback = a

proc sysFatal*(exceptn: typedesc, message: string) {.inline.} =
# assumes `panicCallback != nil`
panicCallback(name(exceptn), message, "")

proc sysFatal*(exceptn: typedesc, message, arg: string) {.inline.} =
# assumes `panicCallback != nil`
panicCallback(name(exceptn), message, arg)

elif (defined(nimQuirky) or defined(nimPanics)) and not defined(nimscript):
import ansi_c

proc name(t: typedesc): string {.magic: "TypeTrait".}

proc sysFatal*(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
writeStackTrace()
var buf = newStringOfCap(200)
add(buf, "Error: unhandled exception: ")
add(buf, message)
add(buf, arg)
add(buf, " [")
add(buf, name exceptn)
add(buf, "]\n")
cstderr.rawWrite buf
quit 1

proc sysFatal*(exceptn: typedesc, message: string) {.inline, noreturn.} =
sysFatal(exceptn, message, "")

else:
proc sysFatal*(exceptn: typedesc, message: string) {.inline, noreturn.} =
raise (ref exceptn)(msg: message)

proc sysFatal*(exceptn: typedesc, message, arg: string) {.inline, noreturn.} =
raise (ref exceptn)(msg: message & arg)

{.pop.}
6 changes: 4 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,10 @@ when not defined(nimscript):
## for debug builds. Since it's usually used for debugging, this
## is proclaimed to have no IO effect!

when not declared(sysFatal):
include "system/fatal"
# when not declared(sysFatal):
# include "system/fatal"
import std/private/fatal
# from std/private/fatal import sysFatal

when not defined(nimscript):
{.push stackTrace: off, profiler: off.}
Expand Down
5 changes: 3 additions & 2 deletions lib/system/assertions.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
when not declared(sysFatal):
include "system/fatal"
# when not declared(sysFatal):
# include "system/fatal"

from std/private/fatal import sysFatal
import std/private/miscdollars
# ---------------------------------------------------------------------------
# helpers
Expand Down
54 changes: 0 additions & 54 deletions lib/system/fatal.nim

This file was deleted.

15 changes: 13 additions & 2 deletions tests/manyloc/standalone/barebone.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
discard """
ccodecheck: "\\i !@('systemInit')"
ccodecheck: "\\i !@('systemDatInit')"
output: "hello"
matrix: "--os:standalone --gc:none"
output: "hi 4778"
"""
# bug #2041: Macros need to be available for os:standalone!
import macros

proc printf(frmt: cstring) {.varargs, header: "<stdio.h>", cdecl.}
proc printf(frmt: cstring) {.varargs, importc, header: "<stdio.h>", cdecl.}
proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}

{.push stack_trace: off, profiler:off.}
proc panicCallback(exceptionName: string, message: string, arg: string) {.noreturn.} =
printf("panic: exception: %s, message: %s, arg: %s\n", exceptionName, message, arg)
exit(1)
{.pop.}

setPanicCallback(panicCallback)

var x = 0
inc x
printf("hi %ld\n", x+4777)

proc substr(a: string): string = a[0 .. 3] # This should compile. See #9762
const a = substr("foobar")
# doAssert false
2 changes: 0 additions & 2 deletions tests/manyloc/standalone/barebone.nim.cfg

This file was deleted.

19 changes: 0 additions & 19 deletions tests/manyloc/standalone/panicoverride.nim

This file was deleted.

36 changes: 36 additions & 0 deletions tests/misc/tstandalone.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
discard """
ccodecheck: "\\i !@('systemInit')"
ccodecheck: "\\i !@('systemDatInit')"
exitcode: 1
output: '''
hi 4778
panic: exception: AssertionDefect, message: tstandalone.nim(36, 10) `false` foo, arg:
'''
matrix: "--os:standalone --gc:none"
"""

# issue #2041: Macros need to be available for os:standalone!
import macros

from std/private/fatal import setPanicCallback

proc printf(frmt: cstring) {.varargs, importc, header: "<stdio.h>", cdecl.}
proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}

{.push stack_trace: off, profiler:off.}
proc panicCallback(exceptionName: string, message: string, arg: string) {.noreturn.} =
printf("panic: exception: %s, message: %s, arg: %s\n", exceptionName, message, arg)
exit(1)
{.pop.}

setPanicCallback(panicCallback)

var x = 0
inc x
printf("hi %ld\n", x+4777)

proc substr(a: string): string = a[0 .. 3] # This should compile. See #9762
const a = substr("foobar")

## line 35
doAssert false, "foo"

0 comments on commit f7af90d

Please sign in to comment.