Class Decompiler
- java.lang.Object
-
- org.htmlunit.corejs.javascript.Decompiler
-
public class Decompiler extends java.lang.Object
The following class save decompilation information about the source. Source information is returned from the parser as a String associated with function nodes and with the toplevel script. When saved in the constant pool of a class, this string will be UTF-8 encoded, and token values will occupy a single byte.Source is saved (mostly) as token numbers. The tokens saved pretty much correspond to the token stream of a 'canonical' representation of the input program, as directed by the parser. (There were a few cases where tokens could have been left out where decompiler could easily reconstruct them, but I left them in for clarity). (I also looked adding source collection to TokenStream instead, where I could have limited the changes to a few lines in getToken... but this wouldn't have saved any space in the resulting source representation, and would have meant that I'd have to duplicate parser logic in the decompiler to disambiguate situations where newlines are important.) The function decompile expands the tokens back into their string representations, using simple lookahead to correct spacing and indentation.
Assignments are saved as two-token pairs (Token.ASSIGN, op). Number tokens are stored inline, as a NUMBER token, a character representing the type, and either 1 or 4 characters representing the bit-encoding of the number. String types NAME, STRING and OBJECT are currently stored as a token type, followed by a character giving the length of the string (assumed to be less than 2^16), followed by the characters of the string inlined into the source string. Changing this to some reference to to the string in the compiled class' constant pool would probably save a lot of space... but would require some method of deriving the final constant pool entry from information available at parse time.
-
-
Field Summary
Fields Modifier and Type Field Description static int
CASE_GAP_PROP
Decompilation property to specify identation offset for case labels.private static int
FUNCTION_END
static int
INDENT_GAP_PROP
Decompilation property to specify default identation offset.static int
INITIAL_INDENT_PROP
Decompilation property to specify initial ident value.static int
ONLY_BODY_FLAG
Flag to indicate that the decompilation should omit the function header and trailing brace.private static boolean
printSource
private char[]
sourceBuffer
private int
sourceTop
static int
TO_SOURCE_FLAG
Flag to indicate that the decompilation generates toSource result.
-
Constructor Summary
Constructors Constructor Description Decompiler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) void
addBigInt(java.math.BigInteger n)
(package private) void
addEOL(int token)
(package private) void
addName(java.lang.String str)
(package private) void
addNumber(double n)
(package private) void
addRegexp(java.lang.String regexp, java.lang.String flags)
(package private) void
addString(java.lang.String str)
(package private) void
addTemplateLiteral(java.lang.String str)
(package private) void
addToken(int token)
private void
append(char c)
private void
appendString(java.lang.String str)
static java.lang.String
decompile(java.lang.String source, int flags, UintMap properties)
Decompile the source information associated with this js function/script back into a string.(package private) int
getCurrentOffset()
(package private) java.lang.String
getEncodedSource()
private static int
getNext(java.lang.String source, int length, int i)
private static int
getSourceStringEnd(java.lang.String source, int offset)
private void
increaseSourceCapacity(int minimalCapacity)
(package private) int
markFunctionEnd(int functionStart)
(package private) int
markFunctionStart(int functionType)
Deprecated.usemarkFunctionStart(int, boolean)
instead(package private) int
markFunctionStart(int functionType, boolean isGenerator)
private static int
printSourceBigInt(java.lang.String source, int offset, java.lang.StringBuilder sb)
private static int
printSourceNumber(java.lang.String source, int offset, java.lang.StringBuilder sb)
private static int
printSourceString(java.lang.String source, int offset, boolean asQuotedString, java.lang.StringBuilder sb)
private java.lang.String
sourceToString(int offset)
-
-
-
Field Detail
-
ONLY_BODY_FLAG
public static final int ONLY_BODY_FLAG
Flag to indicate that the decompilation should omit the function header and trailing brace.- See Also:
- Constant Field Values
-
TO_SOURCE_FLAG
public static final int TO_SOURCE_FLAG
Flag to indicate that the decompilation generates toSource result.- See Also:
- Constant Field Values
-
INITIAL_INDENT_PROP
public static final int INITIAL_INDENT_PROP
Decompilation property to specify initial ident value.- See Also:
- Constant Field Values
-
INDENT_GAP_PROP
public static final int INDENT_GAP_PROP
Decompilation property to specify default identation offset.- See Also:
- Constant Field Values
-
CASE_GAP_PROP
public static final int CASE_GAP_PROP
Decompilation property to specify identation offset for case labels.- See Also:
- Constant Field Values
-
FUNCTION_END
private static final int FUNCTION_END
- See Also:
- Constant Field Values
-
sourceBuffer
private char[] sourceBuffer
-
sourceTop
private int sourceTop
-
printSource
private static final boolean printSource
- See Also:
- Constant Field Values
-
-
Method Detail
-
getEncodedSource
java.lang.String getEncodedSource()
-
getCurrentOffset
int getCurrentOffset()
-
markFunctionStart
int markFunctionStart(int functionType, boolean isGenerator)
-
markFunctionStart
@Deprecated int markFunctionStart(int functionType)
Deprecated.usemarkFunctionStart(int, boolean)
instead
-
markFunctionEnd
int markFunctionEnd(int functionStart)
-
addToken
void addToken(int token)
-
addEOL
void addEOL(int token)
-
addName
void addName(java.lang.String str)
-
addString
void addString(java.lang.String str)
-
addTemplateLiteral
void addTemplateLiteral(java.lang.String str)
-
addRegexp
void addRegexp(java.lang.String regexp, java.lang.String flags)
-
addNumber
void addNumber(double n)
-
addBigInt
void addBigInt(java.math.BigInteger n)
-
appendString
private void appendString(java.lang.String str)
-
append
private void append(char c)
-
increaseSourceCapacity
private void increaseSourceCapacity(int minimalCapacity)
-
sourceToString
private java.lang.String sourceToString(int offset)
-
decompile
public static java.lang.String decompile(java.lang.String source, int flags, UintMap properties)
Decompile the source information associated with this js function/script back into a string. For the most part, this just means translating tokens back to their string representations; there's a little bit of lookahead logic to decide the proper spacing/indentation. Most of the work in mapping the original source to the prettyprinted decompiled version is done by the parser.- Parameters:
source
- encoded source tree presentationflags
- flags to select output formatproperties
- indentation properties
-
getNext
private static int getNext(java.lang.String source, int length, int i)
-
getSourceStringEnd
private static int getSourceStringEnd(java.lang.String source, int offset)
-
printSourceString
private static int printSourceString(java.lang.String source, int offset, boolean asQuotedString, java.lang.StringBuilder sb)
-
printSourceNumber
private static int printSourceNumber(java.lang.String source, int offset, java.lang.StringBuilder sb)
-
printSourceBigInt
private static int printSourceBigInt(java.lang.String source, int offset, java.lang.StringBuilder sb)
-
-