HoverRace  2.0
Core.h
Go to the documentation of this file.
1 
2 // Core.h
3 //
4 // Copyright (c) 2009, 2010, 2014, 2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <list>
25 
26 #include <luabind/object.hpp>
27 
28 #include "Lua.h"
29 #include "ScriptExn.h"
30 
31 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
32 # ifdef MR_ENGINE
33 # define MR_DllDeclare __declspec( dllexport )
34 # else
35 # define MR_DllDeclare __declspec( dllimport )
36 # endif
37 #else
38 # define MR_DllDeclare
39 #endif
40 
41 namespace HoverRace {
42  namespace Script {
43  namespace Help {
44  class Class;
45  class HelpHandler;
46  }
47  }
48 }
49 
50 namespace HoverRace {
51 namespace Script {
52 
54 class IncompleteExn : public ScriptExn
55 {
56  using SUPER = ScriptExn;
57 
58 public:
59  IncompleteExn(const std::string &s) : SUPER(s) { }
60 };
61 
67 {
68 public:
69  Core();
70  Core(const Core &core) = delete;
71  virtual ~Core();
72 
73  Core &operator=(const Core&) = delete;
74 
75 public:
76  lua_State *GetState() const { return state; }
77  virtual Core *Reset();
78  void ActivateSandbox();
79 
80 private:
81  static int ErrorFunc(lua_State *L);
82 
83 private:
84  using outs_t = std::list<std::shared_ptr<std::ostream>>;
85 public:
86  using OutHandle = outs_t::iterator;
87  OutHandle AddOutput(std::shared_ptr<std::ostream> out);
88  void RemoveOutput(const OutHandle &handle);
89 
90  std::string GetVersionString() const;
91 
92 public:
93  struct Chunk
94  {
103  Chunk(const std::string &src,
104  const std::string &name = DEFAULT_CHUNK_NAME) :
105  src(src), name(name) { }
106  Chunk(const Chunk &o) = default;
107  Chunk(Chunk &&o) = default;
108 
109  Chunk &operator=(const Chunk &o) = default;
110  Chunk &operator=(Chunk &&o) = default;
111 
112  std::string src;
113  std::string name;
114  };
115 
116 public:
119  {
120  StackRestore(lua_State *state);
121  ~StackRestore();
122 
123  lua_State *state;
125  };
126 
127 private:
128  static void PrintFromStack(lua_State *state, int n);
129 public:
131  struct PassReturn
132  {
133  int operator()(lua_State*, int n) const
134  {
135  return n;
136  }
137  };
138 
143  struct PrintReturn
144  {
145  int operator()(lua_State *state, int n) const
146  {
147  PrintFromStack(state, n);
148  return 0;
149  }
150  };
151 
152 public:
153  void Print(const std::string &s);
154 
155  static const std::string DEFAULT_CHUNK_NAME;
156  void Compile(const Chunk &chunk);
157  int Call(int numParams = 0, Help::HelpHandler *helpHandler = nullptr);
158 
167  template<class ReturnPolicy = PrintReturn>
168  int Invoke(int numParams = 0, Help::HelpHandler *helpHandler = nullptr,
169  ReturnPolicy rp = ReturnPolicy())
170  {
171  return rp(state, Call(numParams, helpHandler));
172  }
173 
175  void CallAndPrint(int numParams = 0, Help::HelpHandler *helpHandler = nullptr)
176  {
177  Invoke(numParams, helpHandler);
178  }
179 
194  template<class ReturnPolicy = PrintReturn>
195  void Execute(const Chunk &chunk, Help::HelpHandler *helpHandler = nullptr,
196  ReturnPolicy rp = ReturnPolicy())
197  {
198  // Explicitly throw away any return values leftover from the
199  // return policy so that the stack is exactly how we began.
200  StackRestore sr(state);
201 
202  Compile(chunk);
203  Invoke(0, helpHandler, rp);
204  }
205 
206  void PrintStack();
207 
208  void ReqHelp(const std::string &className);
209  void ReqHelp(const std::string &className, const std::string &methodName);
210 
211 public:
212  std::unique_ptr<luabind::object> NIL;
213 private:
214  void LoadClassHelp(const std::string &className);
215  static std::string PopError(lua_State *state);
216 
217  static int LPrint(lua_State *state);
218  static int LSandboxedFunction(lua_State *state);
219 
220 private:
222  lua_State *state;
224  using helpClasses_t = std::map<
225  const std::string, std::shared_ptr<Help::Class>>;
227 };
228 
229 } // namespace Script
230 } // namespace HoverRace
231 
232 #undef MR_DllDeclare
Generic script engine exception.
Definition: ScriptExn.h:41
Exception to signal that the current chunk is incomplete.
Definition: Core.h:54
int operator()(lua_State *, int n) const
Definition: Core.h:133
Chunk(const std::string &src, const std::string &name=DEFAULT_CHUNK_NAME)
Constructor.
Definition: Core.h:103
outs_t::iterator OutHandle
Definition: Core.h:86
Definition: Core.h:93
IncompleteExn(const std::string &s)
Definition: Core.h:59
lua_State * state
Definition: Core.h:123
int operator()(lua_State *state, int n) const
Definition: Core.h:145
std::list< std::shared_ptr< std::ostream >> outs_t
Definition: Core.h:84
std::string src
Definition: Core.h:112
Return policy that leaves the return values on the stack.
Definition: Core.h:131
int initStack
Definition: Core.h:124
Return policy that calls Lua&#39;s print() function on each value.
Definition: Core.h:143
lua_State * GetState() const
Definition: Core.h:76
helpClasses_t helpClasses
Definition: Core.h:226
Abstract base class for handlers of help requests.
Definition: HelpHandler.h:45
int Invoke(int numParams=0, Help::HelpHandler *helpHandler=nullptr, ReturnPolicy rp=ReturnPolicy())
Pop a function off the stack and execute it, printing any return values.
Definition: Core.h:168
std::map< const std::string, std::shared_ptr< Help::Class >> helpClasses_t
Definition: Core.h:225
std::string name
Definition: Core.h:113
outs_t outs
Definition: Core.h:221
Help::HelpHandler * curHelpHandler
Definition: Core.h:223
Ensures that the Lua stack is unchanged in a block.
Definition: Core.h:118
std::unordered_map< std::string, std::string > map
Definition: SdlDisplay.cpp:115
static const std::string DEFAULT_CHUNK_NAME
Definition: Core.h:155
void CallAndPrint(int numParams=0, Help::HelpHandler *helpHandler=nullptr)
Definition: Core.h:175
Definition: Announcement.h:24
void Execute(const Chunk &chunk, Help::HelpHandler *helpHandler=nullptr, ReturnPolicy rp=ReturnPolicy())
Safely compile and execute a chunk of code.
Definition: Core.h:195
lua_State * state
Definition: Core.h:222
#define MR_DllDeclare
Definition: Core.h:38
std::unique_ptr< luabind::object > NIL
Definition: Core.h:212
A script environment.
Definition: Core.h:66