Lluvia
Interpreter.h
Go to the documentation of this file.
1 
8 #ifndef LLUVIA_CORE_INTERPRETER_H_
9 #define LLUVIA_CORE_INTERPRETER_H_
10 
11 #include <cstdint>
12 #include <cstdlib>
13 #include <limits>
14 #include <memory>
15 #include <string>
16 #include <type_traits>
17 
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wunused-lambda-capture"
20 #pragma clang diagnostic ignored "-Wunused-parameter"
21 #pragma clang diagnostic ignored "-Wshadow"
22 
23 #define SOL_ALL_SAFETIES_ON 1
24 #include "sol/sol.hpp"
25 
26 #pragma clang diagnostic pop
27 
28 #include "lluvia/core/error.h"
29 
30 namespace ll {
31 
32 class Session;
33 
34 class Interpreter {
35 
36 public:
38  Interpreter(const Interpreter& interpreter) = delete;
39  Interpreter(Interpreter&& interpreter) = default;
40 
42 
43  Interpreter& operator=(const Interpreter& interpreter) = delete;
44  Interpreter& operator=(Interpreter&& interpreter) = default;
45 
46  void run(const std::string& code);
47  void runFile(const std::string& filename);
48 
49  sol::load_result load(const std::string& code);
50 
51  void setActiveSession(ll::Session* session);
52 
53  template <typename T, typename... Args>
54  T loadAndRun(const std::string&& code, Args&&... args)
55  {
56 
57  auto loadCode = load(std::forward<const std::string>(code));
58 
59  auto scriptFunction = static_cast<sol::protected_function>(loadCode);
60  sol::protected_function_result scriptResult = scriptFunction(std::forward<Args>(args)...);
61 
62  if (!scriptResult.valid()) {
63  const sol::error err = scriptResult;
64 
66  "error running code: " + sol::to_string(loadCode.status()) + "\n\t" + err.what());
67  }
68 
69  // return static_cast<T>(scriptResult);
70  return scriptResult.get<T>();
71  }
72 
73  template <typename... Args>
74  void loadAndRunNoReturn(const std::string&& code, Args&&... args)
75  {
76 
77  auto loadCode = load(std::forward<const std::string>(code));
78 
79  auto scriptFunction = static_cast<sol::protected_function>(loadCode);
80  sol::protected_function_result scriptResult = scriptFunction(std::forward<Args>(args)...);
81 
82  if (!scriptResult.valid()) {
83  const sol::error err = scriptResult;
84 
86  "error running code: " + sol::to_string(loadCode.status()) + "\n\t" + err.what());
87  }
88  }
89 
90 private:
91  std::unique_ptr<sol::state> m_lua;
92  sol::table m_lib;
93  sol::table m_libImpl;
94 };
95 
96 } // namespace ll;
97 
98 #endif // LLUVIA_CORE_INTERPRETER_H_
Definition: Interpreter.h:34
Interpreter & operator=(Interpreter &&interpreter)=default
void setActiveSession(ll::Session *session)
Interpreter & operator=(const Interpreter &interpreter)=delete
Interpreter(const Interpreter &interpreter)=delete
void loadAndRunNoReturn(const std::string &&code, Args &&... args)
Definition: Interpreter.h:74
sol::load_result load(const std::string &code)
T loadAndRun(const std::string &&code, Args &&... args)
Definition: Interpreter.h:54
void run(const std::string &code)
void runFile(const std::string &filename)
Interpreter(Interpreter &&interpreter)=default
Class that contains all the state required to run compute operations on a compute device.
Definition: Session.h:51
error related classes and methods.
Definition: Buffer.h:28
void throwSystemError(ll::ErrorCode errorCode, T &&msg)
Throws a std::system_error exception with error code and message.
Definition: error.h:173