载入中...
搜索中...
未找到
Runtime.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Export.h"
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace eve {
17
18enum class ScriptState {
20 Running,
21 Loaded,
24 Failed,
25};
26
27enum class ScriptStage {
28 Compile,
29 Execute,
30 Reflect,
31 Unload,
33};
34
36class EVENGINE_API ScriptException : public std::runtime_error {
37public:
38 ScriptException(ScriptStage stage, std::string source, uint64_t scriptId,
39 const std::string& message);
47 ScriptException(ScriptStage stage, std::string source, uint64_t scriptId,
48 const script::ScriptErrorContext& context);
49
50 ScriptStage stage() const noexcept { return stage_; }
51 const std::string& source() const noexcept { return source_; }
52 uint64_t scriptId() const noexcept { return script_id_; }
54 bool hasLocation() const noexcept { return line_ > 0; }
56 int line() const noexcept { return line_; }
58 int column() const noexcept { return column_; }
60 const std::string& function() const noexcept { return function_; }
62 const std::string& stackTrace() const noexcept { return stack_trace_; }
64 bool reported() const noexcept { return reported_; }
65
66private:
67 ScriptStage stage_;
68 std::string source_;
69 uint64_t script_id_;
70 int line_ = -1;
71 int column_ = -1;
72 std::string function_;
73 std::string stack_trace_;
74 bool reported_ = false;
75};
76
78 std::string name;
79 ssq::Type type = ssq::Type::NULLPTR;
80 std::string value;
81};
82
84 std::string name;
85 ssq::Type type = ssq::Type::NULLPTR;
86 bool method = false;
87 std::vector<ReflectedAttribute> attributes;
88};
89
91 std::string name;
92 std::string source;
93 std::string base;
94 std::vector<ReflectedMember> members;
95};
96
98 uint64_t id = 0;
99 std::string source;
100 ScriptState state = ScriptState::Compiled;
101 std::vector<std::string> classes;
102 std::string error;
103};
104
106public:
107 using ScriptId = uint64_t;
108 using ErrorHandler = std::function<void(const ScriptException&)>;
109 using LifecycleHandler = std::function<void(const ScriptInfo&)>;
110
113 public:
114 explicit StackGuard(Runtime& runtime) noexcept;
115 ~StackGuard();
116 StackGuard(StackGuard&& other) noexcept;
117 StackGuard& operator=(StackGuard&& other) noexcept;
118 StackGuard(const StackGuard&) = delete;
119 StackGuard& operator=(const StackGuard&) = delete;
120
121 int top() const noexcept { return top_; }
122 void dismiss() noexcept { vm_ = nullptr; }
123
124 private:
125 HSQUIRRELVM vm_ = nullptr;
126 SQInteger top_ = 0;
127 };
128
131 public:
132 explicit Scope(Runtime& runtime);
133 ~Scope();
134 Scope(Scope&& other) noexcept;
135 Scope& operator=(Scope&& other) = delete;
136 Scope(const Scope&) = delete;
137 Scope& operator=(const Scope&) = delete;
138
139 private:
140 Runtime* runtime_ = nullptr;
141 };
142
148 explicit Runtime(size_t stackSize = 2048, ssq::Libs::Flag libraries = ssq::Libs::ALL);
149 ~Runtime();
150 Runtime(Runtime&&) = delete;
152 Runtime(const Runtime&) = delete;
153 Runtime& operator=(const Runtime&) = delete;
154
156 void initialize();
158 void shutdown() noexcept;
160 bool initialized() const noexcept;
161
163 ssq::VM& vm() noexcept;
164 const ssq::VM& vm() const noexcept;
166 HSQUIRRELVM handle() const noexcept;
168 ssq::Table root() const;
173 ssq::Table table(const char* name) const;
174
176 StackGuard guard() noexcept { return StackGuard(*this); }
178 Scope enter() { return Scope(*this); }
180 static Runtime* current() noexcept;
182 static size_t stackDepth() noexcept;
183
191 ScriptId compileSource(std::string source, std::string sourceName = "buffer");
198 ScriptId compileFile(const std::string& path);
200 const ScriptInfo& execute(ScriptId id);
202 ScriptId runSource(std::string source, std::string sourceName = "buffer");
204 ScriptId runFile(const std::string& path);
206 ScriptId reflectFile(const std::string& path) { return runFile(path); }
212 const ReflectedClass& reflectClass(const std::string& name,
213 const std::string& source = {});
215 ScriptId reload(ScriptId id);
217 bool unload(ScriptId id);
219 void unloadAll() noexcept;
220
222 bool contains(ScriptId id) const noexcept;
224 const ScriptInfo* script(ScriptId id) const noexcept;
226 std::vector<ScriptInfo> scripts() const;
228 const ReflectedClass* reflectedClass(const std::string& name) const noexcept;
230 std::vector<ReflectedClass> reflectedClasses() const;
232 ssq::Class findClass(const std::string& name) const;
233
235 void setErrorHandler(ErrorHandler handler) { error_handler_ = std::move(handler); }
237 void setLifecycleHandler(LifecycleHandler handler) { lifecycle_handler_ = std::move(handler); }
238
239private:
240 struct ScriptRecord;
241
243 void installErrorHandler();
244
245 std::unique_ptr<ssq::VM> vm_;
246 std::unordered_map<ScriptId, std::unique_ptr<ScriptRecord>> scripts_;
247 std::unordered_map<std::string, ReflectedClass> classes_;
248 std::unordered_map<std::string, ScriptId> class_owners_;
249 ScriptId next_script_id_ = 1;
250 bool initialized_ = false;
251 bool shutting_down_ = false;
252 bool stopped_ = false;
253 ErrorHandler error_handler_;
254 LifecycleHandler lifecycle_handler_;
255
256 void notifyLifecycle(const ScriptInfo& info) noexcept;
257 script::ScriptErrorContext compileErrorContext(const std::string& what,
258 const std::string& sourceText);
259 [[noreturn]] void fail(ScriptStage stage, const std::string& source, ScriptId id,
260 const std::exception& error);
261 [[noreturn]] void fail(ScriptStage stage, const std::string& source, ScriptId id,
263 void discoverClasses(ScriptRecord& record,
264 const std::unordered_map<std::string, SQUserPointer>& before);
265 std::unordered_map<std::string, SQUserPointer> rootClasses() const;
266 ReflectedClass inspectClass(const std::string& name, const ssq::Class& cls,
267 const std::string& source) const;
268};
269
270} // namespace eve
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
HSQOBJECT cls
Definition ECS.cpp:21
std::string type
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:16
std::string error
JobSystemThreadPool::State * state
const char * name
Definition RockMesh.cpp:21
Pushes this Runtime on the current thread's runtime stack.
Definition Runtime.h:130
Scope(const Scope &)=delete
Scope & operator=(Scope &&other)=delete
Scope & operator=(const Scope &)=delete
Restores the native Squirrel stack when a binding operation leaves scope.
Definition Runtime.h:112
StackGuard & operator=(const StackGuard &)=delete
void dismiss() noexcept
Definition Runtime.h:122
StackGuard(const StackGuard &)=delete
int top() const noexcept
Definition Runtime.h:121
std::function< void(const ScriptInfo &)> LifecycleHandler
Definition Runtime.h:109
Runtime(Runtime &&)=delete
std::function< void(const ScriptException &)> ErrorHandler
Definition Runtime.h:108
Runtime & operator=(Runtime &&)=delete
Runtime(const Runtime &)=delete
uint64_t ScriptId
Definition Runtime.h:107
Runtime & operator=(const Runtime &)=delete
Scope enter()
Pushes this runtime on the thread-local runtime stack (RAII pop).
Definition Runtime.h:178
void setLifecycleHandler(LifecycleHandler handler)
Replaces the handler notified on every script lifecycle change.
Definition Runtime.h:237
Exception raised at the public Runtime boundary.
Definition Runtime.h:36
bool reported() const noexcept
True when a reporter (e.g. DevTool hook) already handled it.
Definition Runtime.h:64
const std::string & stackTrace() const noexcept
Multi-line call stack captured at the throw site.
Definition Runtime.h:62
bool hasLocation() const noexcept
True when the exception carries a script source line.
Definition Runtime.h:54
int column() const noexcept
1-based compile column; -1 when unknown.
Definition Runtime.h:58
ScriptStage stage() const noexcept
Definition Runtime.h:50
const std::string & function() const noexcept
Function name at the throw site; empty when unknown.
Definition Runtime.h:60
const std::string & source() const noexcept
Definition Runtime.h:51
uint64_t scriptId() const noexcept
Definition Runtime.h:52
int line() const noexcept
1-based throw/compile site line; -1 when unknown.
Definition Runtime.h:56
Definition Build.cpp:11
ScriptState
Definition Runtime.h:18
ScriptStage
Definition Runtime.h:27
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:12
std::string value
Definition Runtime.h:80
std::string base
Definition Runtime.h:93
std::string source
Definition Runtime.h:92
std::vector< ReflectedMember > members
Definition Runtime.h:94
std::string name
Definition Runtime.h:91
std::vector< ReflectedAttribute > attributes
Definition Runtime.h:87
std::string name
Definition Runtime.h:84
std::string source
Definition Runtime.h:99
std::vector< std::string > classes
Definition Runtime.h:101
std::string error
Definition Runtime.h:102
Structured snapshot of a script error: message, throw site and stack.
Definition ScriptError.h:25