Fixed string concatenation and other bugs in the JS backend. Fixed a small bug in the IRC module.
This commit is contained in:
parent
2359b8b107
commit
6deda5a973
9 changed files with 117 additions and 44 deletions
|
|
@ -778,9 +778,10 @@ proc genSwap(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
proc genFieldAddr(p: var TProc, n: PNode, r: var TCompRes) =
|
proc genFieldAddr(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
var a: TCompRes
|
var a: TCompRes
|
||||||
r.kind = etyBaseIndex
|
r.kind = etyBaseIndex
|
||||||
gen(p, n.sons[0], a)
|
var b = if n.kind == nkHiddenAddr: n.sons[0] else: n
|
||||||
if n.sons[1].kind != nkSym: InternalError(n.sons[1].info, "genFieldAddr")
|
gen(p, b.sons[0], a)
|
||||||
var f = n.sons[1].sym
|
if b.sons[1].kind != nkSym: InternalError(b.sons[1].info, "genFieldAddr")
|
||||||
|
var f = b.sons[1].sym
|
||||||
if f.loc.r == nil: f.loc.r = mangleName(f)
|
if f.loc.r == nil: f.loc.r = mangleName(f)
|
||||||
r.res = makeCString(ropeToStr(f.loc.r))
|
r.res = makeCString(ropeToStr(f.loc.r))
|
||||||
r.com = mergeExpr(a)
|
r.com = mergeExpr(a)
|
||||||
|
|
@ -1055,15 +1056,41 @@ proc genOrd(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
else: InternalError(n.info, "genOrd")
|
else: InternalError(n.info, "genOrd")
|
||||||
|
|
||||||
proc genConStrStr(p: var TProc, n: PNode, r: var TCompRes) =
|
proc genConStrStr(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
var a, b: TCompRes
|
var a: TCompRes
|
||||||
|
|
||||||
gen(p, n.sons[1], a)
|
gen(p, n.sons[1], a)
|
||||||
gen(p, n.sons[2], b)
|
r.com = mergeExpr(r.com, a.com)
|
||||||
r.com = mergeExpr(a.com, b.com)
|
|
||||||
if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyChar:
|
if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyChar:
|
||||||
a.res = ropef("[$1, 0]", [a.res])
|
r.res.app(ropef("[$1].concat(", [a.res]))
|
||||||
if skipTypes(n.sons[2].typ, abstractVarRange).kind == tyChar:
|
else:
|
||||||
b.res = ropef("[$1, 0]", [b.res])
|
r.res.app(ropef("($1.slice(0,-1)).concat(", [a.res]))
|
||||||
r.res = ropef("($1.slice(0,-1)).concat($2)", [a.res, b.res])
|
|
||||||
|
for i in countup(2, sonsLen(n) - 2):
|
||||||
|
gen(p, n.sons[i], a)
|
||||||
|
r.com = mergeExpr(r.com, a.com)
|
||||||
|
|
||||||
|
if skipTypes(n.sons[i].typ, abstractVarRange).kind == tyChar:
|
||||||
|
r.res.app(ropef("[$1],", [a.res]))
|
||||||
|
else:
|
||||||
|
r.res.app(ropef("$1.slice(0,-1),", [a.res]))
|
||||||
|
|
||||||
|
gen(p, n.sons[sonsLen(n) - 1], a)
|
||||||
|
r.com = mergeExpr(r.com, a.com)
|
||||||
|
if skipTypes(n.sons[sonsLen(n) - 1].typ, abstractVarRange).kind == tyChar:
|
||||||
|
r.res.app(ropef("[$1, 0])", [a.res]))
|
||||||
|
else:
|
||||||
|
r.res.app(ropef("$1)", [a.res]))
|
||||||
|
|
||||||
|
proc genRepr(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
|
var t = skipTypes(n.sons[1].typ, abstractVarRange)
|
||||||
|
case t.kind
|
||||||
|
of tyInt..tyInt64:
|
||||||
|
unaryExpr(p, n, r, "", "reprInt($1)")
|
||||||
|
of tyEnum, tyOrdinal:
|
||||||
|
binaryExpr(p, n, r, "", "reprEnum($1, $2)")
|
||||||
|
else:
|
||||||
|
# XXX:
|
||||||
|
internalError(n.info, "genRepr: Not implemented")
|
||||||
|
|
||||||
proc genMagic(p: var TProc, n: PNode, r: var TCompRes) =
|
proc genMagic(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
var
|
var
|
||||||
|
|
@ -1073,7 +1100,8 @@ proc genMagic(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
case op
|
case op
|
||||||
of mOr: genOr(p, n.sons[1], n.sons[2], r)
|
of mOr: genOr(p, n.sons[1], n.sons[2], r)
|
||||||
of mAnd: genAnd(p, n.sons[1], n.sons[2], r)
|
of mAnd: genAnd(p, n.sons[1], n.sons[2], r)
|
||||||
of mAddi..mStrToStr: arith(p, n, r, op) #mRepr: genRepr(p, n, r);
|
of mAddi..mStrToStr: arith(p, n, r, op)
|
||||||
|
of mRepr: genRepr(p, n, r)
|
||||||
of mSwap: genSwap(p, n, r)
|
of mSwap: genSwap(p, n, r)
|
||||||
of mUnaryLt:
|
of mUnaryLt:
|
||||||
# XXX: range checking?
|
# XXX: range checking?
|
||||||
|
|
@ -1089,14 +1117,18 @@ proc genMagic(p: var TProc, n: PNode, r: var TCompRes) =
|
||||||
else: binaryExpr(p, n, r, "addInt", "addInt($1, $2)")
|
else: binaryExpr(p, n, r, "addInt", "addInt($1, $2)")
|
||||||
of mAppendStrCh: binaryStmt(p, n, r, "addChar", "$1 = addChar($1, $2)")
|
of mAppendStrCh: binaryStmt(p, n, r, "addChar", "$1 = addChar($1, $2)")
|
||||||
of mAppendStrStr:
|
of mAppendStrStr:
|
||||||
|
if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyCString:
|
||||||
|
binaryStmt(p, n, r, "", "$1 += $2")
|
||||||
|
else:
|
||||||
binaryStmt(p, n, r, "", "$1 = ($1.slice(0,-1)).concat($2)")
|
binaryStmt(p, n, r, "", "$1 = ($1.slice(0,-1)).concat($2)")
|
||||||
# XXX: make a copy of $2, because of EMCAScript's sucking semantics
|
# XXX: make a copy of $2, because of ECMAScript's sucking semantics
|
||||||
of mAppendSeqElem: binaryStmt(p, n, r, "", "$1.push($2)")
|
of mAppendSeqElem: binaryStmt(p, n, r, "", "$1.push($2)")
|
||||||
of mConStrStr: genConStrStr(p, n, r)
|
of mConStrStr: genConStrStr(p, n, r)
|
||||||
of mEqStr: binaryExpr(p, n, r, "eqStrings", "eqStrings($1, $2)")
|
of mEqStr: binaryExpr(p, n, r, "eqStrings", "eqStrings($1, $2)")
|
||||||
of mLeStr: binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) <= 0)")
|
of mLeStr: binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) <= 0)")
|
||||||
of mLtStr: binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
|
of mLtStr: binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
|
||||||
of mIsNil: unaryExpr(p, n, r, "", "$1 == null")
|
of mIsNil: unaryExpr(p, n, r, "", "$1 == null")
|
||||||
|
of mEnumToStr: genRepr(p, n, r)
|
||||||
of mAssert:
|
of mAssert:
|
||||||
if (optAssert in p.Options):
|
if (optAssert in p.Options):
|
||||||
useMagic(p, "internalAssert")
|
useMagic(p, "internalAssert")
|
||||||
|
|
|
||||||
|
|
@ -274,6 +274,12 @@ Database support
|
||||||
redis-server instance, send commands and receive replies.
|
redis-server instance, send commands and receive replies.
|
||||||
|
|
||||||
|
|
||||||
|
Modules for JS backend
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
* `dom <dom.html>`_
|
||||||
|
Declaration of the Document Object Model for the ECMAScript backend.
|
||||||
|
|
||||||
|
|
||||||
Impure libraries
|
Impure libraries
|
||||||
================
|
================
|
||||||
|
|
|
||||||
|
|
@ -227,6 +227,7 @@ type
|
||||||
getAttributeNode*: proc (attr: cstring): ref TNode
|
getAttributeNode*: proc (attr: cstring): ref TNode
|
||||||
getElementsByTagName*: proc (): seq[ref TNode]
|
getElementsByTagName*: proc (): seq[ref TNode]
|
||||||
hasChildNodes*: proc (): bool
|
hasChildNodes*: proc (): bool
|
||||||
|
innerHTML*: cstring
|
||||||
insertBefore*: proc (newNode, before: ref TNode)
|
insertBefore*: proc (newNode, before: ref TNode)
|
||||||
insertData*: proc (position: int, data: cstring)
|
insertData*: proc (position: int, data: cstring)
|
||||||
removeAttribute*: proc (attr: cstring)
|
removeAttribute*: proc (attr: cstring)
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,12 @@ type
|
||||||
|
|
||||||
proc send*(irc: var TIRC, message: string) =
|
proc send*(irc: var TIRC, message: string) =
|
||||||
## Sends ``message`` as a raw command. It adds ``\c\L`` for you.
|
## Sends ``message`` as a raw command. It adds ``\c\L`` for you.
|
||||||
|
try:
|
||||||
irc.sock.send(message & "\c\L")
|
irc.sock.send(message & "\c\L")
|
||||||
|
except EOS:
|
||||||
|
# Assuming disconnection of every EOS could be bad,
|
||||||
|
# but I can't exactly check for EBrokenPipe.
|
||||||
|
irc.connected = false
|
||||||
|
|
||||||
proc privmsg*(irc: var TIRC, target, message: string) =
|
proc privmsg*(irc: var TIRC, target, message: string) =
|
||||||
## Sends ``message`` to ``target``. ``Target`` can be a channel, or a user.
|
## Sends ``message`` to ``target``. ``Target`` can be a channel, or a user.
|
||||||
|
|
@ -199,6 +204,7 @@ proc poll*(irc: var TIRC, ev: var TIRCEvent,
|
||||||
##
|
##
|
||||||
## This function should be called often as it also handles pinging
|
## This function should be called often as it also handles pinging
|
||||||
## the server.
|
## the server.
|
||||||
|
if not irc.connected: ev.typ = EvDisconnected
|
||||||
var line = ""
|
var line = ""
|
||||||
var socks = @[irc.sock]
|
var socks = @[irc.sock]
|
||||||
var ret = socks.select(timeout)
|
var ret = socks.select(timeout)
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ elif defined(windows):
|
||||||
|
|
||||||
elif defined(ECMAScript):
|
elif defined(ECMAScript):
|
||||||
type
|
type
|
||||||
TTime* {.final.} = object
|
TTime* {.final, importc.} = object
|
||||||
getDay: proc (): int
|
getDay: proc (): int
|
||||||
getFullYear: proc (): int
|
getFullYear: proc (): int
|
||||||
getHours: proc (): int
|
getHours: proc (): int
|
||||||
|
|
@ -62,6 +62,7 @@ elif defined(ECMAScript):
|
||||||
getSeconds: proc (): int
|
getSeconds: proc (): int
|
||||||
getTime: proc (): int
|
getTime: proc (): int
|
||||||
getTimezoneOffset: proc (): int
|
getTimezoneOffset: proc (): int
|
||||||
|
getDate: proc (): int
|
||||||
getUTCDate: proc (): int
|
getUTCDate: proc (): int
|
||||||
getUTCFullYear: proc (): int
|
getUTCFullYear: proc (): int
|
||||||
getUTCHours: proc (): int
|
getUTCHours: proc (): int
|
||||||
|
|
@ -310,7 +311,8 @@ when not defined(ECMAScript):
|
||||||
result = toFloat(int(clock())) / toFloat(clocksPerSec)
|
result = toFloat(int(clock())) / toFloat(clocksPerSec)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
proc getTime(): TTime {.importc: "new Date", nodecl.}
|
proc newDate(): TTime {.importc: "new Date", nodecl.}
|
||||||
|
proc getTime(): TTime = return newDate()
|
||||||
|
|
||||||
const
|
const
|
||||||
weekDays: array [0..6, TWeekDay] = [
|
weekDays: array [0..6, TWeekDay] = [
|
||||||
|
|
@ -346,7 +348,7 @@ else:
|
||||||
result.setDate(timeInfo.monthday)
|
result.setDate(timeInfo.monthday)
|
||||||
|
|
||||||
proc `$`(timeInfo: TTimeInfo): string = return $(TimeInfoToTIme(timeInfo))
|
proc `$`(timeInfo: TTimeInfo): string = return $(TimeInfoToTIme(timeInfo))
|
||||||
proc `$`(time: TTime): string = $time.toLocaleString()
|
proc `$`(time: TTime): string = return $time.toLocaleString()
|
||||||
|
|
||||||
proc `-` (a, b: TTime): int64 =
|
proc `-` (a, b: TTime): int64 =
|
||||||
return a.getTime() - b.getTime()
|
return a.getTime() - b.getTime()
|
||||||
|
|
|
||||||
|
|
@ -1495,6 +1495,8 @@ else:
|
||||||
`x`[0][len] = 0
|
`x`[0][len] = 0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
|
||||||
|
|
||||||
proc echo*[Ty](x: openarray[Ty]) {.magic: "Echo", noSideEffect.}
|
proc echo*[Ty](x: openarray[Ty]) {.magic: "Echo", noSideEffect.}
|
||||||
## special built-in that takes a variable number of arguments. Each argument
|
## special built-in that takes a variable number of arguments. Each argument
|
||||||
## is converted to a string via ``$``, so it works for user-defined
|
## is converted to a string via ``$``, so it works for user-defined
|
||||||
|
|
@ -1886,6 +1888,7 @@ elif defined(ecmaScript) or defined(NimrodVM):
|
||||||
|
|
||||||
when defined(ecmaScript):
|
when defined(ecmaScript):
|
||||||
include "system/ecmasys"
|
include "system/ecmasys"
|
||||||
|
include "system/reprjs"
|
||||||
elif defined(NimrodVM):
|
elif defined(NimrodVM):
|
||||||
proc cmp(x, y: string): int =
|
proc cmp(x, y: string): int =
|
||||||
if x == y: return 0
|
if x == y: return 0
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ proc cmp(x, y: string): int = return cmpStrings(x, y)
|
||||||
|
|
||||||
proc eqStrings(a, b: string): bool {.noStackFrame, compilerProc.} =
|
proc eqStrings(a, b: string): bool {.noStackFrame, compilerProc.} =
|
||||||
asm """
|
asm """
|
||||||
if (`a == `b`) return true;
|
if (`a` == `b`) return true;
|
||||||
if ((!`a`) || (!`b`)) return false;
|
if ((!`a`) || (!`b`)) return false;
|
||||||
var alen = `a`.length;
|
var alen = `a`.length;
|
||||||
if (alen != `b`.length) return false;
|
if (alen != `b`.length) return false;
|
||||||
|
|
|
||||||
23
lib/system/reprjs.nim
Normal file
23
lib/system/reprjs.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nimrod's Runtime Library
|
||||||
|
# (c) Copyright 2011 Andreas Rumpf
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
proc reprInt(x: int64): string {.compilerproc.} = return $x
|
||||||
|
|
||||||
|
proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
|
||||||
|
if ntfEnumHole notin typ.flags:
|
||||||
|
if e <% typ.node.len:
|
||||||
|
return $typ.node.sons[e].name
|
||||||
|
else:
|
||||||
|
# ugh we need a slow linear search:
|
||||||
|
var n = typ.node
|
||||||
|
var s = n.sons
|
||||||
|
for i in 0 .. n.len-1:
|
||||||
|
if s[i].offset == e: return $s[i].name
|
||||||
|
result = $e & " (invalid data!)"
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ srcdoc: "pure/json;pure/base64;pure/scgi;pure/redis;impure/graphics"
|
||||||
srcdoc: "impure/rdstdin;wrappers/zmq;wrappers/sphinx"
|
srcdoc: "impure/rdstdin;wrappers/zmq;wrappers/sphinx"
|
||||||
srcdoc: "pure/collections/tables;pure/collections/sets;pure/collections/lists"
|
srcdoc: "pure/collections/tables;pure/collections/sets;pure/collections/lists"
|
||||||
srcdoc: "pure/collections/intsets;pure/collections/queues;pure/encodings"
|
srcdoc: "pure/collections/intsets;pure/collections/queues;pure/encodings"
|
||||||
srcdoc: "pure/events;pure/collections/sequtils;pure/irc"
|
srcdoc: "pure/events;pure/collections/sequtils;pure/irc;ecmas/dom"
|
||||||
|
|
||||||
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql;wrappers/iup"
|
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql;wrappers/iup"
|
||||||
webdoc: "wrappers/sqlite3;wrappers/postgres;wrappers/tinyc"
|
webdoc: "wrappers/sqlite3;wrappers/postgres;wrappers/tinyc"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue