Merge pull request #3733 from yglukhov/js-unicode
Fixed unicode handling in JS. Fixes #3714.
This commit is contained in:
commit
263ee143f1
2 changed files with 45 additions and 8 deletions
|
|
@ -163,8 +163,26 @@ proc mangleName(s: PSym): Rope =
|
||||||
add(result, rope(s.id))
|
add(result, rope(s.id))
|
||||||
s.loc.r = result
|
s.loc.r = result
|
||||||
|
|
||||||
|
proc escapeJSString(s: string): string =
|
||||||
|
result = newStringOfCap(s.len + s.len shr 2)
|
||||||
|
result.add("\"")
|
||||||
|
for c in items(s):
|
||||||
|
case c
|
||||||
|
of '\l': result.add("\\n")
|
||||||
|
of '\r': result.add("\\r")
|
||||||
|
of '\t': result.add("\\t")
|
||||||
|
of '\b': result.add("\\b")
|
||||||
|
of '\a': result.add("\\a")
|
||||||
|
of '\e': result.add("\\e")
|
||||||
|
of '\v': result.add("\\v")
|
||||||
|
of '\\': result.add("\\\\")
|
||||||
|
of '\'': result.add("\\'")
|
||||||
|
of '\"': result.add("\\\"")
|
||||||
|
else: add(result, c)
|
||||||
|
result.add("\"")
|
||||||
|
|
||||||
proc makeJSString(s: string): Rope =
|
proc makeJSString(s: string): Rope =
|
||||||
(if s.isNil: "null".rope else: strutils.escape(s).rope)
|
(if s.isNil: "null".rope else: escapeJSString(s).rope)
|
||||||
|
|
||||||
include jstypes
|
include jstypes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -166,14 +166,33 @@ proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||||
asm """
|
{.emit: """
|
||||||
var result = [];
|
var ln = `c`.length;
|
||||||
for (var i = 0; i < `c`.length; ++i) {
|
var result = new Array(ln);
|
||||||
result[i] = `c`.charCodeAt(i);
|
var r = 0;
|
||||||
|
for (var i = 0; i < ln; ++i) {
|
||||||
|
var ch = `c`.charCodeAt(i);
|
||||||
|
|
||||||
|
if (ch < 128) {
|
||||||
|
result[r] = ch;
|
||||||
}
|
}
|
||||||
result[result.length] = 0; // terminating zero
|
else if((ch > 127) && (ch < 2048)) {
|
||||||
return result;
|
result[r] = (ch >> 6) | 192;
|
||||||
"""
|
++r;
|
||||||
|
result[r] = (ch & 63) | 128;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result[r] = (ch >> 12) | 224;
|
||||||
|
++r;
|
||||||
|
result[r] = ((ch >> 6) & 63) | 128;
|
||||||
|
++r;
|
||||||
|
result[r] = (ch & 63) | 128;
|
||||||
|
}
|
||||||
|
++r;
|
||||||
|
}
|
||||||
|
result[r] = 0; // terminating zero
|
||||||
|
return result;
|
||||||
|
""".}
|
||||||
|
|
||||||
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
||||||
asm """
|
asm """
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue