Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of unicode characters in the lexer #1464

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/org/mozilla/javascript/TokenStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ final int getToken() throws IOException {
c = '\\';
}
} else {
identifierStart = Character.isJavaIdentifierStart((char) c);
identifierStart = Character.isUnicodeIdentifierStart(c) || c == '$' || c == '_';
if (identifierStart) {
stringBufferTop = 0;
addToString(c);
Expand Down Expand Up @@ -751,7 +751,7 @@ final int getToken() throws IOException {
} else {
if (c == EOF_CHAR
|| c == BYTE_ORDER_MARK
|| !Character.isJavaIdentifierPart((char) c)) {
|| !(Character.isUnicodeIdentifierPart(c) || c == '$')) {
break;
}
addToString(c);
Expand Down Expand Up @@ -2058,13 +2058,19 @@ private String getStringFromBuffer() {

private void addToString(int c) {
int N = stringBufferTop;
if (N == stringBuffer.length) {
int codePointLen = Character.charCount(c);
if (N + codePointLen >= stringBuffer.length) {
char[] tmp = new char[stringBuffer.length * 2];
System.arraycopy(stringBuffer, 0, tmp, 0, N);
stringBuffer = tmp;
}
stringBuffer[N] = (char) c;
stringBufferTop = N + 1;
if (codePointLen == 1) {
stringBuffer[N] = (char) c;
} else {
stringBuffer[N] = Character.highSurrogate(c);
stringBuffer[N + 1] = Character.lowSurrogate(c);
}
stringBufferTop = N + codePointLen;
}

private boolean canUngetChar() {
Expand Down Expand Up @@ -2116,7 +2122,8 @@ private int getChar(boolean skipFormattingChars, boolean ignoreLineEnd) throws I
return EOF_CHAR;
}
cursor++;
c = sourceString.charAt(sourceCursor++);
c = sourceString.codePointAt(sourceCursor);
sourceCursor += Character.charCount(c);
} else {
if (sourceCursor == sourceEnd) {
if (!fillSourceBuffer()) {
Expand Down
18 changes: 18 additions & 0 deletions testsrc/org/mozilla/javascript/tests/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,24 @@ public void parseUnicodeFormatName() {
assertEquals("AB", first.getString());
}

@Test
public void testParseUnicodeMultibyteCharacter() {
AstRoot root = parse("\uD842\uDFB7");
AstNode first = ((ExpressionStatement) root.getFirstChild()).getExpression();
assertEquals("𠮷", first.getString());
}

@Test
public void testParseUnicodeIdentifierPartWhichIsNotJavaIdentifierPart() {
// On the JDK 11 I'm using, Character.isUnicodeIdentifierPart(U+9FEB) returns true
// but Character.isJavaIdentifierPart(U+9FEB) returns false. On a JDK 17 results
// seem to vary, but I think it's enough to verify that TokenStream uses
// the unicode methods and not the java methods.
AstRoot root = parse("a\u9FEB");
AstNode first = ((ExpressionStatement) root.getFirstChild()).getExpression();
assertEquals("a鿫", first.getString());
}

@Test
public void parseUnicodeReservedKeywords1() {
AstRoot root = parse("\\u0069\\u0066");
Expand Down
9 changes: 1 addition & 8 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5027,7 +5027,7 @@ language/global-code 29/41 (70.73%)

language/identifier-resolution 0/13 (0.0%)

language/identifiers 45/188 (23.94%)
language/identifiers 38/188 (20.21%)
other_id_continue.js
other_id_continue-escaped.js
other_id_start.js
Expand All @@ -5042,8 +5042,6 @@ language/identifiers 45/188 (23.94%)
part-unicode-13.0.0-escaped.js
part-unicode-5.2.0.js
part-unicode-5.2.0-escaped.js
part-unicode-6.0.0.js
part-unicode-6.1.0.js
part-unicode-7.0.0.js
part-unicode-7.0.0-escaped.js
part-unicode-8.0.0.js
Expand All @@ -5058,11 +5056,6 @@ language/identifiers 45/188 (23.94%)
start-unicode-12.0.0-escaped.js
start-unicode-13.0.0.js
start-unicode-13.0.0-escaped.js
start-unicode-5.2.0.js
start-unicode-5.2.0-escaped.js
start-unicode-6.0.0.js
start-unicode-6.1.0.js
start-unicode-6.1.0-escaped.js
start-unicode-7.0.0.js
start-unicode-7.0.0-escaped.js
start-unicode-8.0.0.js
Expand Down
Loading