diff options
Diffstat (limited to 'lib/system.nim')
-rw-r--r-- | lib/system.nim | 256 |
1 files changed, 182 insertions, 74 deletions
diff --git a/lib/system.nim b/lib/system.nim index 4d8610737..b8aa170ea 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -105,12 +105,14 @@ type ## type class matching all ordinal types; however this includes enums with ## holes. - SomeReal* = float|float32|float64 + SomeFloat* = float|float32|float64 ## type class matching all floating point number types - SomeNumber* = SomeInteger|SomeReal + SomeNumber* = SomeInteger|SomeFloat ## type class matching all number types +{.deprecated: [SomeReal: SomeFloat].} + proc defined*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.} ## Special compile-time procedure that checks whether `x` is ## defined. @@ -128,7 +130,7 @@ when defined(nimalias): TSignedInt: SomeSignedInt, TUnsignedInt: SomeUnsignedInt, TInteger: SomeInteger, - TReal: SomeReal, + TReal: SomeFloat, TNumber: SomeNumber, TOrdinal: SomeOrdinal].} @@ -144,7 +146,7 @@ proc declared*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.} ## # missing it. when defined(useNimRtl): - {.deadCodeElim: on.} + {.deadCodeElim: on.} # dce option deprecated proc definedInScope*(x: untyped): bool {. magic: "DefinedInScope", noSideEffect, deprecated, compileTime.} @@ -249,6 +251,10 @@ type when defined(nimHasOpt): type opt*{.magic: "Opt".}[T] +when defined(nimNewRuntime): + type sink*{.magic: "BuiltinType".}[T] + type lent*{.magic: "BuiltinType".}[T] + proc high*[T: Ordinal](x: T): T {.magic: "High", noSideEffect.} ## returns the highest possible index of an array, a sequence, a string or ## the highest possible value of an ordinal value `x`. As a special @@ -314,7 +320,7 @@ type Slice*[T] = HSlice[T, T] ## an alias for ``HSlice[T, T]`` proc `..`*[T, U](a: T, b: U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDot".} = - ## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a` + ## binary `slice`:idx: operator that constructs an interval ``[a, b]``, both `a` ## and `b` are inclusive. Slices can also be used in the set constructor ## and in ordinal case statements, but then they are special-cased by the ## compiler. @@ -322,7 +328,7 @@ proc `..`*[T, U](a: T, b: U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDo result.b = b proc `..`*[T](b: T): HSlice[int, T] {.noSideEffect, inline, magic: "DotDot".} = - ## `slice`:idx: operator that constructs an interval ``[default(int), b]`` + ## unary `slice`:idx: operator that constructs an interval ``[default(int), b]`` result.b = b when not defined(niminheritable): @@ -479,6 +485,7 @@ type trace: string else: trace: seq[StackTraceEntry] + raise_id: uint # set when exception is raised up: ref Exception # used for stacking exceptions. Not exported! SystemError* = object of Exception ## \ @@ -613,6 +620,11 @@ type ## ## This is only raised if the ``segfaults.nim`` module was imported! +when defined(nimNewRuntime): + type + MoveError* = object of SystemError ## \ + ## Raised on attempts to re-sink an already consumed ``sink`` parameter. + {.deprecated: [TObject: RootObj, PObject: RootRef, TEffect: RootEffect, FTime: TimeEffect, FIO: IOEffect, FReadIO: ReadIOEffect, FWriteIO: WriteIOEffect, FExecIO: ExecIOEffect, @@ -639,6 +651,11 @@ type ESynch: Exception ].} +when defined(js) or defined(nimdoc): + type + JsRoot* = ref object of RootObj + ## Root type of the JavaScript object hierarchy + proc unsafeNew*[T](a: var ref T, size: Natural) {.magic: "New", noSideEffect.} ## creates a new object of type ``T`` and returns a safe (traced) ## reference to it in ``a``. This is **unsafe** as it allocates an object @@ -673,12 +690,12 @@ proc `<`*[T](x: Ordinal[T]): T {.magic: "UnaryLt", noSideEffect, deprecated.} ## write ``0 ..< 10`` instead of ``0 .. < 10`` (look at the spacing). ## For ``<x`` write ``pred(x)``. -proc succ*[T](x: Ordinal[T], y = 1): T {.magic: "Succ", noSideEffect.} +proc succ*[T: Ordinal](x: T, y = 1): T {.magic: "Succ", noSideEffect.} ## returns the ``y``-th successor of the value ``x``. ``T`` has to be ## an ordinal type. If such a value does not exist, ``EOutOfRange`` is raised ## or a compile time error occurs. -proc pred*[T](x: Ordinal[T], y = 1): T {.magic: "Pred", noSideEffect.} +proc pred*[T: Ordinal](x: T, y = 1): T {.magic: "Pred", noSideEffect.} ## returns the ``y``-th predecessor of the value ``x``. ``T`` has to be ## an ordinal type. If such a value does not exist, ``EOutOfRange`` is raised ## or a compile time error occurs. @@ -1263,15 +1280,13 @@ proc setLen*[T](s: var seq[T], newlen: Natural) {. ## sets the length of `s` to `newlen`. ## ``T`` may be any sequence type. ## If the current length is greater than the new length, - ## ``s`` will be truncated. `s` cannot be nil! To initialize a sequence with - ## a size, use ``newSeq`` instead. + ## ``s`` will be truncated. proc setLen*(s: var string, newlen: Natural) {. magic: "SetLengthStr", noSideEffect.} ## sets the length of `s` to `newlen`. ## If the current length is greater than the new length, - ## ``s`` will be truncated. `s` cannot be nil! To initialize a string with - ## a size, use ``newString`` instead. + ## ``s`` will be truncated. ## ## .. code-block:: Nim ## var myS = "Nim is great!!" @@ -1368,7 +1383,8 @@ const hostCPU* {.magic: "HostCPU".}: string = "" ## a string that describes the host CPU. Possible values: ## "i386", "alpha", "powerpc", "powerpc64", "powerpc64el", "sparc", - ## "amd64", "mips", "mipsel", "arm", "arm64", "mips64", "mips64el". + ## "amd64", "mips", "mipsel", "arm", "arm64", "mips64", "mips64el", + ## "riscv64". seqShallowFlag = low(int) strlitFlag = 1 shl (sizeof(int)*8 - 2) # later versions of the codegen \ @@ -1457,9 +1473,9 @@ when defined(nodejs): programResult = 0 else: var programResult* {.exportc: "nim_program_result".}: int - ## modify this variable to specify the exit code of the program - ## under normal circumstances. When the program is terminated - ## prematurely using ``quit``, this value is ignored. + ## modify this variable to specify the exit code of the program + ## under normal circumstances. When the program is terminated + ## prematurely using ``quit``, this value is ignored. when defined(nimdoc): proc quit*(errorcode: int = QuitSuccess) {.magic: "Exit", noreturn.} @@ -1507,7 +1523,6 @@ const hasAlloc = (hostOS != "standalone" or not defined(nogc)) and not defined(n when not defined(JS) and not defined(nimscript) and hostOS != "standalone": include "system/cgprocs" when not defined(JS) and not defined(nimscript) and hasAlloc: - proc setStackBottom(theStackBottom: pointer) {.compilerRtl, noinline, benign.} proc addChar(s: NimString, c: char): NimString {.compilerProc, benign.} proc add *[T](x: var seq[T], y: T) {.magic: "AppendSeqElem", noSideEffect.} @@ -1770,7 +1785,7 @@ when not defined(nimscript): proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline, benign.} = ## allocates a new memory block with at least ``T.sizeof * size`` ## bytes. The block has to be freed with ``resize(block, 0)`` or - ## ``free(block)``. The block is not initialized, so reading + ## ``dealloc(block)``. The block is not initialized, so reading ## from it before writing to it is undefined behaviour! ## The allocated memory belongs to its allocating thread! ## Use `createSharedU` to allocate from a shared heap. @@ -1785,7 +1800,7 @@ when not defined(nimscript): proc create*(T: typedesc, size = 1.Positive): ptr T {.inline, benign.} = ## allocates a new memory block with at least ``T.sizeof * size`` ## bytes. The block has to be freed with ``resize(block, 0)`` or - ## ``free(block)``. The block is initialized with all bytes + ## ``dealloc(block)``. The block is initialized with all bytes ## containing zero, so it is somewhat safer than ``createU``. ## The allocated memory belongs to its allocating thread! ## Use `createShared` to allocate from a shared heap. @@ -1803,7 +1818,7 @@ when not defined(nimscript): ## grows or shrinks a given memory block. If p is **nil** then a new ## memory block is returned. In either way the block has at least ## ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is not - ## **nil** ``resize`` calls ``free(p)``. In other cases the block + ## **nil** ``resize`` calls ``dealloc(p)``. In other cases the block ## has to be freed with ``free``. The allocated memory belongs to ## its allocating thread! ## Use `resizeShared` to reallocate from a shared heap. @@ -1913,7 +1928,7 @@ proc `$` *(x: float): string {.magic: "FloatToStr", noSideEffect.} proc `$` *(x: bool): string {.magic: "BoolToStr", noSideEffect.} ## The stringify operator for a boolean argument. Returns `x` ## converted to the string "false" or "true". -# + proc `$` *(x: char): string {.magic: "CharToStr", noSideEffect.} ## The stringify operator for a character argument. Returns `x` ## converted to a string. @@ -1951,13 +1966,13 @@ const ## that you cannot compare a floating point value to this value ## and expect a reasonable result - use the `classify` procedure ## in the module ``math`` for checking for NaN. - NimMajor*: int = 0 + NimMajor* {.intdefine.}: int = 0 ## is the major number of Nim's version. - NimMinor*: int = 17 + NimMinor* {.intdefine.}: int = 18 ## is the minor number of Nim's version. - NimPatch*: int = 3 + NimPatch* {.intdefine.}: int = 1 ## is the patch number of Nim's version. NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch @@ -2001,7 +2016,13 @@ iterator countdown*[T](a, b: T, step = 1): T {.inline.} = ## step count. `T` may be any ordinal type, `step` may only ## be positive. **Note**: This fails to count to ``low(int)`` if T = int for ## efficiency reasons. - when T is IntLikeForCount: + when T is (uint|uint64): + var res = a + while res >= b: + yield res + if res == b: break + dec(res, step) + elif T is IntLikeForCount: var res = int(a) while res >= int(b): yield T(res) @@ -2149,8 +2170,8 @@ proc max*[T](x, y: T): T = if y <= x: x else: y {.pop.} -proc high*(T: typedesc[SomeReal]): T = Inf -proc low*(T: typedesc[SomeReal]): T = NegInf +proc high*(T: typedesc[SomeFloat]): T = Inf +proc low*(T: typedesc[SomeFloat]): T = NegInf proc clamp*[T](x, a, b: T): T = ## limits the value ``x`` within the interval [a, b] @@ -2380,7 +2401,7 @@ proc `==` *[T](x, y: seq[T]): bool {.noSideEffect.} = if x.isNil and y.isNil: return true else: - when not defined(JS) or defined(nimphp): + when not defined(JS): proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} = result = cast[pointer](x) else: @@ -2390,8 +2411,9 @@ proc `==` *[T](x, y: seq[T]): bool {.noSideEffect.} = if seqToPtr(x) == seqToPtr(y): return true - if x.isNil or y.isNil: - return false + when not defined(nimNoNil): + if x.isNil or y.isNil: + return false if x.len != y.len: return false @@ -2616,6 +2638,11 @@ when not defined(nimscript) and hasAlloc: proc GC_unref*(x: string) {.magic: "GCunref", benign.} ## see the documentation of `GC_ref`. + when not defined(JS) and not defined(nimscript) and hasAlloc: + proc nimGC_setStackBottom*(theStackBottom: pointer) {.compilerRtl, noinline, benign.} + ## Expands operating GC stack range to `theStackBottom`. Does nothing + ## if current stack bottom is already lower than `theStackBottom`. + else: template GC_disable* = {.warning: "GC_disable is a no-op in JavaScript".} @@ -2742,17 +2769,14 @@ type when defined(JS): proc add*(x: var string, y: cstring) {.asmNoStackFrame.} = - when defined(nimphp): - asm """`x` .= `y`;""" - else: - asm """ - var len = `x`[0].length-1; - for (var i = 0; i < `y`.length; ++i) { - `x`[0][len] = `y`.charCodeAt(i); - ++len; - } - `x`[0][len] = 0 - """ + asm """ + var len = `x`[0].length-1; + for (var i = 0; i < `y`.length; ++i) { + `x`[0][len] = `y`.charCodeAt(i); + ++len; + } + `x`[0][len] = 0 + """ proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".} elif hasAlloc: @@ -2878,16 +2902,16 @@ when not defined(JS): #and not defined(nimscript): # WARNING: This is very fragile! An array size of 8 does not work on my # Linux 64bit system. -- That's because the stack direction is the other # way round. - when declared(setStackBottom): + when declared(nimGC_setStackBottom): var locals {.volatile.}: pointer locals = addr(locals) - setStackBottom(locals) + nimGC_setStackBottom(locals) proc initStackBottomWith(locals: pointer) {.inline, compilerproc.} = # We need to keep initStackBottom around for now to avoid # bootstrapping problems. - when declared(setStackBottom): - setStackBottom(locals) + when declared(nimGC_setStackBottom): + nimGC_setStackBottom(locals) {.push profiler: off.} var @@ -3021,9 +3045,9 @@ when not defined(JS): #and not defined(nimscript): proc endOfFile*(f: File): bool {.tags: [], benign.} ## Returns true iff `f` is at the end. - proc readChar*(f: File): char {.tags: [ReadIOEffect], deprecated.} - ## Reads a single character from the stream `f`. **Deprecated** since - ## version 0.16.2. Use some variant of ``readBuffer`` instead. + proc readChar*(f: File): char {.tags: [ReadIOEffect].} + ## Reads a single character from the stream `f`. Should not be used in + ## performance sensitive code. proc flushFile*(f: File) {.tags: [WriteIOEffect].} ## Flushes `f`'s buffer. @@ -3203,13 +3227,14 @@ when not defined(JS): #and not defined(nimscript): when declared(initGC): initGC() when not defined(nimscript): - proc setControlCHook*(hook: proc () {.noconv.} not nil) + proc setControlCHook*(hook: proc () {.noconv.}) ## allows you to override the behaviour of your application when CTRL+C ## is pressed. Only one such hook is supported. - proc writeStackTrace*() {.tags: [WriteIOEffect], gcsafe.} + proc writeStackTrace*() {.tags: [], gcsafe.} ## writes the current stack trace to ``stderr``. This is only works - ## for debug builds. + ## for debug builds. Since it's usually used for debugging, this + ## is proclaimed to have no IO effect! when hostOS != "standalone": proc getStackTrace*(): string {.gcsafe.} ## gets the current stack trace. This only works for debug builds. @@ -3417,9 +3442,17 @@ elif defined(JS): when defined(nimffi): include "system/sysio" +when not defined(nimNoArrayToString): + proc `$`*[T, IDX](x: array[IDX, T]): string = + ## generic ``$`` operator for arrays that is lifted from the components + collectionToString(x, "[", ", ", "]") -proc `$`*[T, IDX](x: array[IDX, T]): string = - ## generic ``$`` operator for arrays that is lifted from the components +proc `$`*[T](x: openarray[T]): string = + ## generic ``$`` operator for openarrays that is lifted from the components + ## of `x`. Example: + ## + ## .. code-block:: nim + ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]" collectionToString(x, "[", ", ", "]") proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} = @@ -3500,8 +3533,8 @@ template `..^`*(a, b: untyped): untyped = a .. ^b template `..<`*(a, b: untyped): untyped = - ## a shortcut for 'a..pred(b)'. - a .. pred(b) + ## a shortcut for 'a .. (when b is BackwardsIndex: succ(b) else: pred(b))'. + a .. (when b is BackwardsIndex: succ(b) else: pred(b)) when defined(nimNewRoof): iterator `..<`*[T](a, b: T): T = @@ -3719,7 +3752,7 @@ proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.} ## for debugging. proc instantiationInfo*(index = -1, fullPaths = false): tuple[ - filename: string, line: int] {. magic: "InstantiationInfo", noSideEffect.} + filename: string, line: int, column: int] {. magic: "InstantiationInfo", noSideEffect.} ## provides access to the compiler's instantiation stack line information ## of a template. ## @@ -3764,7 +3797,6 @@ proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} = # by ``assert``. type Hide = proc (msg: string) {.noinline, raises: [], noSideEffect, tags: [].} - {.deprecated: [THide: Hide].} Hide(raiseAssert)(msg) template assert*(cond: bool, msg = "") = @@ -3788,7 +3820,9 @@ template doAssert*(cond: bool, msg = "") = bind instantiationInfo {.line: instantiationInfo().}: if not cond: - raiseAssert(astToStr(cond) & ' ' & msg) + raiseAssert(astToStr(cond) & ' ' & + instantiationInfo(-1, false).fileName & '(' & + $instantiationInfo(-1, false).line & ") " & msg) iterator items*[T](a: seq[T]): T {.inline.} = ## iterates over each item of `a`. @@ -3912,29 +3946,48 @@ proc addEscapedChar*(s: var string, c: char) {.noSideEffect, inline.} = ## * replaces any ``\`` by ``\\`` ## * replaces any ``'`` by ``\'`` ## * replaces any ``"`` by ``\"`` - ## * replaces any other character in the set ``{'\0'..'\31', '\127'..'\255'}`` + ## * replaces any ``\a`` by ``\\a`` + ## * replaces any ``\b`` by ``\\b`` + ## * replaces any ``\t`` by ``\\t`` + ## * replaces any ``\n`` by ``\\n`` + ## * replaces any ``\v`` by ``\\v`` + ## * replaces any ``\f`` by ``\\f`` + ## * replaces any ``\c`` by ``\\c`` + ## * replaces any ``\e`` by ``\\e`` + ## * replaces any other character not in the set ``{'\21..'\126'} ## by ``\xHH`` where ``HH`` is its hexadecimal value. ## ## The procedure has been designed so that its output is usable for many ## different common syntaxes. ## **Note**: This is not correct for producing Ansi C code! case c - of '\0'..'\31', '\127'..'\255': - add(s, "\\x") + of '\a': s.add "\\a" # \x07 + of '\b': s.add "\\b" # \x08 + of '\t': s.add "\\t" # \x09 + of '\n': s.add "\\n" # \x0A + of '\v': s.add "\\v" # \x0B + of '\f': s.add "\\f" # \x0C + of '\c': s.add "\\c" # \x0D + of '\e': s.add "\\e" # \x1B + of '\\': s.add("\\\\") + of '\'': s.add("\\'") + of '\"': s.add("\\\"") + of {'\32'..'\126'} - {'\\', '\'', '\"'}: s.add(c) + else: + s.add("\\x") const HexChars = "0123456789ABCDEF" let n = ord(c) s.add(HexChars[int((n and 0xF0) shr 4)]) s.add(HexChars[int(n and 0xF)]) - of '\\': add(s, "\\\\") - of '\'': add(s, "\\'") - of '\"': add(s, "\\\"") - else: add(s, c) proc addQuoted*[T](s: var string, x: T) = ## Appends `x` to string `s` in place, applying quoting and escaping ## if `x` is a string or char. See ## `addEscapedChar <system.html#addEscapedChar>`_ - ## for the escaping scheme. + ## for the escaping scheme. When `x` is a string, characters in the + ## range ``{\128..\255}`` are never escaped so that multibyte UTF-8 + ## characters are untouched (note that this behavior is different from + ## ``addEscapedChar``). ## ## The Nim standard library uses this function on the elements of ## collections when producing a string representation of a collection. @@ -3953,7 +4006,12 @@ proc addQuoted*[T](s: var string, x: T) = when T is string: s.add("\"") for c in x: - s.addEscapedChar(c) + # Only ASCII chars are escaped to avoid butchering + # multibyte UTF-8 characters. + if c <= 127.char: + s.addEscapedChar(c) + else: + s.add c s.add("\"") elif T is char: s.add("'") @@ -3967,18 +4025,18 @@ proc addQuoted*[T](s: var string, x: T) = when hasAlloc: # XXX: make these the default (or implement the NilObject optimization) - proc safeAdd*[T](x: var seq[T], y: T) {.noSideEffect.} = + proc safeAdd*[T](x: var seq[T], y: T) {.noSideEffect, deprecated.} = ## Adds ``y`` to ``x`` unless ``x`` is not yet initialized; in that case, ## ``x`` becomes ``@[y]`` if x == nil: x = @[y] else: x.add(y) - proc safeAdd*(x: var string, y: char) = + proc safeAdd*(x: var string, y: char) {.noSideEffect, deprecated.} = ## Adds ``y`` to ``x``. If ``x`` is ``nil`` it is initialized to ``""`` if x == nil: x = "" x.add(y) - proc safeAdd*(x: var string, y: string) = + proc safeAdd*(x: var string, y: string) {.noSideEffect, deprecated.} = ## Adds ``y`` to ``x`` unless ``x`` is not yet initalized; in that ## case, ``x`` becomes ``y`` if x == nil: x = y @@ -4063,6 +4121,25 @@ template closureScope*(body: untyped): untyped = ## myClosure() # outputs 3 (proc() = body)() +template once*(body: untyped): untyped = + ## Executes a block of code only once (the first time the block is reached). + ## When hot code reloading is enabled, protects top-level code from being + ## re-executed on each module reload. + ## + ## .. code-block:: nim + ## proc draw(t: Triangle) = + ## once: + ## graphicsInit() + ## + ## line(t.p1, t.p2) + ## line(t.p2, t.p3) + ## line(t.p3, t.p1) + ## + var alreadyExecuted {.global.} = false + if not alreadyExecuted: + alreadyExecuted = true + body + {.pop.} #{.push warning[GcMem]: off, warning[Uninit]: off.} when defined(nimconfig): @@ -4096,14 +4173,45 @@ template doAssertRaises*(exception, code: untyped): typed = runnableExamples: doAssertRaises(ValueError): raise newException(ValueError, "Hello World") - + var wrong = false try: - block: - code - raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code)) + code + wrong = true except exception: discard except Exception as exc: raiseAssert(astToStr(exception) & " wasn't raised, another error was raised instead by:\n"& astToStr(code)) + if wrong: + raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code)) + +when defined(cpp) and appType != "lib" and + not defined(js) and not defined(nimscript) and + hostOS != "standalone" and not defined(noCppExceptions): + proc setTerminate(handler: proc() {.noconv.}) + {.importc: "std::set_terminate", header: "<exception>".} + setTerminate proc() {.noconv.} = + # Remove ourself as a handler, reinstalling the default handler. + setTerminate(nil) + + let ex = getCurrentException() + let trace = ex.getStackTrace() + stderr.write trace & "Error: unhandled exception: " & ex.msg & + " [" & $ex.name & "]\n" + quit 1 + +when not defined(js): + proc toOpenArray*[T](x: seq[T]; first, last: int): openarray[T] {. + magic: "Slice".} + proc toOpenArray*[T](x: openarray[T]; first, last: int): openarray[T] {. + magic: "Slice".} + proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openarray[T] {. + magic: "Slice".} + proc toOpenArray*(x: string; first, last: int): openarray[char] {. + magic: "Slice".} + + +type + ForLoopStmt* {.compilerProc.} = object ## special type that marks a macro + ## as a `for-loop macro`:idx: |