Spinning Topp Logo BlackTopp Studios
inc
lua51scriptingengine.cpp
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_cpp
41 #define _lua51scriptingengine_cpp
42 
43 #include "datatypes.h"
44 
45 #ifdef MEZZLUA51
46 
47 #include "lua51script.h"
48 #include "lua51scriptargument.h"
49 #include "lua51scriptingengine.h"
50 #include "exception.h"
51 #include "stringtool.h"
52 
53 #include <cstring>
54 #include <cctype>
55 #include <cassert>
56 
57 /// @file
58 /// @brief This file has the implementation for the Lua based Scripting system.
59 
60 extern "C"
61 {
62  #include "lua.h" // Lua Core
63  #include "lualib.h" // for opening the base state
64  #include "lauxlib.h" // Extra Lua Goodies like lua_open()
65 
66  /// @internal
67  /// @brief Entry to Swig binding for loading the core Mezzanine library.
68  /// @param L the lua_State to load the library into.
69  /// @return A Lua error code capable of being converted to an exception by
70  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
71  int luaopen_Mezzanine(lua_State* L);
72  /// @internal
73  /// @brief Entry to Swig binding for loading the safe versions of the core Mezzanine library.
74  /// @param L the lua_State to load the library into.
75  /// @return A Lua error code capable of being converted to an exception by
76  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
77  int luaopen_MezzanineSafe(lua_State* L);
78 
79  /// @internal
80  /// @brief Entry to Swig binding for loading the XML library.
81  /// @param L the lua_State to load the library into.
82  /// @return A Lua error code capable of being converted to an exception by
83  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
84  int luaopen_MezzanineXML(lua_State* L);
85  /// @internal
86  /// @brief Entry to Swig binding for loading the safe version XML library.
87  /// @param L the lua_State to load the library into.
88  /// @return A Lua error code capable of being converted to an exception by
89  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
90  int luaopen_MezzanineXMLSafe(lua_State* L);
91 
92  /// @internal
93  /// @brief Entry to Swig binding for loading the FrameScheduler.
94  /// @param L the lua_State to load the library into.
95  /// @return A Lua error code capable of being converted to an exception by
96  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
97  int luaopen_MezzanineThreading(lua_State* L);
98  /// @internal
99  /// @brief Entry to Swig binding for loading the safe version of the FrameScheduler.
100  /// @param L the lua_State to load the library into.
101  /// @return A Lua error code capable of being converted to an exception by
102  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
103  int luaopen_MezzanineThreadingSafe(lua_State* L);
104 
105  /// @internal
106  /// @brief Entry to Swig binding for loading the Physics system.
107  /// @param L the lua_State to load the library into.
108  /// @return A Lua error code capable of being converted to an exception by
109  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
110  int luaopen_MezzaninePhysics(lua_State* L);
111  /// @internal
112  /// @brief Entry to Swig binding for loading the safe version of the Physics system.
113  /// @param L the lua_State to load the library into.
114  /// @return A Lua error code capable of being converted to an exception by
115  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
116  int luaopen_MezzaninePhysicsSafe(lua_State* L);
117 
118  /// @internal
119  /// @brief Entry to Swig binding for loading the MathTools.
120  /// @param L the lua_State to load the library into.
121  /// @return A Lua error code capable of being converted to an exception by
122  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
123  int luaopen_MezzanineMathTools(lua_State* L);
124  /// @internal
125  /// @brief Entry to Swig binding for loading the safe version of the MathTools.
126  /// @param L the lua_State to load the library into.
127  /// @return A Lua error code capable of being converted to an exception by
128  /// Lua51ScriptingEngine::ThrowFromLuaErrorCode(int).
129  int luaopen_MezzanineMathToolsSafe(lua_State* L);
130 }
131 
132 namespace {
133  // define a function to push a literal and a string based lua function
134 }
135 
136 namespace Mezzanine
137  {
138  namespace Scripting
139  {
140  namespace Lua
141  {
142 
143  ///////////////////////////////////////////////////////////////////////////////////////
144  // LuaScriptEngine and helpers
145  namespace
146  {
147  /// @internal
148  /// @brief Used in the LuaScriptingEngine to get data from lua_dump during script compilation
149  /// @param State The Lua state as provide by lua_dump
150  /// @param Buffer A pointer to the compiled Lua chunk.
151  /// @param Size The Size of the Lua chunk in bytes
152  /// @param BinBuff A pointer to a BinaryTools::BinaryBuffer that will serve as the real output
153  /// @note This is a lua_Writer as per http://www.lua.org/manual/5.1/manual.html#lua_Writer
154  int LuaBytecodeDumper(lua_State *State, const void* Buffer, size_t Size, void* BinBuff)
155  {
156  BinaryTools::BinaryBuffer* CompilingScript = reinterpret_cast<BinaryTools::BinaryBuffer*>(BinBuff);
157  CompilingScript->Concatenate(
159  Size
160  );
161  return 0;
162  }
163 
164  /// @internal
165  /// @param State The Lua state as provided by lua_load()
166  /// @param Buffer A BinaryBuffer containing the bytecode to load into Lua
167  /// @param Size an output parameter to convey the size of the return to Lua.
168  /// @return A pointer to a binary buffer suitable for Lua's use and the size of that buffer in the output parameter Size
169  /// @warning The Lua documentation clearly indicates sec ond parameter should be const and the third parameter should be a size_t* but the compiler says long unsigned int*
170  /// @note This is a lua_Reader as per http://www.lua.org/manual/5.1/manual.html#lua_Reader
171  const char* LuaBytecodeLoader(lua_State* State, void* BinBuff, size_t* Size)
172  {
173  FlaggedBuffer* LoadingBuffer = reinterpret_cast<FlaggedBuffer*>(BinBuff);
174  *Size = LoadingBuffer->Size;
175 
176  return (const char*)LoadingBuffer->Binary;
177  }
178 
179  /// @internal
180  /// @param State The Lua state as provided by lua_load()
181  /// @param Buffer A Lua51Script* containing the source to load into Lua
182  /// @param Size an output parameter to convey the size of the return to Lua.
183  /// @return A pointer to a binary buffer suitable for Lua's use and the size of that buffer in the output parameter Size
184  /// @warning The Lua documentation clearly indicates second parameter should be const and the third parameter should be a size_t* but the compiler says unsigned int*
185  /// @note This is a lua_Reader as per http://www.lua.org/manual/5.1/manual.html#lua_Reader
186  const char* LuaSourceLoader(lua_State* State, void* BinBuff, size_t* Size)
187  {
188  Lua51Script* LoadingScript = reinterpret_cast<Lua51Script*>(BinBuff);
189  if(LoadingScript->GetByteCodeReference().Loaded)
190  {
191  LoadingScript->GetByteCodeReference().Loaded = false;
192  return 0;
193  }else{
194  *Size = LoadingScript->GetSourceCode().size();
195  LoadingScript->GetByteCodeReference().Loaded = true;
196  return LoadingScript->GetSourceCode().c_str();
197  }
198  }
199 
200  }
201 
202  ///////////////////////////////////////////////////////////////////////////////////////
203  // Publically visible internals
204 
206  {
207  if(0==LuaReturn)
208  { return; }
209 
210  String ErrorMessage;
211  size_t Length;
212  const char* ErrorCString = lua_tolstring(this->State, -1, &Length);
213  ErrorMessage.reserve(Length+2);
214  ErrorMessage = String(ErrorCString);
215  lua_pop(State,1);
216  if(ErrorMessage.length()!=Length)
217  { MEZZ_EXCEPTION(ExceptionBase::SCRIPT_EXCEPTION_LUA, "Lua is putting odd things in error messages:\n"+ErrorMessage); }
218  ErrorMessage += "\n";
219 
220  switch(LuaReturn)
221  {
222  case LUA_YIELD:
223  MEZZ_EXCEPTION(ExceptionBase::SCRIPT_EXCEPTION_LUA_YIELD, "Lua returned a LUA_YIELD instead of completing:\n"+ErrorMessage);
224  case LUA_ERRRUN:
225  MEZZ_EXCEPTION(ExceptionBase::SCRIPT_EXCEPTION_LUA_RUNTIME, "There was a runtime Error handling the Lua script:\n"+ErrorMessage);
226  case LUA_ERRSYNTAX:
227  MEZZ_EXCEPTION(ExceptionBase::SYNTAX_ERROR_EXCEPTION_LUA, "There was an error with the syntax of the Lua script:\n"+ErrorMessage);
228  case LUA_ERRERR:
229  MEZZ_EXCEPTION(ExceptionBase::SCRIPT_EXCEPTION_LUA_ERRERR, "There was an error when Lua attempted to handle an error:\n"+ErrorMessage);
230  case LUA_ERRMEM:
231  MEZZ_EXCEPTION(ExceptionBase::MM_OUT_OF_MEMORY_EXCEPTION, "Lua could not allocate memory:\n"+ErrorMessage);
232  case LUA_ERRFILE:
233  MEZZ_EXCEPTION(ExceptionBase::IO_FILE_EXCEPTION, "Lua had an error with file IO:\n"+ErrorMessage);
234  default:
235  MEZZ_EXCEPTION(ExceptionBase::SCRIPT_EXCEPTION_LUA, "Lua had an error and we are not sure what it was:\n"+ErrorMessage);
236  }
237  }
238 
240  {
241  if(NULL==State)
242  { MEZZ_EXCEPTION(ExceptionBase::MM_OUT_OF_MEMORY_EXCEPTION, "Could not allocate Memory for Lua interpretter"); }
243  }
244 
246  {
247  if(ScriptToLoad->FunctionCall)
248  {
249  lua_getglobal(this->State,ScriptToLoad->SourceCode.c_str());
250  }else{
251  if(!ScriptToLoad->IsCompiled())
252  { Compile(ScriptToLoad); }
253  else
254  {
256  lua_load(this->State, LuaBytecodeLoader, &(ScriptToLoad->GetByteCodeReference()), ScriptToLoad->GetName().c_str())
257  );
258  }
259  }
260  }
261 
263  {
264  LuaArgument* Current;
265  for(ArgumentGroup::const_iterator Iter = ScriptToLoad->Args.begin();
266  Iter != ScriptToLoad->Args.end();
267  Iter++ )
268  {
269  Current = dynamic_cast<LuaArgument*>(Iter->Get());
270  if(Current)
271  { Current->Push(this->State); }
272  else
273  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for parameter purposes.") }
274  }
275  }
276 
278  {
280  lua_pcall(this->State, ArgumentCount, LUA_MULTRET, 0)
281  );
282  }
283 
285  {
286  while(lua_gettop(State)>PreviousStackSize)
287  { ScriptWasRun->AddReturn(ScriptArgFromStack()); }
288 
289  return PreviousStackSize-lua_gettop(State);
290 // LuaArgument* Current;
291 // for(ArgumentGroup::iterator Iter = ScriptWasRun->Returns.begin();
292 // Iter != ScriptWasRun->Returns.end();
293 // Iter++ )
294 // {
295 // Current = dynamic_cast<LuaArgument*>(Iter->Get());
296 // if(Current)
297 // { Current->Pop(this->State); }
298 // else
299 // { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes.") }
300 // }
301  }
302 
304  {
305  iScriptArgument* Results = NULL;
306  Integer StackLocation = lua_gettop(State);
307  if(LUA_TNIL==lua_type(State,StackLocation))
308  { Results = new Lua51NilArgument; }
309  if(LUA_TBOOLEAN==lua_type(State,StackLocation))
310  { Results = new Lua51BoolArgument; }
311  if(LUA_TLIGHTUSERDATA==lua_type(State,StackLocation))
312  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes. Found a LightUserData."); }
313  if(LUA_TNUMBER==lua_type(State,StackLocation))
314  { Results = new Lua51RealArgument; }
315  if(LUA_TSTRING==lua_type(State,StackLocation))
316  { Results = new Lua51StringArgument; }
317  if(LUA_TTABLE==lua_type(State,StackLocation))
318  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes. Found a Table."); }
319  if(LUA_TFUNCTION==lua_type(State,StackLocation))
320  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes. Found a Function."); }
321  if(LUA_TUSERDATA==lua_type(State,StackLocation))
322  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes. Found a UserData."); }
323  if(LUA_TTHREAD==lua_type(State,StackLocation))
324  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "A LuaArgument could not be converted as one for return value purposes. Found a Thread."); }
325 
326  dynamic_cast<LuaArgument*>(Results)->Pop(State);
327  return CountedPtr<iScriptArgument>(Results);
328  }
329 
339  const String Lua51ScriptingEngine::MezzLibName = "Mezzanine";
340  const String Lua51ScriptingEngine::MezzSafeLibName = "MezzanineSafe";
341  const String Lua51ScriptingEngine::MezzXMLLibName = "MezzanineXML";
342  const String Lua51ScriptingEngine::MezzXMLSafeLibName = "MezzanineXMLSafe";
343  const String Lua51ScriptingEngine::MezzThreadingLibName = "MezzanineThreading";
344  const String Lua51ScriptingEngine::MezzThreadingSafeLibName = "MezzanineThreadingSafe";
345  const String Lua51ScriptingEngine::MezzPhysicsLibName = "MezzaninePhysics";
346  const String Lua51ScriptingEngine::MezzPhysicsSafeLibName = "MezzaninePhysicsSafe";
347  const String Lua51ScriptingEngine::MezzMathToolsLibName = "MezzanineMathTools";
348  const String Lua51ScriptingEngine::MezzMathToolsSafeLibName = "MezzanineMathToolsSafe";
351 
352  const String Lua51ScriptingEngine::BaseTableName = "coroutine";
360  const String Lua51ScriptingEngine::MezzTableName = "Mezzanine";
361  const String Lua51ScriptingEngine::MezzSafeTableName = "MezzanineSafe";
362  const String Lua51ScriptingEngine::MezzXMLTableName = "MezzanineXML";
363  const String Lua51ScriptingEngine::MezzXMLSafeTableName = "MezzanineXMLSafe";
364  const String Lua51ScriptingEngine::MezzThreadingTableName = "MezzanineThreading";
365  const String Lua51ScriptingEngine::MezzThreadingSafeTableName = "MezzanineThreadingSafe";
366  const String Lua51ScriptingEngine::MezzPhysicsTableName = "MezzaninePhysics";
367  const String Lua51ScriptingEngine::MezzPhysicsSafeTableName = "MezzaninePhysicsSafe";
368  const String Lua51ScriptingEngine::MezzMathToolsTableName = "MezzanineMathTools";
369  const String Lua51ScriptingEngine::MezzMathToolsSafeTableName = "MezzanineMathToolsSafe";
370 
373  const String Lua51ScriptingEngine::TypeNameLightUserData = "Light User Data";
380 
382 
383  const String Lua51ScriptingEngine::ScriptEngineName = "Lua51ScriptingEngine";
384  const ManagerBase::ManagerType Lua51ScriptingEngine::InterfaceType = ManagerBase::MT_ScriptingManager;
385 
387  {
388  switch(Lib)
389  {
390  case NoLib: return NoLibName;
391  case BaseLib: return BaseLibName;
392  case PackageLib: return PackageLibName;
393  case StringLib: return StringLibName;
394  case TableLib: return TableLibName;
395  case MathLib: return MathLibName;
396  case IOLib: return IOLibName;
397  case OSLib: return OSLibName;
398  case DebugLib: return DebugLibName;
399  case MezzLib: return MezzLibName;
400  case MezzSafeLib: return MezzSafeLibName;
401  case MezzXMLLib: return MezzXMLLibName;
402  case MezzXMLSafeLib: return MezzXMLSafeLibName;
405  case MezzPhysicsLib: return MezzPhysicsLibName;
409  case DefaultLibs: return DefaultLibsName;
410  case AllLibs: return AllLibsName;
411  default: MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Cannot convert given value to library string: " + ToString(Lib));
412  }
413  }
414 
416  {
417  switch(Lib)
418  {
419  case BaseLib: return BaseTableName;
420  case PackageLib: return PackageTableName;
421  case StringLib: return StringTableName;
422  case TableLib: return TableTableName;
423  case MathLib: return MathTableName;
424  case IOLib: return IOTableName;
425  case OSLib: return OSTableName;
426  case DebugLib: return DebugTableName;
427  case MezzLib: return MezzTableName;
428  case MezzSafeLib: return MezzSafeTableName;
429  case MezzXMLLib: return MezzXMLTableName;
437  default: MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Cannot convert given value to table string: " + ToString(Lib));
438  }
439  }
440 
442  {
443  if(!Name.size())
444  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Cannot convert zero length name to valid lua library name"); }
445  using namespace Mezzanine::StringTools;
446  ToLowerCase(Name);
447 
448  switch(Name[0])
449  {
450  case 'n': if(Name==LowerCaseCopy(NoLibName)) { return NoLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with n: " + Name); }
451  case 'b': if(Name==LowerCaseCopy(BaseLibName)) { return BaseLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with b: " + Name); }
452  case 's': if(Name==LowerCaseCopy(StringLibName)) { return StringLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with s: " + Name); }
453  case 't': if(Name==LowerCaseCopy(TableLibName)) { return TableLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with t: " + Name); }
454  case 'i': if(Name==LowerCaseCopy(IOLibName)) { return IOLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with i: " + Name); }
455  case 'o': if(Name==LowerCaseCopy(OSLibName)) { return OSLib; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with o: " + Name); }
456  case 'a': if(Name==LowerCaseCopy(AllLibsName)) { return AllLibs; } else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with a: " + Name); }
457  case 'p':
458  if (Name==LowerCaseCopy(PackageLibName)) { return PackageLib; }
459  else if(Name==LowerCaseCopy(MezzPhysicsLibName)) { return MezzPhysicsLib; }
460  else if(Name==LowerCaseCopy(MezzPhysicsSafeLibName)) { return MezzPhysicsSafeLib; }
461  else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with p: " + Name); }
462  case 'm':
463  if (Name==LowerCaseCopy(MathLibName)) { return MathLib; }
464  else if(Name==LowerCaseCopy(MezzLibName)) { return MezzLib; }
465  else if(Name==LowerCaseCopy(MezzSafeLibName)) { return MezzSafeLib; }
466  else if(Name==LowerCaseCopy(MezzXMLLibName)) { return MezzXMLLib; }
467  else if(Name==LowerCaseCopy(MezzXMLSafeLibName)) { return MezzXMLSafeLib; }
468  else if(Name==LowerCaseCopy(MezzThreadingLibName)) { return MezzThreadingLib; }
470  else if(Name==LowerCaseCopy(MezzThreadingLibName)) { return MezzMathToolsLib; }
472  else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with m: " + Name); }
473  case 'd':
474  if (Name==LowerCaseCopy(DebugLibName)) { return DebugLib; }
475  else if(Name==LowerCaseCopy(DefaultLibsName)) { return DefaultLibs; }
476  else { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert name starting with d: " + Name); }
477  default: MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Could not convert: " + Name);
478  }
479  }
480 
481  ///////////////////////////////////////////////////////////////////////////////////////
482  // Construction/Deconstruction
483  Lua51ScriptingEngine::Lua51ScriptingEngine(Lua51Libraries LibrariesToOpen) : State(luaL_newstate())
484  {
486  OpenLibraries(LibrariesToOpen);
487  }
488 
489  Lua51ScriptingEngine::Lua51ScriptingEngine(const NameValuePairList& Params) : State(luaL_newstate())
490  {
492  Integer ToLoad = NoLib;
493  if(Params.empty())
494  { ToLoad = DefaultLibs; }
495  else
496  {
497  for(NameValuePairList::const_iterator Iter = Params.begin(); Params.end() != Iter; Iter++ )
498  {
499  Lua51Libraries Lib( GetLibFromName(Iter->first) );
500  String LoadState(Iter->second);
501  if(String("Load")==LoadState)
502  { ToLoad |= Lib; }
503  else{
504  if(String("Unload")==LoadState)
505  { ToLoad &= ~Lib; }
506  else
507  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Unknown loadstate parameter, during name value pair construction."); }
508  }
509  }
510  }
511  OpenLibraries( (Lua51Libraries)ToLoad );
512  }
513 
514  Lua51ScriptingEngine::Lua51ScriptingEngine(const XML::Node& XMLNode) : State(luaL_newstate())
515  {
518  }
519 
521  { lua_close(State); }
522 
523  ///////////////////////////////////////////////////////////////////////////////////////
524  // Execution
526  {
527  CountedPtr<Lua51Script> Results = Compile(ScriptSource);
528  Execute(Results);
529  return CountedPtrCast<iScript>(Results);
530  }
531 
533  {
534  CountedPtr<Lua51Script> ScriptToCompile = CountedPtrCast<Lua51Script>(ScriptToRun);
535  if(ScriptToCompile)
536  {
537  Execute(ScriptToCompile);
538  }else{
539  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "Lua51 Engine attempted to execute a script, but it did not appear to bea Lua51 script.")
540  }
541  }
542 
544  { Execute(ScriptToRun.Get()); }
545 
547  { Execute(&ScriptToRun); }
549  {
550  Integer StackSize = lua_gettop(this->State);
551  ScriptOntoStack(ScriptToRun);
552  assert(lua_gettop(this->State) == StackSize + 1);
553  ScriptArgsOntoStack(ScriptToRun);
554  assert(lua_gettop(this->State) == StackSize + 1 + Integer(ScriptToRun->GetArgumentCount()));
555  StackExecute(ScriptToRun->GetArgumentCount());
556  ScriptArgsFromStack(ScriptToRun, StackSize);
557  assert(lua_gettop(this->State) == StackSize);
558  }
559 
560  ///////////////////////////////////////////////////////////////////////////////////////
561  // Compilation
563  {
564  CountedPtr<Lua51Script> Results(
565  new Lua51Script(SourceToCompile,this)
566  );
567  return Results;
568  }
569 
571  {
572  CountedPtr<Lua51Script> ConvertedScript = CountedPtrCast<Lua51Script>(ScriptToCompile);
573  if(ConvertedScript)
574  {
575  Compile(ConvertedScript);
576  }else{
577  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_CAST_EXCEPTION, "Lua51 Engine attempted to compile a script, but it did not appear to bea Lua51 script.")
578  }
579  }
580 
582  { Compile(ScriptToCompile.Get()); }
583 
585  { Compile(&ScriptToCompile); }
586 
588  {
590  lua_load(this->State, LuaSourceLoader, ScriptToCompile, ScriptToCompile->GetName().c_str())
591  );
593  //lua_dump(this->State, LuaBytecodeDumper, &(ScriptToCompile->CompiledByteCode) )
594  lua_dump(this->State, LuaBytecodeDumper, &ScriptToCompile->GetByteCodeReference() )
595  );
596  lua_pop(State,1);
597  }
598 
599  ///////////////////////////////////////////////////////////////////////////////////////
600  // For Inheritance
602  { return ScriptEngineName; }
603 
604  ///////////////////////////////////////////////////////////////////////////////////////
605  // Library Manipulation
606  void Lua51ScriptingEngine::OpenLibraries(int LibrariesToOpen)
607  {
608  if(LibrariesToOpen & BaseLib)
609  { OpenBaseLibrary(); }
610  if(LibrariesToOpen & PackageLib)
611  { OpenPackageLibrary(); }
612  if(LibrariesToOpen & StringLib)
613  { OpenStringLibrary(); }
614  if(LibrariesToOpen & TableLib)
615  { OpenTableLibrary(); }
616  if(LibrariesToOpen & MathLib)
617  { OpenMathLibrary(); }
618  if(LibrariesToOpen & IOLib)
619  { OpenIOLibrary(); }
620  if(LibrariesToOpen & OSLib)
621  { OpenOSLibrary(); }
622  if(LibrariesToOpen & DebugLib)
623  { OpenDebugLibrary(); }
624  if(LibrariesToOpen & MezzLib)
625  { OpenMezzanineLibrary(); }
626  if(LibrariesToOpen & MezzSafeLib)
628  if(LibrariesToOpen & MezzXMLLib)
630  if(LibrariesToOpen & MezzXMLSafeLib)
632  if(LibrariesToOpen & MezzThreadingLib)
634  if(LibrariesToOpen & MezzThreadingSafeLib)
636  if(LibrariesToOpen & MezzPhysicsLib)
638  if(LibrariesToOpen & MezzPhysicsSafeLib)
640  if(LibrariesToOpen & MezzMathToolsLib)
642  if(LibrariesToOpen & MezzMathToolsSafeLib)
644  }
645 
647  {
648  lua_getglobal(State, GetTableName(LibToCheck).c_str());
649  if (lua_istable(State, -1))
650  {
651  lua_pop(State,1);
652  return true;
653  }
654  lua_pop(State,1);
655  return false;
656  }
657 
658  void Lua51ScriptingEngine::AliasLibrary(const String& Base, const String& Sub, const String& Alias)
659  {
660  lua_getglobal(State, Base.c_str());
661  if (lua_istable(State, -1))
662  {
663  lua_pushstring(State, Alias.c_str());
664  lua_getglobal(State, Sub.c_str());
665  lua_settable(State, -3); // Set the table a -3, Mezzanine to have the index defined by -2 "XML" set to the value at -1 "The MezzanineXML Table"
666  lua_pop(State,1);
667  } //else Fail Silently
668 
669  lua_getglobal(State, GlobalTableName.c_str());
670  if (lua_istable(State, -1))
671  {
672  lua_pushstring(State, Alias.c_str());
673  lua_getglobal(State, Sub.c_str());
674  lua_settable(State, -3);
675  lua_pop(State,1);
676  } //else Fail Silently
677  }
678 
682  { OpenLibraries(AllLibs); }
683 
685  {
686  lua_pushcfunction(State, luaopen_base);
687  lua_pushliteral(State, LUA_COLIBNAME);
688  lua_call(State, 1, 0);
689  }
691  {
692  lua_pushcfunction(State, luaopen_package);
693  lua_pushliteral(State, LUA_LOADLIBNAME);
694  lua_call(State, 1, 0);
695  }
697  {
698  lua_pushcfunction(State, luaopen_string);
699  lua_pushliteral(State, LUA_STRLIBNAME);
700  lua_call(State, 1, 0);
701  }
703  {
704  lua_pushcfunction(State, luaopen_table);
705  lua_pushliteral(State, LUA_TABLIBNAME);
706  lua_call(State, 1, 0);
707  }
709  {
710  lua_pushcfunction(State, luaopen_math);
711  lua_pushliteral(State, LUA_MATHLIBNAME);
712  lua_call(State, 1, 0);
713  }
715  {
716  lua_pushcfunction(State, luaopen_io);
717  lua_pushliteral(State, LUA_IOLIBNAME);
718  lua_call(State, 1, 0);
719  }
721  {
722  lua_pushcfunction(State, luaopen_os);
723  lua_pushliteral(State, LUA_OSLIBNAME);
724  lua_call(State, 1, 0);
725  }
727  {
728  lua_pushcfunction(State, luaopen_debug);
729  lua_pushliteral(State, LUA_DBLIBNAME);
730  lua_call(State, 1, 0);
731  }
732 
734  {
735  lua_pushcfunction(State, luaopen_Mezzanine);
736  lua_pushstring(State, (MezzLibName.c_str()) );
737  lua_call(State, 1, 0);
738  //SetM();
739  }
741  {
742  lua_pushcfunction(State, luaopen_MezzanineSafe);
743  lua_pushstring(State, (MezzSafeLibName.c_str()) );
744  lua_call(State, 1, 0);
745  //SetXMLSafe();
746  }
747 
749  {
750  lua_pushcfunction(State, luaopen_MezzanineXML);
751  lua_pushstring(State, (MezzXMLLibName.c_str()) );
752  lua_call(State, 1, 0);
753  SetXML();
754  }
756  {
757  lua_pushcfunction(State, luaopen_MezzanineXMLSafe);
758  lua_pushstring(State, (MezzXMLSafeLibName.c_str()) );
759  lua_call(State, 1, 0);
760  SetXMLSafe();
761  }
762 
764  {
765  lua_pushcfunction(State, luaopen_MezzanineThreading);
766  lua_pushstring(State, (MezzThreadingLibName.c_str()) );
767  lua_call(State, 1, 0);
768  SetThreading();
769  }
771  {
772  lua_pushcfunction(State, luaopen_MezzanineThreadingSafe);
773  lua_pushstring(State, (MezzThreadingSafeLibName.c_str()) );
774  lua_call(State, 1, 0);
776  }
777 
779  {
780  lua_pushcfunction(State, luaopen_MezzaninePhysics);
781  lua_pushstring(State, (MezzPhysicsLibName.c_str()) );
782  lua_call(State, 1, 0);
783  SetPhysics();
784  }
786  {
787  lua_pushcfunction(State, luaopen_MezzaninePhysicsSafe);
788  lua_pushstring(State, (MezzPhysicsSafeLibName.c_str()) );
789  lua_call(State, 1, 0);
790  SetPhysicsSafe();
791  }
792 
794  {
795  lua_pushcfunction(State, luaopen_MezzanineMathTools);
796  lua_pushstring(State, (MezzPhysicsLibName.c_str()) );
797  lua_call(State, 1, 0);
798  SetMathTools();
799  }
801  {
802  lua_pushcfunction(State, luaopen_MezzanineMathToolsSafe);
803  lua_pushstring(State, (MezzPhysicsSafeLibName.c_str()) );
804  lua_call(State, 1, 0);
806  }
807 
809  { AliasLibrary("Mezzanine", "MezzanineXML", "XML"); }
811  { AliasLibrary("MezzanineSafe", "MezzanineXMLSafe", "XML"); }
812 
814  { AliasLibrary("Mezzanine", "MezzanineThreading", "Threading"); }
816  { AliasLibrary("MezzanineSafe", "MezzanineThreadingSafe", "Threading"); }
817 
819  { AliasLibrary("Mezzanine", "MezzaninePhysics", "Physics"); }
821  { AliasLibrary("MezzanineSafe", "MezzaninePhysicsSafe", "Physics"); }
822 
824  { AliasLibrary("Mezzanine", "MezzanineMathTools", "MathTools"); }
826  { AliasLibrary("MezzanineSafe", "MezzanineMathToolsSafe", "MathTools"); }
827 
829  { return State; }
830 
832  { return lua_gettop(State); }
833 
835  { return isalpha(IdChar) || IdChar=='_'; }
836 
838  { return isalnum(IdChar) || IdChar=='_'; }
839 
841  { return isalnum(IdChar) || IdChar=='_' || IdChar=='.' || IdChar==':'; }
842 
844  {
845  String::const_iterator Iter = Id.begin();
846  if( Id.size()==0 || Iter==Id.end() || !IsValidCharStartIdentifier(*Iter) )
847  { return false; }
848  for(Iter++; Iter!=Id.end(); Iter++)
849  {
850  if(!IsValidCharInIdentifier(*Iter))
851  { return false; }
852  }
853  return true;
854  }
855 
857  {
858  if(std::abs(StackLocation)>GetStackCount())
859  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Attempting to inspect beyond Lua stack while getting typestring."); }
860 
861  if(LUA_TNIL==lua_type(State,StackLocation))
862  { return TypeNameNil; }
863  if(LUA_TBOOLEAN==lua_type(State,StackLocation))
864  { return TypeNameBoolean; }
865  if(LUA_TLIGHTUSERDATA==lua_type(State,StackLocation))
866  { return TypeNameLightUserData; }
867  if(LUA_TNUMBER==lua_type(State,StackLocation))
868  { return TypeNameNumber; }
869  if(LUA_TSTRING==lua_type(State,StackLocation))
870  { return TypeNameString; }
871  if(LUA_TTABLE==lua_type(State,StackLocation))
872  { return TypeNameTable; }
873  if(LUA_TFUNCTION==lua_type(State,StackLocation))
874  { return TypeNameFunction; }
875  if(LUA_TUSERDATA==lua_type(State,StackLocation))
876  { return TypeNameUserData; }
877  if(LUA_TTHREAD==lua_type(State,StackLocation))
878  { return TypeNameThread; }
879  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "The thing on the Lua stack match no known types.");
880  return NoLibName;
881  }
882 
884  {
885  lua_getglobal(State, LuaIdentifier.c_str());
886  return ScriptArgFromStack();
887  }
888 
889  void Lua51ScriptingEngine::PopulateTabCompletionTrie(CommandTrie& CommandGroup, const String& TableName, std::vector<String> AlreadyDidTables)
890  {
891  // If no table is based start at the basemost global table
892  int Top = GetStackCount();
893  if(TableName=="")
894  {
895  lua_getglobal(State, GlobalTableName.c_str());
896  AlreadyDidTables.push_back(GlobalTableName);
897  }
898  else
899  {
900  lua_getglobal(State, TableName.c_str());
901  AlreadyDidTables.push_back(TableName);
902  }
903 
904  // Handle errors
905  if(GetStackCount()==Top) // if lua_getglobal puts nothing on the stack
906  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Lua51 Engine needs a table name to read for tab completion data instead an invalid identifier was passed."); }
907  if(!(LUA_TTABLE==lua_type(State,-1)))
908  {
909  lua_pop(State, 1);
910  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_RANGE_EXCEPTION, "Lua51 Engine needs a able name to read for tab completion data instead something else was passed.");
911  }
912 
913  // iterate of each entry, gather its name and type and recurse into subtables
914  lua_pushnil(State);
915  while (lua_next(State, -2) != 0)
916  {
917  // Capture Strings(names) and their associated types
918  if(LUA_TSTRING==lua_type(State,-2))
919  {
920  String TablePrefix;
921  if(TableName!=String(""))
922  { TablePrefix = TableName + "."; }
923  CommandGroup.insert((TablePrefix + lua_tostring(State,-2)).c_str(), &GetLuaTypeString(-1));
924  }
925 
926  // If it is a table and we have no done it yet, recurse
927  if(LUA_TTABLE==lua_type(State,-1))
928  {
929  lua_pop(State, 1);
930  bool AlreadyParsed = false;
931 
932  for(std::vector<String>::const_iterator Iter = AlreadyDidTables.begin();
933  AlreadyDidTables.end() != Iter;
934  Iter++)
935  {
936  if(*Iter==TableName)
937  {
938  AlreadyParsed = true;
939  break;
940  }
941  }
942  if(!AlreadyParsed)
943  { PopulateTabCompletionTrie(CommandGroup, lua_tostring(State,-1), AlreadyDidTables); }
944  }else{
945  lua_pop(State, 1);
946  }
947  }
948  lua_pop(State, 1);
949  }
950 
953 
956 
958  { return new Lua51ScriptingEngine(Params); }
959 
961  { return new Lua51ScriptingEngine(XMLNode); }
962 
964  { delete ToBeDestroyed; }
965  } // Lua
966  } // Scripting
967 } // Mezzanine
968 
969 
970 #endif // MEZZLUA51
971 #endif
Correlates to Lua51ScriptingEngine::OpenMezzanineXMLLibrary.
CountedPtr< iScriptArgument > ScriptArgFromStack()
Get the top value of the Lua stack.
Thrown when Lua returns a yield and it should not have.
Definition: exception.h:105
virtual CountedPtr< iScriptCompilable > Compile(const String &SourceToCompile)
Calls Compile(CountedPtr) and returns a CountedPtr to the script created...
Correlates to Lua51ScriptingEngine::OpenMezzanineMathToolsLibrary.
static const String MezzSafeTableName
The name used to identify a table loaded by the MezzanineSafe library, "MezzanineSafe".
virtual void AliasLibrary(const String &Base, const String &Sub, const String &Alias)
Nest Lua modules to put libraries in more clean positions.
virtual String GetImplementationTypeName() const
This Allows any manager name to be sent to a stream. Primarily used for logging.
void SetXMLSafe()
Set The MezzanineXMLSafe library as the XML member of the MezzanineSafe library or fail silently...
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...
A quick way to refer to all the libraries opened by Lua51ScriptingEngine::OpenAllLibraries.
static const String TypeNameNil
A human friendly representation of the Lua type nil.
virtual void OpenMezzanineThreadingLibrary()
Make the Threading parts of the Mezzanine Libary available for use in Lua51 scripts.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual void OpenMezzaninePhysicsLibrary()
Make the Physics parts of the Mezzanine Libary available for use in Lua51 scripts.
virtual void OpenTableLibrary()
Enable Lua table manipulation functionality.
static const String TypeNameUserData
A human friendly representation of the Lua type user data.
virtual void ThrowFromLuaErrorCode(int LuaReturn)
This will throw an exception.
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
String ToString(const T &Datum)
Converts whatever to a String as long as a streaming operator is available for it.
Definition: datatypes.h:242
virtual CountedPtr< iScript > Execute(const String &ScriptSource)
Compile and execute a passed string.
virtual void OpenMezzanineThreadingSafeLibrary()
Make the Threading parts of the Mezzanine Libary available for use in Lua51 scripts.
virtual void OpenMezzanineXMLLibrary()
Make the XML parts of the Mezzanine Libary available for use in Lua51 scripts.
This is a utility to help perform all kinds of string related tasks.
Definition: stringtool.cpp:93
static const String MezzThreadingSafeTableName
The name used to identify a table loaded by the MezzanineThreadingSafe library, "MezzanineThreadingSa...
static const String MezzLibName
The name used to identify the Mezzanine library, "Mezzanine".
int luaopen_MezzanineMathToolsSafe(lua_State *L)
Entry to Swig binding for loading the safe version of the MathTools.
virtual Whole GetArgumentCount() const
How many arguments have been attached to this script?
virtual void OpenStringLibrary()
Allow Lua scripts access to the Lua string manipulation libary.
void SetMathTools()
Set The MezzanineMathTools library as the MathTools member of the Mezzanine library or fail silently...
String LowerCaseCopy(String Source)
Create a copy of the String that is lower case.
Definition: stringtool.cpp:198
virtual void OpenMezzanineSafeLibrary()
Make a subset of the Mezzanine Library available for use in Lua51 scripts.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
All the definitions for datatypes as well as some basic conversion functions are defined here...
Correlates to Lua51ScriptingEngine::OpenOSLibrary.
static const String & GetLibName(Lua51Libraries Lib)
Convert a Lua51Libraries value to its name.
Correlates to Lua51ScriptingEngine::OpenMezzanineThreadingSafeLibrary.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
void SetPhysics()
Set The MezzaninePhysics library as the Physics member of the Mezzanine library or fail silently...
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".
Correlates to Lua51ScriptingEngine::OpenMezzaninePhysicsSafeLibrary.
Correlates to Lua51ScriptingEngine::OpenMezzanineThreadingLibrary.
Correlates to Lua51ScriptingEngine::OpenMezzaninePhysicsLibrary.
static const String IOLibName
The name used to identify the IO library, "IO".
static bool IsValidCharInTableIdentifier(const char IdChar)
Is the given character valid for is in a Lua Identifier including tables leading up to it and scoping...
virtual Boole IsLibraryOpen(Lua51Libraries LibToCheck)
Check the Lua state to see if a table exists.
lua_State * GetRawLuaState()
Get the underlying Lua state that can be used with lua api calls directly.
static const String MezzPhysicsSafeTableName
The name used to identify a table loaded by the MezzaninePhysicsSafe library, "MezzaninePhysicsSafe"...
int luaopen_MezzanineMathTools(lua_State *L)
Entry to Swig binding for loading the MathTools.
static const String BaseTableName
The name used to identify a table loaded by the Base library, "coroutine".
virtual void OpenDebugLibrary()
Enable Debugging Lua features.
This implements the exception hiearchy for Mezzanine.
virtual bool IsCompiled() const
Has this script already been compiled into a bytecode.
static const String PackageLibName
The name used to identify the Package library, "Package".
void ToLowerCase(String &Source)
Converts all upper case characters in a string to their respective lower case.
Definition: stringtool.cpp:193
int GetStackCount()
The stack is a specific part of the State relating to how data is passed between functions. How big is that?
Mezzanine::Trie< char, const Mezzanine::String * > CommandTrie
A type for efficiently storing the kinds of Lexigraphical data aboutthe Lua runtime.
Correlates to Lua51ScriptingEngine::OpenBaseLibrary.
void ScriptOntoStack(Lua51Script *ScriptToLoad)
Put a lua script onto the stack, compiling it if needed.
virtual FlaggedBuffer & GetByteCodeReference()
Get a reference to bytecode instead of a copy.
virtual void OpenIOLibrary()
Enable Input/Output in lua scripts for reading and writing files.
static const String MezzPhysicsTableName
The name used to identify a table loaded by the MezzaninePhysics library, "MezzaninePhysics".
ManagerBase::ManagerType GetManagerType() const
Gets the type of manager that is created by this factory.
int luaopen_MezzaninePhysicsSafe(lua_State *L)
Entry to Swig binding for loading the safe version of the Physics system.
virtual void OpenDefaultLibraries()
Prepare most Mezzanine and some Lua functionality for use in Lua scripts.
Correlates to Lua51ScriptingEngine::OpenMezzanineXMLSafeLibrary.
static const String TypeNameFunction
A human friendly representation of the Lua type function.
void SetXML()
Set The MezzanineXML library as the XML member of the Mezzanine library or fail silently.
int luaopen_MezzanineXML(lua_State *L)
Entry to Swig binding for loading the XML library.
Lua51ScriptingEngine(Lua51Libraries LibrariesToOpen=DefaultLibs)
Constructs a Scripting engine with a set of libraries preloaded.
static const String MezzMathToolsLibName
The name used to identify the MezzanineMathTools library, "MezzanineMathTools".
No special care is required for String Lua Arguments, so a simple typedef is used.
void SetMathToolsSafe()
Set The MezzanineMathToolsSafe library as the MathTools member of the MezzanineSafe library or fail s...
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...
This file has the interface for the Lua scripts.
TypePointedTo * Get() const
Get the raw pointer to the managed object.
Definition: countedptr.h:458
void CheckLuaStateAfterConstruction() const
Checks the internal Lua to see if memory was correctly allocated during its creation.
Thrown when there is an unknown issue with a file.
Definition: exception.h:74
Correlates to Lua51ScriptingEngine::OpenStringLibrary.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
This file has the definition of the Script Arguments for Lua 51.
A quick way to refer to all the libraries opened by Lua51ScriptingEngine::OpenDefaultLibraries.
Correlates to Lua51ScriptingEngine::OpenPackageLibrary.
Correlates to Lua51ScriptingEngine::OpenMezzanineSafeLibrary.
static const String StringTableName
The name used to identify a table loaded by the String library, "string".
int luaopen_MezzanineThreading(lua_State *L)
Entry to Swig binding for loading the FrameScheduler.
Correlates to Lua51ScriptingEngine::OpenIOLibrary.
virtual void OpenMathLibrary()
Enable Lua math and random number functionality.
Whole ScriptArgsFromStack(Lua51Script *ScriptWasRun, Integer PreviousStackSize)
Remove Scripts from Lua stack and put them into arguments on the passed script.
Correlates to Lua51ScriptingEngine::OpenMezzanineMathToolsSafeLibrary.
Correlates to Lua51ScriptingEngine::OpenMathLibrary.
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".
virtual void OpenMezzaninePhysicsSafeLibrary()
Make the Physics parts of the Mezzanine Libary available for use in Lua51 scripts.
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".
const String & GetLuaTypeString(int StackLocation)
Get a human friendly string containing the type of the thing at the given stack location.
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".
Int8 Byte
The type of data this buffer can hold, it is intended to be some type one byte in length...
Definition: binarybuffer.h:75
virtual void Push(lua_State *TargetState) const =0
Handle the details of putting this data onto Lua's Stack.
Thrown when an unknown error happens in a Lua script.
Definition: exception.h:104
Thrown when a passed parameter is checked at runtime and not in the expected range.
Definition: exception.h:110
void PopulateTabCompletionTrie(CommandTrie &CommandGroup, const String &TableName="", std::vector< String > AlreadyDidTables=std::vector< String >())
Populate a Trie with all the members and types in the current lua runtime.
Thrown when lua code in incorrect.
Definition: exception.h:102
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.
virtual ~Lua51ScriptingEngine()
Virtual Deconstructor.
static bool IsValidCharStartIdentifier(const char IdChar)
Is the given character valid for starting a Lua Identifier.
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
void ScriptArgsOntoStack(Lua51Script *ScriptToLoad)
Put the arguments in a script onto the stack.
static const String & GetTableName(Lua51Libraries Lib)
Convert a Lua51Libraries value to the name of a table it loads.
static const String GlobalTableName
The place Lua keeps all the identifiers in a single Lua State.
The ScriptArgumentGeneric needs just a bit of Lua specific functionality so we add push/pop for th...
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.
virtual void AddReturn(CountedPtr< iScriptArgument > ReturnArg)
Add another value to be returned.
int luaopen_MezzanineThreadingSafe(lua_State *L)
Entry to Swig binding for loading the safe version of the FrameScheduler.
static const String MathLibName
The name used to identify the Math library, "Math".
String GetName() const
Get the name of the current Script.
int luaopen_MezzanineSafe(lua_State *L)
Entry to Swig binding for loading the safe versions of the core Mezzanine library.
Correlates to Lua51ScriptingEngine::OpenDebugLibrary.
Thrown when Lua has an error handling an error.
Definition: exception.h:107
void SetPhysicsSafe()
Set The MezzaninePhysicsSafe library as the Physics member of the MezzanineSafe library or fail silen...
virtual void OpenAllLibraries()
Make all Mezzanine and Lua functionality.
Thrown when a Lua script has a runtime error.
Definition: exception.h:106
static bool IsValidCharInIdentifier(const char IdChar)
Is the given character valid for is in a Lua Identifier.
static const String TypeNameString
A human friendly representation of the Lua type string.
void SetThreading()
Set The MezzanineThreading library as the Threading member of the Mezzanine library or fail silently...
virtual void OpenMezzanineMathToolsSafeLibrary()
Make the MathTools parts of the Mezzanine Libary available for use in Lua51 scripts.
virtual void OpenOSLibrary()
EnableOS facilities in lua scriptsm such as file managements, time and shell execution.
No special care is required for Bool Lua Arguments, so a simple typedef is used.
virtual void OpenBaseLibrary()
Make some eof the more core functionality available to lua scripts.
virtual void OpenMezzanineXMLSafeLibrary()
Make the XML parts of the Mezzanine Libary available for use in Lua51 scripts.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
int luaopen_MezzaninePhysics(lua_State *L)
Entry to Swig binding for loading the Physics system.
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
virtual void OpenMezzanineLibrary()
Make everything in the Mezzanine Libary available for use in Lua51 scripts.
virtual void OpenMezzanineMathToolsLibrary()
Make the MathTools parts of the Mezzanine Libary available for use in Lua51 scripts.
Correlates to Lua51ScriptingEngine::OpenTableLibrary.
virtual void OpenPackageLibrary()
Allow Lua scripts to load other libraries.
String GetManagerImplName() const
Gets the name of the manager that is created by this factory.
void SetThreadingSafe()
Set The MezzanineThreadingSafe library as the Threading member of the MezzanineSafe library or fail s...
CountedPtr< iScriptArgument > GetValue(const String &LuaIdentifier)
Get the Value stored in a lua identifier.
static bool IsValidIdentifier(const String &Id)
Is a string a valid identifier in Lua.
Thrown when a pointer parameter is checked at runtime and cannot be cast as expected.
Definition: exception.h:109
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".
int luaopen_Mezzanine(lua_State *L)
Entry to Swig binding for loading the core Mezzanine library.
Thrown when A memory allocation was attempted and failed.
Definition: exception.h:97
virtual void OpenLibraries(int LibrariesToOpen)
Makes Lua function calls in Lua standard libraries available for use in Lua scripts.
static const String MezzThreadingSafeLibName
The name used to identify the MezzanineThreadingSafe library, "MezzanineThreadingSafe".
void DestroyManager(EntresolManager *ToBeDestroyed)
Destroys a Manager created by this factory.
static const String MezzThreadingTableName
The name used to identify a table loaded by the MezzanineThreading library, "MezzanineThreading".
int luaopen_MezzanineXMLSafe(lua_State *L)
Entry to Swig binding for loading the safe version XML library.
This file has the interface for the Lua based implementation of the Scripting system.
static const String MezzXMLSafeTableName
The name used to identify a table loaded by the MezzanineXMLSafe library, "MezzanineXMLSafe".
The interface for a script argument.
static Lua51Libraries GetLibFromName(String Name)
Convert a string similar to one of the names on the Lua51ScriptingEngine to number.
void StackExecute(Whole ArgumentCount)
Execute a thing on the top of the stack with arguments already put there.
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
A Real that can readily be passed into lua scripts.
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.
Represents not much of anything but will insert and retrieves Nils from Lua51.
Lua51Libraries
Intended only to make constructing an Lua51ScriptingEngine with the desired libraries open a little e...
Correlates to Lua51ScriptingEngine::OpenMezzanineLibrary.
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".
EntresolManager * CreateManager(const NameValuePairList &Params)
Creates a manager of the type represented by this factory.
static const String AllLibsName
The name used to identify the set of all libraries, "All".
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".