Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2013-02-16 22:54:03 +01:00
commit 8e9b39084c
27 changed files with 163 additions and 141 deletions

View file

@ -35,7 +35,7 @@ proc `!$`*(h: THash): THash {.inline.} =
proc hashData*(Data: Pointer, Size: int): THash =
## hashes an array of bytes of size `size`
var h: THash = 0
when defined(ecmascript):
when defined(js):
var p: cstring
asm """`p` = `Data`;"""
else:
@ -48,12 +48,12 @@ proc hashData*(Data: Pointer, Size: int): THash =
Dec(s)
result = !$h
when defined(ecmascript):
when defined(js):
var objectID = 0
proc hash*(x: Pointer): THash {.inline.} =
## efficient hashing of pointers
when defined(ecmascript):
when defined(js):
asm """
if (typeof `x` == "object") {
if ("_NimID" in `x`)

View file

@ -10,7 +10,7 @@
## Constructive mathematics is naturally typed. -- Simon Thompson
##
## Basic math routines for Nimrod.
## This module is available for the ECMAScript target.
## This module is available for the JavaScript target.
{.push debugger:off .} # the user does not want to trace a part
# of the standard library!
@ -50,7 +50,7 @@ proc classify*(x: float): TFloatClass =
## classifies a floating point value. Returns `x`'s class as specified by
## `TFloatClass`.
# ECMAScript and most C compilers have no classify:
# JavaScript and most C compilers have no classify:
if x == 0.0:
if 1.0/x == Inf:
return fcZero
@ -138,15 +138,15 @@ when not defined(windows):
proc randomize*()
## initializes the random number generator with a "random"
## number, i.e. a tickcount. Note: Does nothing for the ECMAScript target,
## as ECMAScript does not support this.
## number, i.e. a tickcount. Note: Does nothing for the JavaScript target,
## as JavaScript does not support this.
proc randomize*(seed: int)
## initializes the random number generator with a specific seed.
## Note: Does nothing for the ECMAScript target,
## as ECMAScript does not support this.
## Note: Does nothing for the JavaScript target,
## as JavaScript does not support this.
when not defined(ECMAScript):
when not defined(JS):
proc sqrt*(x: float): float {.importc: "sqrt", header: "<math.h>".}
## computes the square root of `x`.
@ -310,7 +310,7 @@ proc standardDeviation*(s: TRunningStat): float =
{.pop.}
{.pop.}
when isMainModule and not defined(ECMAScript):
when isMainModule and not defined(JS):
# Verifies random seed initialization.
let seed = gettime(nil)
randomize(seed)

View file

@ -9,7 +9,7 @@
## This module contains routines and types for dealing with time.
## This module is available for the ECMAScript target.
## This module is available for the JavaScript target.
{.push debugger:off.} # the user does not want to trace a part
# of the standard library!
@ -58,7 +58,7 @@ elif defined(windows):
type
TTime* = distinct TTimeImpl
elif defined(ECMAScript):
elif defined(JS):
type
TTime* {.final, importc.} = object
getDay: proc (): int
@ -78,6 +78,7 @@ elif defined(ECMAScript):
getUTCMinutes: proc (): int
getUTCMonth: proc (): int
getUTCSeconds: proc (): int
getUTCDay: proc (): int
getYear: proc (): int
parse: proc (s: cstring): TTime
setDate: proc (x: int)
@ -115,7 +116,7 @@ type
weekday*: TWeekDay ## The current day of the week.
yearday*: range[0..365] ## The number of days since January 1,
## in the range 0 to 365.
## Always 0 if the target is ECMAScript.
## Always 0 if the target is JS.
isDST*: bool ## Determines whether DST is in effect. Always
## ``False`` if time is UTC.
tzname*: string ## The timezone this time is in. E.g. GMT
@ -246,7 +247,7 @@ proc `-`*(a: TTimeInfo, interval: TTimeInterval): TTimeInfo =
else:
result = getLocalTime(TTime(float(t) - secs))
when not defined(ECMAScript):
when not defined(JS):
proc epochTime*(): float {.rtl, extern: "nt$1", tags: [FTime].}
## gets time after the UNIX epoch (1970) in seconds. It is a float
## because sub-second resolution is likely to be supported (depending
@ -265,7 +266,7 @@ when not defined(ECMAScript):
## doWork()
## echo "CPU time [s] ", cpuTime() - t0
when not defined(ECMAScript):
when not defined(JS):
# C wrapper:
type
@ -427,7 +428,7 @@ when not defined(ECMAScript):
proc cpuTime(): float =
result = toFloat(int(clock())) / toFloat(clocksPerSec)
else:
elif defined(JS):
proc newDate(): TTime {.importc: "new Date", nodecl.}
proc getTime(): TTime = return newDate()
@ -452,7 +453,7 @@ else:
result.monthday = t.getUTCDate()
result.month = TMonth(t.getUTCMonth())
result.year = t.getUTCFullYear()
result.weekday = weekDays[t.getDay()]
result.weekday = weekDays[t.getUTCDay()]
result.yearday = 0
proc TimeInfoToTime*(timeInfo: TTimeInfo): TTime =
@ -694,4 +695,4 @@ when isMainModule:
var t4 = getGMTime(TTime(876124714)) # Mon 6 Oct 08:58:34 BST 1997
assert t4.format("M MM MMM MMMM") == "10 10 Oct October"

View file

@ -17,7 +17,13 @@
## It is loosely based on C++'s boost.test and Haskell's QuickTest
import
macros, terminal, os
macros
when defined(stdout):
import os
when not defined(ECMAScript):
import terminal
type
TTestStatus* = enum OK, FAILED
@ -52,12 +58,15 @@ proc testDone(name: string, s: TTestStatus) =
program_result += 1
if OutputLevel != PRINT_NONE and (OutputLevel == PRINT_ALL or s == FAILED):
var color = (if s == OK: fgGreen else: fgRed)
if ColorOutput:
styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"
template rawPrint() = echo("[", $s, "] ", name, "\n")
when not defined(ECMAScript):
if ColorOutput and not defined(ECMAScript):
var color = (if s == OK: fgGreen else: fgRed)
styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"
else:
rawPrint()
else:
echo "[", $s, "] ", name, "\n"
rawPrint()
template test*(name: expr, body: stmt): stmt {.immediate, dirty.} =
bind shouldRun, checkpoints, testDone
@ -87,7 +96,8 @@ template fail* =
for msg in items(checkpoints):
echo msg
if AbortOnError: quit(1)
when not defined(ECMAScript):
if AbortOnError: quit(1)
TestStatusIMPL = FAILED
checkpoints = @[]
@ -171,14 +181,19 @@ macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
result = getAst(expectBody(errorTypes, exp.lineinfo, body))
## Reading settings
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
when defined(stdout):
## Reading settings
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
ColorOutput = not existsEnv("NIMTEST_NO_COLOR")
else:
var envOutLvl = "" # TODO
ColorOutput = false
if envOutLvl.len > 0:
for opt in countup(low(TOutputLevel), high(TOutputLevel)):
if $opt == envOutLvl:
OutputLevel = opt
break
AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
ColorOutput = not existsEnv("NIMTEST_NO_COLOR")