载入中...
搜索中...
未找到
Runtime.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Export.h"
4
5#include <simplesquirrel/simplesquirrel.hpp>
6
7#include <cstdio>
8#include <cstdint>
9#include <functional>
10#include <limits>
11#include <memory>
12#include <stdexcept>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17namespace eve {
18
19namespace script {
20class ScriptCompiler;
21class ScriptModuleResolver;
22struct ScriptErrorContext;
23} // namespace script
24
33inline std::string reflectedFloatString(double value) {
34 char buf[64];
36 std::snprintf(buf, sizeof(buf), "%.*g",
37 std::numeric_limits<double>::max_digits10, value);
38 return buf;
39}
40
42enum class ScriptState {
44 Running,
45 Loaded,
48 Failed,
49};
50
52enum class ScriptStage {
53 Compile,
54 Execute,
55 Reflect,
56 Unload,
58};
59
61class EVENGINE_API_FOUNDATION ScriptException : public std::runtime_error {
62public:
64 ScriptException(ScriptStage stage, std::string source, uint64_t scriptId,
65 const std::string& message);
73 ScriptException(ScriptStage stage, std::string source, uint64_t scriptId,
75
77 ScriptStage stage() const noexcept { return stage_; }
79 const std::string& source() const noexcept { return source_; }
81 uint64_t scriptId() const noexcept { return script_id_; }
83 bool hasLocation() const noexcept { return line_ > 0; }
85 int line() const noexcept { return line_; }
87 int column() const noexcept { return column_; }
89 const std::string& function() const noexcept { return function_; }
91 const std::string& stackTrace() const noexcept { return stack_trace_; }
93 bool reported() const noexcept { return reported_; }
94
95private:
96 ScriptStage stage_;
97 std::string source_;
98 uint64_t script_id_;
99 int line_ = -1;
100 int column_ = -1;
101 std::string function_;
102 std::string stack_trace_;
103 bool reported_ = false;
104};
105
108 std::string name;
109 ssq::Type type = ssq::Type::NULLPTR;
110 std::string value;
111};
112
115 None = 0,
116 Bool = 1,
118 Float = 3,
119 String = 4,
120 Array = 5,
121 Table = 6,
123 Other = 8,
124};
125
133 ReflectedValueKind kind = ReflectedValueKind::None;
134 bool boolean = false;
135 int64_t integer = 0;
136 double floating = 0.0;
137 std::string text;
138
140 bool empty() const noexcept { return kind == ReflectedValueKind::None; }
142 bool asBool() const noexcept { return boolean; }
144 int64_t asInt() const noexcept { return integer; }
146 double asFloat() const noexcept { return floating; }
148 const std::string& asString() const noexcept { return text; }
149};
150
153 std::string name;
154 ssq::Type type = ssq::Type::NULLPTR;
155 bool method = false;
156 std::vector<ReflectedAttribute> attributes;
160 const ReflectedAttribute* findAttribute(const std::string& name) const noexcept;
162 float attrFloat(const std::string& name, float def = 0.f) const noexcept;
164 bool attrBool(const std::string& name, bool def = false) const noexcept;
166 std::string attrString(const std::string& name, const std::string& def = {}) const;
168 std::vector<std::string> attrOptions(const std::string& name) const;
169};
170
173 std::string name;
174 std::string source;
175 std::string base;
176 std::vector<ReflectedMember> members;
177};
178
181 uint64_t id = 0;
182 std::string source;
183 ScriptState state = ScriptState::Compiled;
184 std::vector<std::string> classes;
185 std::string error;
186};
187
190public:
191 using ScriptId = uint64_t;
192 using ErrorHandler = std::function<void(const ScriptException&)>;
193 using LifecycleHandler = std::function<void(const ScriptInfo&)>;
194
197 public:
199 explicit StackGuard(Runtime& runtime) noexcept;
201 ~StackGuard();
203 StackGuard(StackGuard&& other) noexcept;
205 StackGuard& operator=(StackGuard&& other) noexcept;
206 StackGuard(const StackGuard&) = delete;
207 StackGuard& operator=(const StackGuard&) = delete;
208
210 int top() const noexcept { return static_cast<int>(top_); }
212 void dismiss() noexcept { vm_ = nullptr; }
213
214 private:
215 HSQUIRRELVM vm_ = nullptr;
216 SQInteger top_ = 0;
217 };
218
221 public:
223 explicit Scope(Runtime& runtime);
225 ~Scope();
227 Scope(Scope&& other) noexcept;
228 Scope& operator=(Scope&& other) = delete;
229 Scope(const Scope&) = delete;
230 Scope& operator=(const Scope&) = delete;
231
232 private:
233 Runtime* runtime_ = nullptr;
234 };
235
241 explicit Runtime(size_t stackSize = 2048, ssq::Libs::Flag libraries = ssq::Libs::ALL);
242 ~Runtime();
243 Runtime(Runtime&&) = delete;
245 Runtime(const Runtime&) = delete;
246 Runtime& operator=(const Runtime&) = delete;
247
249 void initialize();
251 void shutdown() noexcept;
253 bool initialized() const noexcept;
254
256 ssq::VM& vm() noexcept;
257 const ssq::VM& vm() const noexcept;
259 HSQUIRRELVM handle() const noexcept;
261 script::ScriptModuleResolver& scriptModules() noexcept;
263 const script::ScriptModuleResolver& scriptModules() const noexcept;
265 script::ScriptCompiler& scriptCompiler() noexcept;
267 const script::ScriptCompiler& scriptCompiler() const noexcept;
269 ssq::Table root() const;
274 ssq::Table table(const char* name) const;
275
277 StackGuard guard() noexcept { return StackGuard(*this); }
279 Scope enter() { return Scope(*this); }
281 static Runtime* current() noexcept;
283 static size_t stackDepth() noexcept;
284
292 ScriptId compileSource(std::string source, std::string sourceName = "buffer");
299 ScriptId compileFile(const std::string& path);
301 const ScriptInfo& execute(ScriptId id);
303 ScriptId runSource(std::string source, std::string sourceName = "buffer");
305 ScriptId runFile(const std::string& path);
307 ScriptId reflectFile(const std::string& path) { return runFile(path); }
313 const ReflectedClass& reflectClass(const std::string& name,
314 const std::string& source = {});
322 ssq::Object createInstance(const std::string& name, const std::string& source = {});
327 std::vector<ReflectedMember> reflectInstance(const ssq::Object& instance) const;
332 ReflectedValue readProperty(const ssq::Object& instance,
333 const std::string& name) const;
342 bool writeProperty(const ssq::Object& instance, const std::string& name,
343 const ReflectedValue& value) const;
349 ssq::Object readObjectProperty(const ssq::Object& instance,
350 const std::string& name) const;
351
352 // ---- array member editing ---------------------------------------------
354 size_t arraySize(const ssq::Object& instance, const std::string& name) const;
356 ReflectedValue arrayGet(const ssq::Object& instance, const std::string& name,
357 size_t index) const;
359 bool arraySet(const ssq::Object& instance, const std::string& name, size_t index,
360 const ReflectedValue& value) const;
362 bool arrayAppend(const ssq::Object& instance, const std::string& name,
363 const ReflectedValue& value) const;
365 bool arrayRemove(const ssq::Object& instance, const std::string& name,
366 size_t index) const;
367
368 // ---- table member editing ---------------------------------------------
370 std::vector<std::string> tableKeys(const ssq::Object& instance,
371 const std::string& name) const;
373 ReflectedValue tableGet(const ssq::Object& instance, const std::string& name,
374 const std::string& key) const;
376 bool tableSet(const ssq::Object& instance, const std::string& name,
377 const std::string& key, const ReflectedValue& value) const;
379 bool tableRemove(const ssq::Object& instance, const std::string& name,
380 const std::string& key) const;
382 ScriptId reload(ScriptId id);
384 bool unload(ScriptId id);
386 void unloadAll() noexcept;
387
389 bool contains(ScriptId id) const noexcept;
391 const ScriptInfo* script(ScriptId id) const noexcept;
393 std::vector<ScriptInfo> scripts() const;
395 const ReflectedClass* reflectedClass(const std::string& name) const noexcept;
397 std::vector<ReflectedClass> reflectedClasses() const;
405 size_t scanClasses();
407 ssq::Class findClass(const std::string& name) const;
409 std::string classNameOf(const ssq::Object& instance) const;
410
412 void setErrorHandler(ErrorHandler handler) { error_handler_ = std::move(handler); }
414 void setLifecycleHandler(LifecycleHandler handler) { lifecycle_handler_ = std::move(handler); }
415
416private:
417 struct ScriptRecord;
418
420 void installErrorHandler();
421
422 std::unique_ptr<ssq::VM> vm_;
423 std::unique_ptr<script::ScriptModuleResolver> script_modules_;
424 std::unique_ptr<script::ScriptCompiler> script_compiler_;
425 std::unordered_map<ScriptId, std::unique_ptr<ScriptRecord>> scripts_;
426 std::unordered_map<std::string, ReflectedClass> classes_;
427 std::unordered_map<std::string, ScriptId> class_owners_;
428 std::unordered_map<std::string, SQUserPointer> class_identities_;
429 ScriptId next_script_id_ = 1;
430 bool initialized_ = false;
431 bool shutting_down_ = false;
432 bool stopped_ = false;
433 ErrorHandler error_handler_;
434 LifecycleHandler lifecycle_handler_;
435
436 void notifyLifecycle(const ScriptInfo& info) noexcept;
437 script::ScriptErrorContext compileErrorContext(const std::string& what,
438 const std::string& sourceText);
439 [[noreturn]] void fail(ScriptStage stage, const std::string& source, ScriptId id,
440 const std::exception& error);
441 [[noreturn]] void fail(ScriptStage stage, const std::string& source, ScriptId id,
443 void discoverClasses(ScriptRecord& record,
444 const std::unordered_map<std::string, SQUserPointer>& before);
445 std::unordered_map<std::string, SQUserPointer> rootClasses() const;
446 ReflectedClass inspectClass(const std::string& name, const ssq::Class& cls,
447 const std::string& source) const;
449 std::vector<ReflectedMember> collectClassMembers(const ssq::Class& cls) const;
450};
451
452} // namespace eve
double value
int root
Definition AnimSmr.cpp:119
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
HSQOBJECT cls
Definition ECS.cpp:21
std::uint16_t method
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::string message
std::uint32_t key
std::string text
TokenKind kind
HSQOBJECT handler
std::string name
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
std::string error
Definition Package.cpp:60
std::string script
Definition PlayHost.cpp:116
std::string path
Definition PlayHost.cpp:110
PrimitiveHandle handle
double current
bool boolean
uint32_t index
const UnitySourceAsset & source
const VegetationPresetContext & context
Pushes this Runtime on the current thread's runtime stack.
Definition Runtime.h:220
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:196
StackGuard & operator=(const StackGuard &)=delete
void dismiss() noexcept
Dismiss.
Definition Runtime.h:212
StackGuard(const StackGuard &)=delete
int top() const noexcept
Top.
Definition Runtime.h:210
EVENGINE_API_FOUNDATION public API.
Definition Runtime.h:189
std::function< void(const ScriptInfo &)> LifecycleHandler
Definition Runtime.h:193
Runtime(Runtime &&)=delete
std::function< void(const ScriptException &)> ErrorHandler
Definition Runtime.h:192
Runtime & operator=(Runtime &&)=delete
Runtime(const Runtime &)=delete
uint64_t ScriptId
Definition Runtime.h:191
Runtime & operator=(const Runtime &)=delete
Scope enter()
Pushes this runtime on the thread-local runtime stack (RAII pop).
Definition Runtime.h:279
void setLifecycleHandler(LifecycleHandler handler)
Replaces the handler notified on every script lifecycle change.
Definition Runtime.h:414
Exception raised at the public Runtime boundary.
Definition Runtime.h:61
bool reported() const noexcept
True when a reporter (e.g. DevTool hook) already handled it.
Definition Runtime.h:93
const std::string & stackTrace() const noexcept
Multi-line call stack captured at the throw site.
Definition Runtime.h:91
bool hasLocation() const noexcept
True when the exception carries a script source line.
Definition Runtime.h:83
int column() const noexcept
1-based compile column; -1 when unknown.
Definition Runtime.h:87
ScriptStage stage() const noexcept
Stage.
Definition Runtime.h:77
const std::string & function() const noexcept
Function name at the throw site; empty when unknown.
Definition Runtime.h:89
const std::string & source() const noexcept
Source.
Definition Runtime.h:79
uint64_t scriptId() const noexcept
Script id.
Definition Runtime.h:81
int line() const noexcept
1-based throw/compile site line; -1 when unknown.
Definition Runtime.h:85
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
enum EVENGINE_API_FOUNDATION Float
OT_FLOAT.
Definition Runtime.h:118
enum EVENGINE_API_FOUNDATION Other
Any other slot kind.
Definition Runtime.h:123
enum EVENGINE_API_FOUNDATION Instance
Nested script instance (navigate; ObjectRef in property schema).
Definition Runtime.h:122
enum EVENGINE_API_FOUNDATION Bool
OT_BOOL.
Definition Runtime.h:116
ScriptState
ScriptState public API.
Definition Runtime.h:42
enum EVENGINE_API_FOUNDATION Integer
OT_INTEGER.
Definition Runtime.h:117
std::string reflectedFloatString(double value)
Shortest round-trip float formatting (portable).
Definition Runtime.h:33
ScriptStage
ScriptStage public API.
Definition Runtime.h:52
enum EVENGINE_API_FOUNDATION String
OT_STRING.
Definition Runtime.h:119
enum EVENGINE_API_FOUNDATION ReflectedValueKind
Runtime value kind read from a live script instance slot.
Definition Runtime.h:114
enum EVENGINE_API_FOUNDATION Table
OT_TABLE (editable via Runtime table APIs / ReflectedPropertyModel).
Definition Runtime.h:121
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:14
EVENGINE_API_FOUNDATION_INLINE public API.
Definition Runtime.h:107
EVENGINE_API_FOUNDATION_INLINE public API.
Definition Runtime.h:172
std::string base
Definition Runtime.h:175
std::string source
Definition Runtime.h:174
std::vector< ReflectedMember > members
Definition Runtime.h:176
std::string name
Definition Runtime.h:173
EVENGINE_API_FOUNDATION public API.
Definition Runtime.h:152
ReflectedValue value
Live value; empty for class-level reflection.
Definition Runtime.h:157
std::vector< ReflectedAttribute > attributes
Definition Runtime.h:156
std::string name
Definition Runtime.h:153
Typed snapshot of one live instance property.
Definition Runtime.h:132
const std::string & asString() const noexcept
As string.
Definition Runtime.h:148
bool empty() const noexcept
True when the slot exists and is non-null.
Definition Runtime.h:140
std::string text
Definition Runtime.h:137
bool asBool() const noexcept
As bool.
Definition Runtime.h:142
int64_t asInt() const noexcept
As int.
Definition Runtime.h:144
double asFloat() const noexcept
As float.
Definition Runtime.h:146
EVENGINE_API_FOUNDATION_INLINE public API.
Definition Runtime.h:180
std::string source
Definition Runtime.h:182
std::vector< std::string > classes
Definition Runtime.h:184
std::string error
Definition Runtime.h:185
Structured snapshot of a script error: message, throw site and stack.
Definition ScriptError.h:25
glm::uvec4 info