codeBlock.h

Engine/source/console/codeBlock.h

More...

Classes:

class

Core TorqueScript code management class.

Detailed Description

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