added a bunch of hostOS standalone conditions

This commit is contained in:
Simon Hafner 2013-06-30 16:41:21 -05:00
commit 36b00a107c
3 changed files with 826 additions and 787 deletions

View file

@ -111,6 +111,7 @@ proc `or`*(x, y: bool): bool {.magic: "Or", noSideEffect.}
proc `xor`*(x, y: bool): bool {.magic: "Xor", noSideEffect.} proc `xor`*(x, y: bool): bool {.magic: "Xor", noSideEffect.}
## Boolean `exclusive or`; returns true iff ``x != y``. ## Boolean `exclusive or`; returns true iff ``x != y``.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc new*[T](a: var ref T) {.magic: "New", noSideEffect.} proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced) ## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``. ## reference to it in ``a``.
@ -164,9 +165,12 @@ type
## pointer to the array data and a ## pointer to the array data and a
## length field. ## length field.
varargs*{.magic: "Varargs".}[T] ## Generic type to construct a varargs type. varargs*{.magic: "Varargs".}[T] ## Generic type to construct a varargs type.
seq*{.magic: "Seq".}[T] ## Generic type to construct sequences.
set*{.magic: "Set".}[T] ## Generic type to construct bit sets. set*{.magic: "Set".}[T] ## Generic type to construct bit sets.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
type
seq*{.magic: "Seq".}[T] ## Generic type to construct sequences.
type type
TSlice* {.final, pure.}[T] = object ## builtin slice type TSlice* {.final, pure.}[T] = object ## builtin slice type
a*, b*: T ## the bounds a*, b*: T ## the bounds
@ -187,6 +191,7 @@ when not defined(niminheritable):
{.pragma: inheritable.} {.pragma: inheritable.}
when not defined(JS) and not defined(NimrodVM): when not defined(JS) and not defined(NimrodVM):
when not defined(noDynamicAlloc):
type type
TGenericSeq {.compilerproc, pure, inheritable.} = object TGenericSeq {.compilerproc, pure, inheritable.} = object
len, reserved: int len, reserved: int
@ -369,6 +374,7 @@ proc dec*[T](x: var ordinal[T], y = 1) {.magic: "Dec", noSideEffect.}
## exist, ``EOutOfRange`` is raised or a compile time error occurs. This is a ## exist, ``EOutOfRange`` is raised or a compile time error occurs. This is a
## short notation for: ``x = pred(x, y)``. ## short notation for: ``x = pred(x, y)``.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.} proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.}
## creates a new sequence of type ``seq[T]`` with length ``len``. ## creates a new sequence of type ``seq[T]`` with length ``len``.
## This is equivalent to ``s = @[]; setlen(s, len)``, but more ## This is equivalent to ``s = @[]; setlen(s, len)``, but more
@ -408,6 +414,7 @@ proc len*[TOpenArray: openArray|varargs](x: TOpenArray): int {.
proc len*(x: string): int {.magic: "LengthStr", noSideEffect.} proc len*(x: string): int {.magic: "LengthStr", noSideEffect.}
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.} proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
proc len*[I, T](x: array[I, T]): int {.magic: "LengthArray", noSideEffect.} proc len*[I, T](x: array[I, T]): int {.magic: "LengthArray", noSideEffect.}
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc len*[T](x: seq[T]): int {.magic: "LengthSeq", noSideEffect.} proc len*[T](x: seq[T]): int {.magic: "LengthSeq", noSideEffect.}
## returns the length of an array, an openarray, a sequence or a string. ## returns the length of an array, an openarray, a sequence or a string.
## This is rougly the same as ``high(T)-low(T)+1``, but its resulting type is ## This is rougly the same as ``high(T)-low(T)+1``, but its resulting type is
@ -747,6 +754,7 @@ proc cmp*[T](x, y: T): int {.procvar.} =
proc cmp*(x, y: string): int {.noSideEffect, procvar.} proc cmp*(x, y: string): int {.noSideEffect, procvar.}
## Compare proc for strings. More efficient than the generic version. ## Compare proc for strings. More efficient than the generic version.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc `@` * [IDX, T](a: array[IDX, T]): seq[T] {. proc `@` * [IDX, T](a: array[IDX, T]): seq[T] {.
magic: "ArrToSeq", nosideeffect.} magic: "ArrToSeq", nosideeffect.}
## turns an array into a sequence. This most often useful for constructing ## turns an array into a sequence. This most often useful for constructing
@ -936,6 +944,7 @@ include "system/inclrtl"
when not defined(JS) and not defined(nimrodVm) and hostOS != "standalone": when not defined(JS) and not defined(nimrodVm) and hostOS != "standalone":
include "system/cgprocs" include "system/cgprocs"
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc add *[T](x: var seq[T], y: T) {.magic: "AppendSeqElem", noSideEffect.} proc add *[T](x: var seq[T], y: T) {.magic: "AppendSeqElem", noSideEffect.}
proc add *[T](x: var seq[T], y: openArray[T]) {.noSideEffect.} = proc add *[T](x: var seq[T], y: openArray[T]) {.noSideEffect.} =
## Generic proc for adding a data item `y` to a container `x`. ## Generic proc for adding a data item `y` to a container `x`.
@ -1078,6 +1087,7 @@ proc addQuitProc*(QuitProc: proc() {.noconv.}) {.importc: "atexit", nodecl.}
# In case of an unhandled exeption the exit handlers should # In case of an unhandled exeption the exit handlers should
# not be called explicitly! The user may decide to do this manually though. # not be called explicitly! The user may decide to do this manually though.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc copy*(s: string, first = 0): string {. proc copy*(s: string, first = 0): string {.
magic: "CopyStr", importc: "copyStr", noSideEffect, deprecated.} magic: "CopyStr", importc: "copyStr", noSideEffect, deprecated.}
proc copy*(s: string, first, last: int): string {. proc copy*(s: string, first, last: int): string {.
@ -1126,6 +1136,7 @@ when not defined(nimrodVM):
## otherwise. Like any procedure dealing with raw memory this is ## otherwise. Like any procedure dealing with raw memory this is
## *unsafe*. ## *unsafe*.
when not defined(noDynamicAlloc):
proc alloc*(size: int): pointer {.noconv, rtl, tags: [].} proc alloc*(size: int): pointer {.noconv, rtl, tags: [].}
## allocates a new memory block with at least ``size`` bytes. The ## allocates a new memory block with at least ``size`` bytes. The
## block has to be freed with ``realloc(block, 0)`` or ## block has to be freed with ``realloc(block, 0)`` or
@ -1194,6 +1205,7 @@ template `>%` *(x, y: expr): expr {.immediate.} = y <% x
## treats `x` and `y` as unsigned and compares them. ## treats `x` and `y` as unsigned and compares them.
## Returns true iff ``unsigned(x) > unsigned(y)``. ## Returns true iff ``unsigned(x) > unsigned(y)``.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc `$` *(x: int): string {.magic: "IntToStr", noSideEffect.} proc `$` *(x: int): string {.magic: "IntToStr", noSideEffect.}
## The stingify operator for an integer argument. Returns `x` ## The stingify operator for an integer argument. Returns `x`
## converted to a decimal string. ## converted to a decimal string.
@ -1255,8 +1267,7 @@ const
## in the module ``math`` for checking for NaN. ## in the module ``math`` for checking for NaN.
# GC interface: # GC interface:
when not defined(noDynamicAlloc) and not defined(nimrodVM):
when not defined(nimrodVM):
proc getOccupiedMem*(): int {.rtl.} proc getOccupiedMem*(): int {.rtl.}
## returns the number of bytes that are owned by the process and hold data. ## returns the number of bytes that are owned by the process and hold data.
@ -1267,7 +1278,6 @@ when not defined(nimrodVM):
proc getTotalMem*(): int {.rtl.} proc getTotalMem*(): int {.rtl.}
## returns the number of bytes that are owned by the process. ## returns the number of bytes that are owned by the process.
iterator countdown*[T](a, b: T, step = 1): T {.inline.} = iterator countdown*[T](a, b: T, step = 1): T {.inline.} =
## Counts from ordinal value `a` down to `b` with the given ## Counts from ordinal value `a` down to `b` with the given
## step count. `T` may be any ordinal type, `step` may only ## step count. `T` may be any ordinal type, `step` may only
@ -1352,6 +1362,7 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
if i >= high(IX): break if i >= high(IX): break
inc(i) inc(i)
when not defined(noDynamicAlloc) and not defined(nimrodVM):
iterator items*[T](a: seq[T]): T {.inline.} = iterator items*[T](a: seq[T]): T {.inline.} =
## iterates over each item of `a`. ## iterates over each item of `a`.
var i = 0 var i = 0
@ -1405,6 +1416,7 @@ iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
if i >= high(IX): break if i >= high(IX): break
inc(i) inc(i)
when not defined(noDynamicAlloc) and not defined(nimrodVM):
iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} = iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs. ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
var i = 0 var i = 0
@ -1419,7 +1431,7 @@ iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
yield (i, a[i]) yield (i, a[i])
inc(i) inc(i)
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil".} proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil".}
proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".} proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".}
proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil".} proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil".}
@ -1430,6 +1442,7 @@ proc isNil*[T: proc](x: T): bool {.noSideEffect, magic: "IsNil".}
## Fast check whether `x` is nil. This is sometimes more efficient than ## Fast check whether `x` is nil. This is sometimes more efficient than
## ``== nil``. ## ``== nil``.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc `@`*[T](a: openArray[T]): seq[T] = proc `@`*[T](a: openArray[T]): seq[T] =
## turns an openarray into a sequence. This is not as efficient as turning ## turns an openarray into a sequence. This is not as efficient as turning
## a fixed length array into a sequence as it always copies every element ## a fixed length array into a sequence as it always copies every element
@ -1437,6 +1450,7 @@ proc `@`*[T](a: openArray[T]): seq[T] =
newSeq(result, a.len) newSeq(result, a.len)
for i in 0..a.len-1: result[i] = a[i] for i in 0..a.len-1: result[i] = a[i]
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc `&` *[T](x, y: seq[T]): seq[T] {.noSideEffect.} = proc `&` *[T](x, y: seq[T]): seq[T] {.noSideEffect.} =
newSeq(result, x.len + y.len) newSeq(result, x.len + y.len)
for i in 0..x.len-1: for i in 0..x.len-1:
@ -1456,7 +1470,7 @@ proc `&` *[T](x: T, y: seq[T]): seq[T] {.noSideEffect.} =
result[i] = y[i] result[i] = y[i]
result[y.len] = x result[y.len] = x
when not defined(NimrodVM): when not defined(noDynamicAlloc) and not defined(nimrodVM):
when not defined(JS): when not defined(JS):
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} = proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
result = cast[pointer](x) result = cast[pointer](x)
@ -1489,6 +1503,7 @@ proc contains*[T](a: openArray[T], item: T): bool {.inline.}=
## for ``find(a, item) >= 0``. ## for ``find(a, item) >= 0``.
return find(a, item) >= 0 return find(a, item) >= 0
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} = proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} =
## returns the last item of `s` and decreases ``s.len`` by one. This treats ## returns the last item of `s` and decreases ``s.len`` by one. This treats
## `s` as a stack and implements the common *pop* operation. ## `s` as a stack and implements the common *pop* operation.
@ -1496,6 +1511,7 @@ proc pop*[T](s: var seq[T]): T {.inline, noSideEffect.} =
result = s[L] result = s[L]
setLen(s, L) setLen(s, L)
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] {. proc each*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] {.
deprecated.} = deprecated.} =
## The well-known ``map`` operation from functional programming. Applies ## The well-known ``map`` operation from functional programming. Applies
@ -1513,6 +1529,7 @@ proc each*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) {.
## **Deprecated since version 0.9:** Use the ``map`` proc instead. ## **Deprecated since version 0.9:** Use the ``map`` proc instead.
for i in 0..data.len-1: op(data[i]) for i in 0..data.len-1: op(data[i])
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc map*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] = proc map*[T, S](data: openArray[T], op: proc (x: T): S {.closure.}): seq[S] =
## Returns a new sequence with the results of `op` applied to every item in ## Returns a new sequence with the results of `op` applied to every item in
## `data`. ## `data`.
@ -1633,8 +1650,7 @@ when false:
result.add("]") result.add("]")
# ----------------- GC interface --------------------------------------------- # ----------------- GC interface ---------------------------------------------
when not defined(noDynamicAlloc) and not defined(nimrodVM):
when not defined(nimrodVM):
proc GC_disable*() {.rtl, inl.} proc GC_disable*() {.rtl, inl.}
## disables the GC. If called n-times, n calls to `GC_enable` are needed to ## disables the GC. If called n-times, n calls to `GC_enable` are needed to
## reactivate the GC. Note that in most circumstances one should only disable ## reactivate the GC. Note that in most circumstances one should only disable
@ -1696,6 +1712,7 @@ const nimrodStackTrace = compileOption("stacktrace")
# however, stack-traces are available for most parts # however, stack-traces are available for most parts
# of the code # of the code
when not defined(noDynamicAlloc) and not defined(nimrodVM):
var var
globalRaiseHook*: proc (e: ref E_Base): bool {.nimcall.} globalRaiseHook*: proc (e: ref E_Base): bool {.nimcall.}
## with this hook you can influence exception handling on a global level. ## with this hook you can influence exception handling on a global level.
@ -1743,6 +1760,7 @@ type
filename*: cstring ## filename of the proc that is currently executing filename*: cstring ## filename of the proc that is currently executing
len*: int ## length of the inspectable slots len*: int ## length of the inspectable slots
when not defined(noDynamicAlloc) and not defined(nimrodVM):
when not defined(JS): when not defined(JS):
{.push stack_trace:off, profiler:off.} {.push stack_trace:off, profiler:off.}
proc add*(x: var string, y: cstring) {.noStackFrame.} = proc add*(x: var string, y: cstring) {.noStackFrame.} =
@ -1779,6 +1797,7 @@ proc debugEcho*[T](x: varargs[T, `$`]) {.magic: "Echo", noSideEffect,
## to be free of side effects, so that it can be used for debugging routines ## to be free of side effects, so that it can be used for debugging routines
## marked as ``noSideEffect``. ## marked as ``noSideEffect``.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
template newException*(exceptn: typeDesc, message: string): expr = template newException*(exceptn: typeDesc, message: string): expr =
## creates an exception object of type ``exceptn`` and sets its ``msg`` field ## creates an exception object of type ``exceptn`` and sets its ``msg`` field
## to `message`. Returns the new exception object. ## to `message`. Returns the new exception object.
@ -1795,7 +1814,9 @@ proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo".}
when not defined(JS): #and not defined(NimrodVM): when not defined(JS): #and not defined(NimrodVM):
{.push stack_trace: off, profiler:off.} {.push stack_trace: off, profiler:off.}
when not defined(NimrodVM): when not defined(nimrodVM):
# TODO: apply boolean logic
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc initGC() proc initGC()
when not defined(boehmgc) and not defined(useMalloc): when not defined(boehmgc) and not defined(useMalloc):
proc initAllocator() {.inline.} proc initAllocator() {.inline.}
@ -1840,6 +1861,7 @@ when not defined(JS): #and not defined(NimrodVM):
when defined(endb): when defined(endb):
proc endbStep() proc endbStep()
when hostOS != "standalone":
# ----------------- IO Part ------------------------------------------------ # ----------------- IO Part ------------------------------------------------
type type
CFile {.importc: "FILE", nodecl, final, incompletestruct.} = object CFile {.importc: "FILE", nodecl, final, incompletestruct.} = object
@ -2010,6 +2032,7 @@ when not defined(JS): #and not defined(NimrodVM):
## returns the OS file handle of the file ``f``. This is only useful for ## returns the OS file handle of the file ``f``. This is only useful for
## platform specific programming. ## platform specific programming.
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc cstringArrayToSeq*(a: cstringArray, len: int): seq[string] = proc cstringArrayToSeq*(a: cstringArray, len: int): seq[string] =
## converts a ``cstringArray`` to a ``seq[string]``. `a` is supposed to be ## converts a ``cstringArray`` to a ``seq[string]``. `a` is supposed to be
## of length ``len``. ## of length ``len``.
@ -2026,6 +2049,7 @@ when not defined(JS): #and not defined(NimrodVM):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
when not defined(NimrodVM): when not defined(NimrodVM):
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc allocCStringArray*(a: openArray[string]): cstringArray = proc allocCStringArray*(a: openArray[string]): cstringArray =
## creates a NULL terminated cstringArray from `a`. The result has to ## creates a NULL terminated cstringArray from `a`. The result has to
## be freed with `deallocCStringArray` after it's not needed anymore. ## be freed with `deallocCStringArray` after it's not needed anymore.
@ -2059,7 +2083,10 @@ when not defined(JS): #and not defined(NimrodVM):
status: int status: int
context: C_JmpBuf context: C_JmpBuf
hasRaiseAction: bool hasRaiseAction: bool
when not defined(noDynamicAlloc) and not defined(nimrodVM):
raiseAction: proc (e: ref E_Base): bool {.closure.} raiseAction: proc (e: ref E_Base): bool {.closure.}
elif defined(noDynamicAlloc):
raiseAction: proc (e: ptr E_Base): bool {.closure.}
when defined(initAllocator): when defined(initAllocator):
initAllocator() initAllocator()
@ -2070,7 +2097,7 @@ when not defined(JS): #and not defined(NimrodVM):
when not defined(useNimRtl) and not defined(createNimRtl): initStackBottom() when not defined(useNimRtl) and not defined(createNimRtl): initStackBottom()
initGC() initGC()
when not defined(NimrodVM): when not defined(NimrodVM) and hostOS != "standalone":
proc setControlCHook*(hook: proc () {.noconv.}) proc setControlCHook*(hook: proc () {.noconv.})
## allows you to override the behaviour of your application when CTRL+C ## allows you to override the behaviour of your application when CTRL+C
## is pressed. Only one such hook is supported. ## is pressed. Only one such hook is supported.
@ -2078,7 +2105,7 @@ when not defined(JS): #and not defined(NimrodVM):
proc writeStackTrace*() {.tags: [FWriteIO].} proc writeStackTrace*() {.tags: [FWriteIO].}
## writes the current stack trace to ``stderr``. This is only works ## writes the current stack trace to ``stderr``. This is only works
## for debug builds. ## for debug builds.
when hostOS != "standalone":
proc getStackTrace*(): string proc getStackTrace*(): string
## gets the current stack trace. This only works for debug builds. ## gets the current stack trace. This only works for debug builds.
@ -2086,6 +2113,7 @@ when not defined(JS): #and not defined(NimrodVM):
## gets the stack trace associated with `e`, which is the stack that ## gets the stack trace associated with `e`, which is the stack that
## lead to the ``raise`` statement. This only works for debug builds. ## lead to the ``raise`` statement. This only works for debug builds.
when not defined(NimrodVM):
{.push stack_trace: off, profiler:off.} {.push stack_trace: off, profiler:off.}
when hostOS == "standalone": when hostOS == "standalone":
include "system/embedded" include "system/embedded"
@ -2126,17 +2154,20 @@ when not defined(JS): #and not defined(NimrodVM):
else: else:
result = n.sons[n.len] result = n.sons[n.len]
when not defined(noDynamicAlloc) and not defined(nimrodVM):
include "system/mmdisp" include "system/mmdisp"
{.push stack_trace: off, profiler:off.} {.push stack_trace: off, profiler:off.}
when hostOS != "standalone": include "system/sysstr" when hostOS != "standalone": include "system/sysstr"
{.pop.} {.pop.}
when not defined(noDynamicAlloc) and not defined(nimrodVM):
include "system/sysio" include "system/sysio"
when hasThreadSupport: when hasThreadSupport:
include "system/channels" include "system/channels"
else: elif not defined(noDynamicAlloc) and not defined(nimrodVM):
include "system/sysio" include "system/sysio"
when not defined(noDynamicAlloc) and not defined(nimrodVM):
iterator lines*(filename: string): TaintedString {.tags: [FReadIO].} = iterator lines*(filename: string): TaintedString {.tags: [FReadIO].} =
## Iterate over any line in the file named `filename`. ## Iterate over any line in the file named `filename`.
## If the file does not exist `EIO` is raised. ## If the file does not exist `EIO` is raised.
@ -2262,6 +2293,7 @@ proc `/`*(x, y: int): float {.inline, noSideEffect.} =
## integer division that results in a float. ## integer division that results in a float.
result = toFloat(x) / toFloat(y) result = toFloat(x) / toFloat(y)
when not defined(noDynamicAlloc) and not defined(nimrodVM):
template `-|`*(b, s: expr): expr = template `-|`*(b, s: expr): expr =
(if b >= 0: b else: s.len + b) (if b >= 0: b else: s.len + b)
@ -2406,6 +2438,7 @@ proc `/=`*[T: float|float32|float64] (x: var T, y: T) {.inline, noSideEffect.} =
## Divides in place a floating point number ## Divides in place a floating point number
x = x / y x = x / y
when not defined(noDynamicAlloc) and not defined(nimrodVM):
proc `&=`* (x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.} proc `&=`* (x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.}
proc rand*(max: int): int {.magic: "Rand", sideEffect.} proc rand*(max: int): int {.magic: "Rand", sideEffect.}

View file

@ -28,11 +28,13 @@ type
C_JmpBuf {.importc: "jmp_buf".} = array[0..31, int] C_JmpBuf {.importc: "jmp_buf".} = array[0..31, int]
when not defined(noIO):
var var
c_stdin {.importc: "stdin", noDecl.}: C_TextFileStar c_stdin {.importc: "stdin", noDecl.}: C_TextFileStar
c_stdout {.importc: "stdout", noDecl.}: C_TextFileStar c_stdout {.importc: "stdout", noDecl.}: C_TextFileStar
c_stderr {.importc: "stderr", noDecl.}: C_TextFileStar c_stderr {.importc: "stderr", noDecl.}: C_TextFileStar
when hostOS != "standalone":
# constants faked as variables: # constants faked as variables:
when not defined(SIGINT): when not defined(SIGINT):
var var
@ -54,10 +56,12 @@ else:
proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.nodecl, importc: "longjmp".} proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.nodecl, importc: "longjmp".}
proc c_setjmp(jmpb: var C_JmpBuf): cint {.nodecl, importc: "setjmp".} proc c_setjmp(jmpb: var C_JmpBuf): cint {.nodecl, importc: "setjmp".}
when hostOS != "standalone":
proc c_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {. proc c_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.
importc: "signal", header: "<signal.h>".} importc: "signal", header: "<signal.h>".}
proc c_raise(sig: cint) {.importc: "raise", header: "<signal.h>".} proc c_raise(sig: cint) {.importc: "raise", header: "<signal.h>".}
when hostOS != "standalone":
proc c_fputs(c: cstring, f: C_TextFileStar) {.importc: "fputs", noDecl.} proc c_fputs(c: cstring, f: C_TextFileStar) {.importc: "fputs", noDecl.}
proc c_fgets(c: cstring, n: int, f: C_TextFileStar): cstring {. proc c_fgets(c: cstring, n: int, f: C_TextFileStar): cstring {.
importc: "fgets", noDecl.} importc: "fgets", noDecl.}
@ -69,6 +73,7 @@ proc c_fprintf(f: C_TextFileStar, frmt: CString) {.
proc c_printf(frmt: CString) {. proc c_printf(frmt: CString) {.
importc: "printf", nodecl, varargs.} importc: "printf", nodecl, varargs.}
when hostOS != "standalone":
proc c_fopen(filename, mode: cstring): C_TextFileStar {. proc c_fopen(filename, mode: cstring): C_TextFileStar {.
importc: "fopen", nodecl.} importc: "fopen", nodecl.}
proc c_fclose(f: C_TextFileStar) {.importc: "fclose", nodecl.} proc c_fclose(f: C_TextFileStar) {.importc: "fclose", nodecl.}
@ -77,6 +82,7 @@ proc c_sprintf(buf, frmt: CString) {.nodecl, importc: "sprintf", varargs,
noSideEffect.} noSideEffect.}
# we use it only in a way that cannot lead to security issues # we use it only in a way that cannot lead to security issues
when hostOS != "standalone":
proc c_fread(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {. proc c_fread(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {.
importc: "fread", noDecl.} importc: "fread", noDecl.}
proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {. proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {.
@ -91,10 +97,12 @@ proc c_fflush(stream: C_TextFileStar) {.importc: "fflush", nodecl.}
proc c_abort() {.importc: "abort", nodecl.} proc c_abort() {.importc: "abort", nodecl.}
proc c_feof(stream: C_TextFileStar): bool {.importc: "feof", nodecl.} proc c_feof(stream: C_TextFileStar): bool {.importc: "feof", nodecl.}
when not defined(noDynamicAlloc):
proc c_malloc(size: int): pointer {.importc: "malloc", nodecl.} proc c_malloc(size: int): pointer {.importc: "malloc", nodecl.}
proc c_free(p: pointer) {.importc: "free", nodecl.} proc c_free(p: pointer) {.importc: "free", nodecl.}
proc c_realloc(p: pointer, newsize: int): pointer {.importc: "realloc", nodecl.} proc c_realloc(p: pointer, newsize: int): pointer {.importc: "realloc", nodecl.}
when hostOS != "standalone":
when not defined(errno): when not defined(errno):
var errno {.importc, header: "<errno.h>".}: cint ## error variable var errno {.importc, header: "<errno.h>".}: cint ## error variable
proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".} proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".}

View file

@ -7,9 +7,12 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
when defined(arduino):
include "system/arduino"
# Bare-bones implementation of some things for embedded targets. # Bare-bones implementation of some things for embedded targets.
when not defined(arduino):
proc writeToStdErr(msg: CString) = write(stdout, msg) proc writeToStdErr(msg: CString) = write(stdout, msg)
proc chckIndx(i, a, b: int): int {.inline, compilerproc.} proc chckIndx(i, a, b: int): int {.inline, compilerproc.}
@ -42,11 +45,6 @@ proc reraiseException() {.compilerRtl.} =
proc WriteStackTrace() = nil proc WriteStackTrace() = nil
proc setControlCHook(hook: proc () {.noconv.}) =
# ugly cast, but should work on all architectures:
type TSignalHandler = proc (sig: cint) {.noconv.}
c_signal(SIGINT, cast[TSignalHandler](hook))
proc raiseRangeError(val: biggestInt) {.compilerproc, noreturn, noinline.} = proc raiseRangeError(val: biggestInt) {.compilerproc, noreturn, noinline.} =
writeToStdErr("value out of range") writeToStdErr("value out of range")