HoverRace  2.0
Env.h
Go to the documentation of this file.
1 
2 // Env.h
3 //
4 // Copyright (c) 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 "../Util/OS.h"
25 
26 #include "Core.h"
27 #include "RegistryRef.h"
28 
29 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
30 # ifdef MR_ENGINE
31 # define MR_DllDeclare __declspec( dllexport )
32 # else
33 # define MR_DllDeclare __declspec( dllimport )
34 # endif
35 #else
36 # define MR_DllDeclare
37 #endif
38 
39 namespace HoverRace {
40  namespace Script {
41  namespace Help {
42  class HelpHandler;
43  }
44  }
45 }
46 
47 namespace HoverRace {
48 namespace Script {
49 
55 {
56 public:
57  Env(Core &scripting);
58  Env(const Env&) = delete;
59  virtual ~Env() { }
60 
61  Env &operator=(const Env&) = delete;
62 
63 protected:
64  Core &GetScripting() const { return scripting; }
65  lua_State *GetState() const { return scripting.GetState(); }
66 
67  void LogScriptError(const Script::ScriptExn &ex);
68 
69  virtual void InitEnv() = 0;
70  void CopyGlobals();
71 
72  void SetHelpHandler(Help::HelpHandler *helpHandler);
73 
74 protected:
75  void PushEnv();
76 private:
77  void SetupEnv();
78 
79 protected:
93  template<class ReturnPolicy = Core::PrintReturn>
94  int Execute(const Core::Chunk &chunk, ReturnPolicy rp = ReturnPolicy())
95  {
96  // May throw ScriptExn or IncompleteExn, in which case the stack
97  // will be unchanged.
98  scripting.Compile(chunk);
99 
100  SetupEnv();
101 
102  // May throw ScriptExn, but the function on the stack will be
103  // consumed anyway.
104  return scripting.Invoke(0, helpHandler, rp);
105  }
106 
107 protected:
108  static Core::Chunk LoadChunkFromFile(const Util::OS::path_t &filename);
109 
110 public:
121  bool RunScript(const Util::OS::path_t &filename)
122  {
123  try {
124  Execute(LoadChunkFromFile(filename));
125  return true;
126  }
127  catch (Script::ScriptExn &ex) {
128  LogScriptError(ex);
129  return false;
130  }
131  }
132 
133 private:
138 };
139 
140 } // namespace Script
141 } // namespace HoverRace
142 
143 #undef MR_DllDeclare
Generic script engine exception.
Definition: ScriptExn.h:41
boost::filesystem::path path_t
Definition: OS.h:57
bool RunScript(const Util::OS::path_t &filename)
Execute a script from a file.
Definition: Env.h:121
void Compile(const Chunk &chunk)
Compile a chunk of code.
Definition: Core.cpp:265
Core & scripting
Definition: Env.h:134
Encapsulates a ref to the registry.
Definition: RegistryRef.h:50
Definition: Core.h:93
int Execute(const Core::Chunk &chunk, ReturnPolicy rp=ReturnPolicy())
Execute a chunk of code in the current environment.
Definition: Env.h:94
#define MR_DllDeclare
Definition: Env.h:36
A script environment.
Definition: Env.h:54
lua_State * GetState() const
Definition: Core.h:76
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
Help::HelpHandler * helpHandler
Definition: Env.h:137
RegistryRef envRef
Definition: Env.h:136
bool initialized
Definition: Env.h:135
Definition: Announcement.h:24
lua_State * GetState() const
Definition: Env.h:65
Core & GetScripting() const
Definition: Env.h:64
virtual ~Env()
Definition: Env.h:59
A script environment.
Definition: Core.h:66