codeBlock.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CODEBLOCK_H_
  23. #define _CODEBLOCK_H_
  24. #include "console/compiler.h"
  25. #include "console/consoleParser.h"
  26. class Stream;
  27. /// Core TorqueScript code management class.
  28. ///
  29. /// This class represents a block of code, usually mapped directly to a file.
  30. class CodeBlock
  31. {
  32. private:
  33. static CodeBlock* smCodeBlockList;
  34. static CodeBlock* smCurrentCodeBlock;
  35. public:
  36. static U32 smBreakLineCount;
  37. static bool smInFunction;
  38. static Compiler::ConsoleParser * smCurrentParser;
  39. static CodeBlock* getCurrentBlock()
  40. {
  41. return smCurrentCodeBlock;
  42. }
  43. static CodeBlock *getCodeBlockList()
  44. {
  45. return smCodeBlockList;
  46. }
  47. static StringTableEntry getCurrentCodeBlockName();
  48. static StringTableEntry getCurrentCodeBlockFullPath();
  49. static StringTableEntry getCurrentCodeBlockModName();
  50. static CodeBlock *find(StringTableEntry);
  51. CodeBlock();
  52. ~CodeBlock();
  53. StringTableEntry name;
  54. StringTableEntry fullPath;
  55. StringTableEntry modPath;
  56. char *globalStrings;
  57. char *functionStrings;
  58. F64 *globalFloats;
  59. F64 *functionFloats;
  60. U32 codeSize;
  61. U32 *code;
  62. U32 refCount;
  63. U32 lineBreakPairCount;
  64. U32 *lineBreakPairs;
  65. U32 breakListSize;
  66. U32 *breakList;
  67. CodeBlock *nextFile;
  68. StringTableEntry mRoot;
  69. void addToCodeList();
  70. void removeFromCodeList();
  71. void calcBreakList();
  72. void clearAllBreaks();
  73. void setAllBreaks();
  74. /// Returns the first breakable line or 0 if none was found.
  75. /// @param lineNumber The one based line number.
  76. U32 findFirstBreakLine(U32 lineNumber);
  77. void clearBreakpoint(U32 lineNumber);
  78. /// Set a OP_BREAK instruction on a line. If a break
  79. /// is not possible on that line it returns false.
  80. /// @param lineNumber The one based line number.
  81. bool setBreakpoint(U32 lineNumber);
  82. void findBreakLine(U32 ip, U32 &line, U32 &instruction);
  83. void getFunctionArgs(char buffer[1024], U32 offset);
  84. const char *getFileLine(U32 ip);
  85. bool read(StringTableEntry fileName, Stream &st);
  86. bool compile(const char *dsoName, StringTableEntry fileName, const char *script);
  87. void incRefCount();
  88. void decRefCount();
  89. /// Compiles and executes a block of script storing the compiled code in this
  90. /// CodeBlock. If there is no filename breakpoints will not be generated and
  91. /// the CodeBlock will not be added to the linked list of loaded CodeBlocks.
  92. /// Note that if the script contains no executable statements the CodeBlock
  93. /// will delete itself on return an empty string. The return string is any
  94. /// result of the code executed, if any, or an empty string.
  95. ///
  96. /// @param fileName The file name, including path and extension, for the
  97. /// block of code or an empty string.
  98. /// @param script The script code to compile and execute.
  99. /// @param noCalls Skips calling functions from the script.
  100. /// @param setFrame A zero based index of the stack frame to execute the code
  101. /// with, zero being the top of the stack. If the the index is
  102. /// -1 a new frame is created. If the index is out of range the
  103. /// top stack frame is used.
  104. const char *compileExec(StringTableEntry fileName, const char *script,
  105. bool noCalls, int setFrame = -1 );
  106. /// Executes the existing code in the CodeBlock. The return string is any
  107. /// result of the code executed, if any, or an empty string.
  108. ///
  109. /// @param offset The instruction offset to start executing from.
  110. /// @param fnName The name of the function to execute or null.
  111. /// @param ns The namespace of the function to execute or null.
  112. /// @param argc The number of parameters passed to the function or
  113. /// zero to execute code outside of a function.
  114. /// @param argv The function parameter list.
  115. /// @param noCalls Skips calling functions from the script.
  116. /// @param setFrame A zero based index of the stack frame to execute the code
  117. /// with, zero being the top of the stack. If the the index is
  118. /// -1 a new frame is created. If the index is out of range the
  119. /// top stack frame is used.
  120. /// @param packageName The code package name or null.
  121. const char *exec(U32 offset, const char *fnName, Namespace *ns, U32 argc,
  122. const char **argv, bool noCalls, StringTableEntry packageName,
  123. S32 setFrame = -1);
  124. };
  125. #endif