载入中...
搜索中...
未找到
ModulePlugin.cpp
浏览该文件的文档.
2
3#include <squirrel.h>
4#include <simplesquirrel/simplesquirrel.hpp>
5
6#include <algorithm>
7#include <stdexcept>
8#include <unordered_map>
9
10namespace eve {
11namespace {
12
13struct StackRestore {
15 SQInteger top;
16 ~StackRestore() { sq_settop(vm, top); }
17};
18
19ssq::Table cloneTable(const ssq::Table& source) {
20 auto vm = source.getHandle();
21 sq_pushobject(vm, source.getRaw());
22 if (SQ_FAILED(sq_clone(vm, -1))) throw std::runtime_error("cannot stage plugin namespace");
23 ssq::Object object(vm);
24 sq_getstackobj(vm, -1, &object.getRaw());
25 sq_addref(vm, &object.getRaw());
26 sq_pop(vm, 2);
27 return ssq::Table(object);
28}
29
30using Slots = std::unordered_map<std::string, ssq::Object>;
31
32Slots slots(const ssq::Table& table) {
33 auto vm = table.getHandle();
34 Slots result;
35 sq_pushobject(vm, table.getRaw());
36 sq_pushnull(vm);
37 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
38 const SQChar* key = nullptr;
39 if (SQ_SUCCEEDED(sq_getstring(vm, -2, &key))) {
40 ssq::Object value(vm);
41 sq_getstackobj(vm, -1, &value.getRaw());
42 sq_addref(vm, &value.getRaw());
43 result.emplace(key, std::move(value));
44 }
45 sq_pop(vm, 2);
46 }
47 sq_pop(vm, 2);
48 return result;
49}
50
51void eraseSlot(const ssq::Table& table, const std::string& key) {
52 auto vm = table.getHandle();
53 sq_pushobject(vm, table.getRaw());
54 sq_pushstring(vm, key.c_str(), static_cast<SQInteger>(key.size()));
55 sq_deleteslot(vm, -2, SQFalse);
56 sq_pop(vm, 1);
57}
58
59bool sameObject(const ssq::Object& a, const ssq::Object& b) {
60 // Identity comparison must not invoke _cmp or compare incompatible types:
61 // sq_cmp leaves its result undefined when the VM rejects a comparison.
62 return a.getRaw()._type == b.getRaw()._type && a.getRaw()._unVal.raw == b.getRaw()._unVal.raw;
63}
64
65// SimpleSquirrel's native type map stores unrooted class handles. Keep every
66// generation (including a partially bound candidate) rooted in the VM registry,
67// so those handles and their native closure destructors stay valid until VM close.
68void retainBindings(const ssq::Table& table) {
69 auto vm = table.getHandle();
70 sq_pushregistrytable(vm);
71 sq_pushstring(vm, "eve.plugin.generations", -1);
72 if (SQ_FAILED(sq_rawget(vm, -2))) {
73 sq_pushstring(vm, "eve.plugin.generations", -1);
74 sq_newarray(vm, 0);
75 sq_newslot(vm, -3, SQFalse);
76 sq_pushstring(vm, "eve.plugin.generations", -1);
77 sq_rawget(vm, -2);
78 }
79 sq_pushobject(vm, table.getRaw());
80 sq_arrayappend(vm, -2);
81 sq_pop(vm, 2);
82}
83
84void restoreTypeMap(ssq::VM& machine, const Slots& before) {
85 auto vm = machine.getHandle();
86 for (const auto& [key, object] : before) {
87 (void)key;
88 sq_pushobject(vm, object.getRaw());
89 SQUserPointer tag = nullptr;
90 if (sq_gettype(vm, -1) == OT_CLASS && SQ_SUCCEEDED(sq_gettypetag(vm, -1, &tag)) && tag)
91 machine.addClassObj(reinterpret_cast<size_t>(tag), object.getRaw());
92 sq_pop(vm, 1);
93 }
94}
95
96} // namespace
97
98Result<void> ModuleManager::publishPluginBindings() {
99 auto& state = *inst().state_;
100 auto* machine = vm();
101 if (!machine)
103 DiagnosticCode::Conflict, "plugin loading requires an attached script VM", {}, {}, "module"));
104 auto handle = machine->getHandle();
105 StackRestore stack{handle, sq_gettop(handle)};
106 Slots before;
107 try {
108 auto live = ssq::Table(machine->find("eve"));
109 auto previous = cloneTable(live);
110 auto candidate = cloneTable(live);
111 before = slots(previous);
112 retainBindings(previous);
113 retainBindings(candidate);
114 const auto& owner = state.plugin_registration_owner_;
115 const auto found = state.plugin_binding_keys_.find(owner);
116 const std::vector<std::string> oldKeys =
117 found == state.plugin_binding_keys_.end() ? std::vector<std::string>{} : found->second;
118 for (const auto& key : oldKeys) eraseSlot(candidate, key);
119 for (const auto& name : state.plugin_registration_added_) {
120 auto& info = state.registered_modules.at(name);
121 if (info.exposer) info.exposer(candidate);
122 info.exposed = true;
123 }
124 const auto after = slots(candidate);
125 std::vector<std::string> keys;
126 for (const auto& [key, value] : after) {
127 const auto existing = before.find(key);
128 const bool owned = std::find(oldKeys.begin(), oldKeys.end(), key) != oldKeys.end();
129 if (existing != before.end() && !owned) {
130 if (!sameObject(existing->second, value))
131 throw std::runtime_error("plugin binding conflicts with existing eve slot: " + key);
132 } else {
133 keys.push_back(key);
134 }
135 }
136 // Publication only mutates eve's slots; existing references to the eve
137 // table remain valid. No script or plugin callback runs during publication.
138 state.plugin_binding_keys_[owner] = keys;
139 for (const auto& key : oldKeys) eraseSlot(live, key);
140 for (const auto& key : keys) live.set(key.c_str(), after.at(key));
142 } catch (const std::exception& error) {
143 restoreTypeMap(*machine, before);
145 } catch (...) {
146 restoreTypeMap(*machine, before);
148 Diagnostic::error(DiagnosticCode::Conflict, "plugin script binding threw", {}, {}, "module"));
149 }
150}
151
152} // namespace eve
ActiveSource owned
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * tag
std::shared_ptr< OceanGpuSpectrum > owner
std::shared_ptr< const OceanSprayFrame > previous
std::string error
Definition Package.cpp:174
PrimitiveHandle handle
Object::Ptr value
bool found
std::string_view source
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
Definition Shadow.cpp:159
Json object
std::size_t at
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static ModuleManager & inst()
Singleton registry holding every registered engine module.
static ssq::VM * vm()
Script VM of the last expose call, including raw-VM embedders, or nullptr. @ownership Borrowed; Runti...
Definition Module.cpp:214
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
SQInteger top
Definition Snapshot.cpp:115
void keys(const Value &value, std::initializer_list< const char * > names)
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
glm::uvec4 info