Skip to content

Commit 323f151

Browse files
Nick Santoshns
authored andcommitted
In the parser token stream, If a string contains unicodes, and
converted to a reserved keyword, we convert the last character back to unicode. Fixes issue 490. Committed by Yi Zhu ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=26371770
1 parent 57ab969 commit 323f151

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/org/mozilla/javascript/TokenStream.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static int stringToKeyword(String name)
156156
Id_byte = Token.RESERVED, // ES3 only
157157
Id_catch = Token.CATCH,
158158
Id_char = Token.RESERVED, // ES3 only
159-
Id_class = Token.RESERVED,
159+
Id_class = Token.RESERVED,
160160
Id_const = Token.CONST, // reserved
161161
Id_debugger = Token.DEBUGGER,
162162
Id_double = Token.RESERVED, // ES3 only
@@ -183,7 +183,7 @@ private static int stringToKeyword(String name)
183183
Id_synchronized = Token.RESERVED, // ES3 only
184184
Id_throw = Token.THROW,
185185
Id_throws = Token.RESERVED, // ES3 only
186-
Id_transient = Token.RESERVED, // ES3 only
186+
Id_transient = Token.RESERVED, // ES3 only
187187
Id_try = Token.TRY,
188188
Id_volatile = Token.RESERVED; // ES3 only
189189

@@ -428,7 +428,7 @@ final int getToken() throws IOException
428428
}
429429
// Save the string in case we need to use in
430430
// object literal definitions.
431-
this.string = (String)allStrings.intern(str);
431+
this.string = (String)allStrings.intern(str);
432432
if (result != Token.RESERVED) {
433433
return result;
434434
} else if (!parser.compilerEnv.
@@ -437,6 +437,10 @@ final int getToken() throws IOException
437437
return result;
438438
}
439439
}
440+
} else if (isKeyword(str)) {
441+
// If a string contains unicodes, and converted to a keyword,
442+
// we convert the last character back to unicode
443+
str = convertLastCharToHex(str);
440444
}
441445
this.string = (String)allStrings.intern(str);
442446
return Token.NAME;
@@ -1568,6 +1572,19 @@ final String getAndResetCurrentComment() {
15681572
}
15691573
}
15701574

1575+
private String convertLastCharToHex(String str) {
1576+
int lastIndex = str.length()-1;
1577+
StringBuffer buf = new StringBuffer(
1578+
str.substring(0, lastIndex));
1579+
buf.append("\\u");
1580+
String hexCode = Integer.toHexString(str.charAt(lastIndex));
1581+
for (int i = 0; i < 4-hexCode.length(); ++i) {
1582+
buf.append('0');
1583+
}
1584+
buf.append(hexCode);
1585+
return buf.toString();
1586+
}
1587+
15711588
// stuff other than whitespace since start of line
15721589
private boolean dirtyLine;
15731590

testsrc/org/mozilla/javascript/tests/ParserTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,35 @@ public void testParseUnicodeFormatName() {
10831083
assertEquals("AB", first.getString());
10841084
}
10851085

1086+
public void testParseUnicodeReservedKeywords1() {
1087+
AstRoot root = parse("\\u0069\\u0066");
1088+
AstNode first = ((ExpressionStatement)
1089+
root.getFirstChild()).getExpression();
1090+
assertEquals("i\\u0066", first.getString());
1091+
}
1092+
1093+
public void testParseUnicodeReservedKeywords2() {
1094+
AstRoot root = parse("v\\u0061\\u0072");
1095+
AstNode first = ((ExpressionStatement)
1096+
root.getFirstChild()).getExpression();
1097+
assertEquals("va\\u0072", first.getString());
1098+
}
1099+
1100+
public void testParseUnicodeReservedKeywords3() {
1101+
// All are keyword "while"
1102+
AstRoot root = parse("w\\u0068\\u0069\\u006C\\u0065;" +
1103+
"\\u0077\\u0068il\\u0065; \\u0077h\\u0069le;");
1104+
AstNode first = ((ExpressionStatement)
1105+
root.getFirstChild()).getExpression();
1106+
AstNode second = ((ExpressionStatement)
1107+
root.getFirstChild().getNext()).getExpression();
1108+
AstNode third = ((ExpressionStatement)
1109+
root.getFirstChild().getNext().getNext()).getExpression();
1110+
assertEquals("whil\\u0065", first.getString());
1111+
assertEquals("whil\\u0065", second.getString());
1112+
assertEquals("whil\\u0065", third.getString());
1113+
}
1114+
10861115
public void testParseObjectLiteral1() {
10871116
allowKeywordsAsObjectLiteralsKeys = true;
10881117

0 commit comments

Comments
 (0)