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{
 39private:
 40   static CodeBlock* smCodeBlockList;
 41   static CodeBlock* smCurrentCodeBlock;
 42   
 43public:
 44   static bool                      smInFunction;
 45   static Compiler::ConsoleParser * smCurrentParser;
 46
 47   static CodeBlock* getCurrentBlock()
 48   {
 49      return smCurrentCodeBlock;
 50   }
 51
 52   static CodeBlock *getCodeBlockList()
 53   {
 54      return smCodeBlockList;
 55   }
 56
 57   static StringTableEntry getCurrentCodeBlockName();
 58   static StringTableEntry getCurrentCodeBlockFullPath();
 59   static StringTableEntry getCurrentCodeBlockModName();
 60   static CodeBlock *find(StringTableEntry);
 61
 62   CodeBlock();
 63   ~CodeBlock();
 64
 65   StringTableEntry name;
 66   StringTableEntry fullPath;
 67   StringTableEntry modPath;
 68
 69   char *globalStrings;
 70   char *functionStrings;
 71
 72   U32 functionStringsMaxLen;
 73   U32 globalStringsMaxLen;
 74
 75   F64 *globalFloats;
 76   F64 *functionFloats;
 77
 78   U32 codeSize;
 79   U32 *code;
 80
 81   U32 refCount;
 82   U32 lineBreakPairCount;
 83   U32 *lineBreakPairs;
 84   U32 breakListSize;
 85   U32 *breakList;
 86   CodeBlock *nextFile;
 87
 88   void addToCodeList();
 89   void removeFromCodeList();
 90   void calcBreakList();
 91   void clearAllBreaks();
 92   void setAllBreaks();
 93   void dumpInstructions( U32 startIp = 0, bool upToReturn = false );
 94
 95   /// Returns the first breakable line or 0 if none was found.
 96   /// @param lineNumber The one based line number.
 97   U32 findFirstBreakLine(U32 lineNumber);
 98
 99   void clearBreakpoint(U32 lineNumber);
100
101   /// Set a OP_BREAK instruction on a line. If a break 
102   /// is not possible on that line it returns false.
103   /// @param lineNumber The one based line number.
104   bool setBreakpoint(U32 lineNumber);
105
106   void findBreakLine(U32 ip, U32 &line, U32 &instruction);
107   const char *getFileLine(U32 ip);
108
109   /// 
110   String getFunctionArgs( U32 offset );
111
112   bool read(StringTableEntry fileName, Stream &st);
113   bool compile(const char *dsoName, StringTableEntry fileName, const char *script, bool overrideNoDso = false);
114
115   void incRefCount();
116   void decRefCount();
117
118   /// Compiles and executes a block of script storing the compiled code in this
119   /// CodeBlock. If there is no filename breakpoints will not be generated and 
120   /// the CodeBlock will not be added to the linked list of loaded CodeBlocks. 
121   /// Note that if the script contains no executable statements the CodeBlock
122   /// will delete itself on return an empty string. The return string is any 
123   /// result of the code executed, if any, or an empty string.
124   ///
125   /// @param fileName The file name, including path and extension, for the 
126   /// block of code or an empty string.
127   /// @param script The script code to compile and execute.
128   /// @param noCalls Skips calling functions from the script.
129   /// @param setFrame A zero based index of the stack frame to execute the code 
130   /// with, zero being the top of the stack. If the the index is
131   /// -1 a new frame is created. If the index is out of range the
132   /// top stack frame is used.
133   ConsoleValueRef compileExec(StringTableEntry fileName, const char *script, 
134      bool noCalls, S32 setFrame = -1 );
135
136   /// Executes the existing code in the CodeBlock. The return string is any 
137   /// result of the code executed, if any, or an empty string.
138   ///
139   /// @param offset The instruction offset to start executing from.
140   /// @param fnName The name of the function to execute or null.
141   /// @param ns The namespace of the function to execute or null.
142   /// @param argc The number of parameters passed to the function or
143   /// zero to execute code outside of a function.
144   /// @param argv The function parameter list.
145   /// @param noCalls Skips calling functions from the script.
146   /// @param setFrame A zero based index of the stack frame to execute the code 
147   /// with, zero being the top of the stack. If the the index is
148   /// -1 a new frame is created. If the index is out of range the
149   /// top stack frame is used.
150   /// @param packageName The code package name or null.
151   ConsoleValueRef exec(U32 offset, const char *fnName, Namespace *ns, U32 argc, 
152      ConsoleValueRef *argv, bool noCalls, StringTableEntry packageName,
153      S32 setFrame = -1);
154};
155
156#endif
157