Class Codegen

java.lang.Object
org.mozilla.javascript.optimizer.Codegen
All Implemented Interfaces:
Evaluator

public class Codegen extends Object implements Evaluator
This class generates code for a given IR tree.
  • Field Details

  • Constructor Details

    • Codegen

      public Codegen()
  • Method Details

    • captureStackInfo

      public void captureStackInfo(RhinoException ex)
      Description copied from interface: Evaluator
      Capture stack information from the given exception.
      Specified by:
      captureStackInfo in interface Evaluator
      Parameters:
      ex - an exception thrown during execution
    • getSourcePositionFromStack

      public String getSourcePositionFromStack(Context cx, int[] linep)
      Description copied from interface: Evaluator
      Get the source position information by examining the stack.
      Specified by:
      getSourcePositionFromStack in interface Evaluator
      Parameters:
      cx - Context
      linep - 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 String getPatchedStack(RhinoException ex, 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 interface Evaluator
      Parameters:
      ex - exception
      nativeStackTrace - the native stack trace
      Returns:
      patched stack trace
    • getScriptStack

      public List<String> getScriptStack(RhinoException ex)
      Description copied from interface: Evaluator
      Get the script stack for the given exception
      Specified by:
      getScriptStack in interface Evaluator
      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 interface Evaluator
      Parameters:
      script - script to mark as from eval
    • compile

      public Object compile(CompilerEnvirons compilerEnv, ScriptNode tree, 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 interface Evaluator
      Parameters:
      compilerEnv - Compiler environment
      tree - parse tree
      encodedSource - encoding of the source code for decompilation
      returnFunction - 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(Object bytecode, Object staticSecurityDomain)
      Description copied from interface: Evaluator
      Create a script object.
      Specified by:
      createScriptObject in interface Evaluator
      Parameters:
      bytecode - opaque object returned by compile
      staticSecurityDomain - security domain
      Returns:
      Script object that can be evaluated
    • createFunctionObject

      public Function createFunctionObject(Context cx, Scriptable scope, Object bytecode, Object staticSecurityDomain)
      Description copied from interface: Evaluator
      Create a function object.
      Specified by:
      createFunctionObject in interface Evaluator
      Parameters:
      cx - Current context
      scope - scope of the function
      bytecode - opaque object returned by compile
      staticSecurityDomain - security domain
      Returns:
      Function object that can be called
    • defineClass

      private Class<?> defineClass(Object bytecode, Object staticSecurityDomain)
    • compileToClassFile

      public byte[] compileToClassFile(CompilerEnvirons compilerEnv, String mainClassName, ScriptNode scriptOrFn, 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(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, 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 String getStaticConstantWrapperType(double num)
    • pushUndefined

      static void pushUndefined(ClassFileWriter cfw)
    • getIndex

      int getIndex(ScriptNode n)
    • getDirectCtorName

      String getDirectCtorName(ScriptNode n)
    • getBodyMethodName

      String getBodyMethodName(ScriptNode n)
    • cleanName

      String cleanName(ScriptNode n)
      Gets a Java-compatible "informative" name for the the ScriptOrFnNode
    • getBodyMethodSignature

      String getBodyMethodSignature(ScriptNode n)
    • getFunctionInitMethodName

      String getFunctionInitMethodName(OptFunctionNode ofn)
    • getCompiledRegexpName

      String getCompiledRegexpName(ScriptNode n, int regexpIndex)
    • getTemplateLiteralName

      String getTemplateLiteralName(ScriptNode n)
    • badTree

      static RuntimeException badTree()
    • setMainMethodClass

      public void setMainMethodClass(String className)