λ61
Loading...
Searching...
No Matches
ScriptEnvironment.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Tetex7
3 *
4 * This program 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 * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//
19// Created by tete on 05/01/2025.
20//
21#pragma once
22
23#ifndef L61_SCRIPTENVIRONMENT_HPP
24#define L61_SCRIPTENVIRONMENT_HPP
25
26#include <expected>
27#include <functional>
28#include <stdexcept>
29
30#include "l61/defs.hpp"
31#include "sol/sol.hpp"
34
35namespace l61::ScriptEngine
36{
37
39{
40private:
42 sol::state luaCtx;
43
45
46protected:
47 void lib61_setup();
48 sol::state& getLuaCtx() override;
50
52
53 static sol::table lua_mountLib(sol::this_state state, const std::string& libraryName);
54
55public:
56 template<class T>
57 void addValue(const std::string& key, T&& value)
58 {
59 auto var = getLuaCtx()[key] = std::forward<T>(value);
60 }
61
62
63 template<class T>
64 T getValue(const std::string& key)
65 {
66 auto temp = getLuaCtx()[key];
67 if ((temp != sol::nil))
68 {
69 if (temp.is<T>())
70 {
71 return temp.get<T>();
72 }
73 }
74 throw std::runtime_error(std::format("Value not exist {}", key));
75 }
76
77 template<class T>
78 void setValue(const std::string& key, const T& value)
79 {
80 if (auto temp = getLuaCtx()[key]; temp.is<T>())
81 {
82 temp.set(value);
83 }
84 }
85
86 bool has(const std::string& key);
87
88 sol::table makeTable(const std::string& name);
89
92
94
95 //Yes this does leak the Lua State what are you going to do bite me
96 void specialRun(const std::function<void(sol::state&)>& func) override;
97
102 void exec(const std::string& code) final;
103
104 void attachDebugger(AbstractScriptDebugger* debugger) override;
105
106 sol::protected_function_result runCodeBlock(const std::string& luaCode) override;
107
110 friend void standard_lua_debugger_hook(lua_State* L, lua_Debug* D);
111};
112
113inline sol::table makeSubTable(const std::string&& name, sol::table table)
114{
115 sol::table x = table.create_with();
116 table.set(std::forward<const std::string>(name), x);
117 return x;
118}
119
120}
121
122#endif //L61_SCRIPTENVIRONMENT_HPP
#define l61_abstract_class
Denotes that the class is abstract with most functions being implemented leaving a few functions to b...
Definition PseudoKeywords.hpp:63
Definition Object.hpp:47
void lib61_setup()
Definition ScriptEnvironment.cpp:46
friend l61_abstract_class AbstractScriptDebugger
Definition ScriptEnvironment.hpp:109
void specialRun(const std::function< void(sol::state &)> &func) override
Definition ScriptEnvironment.cpp:127
sol::state luaCtx
Definition ScriptEnvironment.hpp:42
AbstractScriptDebugger * script_debugger_
Definition ScriptEnvironment.hpp:44
sol::protected_function_result runCodeBlock(const std::string &luaCode) override
Definition ScriptEnvironment.cpp:155
void setValue(const std::string &key, const T &value)
Definition ScriptEnvironment.hpp:78
void addValue(const std::string &key, T &&value)
Definition ScriptEnvironment.hpp:57
friend void standard_lua_debugger_hook(lua_State *L, lua_Debug *D)
Definition ScriptEnvironment.cpp:68
void attachDebugger(AbstractScriptDebugger *debugger) override
Definition ScriptEnvironment.cpp:149
sol::state & getLuaCtx() override
Definition ScriptEnvironment.cpp:63
static sol::table lua_mountLib(sol::this_state state, const std::string &libraryName)
Definition ScriptEnvironment.cpp:86
sol::table makeTable(const std::string &name)
Definition ScriptEnvironment.cpp:117
ScriptEnvironment(l61_stat &scriptCtx)
Definition ScriptEnvironment.cpp:77
void exec(const std::string &code) final
for user input
Definition ScriptEnvironment.cpp:143
T getValue(const std::string &key)
Definition ScriptEnvironment.hpp:64
bool has(const std::string &key)
Definition ScriptEnvironment.cpp:122
l61_stat & scriptCtx
Definition ScriptEnvironment.hpp:41
ScriptEnvironment(ScriptEnvironment &&)=delete
ScriptEnvironment(const ScriptEnvironment &)=delete
ScriptEnvironment & operator=(const ScriptEnvironment &)=delete
l61_stat & getScriptCtx()
Definition ScriptEnvironment.cpp:58
Definition defs.hpp:171
sol::table makeSubTable(const std::string &&name, sol::table table)
Definition ScriptEnvironment.hpp:113
l61_interface IBasicScriptEngine
Definition IBasicScriptEngine.hpp:30
Definition defs.hpp:159