Class Codegen
- java.lang.Object
-
- org.htmlunit.corejs.javascript.optimizer.Codegen
-
-
Field Summary
Fields Modifier and Type Field Description private CompilerEnvirons
compilerEnv
(package private) static java.lang.String
DEFAULT_MAIN_METHOD_CLASS
private ObjArray
directCallTargets
(package private) static java.lang.String
FUNCTION_CONSTRUCTOR_SIGNATURE
(package private) static java.lang.String
FUNCTION_INIT_SIGNATURE
private static java.lang.Object
globalLock
private static int
globalSerialClassCounter
(package private) static java.lang.String
ID_FIELD_NAME
private double[]
itsConstantList
private int
itsConstantListSize
(package private) java.lang.String
mainClassName
(package private) java.lang.String
mainClassSignature
private java.lang.String
mainMethodClass
(package private) static java.lang.String
REGEXP_INIT_METHOD_NAME
(package private) static java.lang.String
REGEXP_INIT_METHOD_SIGNATURE
private ObjToIntMap
scriptOrFnIndexes
(package private) ScriptNode[]
scriptOrFnNodes
private static java.lang.String
SUPER_CLASS_NAME
(package private) static java.lang.String
TEMPLATE_LITERAL_INIT_METHOD_NAME
(package private) static java.lang.String
TEMPLATE_LITERAL_INIT_METHOD_SIGNATURE
-
Constructor Summary
Constructors Constructor Description Codegen()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
addDoubleWrap(ClassFileWriter cfw)
(package private) static java.lang.RuntimeException
badTree()
void
captureStackInfo(RhinoException ex)
Capture stack information from the given exception.(package private) java.lang.String
cleanName(ScriptNode n)
Gets a Java-compatible "informative" name for the the ScriptOrFnNodeprivate static void
collectScriptNodes_r(ScriptNode n, ObjArray x)
java.lang.Object
compile(CompilerEnvirons compilerEnv, ScriptNode tree, java.lang.String encodedSource, boolean returnFunction)
Compile the script or function from intermediate representation tree into an executable form.byte[]
compileToClassFile(CompilerEnvirons compilerEnv, java.lang.String mainClassName, ScriptNode scriptOrFn, java.lang.String encodedSource, boolean returnFunction)
Function
createFunctionObject(Context cx, Scriptable scope, java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
Create a function object.Script
createScriptObject(java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
Create a script object.private java.lang.Class<?>
defineClass(java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
private void
emitConstantDudeInitializers(ClassFileWriter cfw)
private void
emitDirectConstructor(ClassFileWriter cfw, OptFunctionNode ofn)
private void
emitRegExpInit(ClassFileWriter cfw)
private void
emitTemplateLiteralInit(ClassFileWriter cfw)
Overview:private void
generateCallMethod(ClassFileWriter cfw, boolean isStrictMode)
private byte[]
generateCode(java.lang.String encodedSource)
private static void
generateExecute(ClassFileWriter cfw)
private void
generateFunctionConstructor(ClassFileWriter cfw)
private void
generateFunctionInit(ClassFileWriter cfw, OptFunctionNode ofn)
private void
generateMain(ClassFileWriter cfw)
private void
generateNativeFunctionOverrides(ClassFileWriter cfw, java.lang.String encodedSource)
private void
generateResumeGenerator(ClassFileWriter cfw)
private static void
generateScriptCtor(ClassFileWriter cfw)
(package private) java.lang.String
getBodyMethodName(ScriptNode n)
(package private) java.lang.String
getBodyMethodSignature(ScriptNode n)
(package private) java.lang.String
getCompiledRegexpName(ScriptNode n, int regexpIndex)
(package private) java.lang.String
getDirectCtorName(ScriptNode n)
(package private) java.lang.String
getFunctionInitMethodName(OptFunctionNode ofn)
(package private) int
getIndex(ScriptNode n)
java.lang.String
getPatchedStack(RhinoException ex, java.lang.String nativeStackTrace)
Given a native stack trace, patch it with script-specific source and line informationjava.util.List<java.lang.String>
getScriptStack(RhinoException ex)
Get the script stack for the given exceptionjava.lang.String
getSourcePositionFromStack(Context cx, int[] linep)
Get the source position information by examining the stack.private static java.lang.String
getStaticConstantWrapperType(double num)
(package private) java.lang.String
getTemplateLiteralName(ScriptNode n)
private static void
initOptFunctions_r(ScriptNode scriptOrFn)
private void
initScriptNodesData(ScriptNode scriptOrFn)
(package private) static boolean
isGenerator(ScriptNode node)
(package private) void
pushNumberAsObject(ClassFileWriter cfw, double num)
(package private) static void
pushUndefined(ClassFileWriter cfw)
void
setEvalScriptFlag(Script script)
Mark the given script to indicate it was created by a call to eval() or to a Function constructor.void
setMainMethodClass(java.lang.String className)
private void
transform(ScriptNode tree)
-
-
-
Field Detail
-
DEFAULT_MAIN_METHOD_CLASS
static final java.lang.String DEFAULT_MAIN_METHOD_CLASS
- See Also:
- Constant Field Values
-
SUPER_CLASS_NAME
private static final java.lang.String SUPER_CLASS_NAME
- See Also:
- Constant Field Values
-
ID_FIELD_NAME
static final java.lang.String ID_FIELD_NAME
- See Also:
- Constant Field Values
-
REGEXP_INIT_METHOD_NAME
static final java.lang.String REGEXP_INIT_METHOD_NAME
- See Also:
- Constant Field Values
-
REGEXP_INIT_METHOD_SIGNATURE
static final java.lang.String REGEXP_INIT_METHOD_SIGNATURE
- See Also:
- Constant Field Values
-
TEMPLATE_LITERAL_INIT_METHOD_NAME
static final java.lang.String TEMPLATE_LITERAL_INIT_METHOD_NAME
- See Also:
- Constant Field Values
-
TEMPLATE_LITERAL_INIT_METHOD_SIGNATURE
static final java.lang.String TEMPLATE_LITERAL_INIT_METHOD_SIGNATURE
- See Also:
- Constant Field Values
-
FUNCTION_INIT_SIGNATURE
static final java.lang.String FUNCTION_INIT_SIGNATURE
- See Also:
- Constant Field Values
-
FUNCTION_CONSTRUCTOR_SIGNATURE
static final java.lang.String FUNCTION_CONSTRUCTOR_SIGNATURE
- See Also:
- Constant Field Values
-
globalLock
private static final java.lang.Object globalLock
-
globalSerialClassCounter
private static int globalSerialClassCounter
-
compilerEnv
private CompilerEnvirons compilerEnv
-
directCallTargets
private ObjArray directCallTargets
-
scriptOrFnNodes
ScriptNode[] scriptOrFnNodes
-
scriptOrFnIndexes
private ObjToIntMap scriptOrFnIndexes
-
mainMethodClass
private java.lang.String mainMethodClass
-
mainClassName
java.lang.String mainClassName
-
mainClassSignature
java.lang.String mainClassSignature
-
itsConstantList
private double[] itsConstantList
-
itsConstantListSize
private int itsConstantListSize
-
-
Method Detail
-
captureStackInfo
public void captureStackInfo(RhinoException ex)
Description copied from interface:Evaluator
Capture stack information from the given exception.- Specified by:
captureStackInfo
in interfaceEvaluator
- Parameters:
ex
- an exception thrown during execution
-
getSourcePositionFromStack
public java.lang.String getSourcePositionFromStack(Context cx, int[] linep)
Description copied from interface:Evaluator
Get the source position information by examining the stack.- Specified by:
getSourcePositionFromStack
in interfaceEvaluator
- Parameters:
cx
- Contextlinep
- Array object of length >= 1; getSourcePositionFromStack will assign the line number to linep[0].- Returns:
- the name of the file or other source container
-
getPatchedStack
public java.lang.String getPatchedStack(RhinoException ex, java.lang.String nativeStackTrace)
Description copied from interface:Evaluator
Given a native stack trace, patch it with script-specific source and line information- Specified by:
getPatchedStack
in interfaceEvaluator
- Parameters:
ex
- exceptionnativeStackTrace
- the native stack trace- Returns:
- patched stack trace
-
getScriptStack
public java.util.List<java.lang.String> getScriptStack(RhinoException ex)
Description copied from interface:Evaluator
Get the script stack for the given exception- Specified by:
getScriptStack
in interfaceEvaluator
- Parameters:
ex
- exception from execution- Returns:
- list of strings for the stack trace
-
setEvalScriptFlag
public void setEvalScriptFlag(Script script)
Description copied from interface:Evaluator
Mark the given script to indicate it was created by a call to eval() or to a Function constructor.- Specified by:
setEvalScriptFlag
in interfaceEvaluator
- Parameters:
script
- script to mark as from eval
-
compile
public java.lang.Object compile(CompilerEnvirons compilerEnv, ScriptNode tree, java.lang.String encodedSource, boolean returnFunction)
Description copied from interface:Evaluator
Compile the script or function from intermediate representation tree into an executable form.- Specified by:
compile
in interfaceEvaluator
- Parameters:
compilerEnv
- Compiler environmenttree
- parse treeencodedSource
- encoding of the source code for decompilationreturnFunction
- if true, compiling a function- Returns:
- an opaque object that can be passed to either createFunctionObject or createScriptObject, depending on the value of returnFunction
-
createScriptObject
public Script createScriptObject(java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
Description copied from interface:Evaluator
Create a script object.- Specified by:
createScriptObject
in interfaceEvaluator
- Parameters:
bytecode
- opaque object returned by compilestaticSecurityDomain
- security domain- Returns:
- Script object that can be evaluated
-
createFunctionObject
public Function createFunctionObject(Context cx, Scriptable scope, java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
Description copied from interface:Evaluator
Create a function object.- Specified by:
createFunctionObject
in interfaceEvaluator
- Parameters:
cx
- Current contextscope
- scope of the functionbytecode
- opaque object returned by compilestaticSecurityDomain
- security domain- Returns:
- Function object that can be called
-
defineClass
private java.lang.Class<?> defineClass(java.lang.Object bytecode, java.lang.Object staticSecurityDomain)
-
compileToClassFile
public byte[] compileToClassFile(CompilerEnvirons compilerEnv, java.lang.String mainClassName, ScriptNode scriptOrFn, java.lang.String encodedSource, boolean returnFunction)
-
transform
private void transform(ScriptNode tree)
-
initOptFunctions_r
private static void initOptFunctions_r(ScriptNode scriptOrFn)
-
initScriptNodesData
private void initScriptNodesData(ScriptNode scriptOrFn)
-
collectScriptNodes_r
private static void collectScriptNodes_r(ScriptNode n, ObjArray x)
-
generateCode
private byte[] generateCode(java.lang.String encodedSource)
-
emitDirectConstructor
private void emitDirectConstructor(ClassFileWriter cfw, OptFunctionNode ofn)
-
isGenerator
static boolean isGenerator(ScriptNode node)
-
generateResumeGenerator
private void generateResumeGenerator(ClassFileWriter cfw)
-
generateCallMethod
private void generateCallMethod(ClassFileWriter cfw, boolean isStrictMode)
-
generateMain
private void generateMain(ClassFileWriter cfw)
-
generateExecute
private static void generateExecute(ClassFileWriter cfw)
-
generateScriptCtor
private static void generateScriptCtor(ClassFileWriter cfw)
-
generateFunctionConstructor
private void generateFunctionConstructor(ClassFileWriter cfw)
-
generateFunctionInit
private void generateFunctionInit(ClassFileWriter cfw, OptFunctionNode ofn)
-
generateNativeFunctionOverrides
private void generateNativeFunctionOverrides(ClassFileWriter cfw, java.lang.String encodedSource)
-
emitRegExpInit
private void emitRegExpInit(ClassFileWriter cfw)
-
emitTemplateLiteralInit
private void emitTemplateLiteralInit(ClassFileWriter cfw)
Overview:for each fn in functions(script) do let field = [] for each templateLiteral in templateLiterals(fn) do let values = concat([[cooked(s), raw(s)] | s <- strings(templateLiteral)]) field.push(values) end class[getTemplateLiteralName(fn)] = field end
-
emitConstantDudeInitializers
private void emitConstantDudeInitializers(ClassFileWriter cfw)
-
pushNumberAsObject
void pushNumberAsObject(ClassFileWriter cfw, double num)
-
addDoubleWrap
private static void addDoubleWrap(ClassFileWriter cfw)
-
getStaticConstantWrapperType
private static java.lang.String getStaticConstantWrapperType(double num)
-
pushUndefined
static void pushUndefined(ClassFileWriter cfw)
-
getIndex
int getIndex(ScriptNode n)
-
getDirectCtorName
java.lang.String getDirectCtorName(ScriptNode n)
-
getBodyMethodName
java.lang.String getBodyMethodName(ScriptNode n)
-
cleanName
java.lang.String cleanName(ScriptNode n)
Gets a Java-compatible "informative" name for the the ScriptOrFnNode
-
getBodyMethodSignature
java.lang.String getBodyMethodSignature(ScriptNode n)
-
getFunctionInitMethodName
java.lang.String getFunctionInitMethodName(OptFunctionNode ofn)
-
getCompiledRegexpName
java.lang.String getCompiledRegexpName(ScriptNode n, int regexpIndex)
-
getTemplateLiteralName
java.lang.String getTemplateLiteralName(ScriptNode n)
-
badTree
static java.lang.RuntimeException badTree()
-
setMainMethodClass
public void setMainMethodClass(java.lang.String className)
-
-