Spinning Topp Logo BlackTopp Studios
inc
lua51scriptingengine.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _lua51scriptingengine_h
41 #define _lua51scriptingengine_h
42 
43 #include "datatypes.h"
44 #include "trie.h"
45 
46 #ifdef MEZZLUA51
47 
49 #include "Scripting/script.h"
51 
52 #include "entresolmanagerfactory.h"
53 
54 /// @file
55 /// @brief This file has the interface for the Lua based implementation of the Scripting system.
56 
57 /// @page LuaManual Mezzanine::Scripting Lua Manual
58 /// The files that are SWIG generated are made with the following commands run on an ubuntu machine
59 /// from the Mezzananine/src directory:
60 /// @code
61 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_MAIN -o Scripting/Lua51/scriptbindingmainlua51.cpp mezzanine.h
62 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_MAIN -DSWIG_UNSAFE -o Scripting/Lua51/scriptbindingmainlua51unsafe.cpp mezzanine.h
63 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_XML -o Scripting/Lua51/scriptbindingxmllua51.cpp XML/xml.h
64 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_XML -DSWIG_UNSAFE -o Scripting/Lua51/scriptbindingxmllua51unsafe.cpp XML/xml.h
65 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_THREADING -o Scripting/Lua51/scriptbindingthreadinglua51.cpp Threading/dagframescheduler.h
66 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_THREADING -DSWIG_UNSAFE -o Scripting/Lua51/scriptbindingthreadinglua51unsafe.cpp Threading/dagframescheduler.h
67 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_PHYSICS -o Scripting/Lua51/scriptbindingphysicslua51.cpp Physics/physics.h
68 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_PHYSICS -DSWIG_UNSAFE -o Scripting/Lua51/scriptbindingphysicslua51unsafe.cpp Physics/physics.h
69 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_MATHTOOLS -o Scripting/Lua51/scriptbindingmathtoolslua51.cpp
70 /// swig -c++ -v -Wall -lua -includeall -DMEZZLUA51 -DSWIG_MATHTOOLS -DSWIG_UNSAFE -o Scripting/Lua51/scriptbindingmathtoolslua51unsafe.cpp MathTools/mathtools.h
71 /// @endcode
72 /// Need to document "Mezzanine.", nspaces, calling conventions
73 ///
74 /// script class compilation
75 ///
76 /// This attempts to create "safe" and "open" version Lua binding to the Mezzanine. The safe version should be suitable
77 /// for scripts that can be trusted with the state of the simulation, but not the state of the calling system. Said
78 /// another way, script run against the "safe" version of the runtime cannot call function or create objects that load
79 /// modules, read files, write files, access a system shell or access any Mezzanine facility that can indirectly grant
80 /// access to features like these (the LuaScriptingEngine for example.)
81 
82 struct lua_State;
83 
84 namespace Mezzanine
85 {
86  namespace Scripting
87  {
88  class iScriptCompilationManager;
89  class Lua51Script;
90 
91  /// @brief This contains the Lua51 and Lua52 resources.
92  namespace Lua
93  {
94  ///////////////////////////////////////////////////////////////////////////////////////
95  /// @brief The workhorse of the Lua scripting system. All scripts come here to be executed.
97  {
98  ///////////////////////////////////////////////////////////////////////////////////////
99  // Internal Stuff first
100  private:
101  // Makes passing internal data much easier and all Lua51 are logically encapsulated as a single system still.
102  friend class Lua51Script;
103 
104  /// @brief The current state of the Lua runtime.
105  lua_State *State;
106 
107  protected:
108  /// @brief This will throw an exception.
109  /// @param LuaReturn The return code from a Lua Compile or execution call
110  /// @throws This Throws ScriptLuaYieldException, ScriptLuaRuntimeException, ScriptLuaRuntimeException, ScriptLuaErrErrException, SyntaxErrorLuaException, OutOfMemoryException, FileException, ScriptLuaException with as much precision as possible when thrown.
111  virtual void ThrowFromLuaErrorCode(int LuaReturn);
112 
113  /// @brief Checks the internal Lua to see if memory was correctly allocated during its creation
114  /// @throw If Lua could not get enouugh memory this throws an Exception::MM_OUT_OF_MEMORY_EXCEPTION
115  void CheckLuaStateAfterConstruction() const;
116 
117  /// @internal
118  /// @brief Put a lua script onto the stack, compiling it if needed.
119  /// @param ScriptToLoad A Script to put onto the lua stack.
120  /// @details This adds one item to the the Lua stack
121  void ScriptOntoStack(Lua51Script* ScriptToLoad);
122 
123  /// @internal
124  /// @brief Put the arguments in a script onto the stack
125  /// @param ScriptToLoad A Script with 0 or more arguments to load.
126  void ScriptArgsOntoStack(Lua51Script* ScriptToLoad);
127 
128  /// @internal
129  /// @brief Execute a thing on the top of the stack with arguments already put there
130  /// @param ArgumentCount How many args are onthe stack for the script
131  void StackExecute(Whole ArgumentCount);
132 
133  /// @internal
134  /// @brief Remove Scripts from Lua stack and put them into arguments on the passed script
135  /// @param ScriptWasRun the script was just run and needs to have its returns arguments loaded
136  /// @param PreviousStackSize How big was the stack before execution
137  /// @return The Amount of returns loaded into ScriptWasRun
138  Whole ScriptArgsFromStack(Lua51Script* ScriptWasRun, Integer PreviousStackSize);
139 
140 
141  /// @internal
142  /// @brief Get the top value of the Lua stack
143  /// @return A Counter Pointer to an iScriptArgument.
144  CountedPtr<iScriptArgument> ScriptArgFromStack();
145 
146  public:
147  /// @brief Intended only to make constructing an @ref Lua51ScriptingEngine with the desired libraries open a little easier.
149  {
150  NoLib = 0, ///< No libraries
151  BaseLib = 1, ///< Correlates to @ref Lua51ScriptingEngine::OpenBaseLibrary
152  PackageLib = 2, ///< Correlates to @ref Lua51ScriptingEngine::OpenPackageLibrary
153  StringLib = 4, ///< Correlates to @ref Lua51ScriptingEngine::OpenStringLibrary
154  TableLib = 8, ///< Correlates to @ref Lua51ScriptingEngine::OpenTableLibrary
155  MathLib = 16, ///< Correlates to @ref Lua51ScriptingEngine::OpenMathLibrary
156  IOLib = 32, ///< Correlates to @ref Lua51ScriptingEngine::OpenIOLibrary
157  OSLib = 64, ///< Correlates to @ref Lua51ScriptingEngine::OpenOSLibrary
158  DebugLib = 128, ///< Correlates to @ref Lua51ScriptingEngine::OpenDebugLibrary
159  MezzLib = 256, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineLibrary
160  MezzSafeLib = 512, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineSafeLibrary
161  MezzXMLLib = 1024, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineXMLLibrary
162  MezzXMLSafeLib = 2048, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineXMLSafeLibrary
163  MezzThreadingLib = 4096, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineThreadingLibrary
164  MezzThreadingSafeLib = 8192, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineThreadingSafeLibrary
165  MezzPhysicsLib = 16384, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzaninePhysicsLibrary
166  MezzPhysicsSafeLib = 32768, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzaninePhysicsSafeLibrary
167  MezzMathToolsLib = 65536, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineMathToolsLibrary
168  MezzMathToolsSafeLib = 131072, ///< Correlates to @ref Lua51ScriptingEngine::OpenMezzanineMathToolsSafeLibrary
169 
170 
171  FirstLib = BaseLib, ///< Useful for math based ways to work with libraries, This is equal to the numerically lowest lib
172  LastLib = MezzMathToolsSafeLib, ///< Useful for math based ways to work with libraries, This is equal to the numerically highest lib
173 
174  DefaultLibs = BaseLib | StringLib | TableLib | MathLib |
175  MezzSafeLib | MezzXMLSafeLib | MezzThreadingSafeLib |
176  MezzPhysicsSafeLib | MezzMathToolsSafeLib, ///< A quick way to refer to all the libraries opened by @ref Lua51ScriptingEngine::OpenDefaultLibraries
177  AllLibs = BaseLib | PackageLib | StringLib | TableLib |
178  MathLib | IOLib | OSLib | DebugLib | MezzLib |
179  MezzSafeLib | MezzXMLLib | MezzXMLSafeLib |
180  MezzThreadingLib | MezzThreadingSafeLib |
181  MezzPhysicsSafeLib | MezzPhysicsLib |
182  MezzMathToolsLib | MezzMathToolsSafeLib ///< A quick way to refer to all the libraries opened by @ref Lua51ScriptingEngine::OpenAllLibraries
183  };
184 
185  static const String NoLibName; ///< @brief The name used to identify No libraries, "None"
186  static const String BaseLibName; ///< @brief The name used to identify the Base library, "Base"
187  static const String PackageLibName; ///< @brief The name used to identify the Package library, "Package"
188  static const String StringLibName; ///< @brief The name used to identify the String library, "String"
189  static const String TableLibName; ///< @brief The name used to identify the Table library, "Table"
190  static const String MathLibName; ///< @brief The name used to identify the Math library, "Math"
191  static const String IOLibName; ///< @brief The name used to identify the IO library, "IO"
192  static const String OSLibName; ///< @brief The name used to identify the OS library, "OS"
193  static const String DebugLibName; ///< @brief The name used to identify the Debug library, "Debug"
194  static const String MezzLibName; ///< @brief The name used to identify the Mezzanine library, "Mezzanine"
195  static const String MezzSafeLibName; ///< @brief The name used to identify the MezzanineSafe library, "MezzanineSafe"
196  static const String MezzXMLLibName; ///< @brief The name used to identify the MezzanineXML library, "MezzanineXML"
197  static const String MezzXMLSafeLibName; ///< @brief The name used to identify the MezzanineXMLSafe library, "MezzanineXMLSafe"
198  static const String MezzThreadingLibName; ///< @brief The name used to identify the MezzanineThreading library, "MezzanineThreading"
199  static const String MezzThreadingSafeLibName; ///< @brief The name used to identify the MezzanineThreadingSafe library, "MezzanineThreadingSafe"
200  static const String MezzPhysicsLibName; ///< @brief The name used to identify the MezzaninePhysics library, "MezzaninePhysics"
201  static const String MezzPhysicsSafeLibName; ///< @brief The name used to identify the MezzaninePhysicsSafe library, "MezzaninePhysicsSafe"
202  static const String MezzMathToolsLibName; ///< @brief The name used to identify the MezzanineMathTools library, "MezzanineMathTools"
203  static const String MezzMathToolsSafeLibName; ///< @brief The name used to identify the MezzanineMathToolsSafe library, "MezzanineMathToolsSafe"
204  static const String DefaultLibsName; ///< @brief The name used to identify the Default set of libraries, "Default"
205  static const String AllLibsName; ///< @brief The name used to identify the set of all libraries, "All"
206 
207  static const String BaseTableName; ///< @brief The name used to identify a table loaded by the Base library, "coroutine"
208  static const String PackageTableName; ///< @brief The name used to identify a table loaded by the Package library, "package"
209  static const String StringTableName; ///< @brief The name used to identify a table loaded by the String library, "string"
210  static const String TableTableName; ///< @brief The name used to identify a table loaded by the Table library, "table"
211  static const String MathTableName; ///< @brief The name used to identify a table loaded by the Math library, "math"
212  static const String IOTableName; ///< @brief The name used to identify a table loaded by the IO library, "io"
213  static const String OSTableName; ///< @brief The name used to identify a table loaded by the OS library, "os"
214  static const String DebugTableName; ///< @brief The name used to identify a table loaded by the Debug library, "debug"
215  static const String MezzTableName; ///< @brief The name used to identify a table loaded by the Mezzanine library, "Mezzanine"
216  static const String MezzSafeTableName; ///< @brief The name used to identify a table loaded by the MezzanineSafe library, "MezzanineSafe"
217  static const String MezzXMLTableName; ///< @brief The name used to identify a table loaded by the MezzanineXML library, "MezzanineXML"
218  static const String MezzXMLSafeTableName; ///< @brief The name used to identify a table loaded by the MezzanineXMLSafe library, "MezzanineXMLSafe"
219  static const String MezzThreadingTableName; ///< @brief The name used to identify a table loaded by the MezzanineThreading library, "MezzanineThreading"
220  static const String MezzThreadingSafeTableName; ///< @brief The name used to identify a table loaded by the MezzanineThreadingSafe library, "MezzanineThreadingSafe"
221  static const String MezzPhysicsTableName; ///< @brief The name used to identify a table loaded by the MezzaninePhysics library, "MezzaninePhysics"
222  static const String MezzPhysicsSafeTableName; ///< @brief The name used to identify a table loaded by the MezzaninePhysicsSafe library, "MezzaninePhysicsSafe"
223  static const String MezzMathToolsTableName; ///< @brief The name used to identify a table loaded by the MezzanineMathTools library, "MezzanineMathTools"
224  static const String MezzMathToolsSafeTableName; ///< @brief The name used to identify a table loaded by the MezzanineMathToolsSafe library, "MezzanineMathToolsSafe"
225 
226  static const String TypeNameNil; ///< @brief A human friendly representation of the Lua type nil
227  static const String TypeNameBoolean; ///< @brief A human friendly representation of the Lua type boolean
228  static const String TypeNameLightUserData; ///< @brief A human friendly representation of the Lua type light user data
229  static const String TypeNameNumber; ///< @brief A human friendly representation of the Lua type number
230  static const String TypeNameString; ///< @brief A human friendly representation of the Lua type string
231  static const String TypeNameTable; ///< @brief A human friendly representation of the Lua type table
232  static const String TypeNameFunction; ///< @brief A human friendly representation of the Lua type function
233  static const String TypeNameUserData; ///< @brief A human friendly representation of the Lua type user data
234  static const String TypeNameThread; ///< @brief A human friendly representation of the Lua type thread
235 
236  static const String GlobalTableName; ///< @brief The place Lua keeps all the identifiers in a single Lua State
237 
238  static const String ScriptEngineName; ///< @brief The name of this scripting engine for inspection purposes, "Lua51ScriptingEngine".
239  static const ManagerBase::ManagerType InterfaceType; ///< @brief The type of functionality this manager provides. For ManagerBase compatibility.
240 
241  /// @brief Convert a Lua51Libraries value to its name
242  /// @param Lib A number indicating what libraries a Lua51ScriptingEngine could load
243  /// @return A reference to one of the library names listed on the Lua51ScriptingEngine
244  static const String& GetLibName(Lua51Libraries Lib);
245  /// @brief Convert a Lua51Libraries value to the name of a table it loads
246  /// @param Lib A number indicating what libraries a Lua51ScriptingEngine could load
247  /// @return A reference to one of the table names listed on the Lua51ScriptingEngine
248  static const String& GetTableName(Lua51Libraries Lib);
249  /// @brief Convert a string similar to one of the names on the Lua51ScriptingEngine to number
250  /// @param Name A string containing a name of one of the libraries (without regard to case)
251  /// @return The correlating enumeration value for the passed name.
252  static Lua51Libraries GetLibFromName(String Name);
253 
254  ///////////////////////////////////////////////////////////////////////////////////////
255  // Construction/Deconstruction
256  /// @brief Constructs a Scripting engine with a set of libraries preloaded.
257  /// @param LibrariesToOpen A Lua51Libraries bitmap indicating which libraries to load, this defaults to DefaultLibs
258  explicit Lua51ScriptingEngine(Lua51Libraries LibrariesToOpen=DefaultLibs);
259 
260  /// @brief Construct a from name value pairs
261  /// @param Params A collection of name value pairs indicating what to load and not load.
262  /// @details Each name can be the name of a libname (except None) and the value can
263  /// either be "Load" or "Unload". To indicate whether or not a library will be loaded
264  /// or not during instantation.
265  explicit Lua51ScriptingEngine(const NameValuePairList& Params);
266 
267  /// @brief Construct by deserializing
268  /// @param XMLNode An XML::Node That contains an A valid lua scripting engine.
269  explicit Lua51ScriptingEngine(const XML::Node& XMLNode);
270 
271  /// @brief Virtual Deconstructor
272  virtual ~Lua51ScriptingEngine();
273 
274  ///////////////////////////////////////////////////////////////////////////////////////
275  // Execution
276  /// @brief Compile and execute a passed string.
277  /// @param ScriptSource A String containing the source code to be executed.
278  /// @details This will create a CountPtr to a Lua51Script and assign both its Source
279  /// and Byte code
280  /// @return A CountedPtr to the script executed. This lets the caller retain a
281  /// pointer to the compiled script or this, whichever chooses to hold it longer and
282  /// allow for more advance caching
283  virtual CountedPtr<iScript> Execute(const String& ScriptSource);
284 
285  /// @brief Implements a required for iScriptManager, Calls Execute(CountedPtr<Lua51Script>)
286  /// @param ScriptToRun A CountedPtr<iScript> to be run. This is cast to an CountedPtr<Lua51Script> and called if possible.
287  /// @throw If this cannot be cast this throws a ParametersCastException.
288  /// @todo fill in the kind of exception thrown.
289  virtual void Execute(CountedPtr<iScript>& ScriptToRun);
290 
291  /// @brief This will execute the passed script, compiling it if not present
292  /// @param ScriptToRun The script to execute.
293  /// @details If a bytecode is present on ScriptToRun then it is executed. Otherwise the Source is
294  /// compiled and the result is set as the bytecode and it is executed.
295  virtual void Execute(CountedPtr<Lua51Script>& ScriptToRun);
296 
297  /// @brief Performs the compilation on a script reference
298  /// @param ScriptToRun A reference to the Lua51Script to compile and run.
299  virtual void Execute(Lua51Script& ScriptToRun);
300 
301  /// @brief Performs the compilation on a raw pointer
302  /// @param ScriptToRun A pointer to the Lua51Script to compile and run.
303  virtual void Execute(Lua51Script* ScriptToRun);
304 
305  ///////////////////////////////////////////////////////////////////////////////////////
306  // Compilation
307  /// @brief Calls Compile(CountedPtr<iScriptCompilable>) and returns a CountedPtr to the script created.
308  /// @param SourceToCompile A string Containing valid lua source code.
309  /// @return A CountedPtr<iScriptCompilable> pointing to a created Script object that contains the source and compile binary.
310  virtual CountedPtr<iScriptCompilable> Compile(const String& SourceToCompile);
311 
312  /// @brief Accepts an Counted ptr to a script and compiles it.
313  /// @param ScriptToCompile The CountedPtr to compile
314  /// @details The ByteCode member on the passed script is erased, if present,
315  /// and sets it to corresponding lua binary.
316  /// @throw If an invalid script is passed this throws ParametersCastException.
317  virtual void Compile(CountedPtr<iScriptCompilable>& ScriptToCompile);
318  /// @brief Compile a Lua51 script.
319  /// @param ScriptToCompile A CountedPtr to a Lua51Script.
320  /// @details Compiles the source code present in ScriptToCompile and puts the results back in
321  /// the ByteCode member on the script.
322  virtual void Compile(CountedPtr<Lua51Script>& ScriptToCompile);
323  /// @brief Performs the compilation on a script reference
324  /// @param ScriptToCompile A reference to the Lua51Script to compile.
325  virtual void Compile(Lua51Script& ScriptToCompile);
326  /// @brief Performs the compilation on a raw pointer.
327  /// @param ScriptToCompile A pointer to the Lua51Script to compile.
328  virtual void Compile(Lua51Script* ScriptToCompile);
329 
330  ///////////////////////////////////////////////////////////////////////////////////////
331  // For Inheritance
332  /// @copydoc ManagerBase::GetImplementationTypeName()
333  /// @return A String containing "Lua51ScriptingEngine".
334  virtual String GetImplementationTypeName() const;
335 
336  ///////////////////////////////////////////////////////////////////////////////////////
337  // Library Manipulation
338  /// @brief Makes Lua function calls in Lua standard libraries available for use in Lua scripts.
339  /// @param LibrariesToOpen A Lua51Libraries bitmap indicating which libraries to loads
340  virtual void OpenLibraries(int LibrariesToOpen);
341  /// @brief Check the Lua state to see if a table exists.
342  /// @param LibToCheck Indicator of which library to check
343  /// @return True if the library is ready for use and false otherwise.
344  virtual Boole IsLibraryOpen(Lua51Libraries LibToCheck);
345 
346  /// @brief Nest Lua modules to put libraries in more clean positions
347  /// @details this is used to create the syntax "Mezzanine.XML.Document()" for example.
348  /// "Mezzanine" is the base, "MezzanineXML" another module in the root lua environment
349  /// is the Sub and "XML" is the Alias. This does not "move" the library/module, this
350  /// Simply creates a reference with a new name and location.
351  /// @n @n
352  /// To ensure Tab completion works this also creates an alias/reference in the global
353  /// table. In this above example this will also incidentally create the syntax
354  /// "XML.Document()"
355  /// @param Base The libary that will have another nested in it.
356  /// @param Sub The library to be nest into another.
357  /// @param Alias The new name of the library when referenced from its nested location.
358  /// @warning If this fails this fails silently.
359  virtual void AliasLibrary(const String& Base, const String& Sub, const String& Alias);
360 
361  /// @brief Prepare most Mezzanine and some Lua functionality for use in Lua scripts.
362  /// @details This will load a the Lua Base, String, Table and Math libraries. This
363  /// will make Mezzanine functionality available that does not provide any arbitrary file
364  /// output or execution abilities.
365  /// @warning This enables lua scripts to load and execute arbitrary Lua scripts.
366  virtual void OpenDefaultLibraries();
367  /// @brief Make all Mezzanine and Lua functionality
368  /// @warning This makes arbitrary execution of programs and file IO available to scripts. This is not suitable if untrusted scripts will be run.
369  virtual void OpenAllLibraries();
370 
371  /// @brief Make some eof the more core functionality available to lua scripts.
372  /// @details See the Lua manual at http://www.lua.org/manual/5.1/manual.html#5.1 and http://www.lua.org/manual/5.1/manual.html#5.2 for further details on the
373  /// functionality this enables.
374  /// @warning This enables lua scripts to load and execute arbitrary Lua scripts.
375  virtual void OpenBaseLibrary();
376  /// @brief Allow Lua scripts to load other libraries.
377  /// @details see the Lua manual at http://www.lua.org/manual/5.1/manual.html#5.3 for details on what this enables. This does allow Lua scripts to load
378  /// other libraries, inlcuding the IO or OS libraries.
379  /// @warning This can makes full OS and file IO available to scripts. This is not suitable if untrusted scripts will be run.
380  virtual void OpenPackageLibrary();
381  /// @brief Allow Lua scripts access to the Lua string manipulation libary.
382  /// @details The details of what this enables can be read about in the Lua manual at http://www.lua.org/manual/5.1/manual.html#5.4 .
383  virtual void OpenStringLibrary();
384  /// @brief Enable Lua table manipulation functionality.
385  /// @details See the Lua manual at http://www.lua.org/manual/5.1/manual.html#5.5 for details.
386  virtual void OpenTableLibrary();
387  /// @brief Enable Lua math and random number functionality
388  /// @details The Lua manual at http://www.lua.org/manual/5.1/manual.html#5.6 as further details.
389  virtual void OpenMathLibrary();
390  /// @brief Enable Input/Output in lua scripts for reading and writing files
391  /// @details See http://www.lua.org/manual/5.1/manual.html#5.7 in the Lua manual for details
392  /// @warning This makes file IO available to scripts. This is not suitable if untrusted scripts will be run.
393  virtual void OpenIOLibrary();
394  /// @brief EnableOS facilities in lua scriptsm such as file managements, time and shell execution.
395  /// @details See http://www.lua.org/manual/5.1/manual.html#5.7 in the Lua manual for details
396  /// @warning This makes arbitrary execution of programs and file management available to scripts. This is not suitable if untrusted scripts will be run.
397  virtual void OpenOSLibrary();
398  /// @brief Enable Debugging Lua features.
399  /// @details This is the only Lua library omitted from the Defaults for functionality rather than security reasons. This
400  /// unimportant for most game uses and is primarily include for completeness. More details in the
401  /// Lua manual at http://www.lua.org/manual/5.1/manual.html#5.9 .
402  virtual void OpenDebugLibrary();
403 
404  /// @brief Make everything in the Mezzanine Libary available for use in Lua51 scripts.
405  /// @warning This makes arbitrary execution of programs and file management available to scripts. This is not suitable if untrusted scripts will be run.
406  virtual void OpenMezzanineLibrary();
407  /// @brief Make a subset of the Mezzanine Library available for use in Lua51 scripts.
408  /// @details This should not allow access to any functions, methods or classes than can execute code or manage files.
409  virtual void OpenMezzanineSafeLibrary();
410 
411  /// @brief Make the XML parts of the Mezzanine Libary available for use in Lua51 scripts.
412  /// @warning This makes arbitrary execution of programs and file management available to scripts. This is not suitable if untrusted scripts will be run.
413  virtual void OpenMezzanineXMLLibrary();
414  /// @brief Make the XML parts of the Mezzanine Libary available for use in Lua51 scripts.
415  /// @details This should not allow access to any functions, methods or classes than can execute code or manage files.
416  virtual void OpenMezzanineXMLSafeLibrary();
417 
418  /// @brief Make the Threading parts of the Mezzanine Libary available for use in Lua51 scripts.
419  /// @warning This makes a number of powerful feature available that could cause Denial of Service if untrusted scripts are run.
420  virtual void OpenMezzanineThreadingLibrary();
421  /// @brief Make the Threading parts of the Mezzanine Libary available for use in Lua51 scripts.
422  /// @details This should not allow access to any functions, methods or classes than can execute code or manage files or crash the client malicious ways.
423  virtual void OpenMezzanineThreadingSafeLibrary();
424 
425  /// @brief Make the Physics parts of the Mezzanine Libary available for use in Lua51 scripts.
426  /// @warning This makes a number of powerful feature available that could cause Denial of Service if untrusted scripts are run.
427  virtual void OpenMezzaninePhysicsLibrary();
428  /// @brief Make the Physics parts of the Mezzanine Libary available for use in Lua51 scripts.
429  /// @details This should not allow access to any functions, methods or classes than can execute code or manage files or crash the client malicious ways.
430  virtual void OpenMezzaninePhysicsSafeLibrary();
431 
432  /// @brief Make the MathTools parts of the Mezzanine Libary available for use in Lua51 scripts.
433  virtual void OpenMezzanineMathToolsLibrary();
434  /// @brief Make the MathTools parts of the Mezzanine Libary available for use in Lua51 scripts.
435  /// @details This should not allow access to any functions, methods or classes than can execute code or manage files or crash the client malicious ways.
436  virtual void OpenMezzanineMathToolsSafeLibrary();
437 
438 
439  protected:
440  /// @brief Set The MezzanineXML library as the XML member of the Mezzanine library or fail silently
441  void SetXML();
442  /// @brief Set The MezzanineXMLSafe library as the XML member of the MezzanineSafe library or fail silently
443  void SetXMLSafe();
444 
445  /// @brief Set The MezzanineThreading library as the Threading member of the Mezzanine library or fail silently
446  void SetThreading();
447  /// @brief Set The MezzanineThreadingSafe library as the Threading member of the MezzanineSafe library or fail silently
448  void SetThreadingSafe();
449 
450  /// @brief Set The MezzaninePhysics library as the Physics member of the Mezzanine library or fail silently
451  void SetPhysics();
452  /// @brief Set The MezzaninePhysicsSafe library as the Physics member of the MezzanineSafe library or fail silently
453  void SetPhysicsSafe();
454 
455  /// @brief Set The MezzanineMathTools library as the MathTools member of the Mezzanine library or fail silently
456  void SetMathTools();
457  /// @brief Set The MezzanineMathToolsSafe library as the MathTools member of the MezzanineSafe library or fail silently
458  void SetMathToolsSafe();
459 
460  ///////////////////////////////////////////////////////////////////////////////////////
461  // Other Lua Manipulation
462  public:
463  /// @brief Get the underlying Lua state that can be used with lua api calls directly.
464  /// @return The lua_State this uses.
465  /// @warning Don't use this, this is exposed for troubleshooting and when used for other tasks can cause no end of headaches.
466  lua_State* GetRawLuaState();
467 
468  /// @brief The stack is a specific part of the State relating to how data is passed between functions. How big is that?
469  /// @return
470  int GetStackCount();
471 
472  /// @brief Is the given character valid for starting a Lua Identifier
473  /// @param IdChar A Single character to check
474  /// @return True if IdChar can be used to start a lua variable name false otherwise
475  static bool IsValidCharStartIdentifier(const char IdChar);
476  /// @brief Is the given character valid for is in a Lua Identifier
477  /// @param IdChar A Single character to check
478  /// @return True if IdChar can be used in a lua variable name false otherwise
479  static bool IsValidCharInIdentifier(const char IdChar);
480  /// @brief Is the given character valid for is in a Lua Identifier including tables leading up to it and scoping operators.
481  /// @param IdChar A Single character to check
482  /// @return True if IdChar can be used in a lua variable name false otherwise
483  static bool IsValidCharInTableIdentifier(const char IdChar);
484  /// @brief Is a string a valid identifier in Lua
485  /// @param Id The identifier to check
486  /// @return True if Id is a valid Lua Indentifier
487  static bool IsValidIdentifier(const String& Id);
488 
489  /// @brief A type for efficiently storing the kinds of Lexigraphical data aboutthe Lua runtime
490  typedef Mezzanine::Trie<char,const Mezzanine::String*> CommandTrie;
491 
492  /// @brief Get a human friendly string containing the type of the thing at the given stack location
493  /// @param StackLocation Where to look in the Lua stack for item to inspect
494  /// @return A reference to a constant string on this class.
495  const String& GetLuaTypeString(int StackLocation);
496 
497  /// @brief Get the Value stored in a lua identifier
498  /// @param LuaIdentifier The name of the variable in lua to get the value from.
499  /// @return A counted pointer to an iScriptArgument to allow easy use of the "->GetString()", "->GetInteger()" and similar styles of syntax.
500  CountedPtr<iScriptArgument> GetValue(const String& LuaIdentifier);
501 
502  /// @internal
503  /// @brief Populate a Trie with all the members and types in the current lua runtime
504  /// @return The return value is provided in the CommandGroup argument.
505  /// @param CommandGroup The Trie to be populated.
506  /// @param TableName the name of the table to iterate over and populate the Trie with.
507  /// @param AlreadyDidTables To break cycles a list tables to skip descending into can be provided.
508  /// @warning This is marked as internal because it does far too much and should be broken in many smaller functions
509  void PopulateTabCompletionTrie(CommandTrie& CommandGroup, const String& TableName="", std::vector<String> AlreadyDidTables=std::vector<String>());
510  };
511 
512  ///////////////////////////////////////////////////////////////////////////////
513  /// @brief A factory responsible for the creation and destruction of the default scripting engine providing Lua 5.1 support.
514  /// @details This takes a construction info from the ManagerFactory and massages into something that Lua51ScriptingEngine
515  /// is designed to handle
516  ///////////////////////////////////////
518  {
519  public:
520  /// @brief Class constructor.
522  /// @brief Class destructor.
524 
525  /// @copydoc ManagerFactory::GetManagerImplName()
526  String GetManagerImplName() const;
527  /// @copydoc ManagerFactory::GetManagerType() const
528  ManagerBase::ManagerType GetManagerType() const;
529 
530  /// @copydoc EntresolManagerFactory::CreateManager(const NameValuePairList&)
531  EntresolManager* CreateManager(const NameValuePairList& Params);
532  /// @copydoc EntresolManagerFactory::CreateManager(const XML::Node&)
533  EntresolManager* CreateManager(const XML::Node& XMLNode);
534  /// @copydoc EntresolManagerFactory::DestroyManager(EntresolManager*)
535  void DestroyManager(EntresolManager* ToBeDestroyed);
536  };//ManagerFactory
537 
538  //simplistic error checking function, to be replace with proper exception driven code later.
539  //int MEZZ_LIB PrintErrorMessageOrNothing(int ErrorCode);
540 
541  // super simple lua test
542  //int MEZZ_LIB test();
543 
544  } // Lua
545  } // Scripting
546 } // Mezzanine
547 
548 
549 #endif // MEZZLUA51
550 #endif // \_scriptinglua_h
551 
552 
static const String MezzSafeTableName
The name used to identify a table loaded by the MezzanineSafe library, "MezzanineSafe".
What a scripting manager should look like.
static const String StringLibName
The name used to identify the String library, "String".
static const String MezzMathToolsTableName
The name used to identify a table loaded by the MezzanineMathTools library, "MezzanineMathTools".
static const String MezzMathToolsSafeTableName
The name used to identify a table loaded by the MezzanineMathToolsSafe library, "MezzanineMathToolsSa...
static const String TypeNameNil
A human friendly representation of the Lua type nil.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
static const String TypeNameUserData
A human friendly representation of the Lua type user data.
static const String MezzPhysicsLibName
The name used to identify the MezzaninePhysics library, "MezzaninePhysics".
static const String BaseLibName
The name used to identify the Base library, "Base".
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
static const String MezzThreadingSafeTableName
The name used to identify a table loaded by the MezzanineThreadingSafe library, "MezzanineThreadingSa...
This is a base class for factories that construct managers used by the Entresol class.
static const String MezzLibName
The name used to identify the Mezzanine library, "Mezzanine".
All the definitions for datatypes as well as some basic conversion functions are defined here...
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
This class is used to store a Lua script and in its source and compiled state.
Definition: lua51script.h:94
static const String DebugLibName
The name used to identify the Debug library, "Debug".
static const String PackageTableName
The name used to identify a table loaded by the Package library, "package".
This file has the interfaces for Scripts and tag derived classes.
static const String IOLibName
The name used to identify the IO library, "IO".
static const String MezzPhysicsSafeTableName
The name used to identify a table loaded by the MezzaninePhysicsSafe library, "MezzaninePhysicsSafe"...
static const String BaseTableName
The name used to identify a table loaded by the Base library, "coroutine".
static const String PackageLibName
The name used to identify the Package library, "Package".
Mezzanine::Trie< char, const Mezzanine::String * > CommandTrie
A type for efficiently storing the kinds of Lexigraphical data aboutthe Lua runtime.
static const String MezzPhysicsTableName
The name used to identify a table loaded by the MezzaninePhysics library, "MezzaninePhysics".
static const String TypeNameFunction
A human friendly representation of the Lua type function.
static const String MezzMathToolsLibName
The name used to identify the MezzanineMathTools library, "MezzanineMathTools".
static const String TypeNameBoolean
A human friendly representation of the Lua type boolean.
This is the base class for all managers that do no describe properties of a single world...
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
static const String StringTableName
The name used to identify a table loaded by the String library, "string".
static const String OSLibName
The name used to identify the OS library, "OS".
static const String MathTableName
The name used to identify a table loaded by the Math library, "math".
The workhorse of the Lua scripting system. All scripts come here to be executed.
static const String MezzMathToolsSafeLibName
The name used to identify the MezzanineMathToolsSafe library, "MezzanineMathToolsSafe".
static const String ScriptEngineName
The name of this scripting engine for inspection purposes, "Lua51ScriptingEngine".
static const String TableTableName
The name used to identify a table loaded by the Table library, "table".
A factory responsible for the creation and destruction of the default scripting engine providing Lua ...
static const String MezzPhysicsSafeLibName
The name used to identify the MezzaninePhysicsSafe library, "MezzaninePhysicsSafe".
static const String TypeNameNumber
A human friendly representation of the Lua type number.
static const String DebugTableName
The name used to identify a table loaded by the Debug library, "debug".
static const String TypeNameLightUserData
A human friendly representation of the Lua type light user data.
std::list< NameValuePair > NameValuePairList
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:206
static const String GlobalTableName
The place Lua keeps all the identifiers in a single Lua State.
static const String OSTableName
The name used to identify a table loaded by the OS library, "os".
static const String TypeNameTable
A human friendly representation of the Lua type table.
static const String MathLibName
The name used to identify the Math library, "Math".
This file has the interfaces for Script Arguments and the associated dependency chain.
static const String TypeNameString
A human friendly representation of the Lua type string.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
static const String IOTableName
The name used to identify a table loaded by the IO library, "io".
static const String MezzThreadingLibName
The name used to identify the MezzanineThreading library, "MezzanineThreading".
static const String MezzThreadingSafeLibName
The name used to identify the MezzanineThreadingSafe library, "MezzanineThreadingSafe".
static const String MezzThreadingTableName
The name used to identify a table loaded by the MezzanineThreading library, "MezzanineThreading".
static const String MezzXMLSafeTableName
The name used to identify a table loaded by the MezzanineXMLSafe library, "MezzanineXMLSafe".
static const String MezzXMLTableName
The name used to identify a table loaded by the MezzanineXML library, "MezzanineXML".
static const String MezzXMLLibName
The name used to identify the MezzanineXML library, "MezzanineXML".
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
static const String TableLibName
The name used to identify the Table library, "Table".
static const ManagerBase::ManagerType InterfaceType
The type of functionality this manager provides. For ManagerBase compatibility.
Lua51Libraries
Intended only to make constructing an Lua51ScriptingEngine with the desired libraries open a little e...
static const String MezzXMLSafeLibName
The name used to identify the MezzanineXMLSafe library, "MezzanineXMLSafe".
static const String MezzSafeLibName
The name used to identify the MezzanineSafe library, "MezzanineSafe".
static const String AllLibsName
The name used to identify the set of all libraries, "All".
This file has the interfaces for Scripting managers and the associated dependency chain...
static const String MezzTableName
The name used to identify a table loaded by the Mezzanine library, "Mezzanine".
static const String NoLibName
The name used to identify No libraries, "None".
static const String TypeNameThread
A human friendly representation of the Lua type thread.
static const String DefaultLibsName
The name used to identify the Default set of libraries, "Default".