Fix compile problems and optimize TokenStream

pull/1256/head
litetex 2025-03-05 15:45:08 +01:00
rodzic bcf24def8b
commit 907fc2ac52
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 525B43E6039B3689
1 zmienionych plików z 18 dodań i 82 usunięć

Wyświetl plik

@ -2,7 +2,6 @@ package org.schabi.newpipe.extractor.utils.jsextractor;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Kit; import org.mozilla.javascript.Kit;
import org.mozilla.javascript.ObjToIntMap;
import org.mozilla.javascript.ScriptRuntime; import org.mozilla.javascript.ScriptRuntime;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
@ -38,12 +37,10 @@ class TokenStream {
this.languageVersion = languageVersion; this.languageVersion = languageVersion;
} }
static boolean isKeyword(final String s, final int version, final boolean isStrict) { private static Token stringToKeyword(
return Token.EOF != stringToKeyword(s, version, isStrict); final String name,
} final int version,
final boolean isStrict) {
private static Token stringToKeyword(final String name, final int version,
final boolean isStrict) {
if (version < Context.VERSION_ES6) { if (version < Context.VERSION_ES6) {
return stringToKeywordForJS(name); return stringToKeywordForJS(name);
} }
@ -343,7 +340,7 @@ class TokenStream {
} }
ungetChar(c); ungetChar(c);
String str = getStringFromBuffer(); final String str = getStringFromBuffer();
if (!containsEscape) { if (!containsEscape) {
// OPT we shouldn't have to make a string (object!) to // OPT we shouldn't have to make a string (object!) to
// check if it's a keyword. // check if it's a keyword.
@ -353,30 +350,17 @@ class TokenStream {
if (result != Token.EOF) { if (result != Token.EOF) {
if ((result == Token.LET || result == Token.YIELD) if ((result == Token.LET || result == Token.YIELD)
&& languageVersion < Context.VERSION_1_7) { && languageVersion < Context.VERSION_1_7) {
// LET and YIELD are tokens only in 1.7 and later
string = result == Token.LET ? "let" : "yield";
result = Token.NAME; result = Token.NAME;
} }
// Save the string in case we need to use in // Save the string in case we need to use in
// object literal definitions. // object literal definitions.
this.string = (String) allStrings.intern(str); if (result != Token.RESERVED
if (result != Token.RESERVED) { || languageVersion >= Context.VERSION_ES6
return result; || !IS_RESERVED_KEYWORD_AS_IDENTIFIER) {
} else if (languageVersion >= Context.VERSION_ES6) {
return result;
} else if (!IS_RESERVED_KEYWORD_AS_IDENTIFIER) {
return result; return result;
} }
} }
} else if (isKeyword(
str,
languageVersion,
STRICT_MODE)) {
// If a string contains unicodes, and converted to a keyword,
// we convert the last character back to unicode
str = convertLastCharToHex(str);
} }
this.string = (String) allStrings.intern(str);
return Token.NAME; return Token.NAME;
} }
@ -466,7 +450,7 @@ class TokenStream {
} }
} }
ungetChar(c); ungetChar(c);
this.string = getStringFromBuffer(); tokenEnd = cursor;
return Token.NUMBER; return Token.NUMBER;
} }
@ -562,7 +546,7 @@ class TokenStream {
escapeVal = Kit.xDigitToInt(c, 0); escapeVal = Kit.xDigitToInt(c, 0);
if (escapeVal < 0) { if (escapeVal < 0) {
addToString('x'); addToString('x');
continue strLoop; continue;
} }
final int c1 = c; final int c1 = c;
c = getChar(); c = getChar();
@ -570,7 +554,7 @@ class TokenStream {
if (escapeVal < 0) { if (escapeVal < 0) {
addToString('x'); addToString('x');
addToString(c1); addToString(c1);
continue strLoop; continue;
} }
// got 2 hex digits // got 2 hex digits
c = escapeVal; c = escapeVal;
@ -580,7 +564,7 @@ class TokenStream {
// Remove line terminator after escape to follow // Remove line terminator after escape to follow
// SpiderMonkey and C/C++ // SpiderMonkey and C/C++
c = getChar(); c = getChar();
continue strLoop; continue;
default: default:
if ('0' <= c && c < '8') { if ('0' <= c && c < '8') {
@ -605,8 +589,7 @@ class TokenStream {
c = getChar(false); c = getChar(false);
} }
final String str = getStringFromBuffer(); tokenEnd = cursor;
this.string = (String) allStrings.intern(str);
return quoteChar == '`' ? Token.TEMPLATE_LITERAL : Token.STRING; return quoteChar == '`' ? Token.TEMPLATE_LITERAL : Token.STRING;
} }
@ -722,14 +705,13 @@ class TokenStream {
return Token.GT; return Token.GT;
case '*': case '*':
if (languageVersion >= Context.VERSION_ES6) { if (languageVersion >= Context.VERSION_ES6 && matchChar('*')) {
if (matchChar('*')) { if (matchChar('=')) {
if (matchChar('=')) { return Token.ASSIGN_EXP;
return Token.ASSIGN_EXP;
}
return Token.EXP;
} }
return Token.EXP;
} }
if (matchChar('=')) { if (matchChar('=')) {
return Token.ASSIGN_MUL; return Token.ASSIGN_MUL;
} }
@ -920,7 +902,6 @@ class TokenStream {
} }
if (peekChar() == '*') { if (peekChar() == '*') {
tokenEnd = cursor - 1; tokenEnd = cursor - 1;
this.string = new String(stringBuffer, 0, stringBufferTop);
throw new ParsingException("msg.unterminated.re.lit"); throw new ParsingException("msg.unterminated.re.lit");
} }
} }
@ -944,7 +925,6 @@ class TokenStream {
} }
addToString(c); addToString(c);
} }
final int reEnd = stringBufferTop;
while (true) { while (true) {
c = getCharIgnoreLineEnd(); c = getCharIgnoreLineEnd();
@ -959,7 +939,6 @@ class TokenStream {
} }
tokenEnd = start + stringBufferTop + 2; // include slashes tokenEnd = start + stringBufferTop + 2; // include slashes
this.string = new String(stringBuffer, 0, reEnd);
} }
private String getStringFromBuffer() { private String getStringFromBuffer() {
@ -1019,7 +998,6 @@ class TokenStream {
for (;;) { for (;;) {
if (sourceCursor == sourceString.length()) { if (sourceCursor == sourceString.length()) {
hitEOF = true;
return EOF_CHAR; return EOF_CHAR;
} }
cursor++; cursor++;
@ -1031,7 +1009,6 @@ class TokenStream {
continue; continue;
} }
lineEndChar = -1; lineEndChar = -1;
lineStart = sourceCursor - 1;
lineno++; lineno++;
} }
@ -1078,42 +1055,6 @@ class TokenStream {
tokenEnd = cursor; tokenEnd = cursor;
} }
/** Return the current position of the scanner cursor. */
public int getCursor() {
return cursor;
}
/** Return the absolute source offset of the last scanned token. */
public int getTokenBeg() {
return tokenBeg;
}
/** Return the absolute source end-offset of the last scanned token. */
public int getTokenEnd() {
return tokenEnd;
}
/** Return tokenEnd - tokenBeg */
public int getTokenLength() {
return tokenEnd - tokenBeg;
}
public String getTokenRaw() {
return sourceString.substring(tokenBeg, tokenEnd);
}
private static String convertLastCharToHex(final String str) {
final int lastIndex = str.length() - 1;
final StringBuilder buf = new StringBuilder(str.substring(0, lastIndex));
buf.append("\\u");
final String hexCode = Integer.toHexString(str.charAt(lastIndex));
for (int i = 0; i < 4 - hexCode.length(); ++i) {
buf.append('0');
}
buf.append(hexCode);
return buf.toString();
}
public Token nextToken() throws ParsingException { public Token nextToken() throws ParsingException {
Token tt = getToken(); Token tt = getToken();
while (tt == Token.EOL || tt == Token.COMMENT) { while (tt == Token.EOL || tt == Token.COMMENT) {
@ -1124,19 +1065,14 @@ class TokenStream {
// stuff other than whitespace since start of line // stuff other than whitespace since start of line
private boolean dirtyLine; private boolean dirtyLine;
private String string = "";
private char[] stringBuffer = new char[128]; private char[] stringBuffer = new char[128];
private int stringBufferTop; private int stringBufferTop;
private final ObjToIntMap allStrings = new ObjToIntMap(50);
// Room to backtrace from to < on failed match of the last - in <!-- // Room to backtrace from to < on failed match of the last - in <!--
private final int[] ungetBuffer = new int[3]; private final int[] ungetBuffer = new int[3];
private int ungetCursor; private int ungetCursor;
private boolean hitEOF = false;
private int lineStart = 0;
private int lineEndChar = -1; private int lineEndChar = -1;
int lineno; int lineno;