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

@ -7,10 +7,10 @@
# distribution, for details about the copyright.
#
## Declaration of the Document Object Model for the ECMAScript backend.
## Declaration of the Document Object Model for the JavaScript backend.
when not defined(ecmascript) and not defined(Nimdoc):
{.error: "This module only works on the ECMAScript platform".}
when not defined(js) and not defined(Nimdoc):
{.error: "This module only works on the JavaScript platform".}
type
TEventHandlers* {.importc.} = object of TObject

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")

View file

@ -184,7 +184,7 @@ proc `..`*[T](b: T): TSlice[T] {.noSideEffect, inline.} =
when not defined(niminheritable):
{.pragma: inheritable.}
when not defined(EcmaScript) and not defined(NimrodVM):
when not defined(JS) and not defined(NimrodVM):
type
TGenericSeq* {.compilerproc, pure, inheritable.} = object
len, reserved: int
@ -900,7 +900,7 @@ template sysAssert(cond: bool, msg: string) =
include "system/inclrtl"
when not defined(ecmascript) and not defined(nimrodVm) and hostOS != "standalone":
when not defined(JS) and not defined(nimrodVm) and hostOS != "standalone":
include "system/cgprocs"
proc add *[T](x: var seq[T], y: T) {.magic: "AppendSeqElem", noSideEffect.}
@ -1170,7 +1170,7 @@ proc `$` *(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
## converted to a decimal string.
when not defined(NimrodVM):
when not defined(ECMAScript):
when not defined(JS):
proc `$` *(x: uint64): string {.noSideEffect.}
## The stingify operator for an unsigned integer argument. Returns `x`
## converted to a decimal string.
@ -1422,7 +1422,7 @@ proc `&` *[T](x: T, y: seq[T]): seq[T] {.noSideEffect.} =
result[y.len] = x
when not defined(NimrodVM):
when not defined(ECMAScript):
when not defined(JS):
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
result = cast[pointer](x)
else:
@ -1698,7 +1698,7 @@ type
filename: CString
len: int # length of slots (when not debugging always zero)
when not defined(ECMAScript):
when not defined(JS):
{.push stack_trace:off, profiler:off.}
proc add*(x: var string, y: cstring) {.noStackFrame.} =
var i = 0
@ -1724,7 +1724,7 @@ proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", tags: [FWriteIO].}
## is converted to a string via ``$``, so it works for user-defined
## types that have an overloaded ``$`` operator.
## It is roughly equivalent to ``writeln(stdout, x); flush(stdout)``, but
## available for the ECMAScript target too.
## available for the JavaScript target too.
## Unlike other IO operations this is guaranteed to be thread-safe as
## ``echo`` is very often used for debugging convenience.
@ -1747,7 +1747,7 @@ proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo".}
## get type information for `x`. Ordinary code should not use this, but
## the `typeinfo` module instead.
when not defined(EcmaScript): #and not defined(NimrodVM):
when not defined(JS): #and not defined(NimrodVM):
{.push stack_trace: off, profiler:off.}
when not defined(NimrodVM):
@ -2166,7 +2166,7 @@ when not defined(EcmaScript): #and not defined(NimrodVM):
`result` = *((NI*) `x`.ClEnv) < 0;
""".}
elif defined(ecmaScript):
elif defined(JS):
# Stubs:
proc nimGCvisit(d: pointer, op: int) {.compilerRtl.} = nil
@ -2192,8 +2192,8 @@ elif defined(ecmaScript):
proc deallocShared(p: pointer) = nil
proc reallocShared(p: pointer, newsize: int): pointer = nil
when defined(ecmaScript):
include "system/ecmasys"
when defined(JS):
include "system/jssys"
include "system/reprjs"
elif defined(NimrodVM):
proc cmp(x, y: string): int =
@ -2437,7 +2437,7 @@ proc shallow*[T](s: var seq[T]) {.noSideEffect, inline.} =
## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
## perform deep copies of `s`. This is only useful for optimization
## purposes.
when not defined(EcmaScript) and not defined(NimrodVM):
when not defined(JS) and not defined(NimrodVM):
var s = cast[PGenericSeq](s)
s.reserved = s.reserved or seqShallowFlag
@ -2445,7 +2445,7 @@ proc shallow*(s: var string) {.noSideEffect, inline.} =
## marks a string `s` as `shallow`:idx:. Subsequent assignments will not
## perform deep copies of `s`. This is only useful for optimization
## purposes.
when not defined(EcmaScript) and not defined(NimrodVM):
when not defined(JS) and not defined(NimrodVM):
var s = cast[PGenericSeq](s)
s.reserved = s.reserved or seqShallowFlag

View file

@ -42,7 +42,7 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
result = newString(1)
result[0] = x
proc getCurrentExceptionMsg(): string =
proc getCurrentExceptionMsg*(): string =
if excHandler != nil: return $excHandler.exc.msg
return ""
@ -151,7 +151,7 @@ proc cstrToNimstr(c: cstring): string {.noStackFrame, compilerproc.} =
return result;
"""
proc toEcmaStr(s: string): cstring {.noStackFrame, compilerproc.} =
proc toJSStr(s: string): cstring {.noStackFrame, compilerproc.} =
asm """
var len = `s`.length-1;
var result = new Array(len);
@ -304,7 +304,7 @@ when defined(kwin):
asm """
var buf = "";
for (var i = 0; i < arguments.length; ++i) {
buf += `toEcmaStr`(arguments[i]);
buf += `toJSStr`(arguments[i]);
}
print(buf);
"""
@ -316,7 +316,7 @@ elif defined(nodejs):
asm """
var buf = "";
for (var i = 0; i < arguments.length; ++i) {
buf += `toEcmaStr`(arguments[i]);
buf += `toJSStr`(arguments[i]);
}
console.log(buf);
"""
@ -338,7 +338,7 @@ else:
if node == nil: raise newException(EIO, "<body> element does not exist yet!")
asm """
for (var i = 0; i < arguments.length; ++i) {
var x = `toEcmaStr`(arguments[i]);
var x = `toJSStr`(arguments[i]);
`node`.appendChild(document.createTextNode(x))
}
"""