4#include <simplesquirrel/simplesquirrel.hpp>
8#include <unordered_map>
16 ~StackRestore() { sq_settop(vm, top); }
19ssq::Table cloneTable(
const ssq::Table&
source) {
22 if (SQ_FAILED(sq_clone(
vm, -1)))
throw std::runtime_error(
"cannot stage plugin namespace");
24 sq_getstackobj(
vm, -1, &
object.getRaw());
25 sq_addref(
vm, &
object.getRaw());
27 return ssq::Table(
object);
30using Slots = std::unordered_map<std::string, ssq::Object>;
32Slots
slots(
const ssq::Table& table) {
35 sq_pushobject(
vm,
table.getRaw());
37 while (SQ_SUCCEEDED(sq_next(
vm, -2))) {
38 const SQChar*
key =
nullptr;
39 if (SQ_SUCCEEDED(sq_getstring(
vm, -2, &
key))) {
41 sq_getstackobj(
vm, -1, &
value.getRaw());
51void eraseSlot(
const ssq::Table& table,
const std::string&
key) {
53 sq_pushobject(
vm,
table.getRaw());
54 sq_pushstring(
vm,
key.c_str(),
static_cast<SQInteger
>(
key.size()));
55 sq_deleteslot(
vm, -2, SQFalse);
59bool sameObject(
const ssq::Object&
a,
const ssq::Object&
b) {
62 return a.getRaw()._type ==
b.getRaw()._type &&
a.getRaw()._unVal.raw ==
b.getRaw()._unVal.raw;
68void retainBindings(
const ssq::Table& table) {
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);
75 sq_newslot(
vm, -3, SQFalse);
76 sq_pushstring(
vm,
"eve.plugin.generations", -1);
79 sq_pushobject(
vm,
table.getRaw());
80 sq_arrayappend(
vm, -2);
84void restoreTypeMap(ssq::VM& machine,
const Slots& before) {
85 auto vm = machine.getHandle();
86 for (
const auto& [
key,
object] : before) {
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());
98Result<void> ModuleManager::publishPluginBindings() {
100 auto* machine =
vm();
104 auto handle = machine->getHandle();
108 auto live = ssq::Table(machine->find(
"eve"));
110 auto candidate = cloneTable(
live);
113 retainBindings(candidate);
114 const auto&
owner =
state.plugin_registration_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_) {
121 if (
info.exposer)
info.exposer(candidate);
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);
139 for (
const auto&
key : oldKeys) eraseSlot(
live,
key);
142 }
catch (
const std::exception&
error) {
143 restoreTypeMap(*machine, before);
146 restoreTypeMap(*machine, before);
std::vector< std::unique_ptr< ScriptClimbingEventSink > > live
struct SQVM * HSQUIRRELVM
std::shared_ptr< OceanGpuSpectrum > owner
std::shared_ptr< const OceanSprayFrame > previous
LocalPageCacheEntry slots[ShadowConfig::kLocalSlots]
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...
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
void keys(const Value &value, std::initializer_list< const char * > names)
std::unordered_map< std::string, SkillDefinition > & table()
Build metadata (engine git commit, build time, third-party version).