Spinning Topp Logo BlackTopp Studios
inc
lua51script.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 _lua51script_cpp
41 #define _lua51script_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 
52 #include <algorithm>
53 
54 /// @file
55 /// @brief This file has the implemetation for the Lua script storage.
56 
57 extern "C"
58 {
59  #include "lua.h" // Lua Core
60  #include "lualib.h" // for opening the base state
61  #include "lauxlib.h" // Extra Lua Goodies like lua_open()
62 
63  /// @internal
64  /// @brief Entry point into Swig bindings, required to initialize Lua.
65  int luaopen_Mezzanine(lua_State* L);
66 }
67 
68 namespace Mezzanine
69 {
70  namespace Scripting
71  {
72  namespace Lua
73  {
74 
75  ///////////////////////////////////////////////////////////////////////////////////////
76  // LuaScript basics
77  Lua51Script::Lua51Script() : FunctionCall(false)
78  { }
79 
80  Lua51Script::Lua51Script(const String& InitialSourceCode, Lua51ScriptingEngine* Compiler, Boole JustAFunctionCall, String ScriptName)
81  : SourceCode(InitialSourceCode),
82  Name(ScriptName),
83  FunctionCall(JustAFunctionCall)
84  {
85  if(Compiler && !FunctionCall)
86  { Compile(Compiler); }
87  }
88 
89  Lua51Script::Lua51Script(const String& InitialSourceCode, Lua51ScriptingEngine& Compiler, Boole JustAFunctionCall, String ScriptName)
90  : SourceCode(InitialSourceCode),
91  Name(ScriptName),
92  FunctionCall(JustAFunctionCall)
93  {
94  if(!FunctionCall)
95  { Compile(&Compiler); }
96  }
97 
99  {}
100 
101  ///////////////////////////////////////////////////////////////////////////////////////
102  // Arguments
104  { Args.push_back(Arg); }
105 
107  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51IntegerArgument(Arg))); }
108 
110  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51RealArgument(Arg))); }
111 
113  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51WholeArgument(Arg))); }
114 
116  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51StringArgument(Arg))); }
117 
119  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51BoolArgument(Arg))); }
120 
122  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51NilArgument(Arg))); }
123 
126 
128  { AddArgument((Lua51RealArgument(Arg))); }
129 
131  { AddArgument((Lua51WholeArgument(Arg))); }
132 
134  { AddArgument((Lua51StringArgument(Arg))); }
135 
137  { AddArgument((String(Arg))); }
138 
140  { AddArgument((Lua51BoolArgument(Arg))); }
141 
143  { Args.push_back(CountedPtr<iScriptArgument>(new Lua51NilArgument)); }
144 
146  { Args.erase( std::remove(Args.begin(),Args.end(),Arg) ); }
147 
149  { Args.erase(Args.begin()+ArgNumber); }
150 
152  { return Args.size(); }
153 
155  { Args.clear(); }
156 
158  { return Args.at(ArgNumber); }
159 
160  ///////////////////////////////////////////////////////////////////////////////////////
161  // Source code
163  {
164  CompiledByteCode.DeleteBuffer();
165  SourceCode = Code;
166  }
167 
169  {
170  SetSourceCode(Code);
171  Returns.clear();
172  Returns.push_back(Return1);
173  }
174 
176  { return SourceCode; }
177 
179  { CompiledByteCode = Code; }
180 
182  { return CompiledByteCode; }
183 
185  { return CompiledByteCode; }
186 
188  { return CompiledByteCode.Binary != 0; }
189 
191  { Compiler->Compile(this); }
192 
194  { Compiler.Compile(this); }
195 
196  ///////////////////////////////////////////////////////////////////////////////////////
197  // Name
199  { return Name; }
200 
201  void Lua51Script::SetName(const String& NewName)
202  { Name = NewName; }
203 
204  ///////////////////////////////////////////////////////////////////////////////////////
205  // Returns
207  { return Returns.size(); }
208 
210  { Returns.push_back(ReturnArg); }
211 
213  { return Returns; }
214 
216  { return Returns.at(ReturnNumber); }
217  } // Lua
218  } // Scripting
219 } // Mezzanine
220 
221 
222 
223 
224 #endif // MEZZLUA51
225 #endif
virtual CountedPtr< iScriptCompilable > Compile(const String &SourceToCompile)
Calls Compile(CountedPtr) and returns a CountedPtr to the script created...
void DeleteBuffer(const Whole NewSize=0)
Deletes whatever Binary points at and assigns Size to 0.
void SetName(const String &NewName)
change the name of the Script
virtual void SetSourceCode(const String &Code)
Sets the string version of the script.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
std::vector< CountedPtr< iScriptArgument > > ArgumentGroup
A group of arguments that can be returned from some scripts.
Definition: script.h:295
void Compile(Lua51ScriptingEngine *Compiler)
This will compile the Lua script.
virtual Whole GetArgumentCount() const
How many arguments have been attached to this script?
virtual void ClearArguments()
Remove all the ARGs!!! http://imgur.com/DJhw7.
All the definitions for datatypes as well as some basic conversion functions are defined here...
virtual CountedPtr< iScriptArgument > GetArgument(Whole ArgNumber) const
Retrieve a argument previously passed in.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual String GetSourceCode() const
If present this returns the code of script.
virtual void SetByteCode(BinaryTools::BinaryBuffer Code)
Set the bytecode used when this script is executed.
No special care is required for Whole number Lua Arguments, so a simple typedef is used...
This implements the exception hiearchy for Mezzanine.
virtual bool IsCompiled() const
Has this script already been compiled into a bytecode.
virtual FlaggedBuffer & GetByteCodeReference()
Get a reference to bytecode instead of a copy.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
Lua51Script()
Simple constructor, creates a script that executes a no-op.
Definition: lua51script.cpp:77
No special care is required for String Lua Arguments, so a simple typedef is used.
This file has the interface for the Lua scripts.
An expanded version of the BinaryTools::BinaryBuffer to carry one tiny piece of metadata around with ...
Definition: lua51script.h:70
This file has the definition of the Script Arguments for Lua 51.
virtual void RemoveArgument(CountedPtr< iScriptArgument > Arg)
Remove an argument based on a CountedPtr to the script.
The implementations in the ScriptArgumentGeneric will cover most of what this needs...
int luaopen_Mezzanine(lua_State *L)
Entry point into Swig bindings, required to initialize Lua.
virtual ~Lua51Script()
Virtual destructor.
Definition: lua51script.cpp:98
virtual BinaryTools::BinaryBuffer GetByteCode() const
Get the compiled version of the code if it is available.
The workhorse of the Lua scripting system. All scripts come here to be executed.
virtual Whole GetReturnCount() const
How many values are being returned.
Byte * Binary
A pointer to the actual binary data.
Definition: binarybuffer.h:83
virtual void AddReturn(CountedPtr< iScriptArgument > ReturnArg)
Add another value to be returned.
String GetName() const
Get the name of the current Script.
No special care is required for Bool Lua Arguments, so a simple typedef is used.
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
This file has the interface for the Lua based implementation of the Scripting system.
A way to store and pass binary buffers, for example compiled bytecode.
Definition: binarybuffer.h:68
virtual CountedPtr< iScriptArgument > GetReturn(Whole ReturnNumber) const
Retrieve a value returned from a script.
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.
virtual void AddArgument()
When called with no arguments this inserts a Lua51nil.
Represents not much of anything but will insert and retrieves Nils from Lua51.
virtual ArgumentGroup GetAllReturns() const
Get the returns from the last exection of the script.