diff options
-rw-r--r-- | compiler/jsgen.nim | 13 | ||||
-rw-r--r-- | lib/system/jssys.nim | 6 |
2 files changed, 17 insertions, 2 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index d6b546d8b..8aaad6327 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -944,8 +944,17 @@ proc genArrayAccess(p: PProc, n: PNode, r: var TCompRes) = else: internalError(n.info, "expr(nkBracketExpr, " & $ty.kind & ')') r.typ = etyNone if r.res == nil: internalError(n.info, "genArrayAccess") - if p.target == targetPHP and ty.kind in {tyString, tyCString}: - r.res = "ord($1[$2])" % [r.address, r.res] + if p.target == targetPHP: + if n.sons[0].kind in nkCallKinds+{nkStrLit..nkTripleStrLit}: + useMagic(p, "nimAt") + if ty.kind in {tyString, tyCString}: + r.res = "ord(nimAt($1, $2))" % [r.address, r.res] + else: + r.res = "nimAt($1, $2)" % [r.address, r.res] + elif ty.kind in {tyString, tyCString}: + r.res = "ord($1[$2])" % [r.address, r.res] + else: + r.res = "$1[$2]" % [r.address, r.res] else: r.res = "$1[$2]" % [r.address, r.res] r.address = nil diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index ceeb5563f..c96f2f958 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -276,6 +276,12 @@ proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} = """ when defined(nimphp): + proc nimAt(x: string; i: int): string {.asmNoStackFrame, compilerproc.} = + asm """ + return `x`[`i`]; + """ + +when defined(nimphp): proc nimSubstr(s: string; a, b: int): string {. asmNoStackFrame, compilerproc.} = asm """return substr(`s`,`a`,`b`-`a`+1);""" |