载入中...
搜索中...
未找到
SquirrelBinding.cpp
浏览该文件的文档.
2
3#include <utility>
4
5#include "common/Assert.h"
8
9#include <algorithm>
10#include <cctype>
11#include <cmath>
12#include <cstdint>
13#include <cstdio>
14#include <cstring>
15#include <limits>
16#include <mutex>
17#include <string_view>
18#include <vector>
19
20namespace eve::script {
21
29namespace {
30struct ScriptCallFailureLog {
31 std::mutex mutex;
32 std::string lastMessage;
33 int count = 0;
34};
35
36ScriptCallFailureLog& scriptCallFailureLog() {
37 static ScriptCallFailureLog log;
38 return log;
39}
40
42void reportFailureText(const char* what, const std::string& text) {
43 ScriptCallFailureLog& log = scriptCallFailureLog();
44 std::lock_guard<std::mutex> lock(log.mutex);
45 ++log.count;
46 if (text == log.lastMessage) return;
47 log.lastMessage = text;
48 std::fprintf(stderr, "EVEngine: script call failed (%s): %s\n", what ? what : "?", text.c_str());
49}
50} // namespace
51
52std::string reportScriptCallFailure(HSQUIRRELVM vm, const char* what) {
54 const std::string text = context.empty() ? std::string("unknown script error") : formatScriptError(context);
55 reportFailureText(what, text);
56 return text;
57}
58
59SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char* name, const char* source) {
60 if (!vm || !source) return SQ_ERROR;
61 const SQInteger top = sq_gettop(vm);
62 if (SQ_FAILED(sq_compilebuffer(vm, source, static_cast<SQInteger>(std::strlen(source)), name, SQTrue))) {
64 reportFailureText(name, context.empty() ? std::string("unknown compile error") : formatScriptError(context));
65 sq_settop(vm, top);
66 return SQ_ERROR;
67 }
68 HSQOBJECT chunk;
69 sq_getstackobj(vm, -1, &chunk); // borrowed: still rooted by `top` until the call ends
70 const SQRESULT result = callFunction(vm, chunk, rootTable);
71 sq_settop(vm, top);
72 return result;
73}
74
76 ScriptCallFailureLog& log = scriptCallFailureLog();
77 std::lock_guard<std::mutex> lock(log.mutex);
78 return log.count;
79}
80
82 ScriptCallFailureLog& log = scriptCallFailureLog();
83 std::lock_guard<std::mutex> lock(log.mutex);
84 log.count = 0;
85 log.lastMessage.clear();
86}
87
88namespace {
89
90struct ConversionContext {
91 const SquirrelValueOptions& options;
92 std::size_t elements = 0;
93 std::vector<const void*> activeContainers;
94};
95
96Diagnostic conversionError(const ConversionContext& context, DiagnosticCode code, std::string message,
97 std::string path) {
98 return Diagnostic::error(code, std::move(message), std::move(path), {}, context.options.source);
99}
100
101std::string childPath(std::string_view parent, std::size_t index) {
102 return std::string(parent) + "[" + std::to_string(index) + "]";
103}
104
105std::string memberPath(std::string_view parent, std::string_view key) {
106 if (!key.empty() && (std::isalpha(static_cast<unsigned char>(key.front())) != 0 || key.front() == '_') &&
107 std::all_of(key.begin() + 1, key.end(),
108 [](unsigned char value) { return std::isalnum(value) != 0 || value == '_'; })) {
109 return std::string(parent) + "." + std::string(key);
110 }
111 return std::string(parent) + "[\"" + std::string(key) + "\"]";
112}
113
114bool takeObject(HSQUIRRELVM vm, SQInteger index, HSQOBJECT& object, Diagnostic& diagnostic,
115 const ConversionContext& context, const std::string& path) {
116 if (!vm || SQ_FAILED(sq_getstackobj(vm, index, &object))) {
117 diagnostic =
118 conversionError(context, DiagnosticCode::InvalidArgument, "Squirrel stack value is unavailable", path);
119 return false;
120 }
121 return true;
122}
123
124bool enterContainer(const HSQOBJECT& object, ConversionContext& context, Diagnostic& diagnostic,
125 const std::string& path) {
126 if (context.activeContainers.size() >= context.options.maxDepth) {
127 diagnostic = conversionError(context, DiagnosticCode::InvalidArgument,
128 "Squirrel value exceeds maximum conversion depth", path);
129 return false;
130 }
131 const void* identity = nullptr;
132 if (object._type == OT_ARRAY)
133 identity = static_cast<const void*>(object._unVal.pArray);
134 else if (object._type == OT_TABLE)
135 identity = static_cast<const void*>(object._unVal.pTable);
136 if (identity && std::find(context.activeContainers.begin(), context.activeContainers.end(), identity) !=
137 context.activeContainers.end()) {
138 diagnostic = conversionError(context, DiagnosticCode::InvalidArgument,
139 "cyclic Squirrel container cannot become an owning Value", path);
140 return false;
141 }
142 if (identity) context.activeContainers.push_back(identity);
143 return true;
144}
145
146void leaveContainer(const HSQOBJECT& object, ConversionContext& context) {
147 if ((object._type != OT_ARRAY && object._type != OT_TABLE) || context.activeContainers.empty()) return;
148 context.activeContainers.pop_back();
149}
150
151bool countElement(ConversionContext& context, Diagnostic& diagnostic, const std::string& path) {
152 if (context.elements >= context.options.maxElements) {
153 diagnostic = conversionError(context, DiagnosticCode::InvalidArgument,
154 "Squirrel value exceeds maximum element count", path);
155 return false;
156 }
157 ++context.elements;
158 return true;
159}
160
161bool convertAt(HSQUIRRELVM vm, SQInteger index, ConversionContext& context, const std::string& path, Value& output,
162 Diagnostic& diagnostic) {
163 if (!countElement(context, diagnostic, path)) return false;
164
165 const SQObjectType type = sq_gettype(vm, index);
166 switch (type) {
167 case OT_NULL: output = Value(); return true;
168 case OT_BOOL: {
169 SQBool value = SQFalse;
170 if (SQ_FAILED(sq_getbool(vm, index, &value))) {
171 diagnostic =
172 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel boolean", path);
173 return false;
174 }
175 output = Value(value != SQFalse);
176 return true;
177 }
178 case OT_INTEGER: {
179 SQInteger value = 0;
180 if (SQ_FAILED(sq_getinteger(vm, index, &value))) {
181 diagnostic =
182 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel integer", path);
183 return false;
184 }
185 output = Value(static_cast<std::int64_t>(value));
186 return true;
187 }
188 case OT_FLOAT: {
189 SQFloat value = 0;
190 if (SQ_FAILED(sq_getfloat(vm, index, &value))) {
191 diagnostic =
192 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel number", path);
193 return false;
194 }
195 const double converted = static_cast<double>(value);
196 if (!std::isfinite(converted)) {
197 diagnostic = conversionError(context, DiagnosticCode::InvalidArgument,
198 "non-finite Squirrel number is not representable", path);
199 return false;
200 }
201 output = Value(converted);
202 return true;
203 }
204 case OT_STRING: {
205 const SQChar* value = nullptr;
206 SQInteger size = 0;
207 if (SQ_FAILED(sq_getstringandsize(vm, index, &value, &size)) || !value || size < 0) {
208 diagnostic =
209 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel string", path);
210 return false;
211 }
212 output = Value(std::string(value, static_cast<std::size_t>(size)));
213 return true;
214 }
215 case OT_ARRAY:
216 case OT_TABLE: break;
217 default:
218 diagnostic = conversionError(context, DiagnosticCode::Unsupported,
219 "Squirrel value kind is not part of eve::Value", path);
220 return false;
221 }
222
223 HSQOBJECT object{};
224 if (!takeObject(vm, index, object, diagnostic, context, path)) return false;
225 if (!enterContainer(object, context, diagnostic, path)) return false;
226
227 bool converted = true;
228 if (type == OT_ARRAY) {
229 const SQInteger size = sq_getsize(vm, index);
230 if (size < 0) {
231 diagnostic =
232 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel array size", path);
233 converted = false;
234 } else {
236 values.reserve(static_cast<std::size_t>(size));
237 const SQInteger absolute = index > 0 ? index : sq_gettop(vm) + index + 1;
238 for (SQInteger i = 0; converted && i < size; ++i) {
239 sq_pushinteger(vm, i);
240 if (SQ_FAILED(sq_get(vm, absolute))) {
241 diagnostic =
242 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel array element",
243 childPath(path, static_cast<std::size_t>(i)));
244 converted = false;
245 break;
246 }
248 converted =
249 convertAt(vm, -1, context, childPath(path, static_cast<std::size_t>(i)), element, diagnostic);
250 sq_pop(vm, 1);
251 if (converted) values.push_back(std::move(element));
252 }
253 if (converted) output = Value(std::move(values));
254 }
255 } else {
257 const SQInteger absolute = index > 0 ? index : sq_gettop(vm) + index + 1;
258 sq_pushnull(vm);
259 while (converted && SQ_SUCCEEDED(sq_next(vm, absolute))) {
260 if (sq_gettype(vm, -2) != OT_STRING) {
261 diagnostic = conversionError(context, DiagnosticCode::Unsupported,
262 "Squirrel table keys must be strings", path + "[<non-string-key>]");
263 converted = false;
264 sq_pop(vm, 2);
265 break;
266 }
267 const SQChar* key = nullptr;
268 SQInteger keySize = 0;
269 if (SQ_FAILED(sq_getstringandsize(vm, -2, &key, &keySize)) || !key || keySize < 0) {
270 diagnostic =
271 conversionError(context, DiagnosticCode::ParseError, "failed to read Squirrel table key", path);
272 converted = false;
273 sq_pop(vm, 2);
274 break;
275 }
276 const std::string keyText(key, static_cast<std::size_t>(keySize));
277 const std::string field = memberPath(path, keyText);
278 Value value;
279 converted = convertAt(vm, -1, context, field, value, diagnostic);
280 sq_pop(vm, 2);
281 if (converted) fields.emplace(keyText, std::move(value));
282 }
283 sq_pop(vm, 1); // iterator
284 if (converted) output = Value(std::move(fields));
285 }
286 leaveContainer(object, context);
287 return converted;
288}
289
290bool validateValue(const Value& value, const SquirrelValueOptions& options, std::size_t depth, std::size_t& elements,
291 const std::string& path, Diagnostic& diagnostic) {
293 diagnostic = Diagnostic::error(DiagnosticCode::InvalidArgument, "Value exceeds maximum Squirrel element count",
294 path, {}, options.source);
295 return false;
296 }
297 ++elements;
298 if (value.isDouble() && !std::isfinite(value.asDouble())) {
299 diagnostic = Diagnostic::error(DiagnosticCode::InvalidArgument, "non-finite Value cannot be pushed to Squirrel",
300 path, {}, options.source);
301 return false;
302 }
303 if (!value.isArray() && !value.isObject()) return true;
304 if (depth >= options.maxDepth) {
306 "Value exceeds maximum Squirrel conversion depth", path, {}, options.source);
307 return false;
308 }
309 if (value.isArray()) {
310 for (std::size_t index = 0; index < value.arraySize(); ++index) {
311 if (!validateValue(value.at(index), options, depth + 1, elements, childPath(path, index), diagnostic))
312 return false;
313 }
314 return true;
315 }
316 for (const auto& [key, member] : *value.getIf<Value::Object>()) {
317 if (!validateValue(member, options, depth + 1, elements, memberPath(path, key), diagnostic)) return false;
318 }
319 return true;
320}
321
322bool pushImpl(HSQUIRRELVM vm, const Value& value, const SquirrelValueOptions& options, std::size_t depth,
323 const std::string& path, Diagnostic& diagnostic) {
324 if (value.isNull()) {
325 sq_pushnull(vm);
326 return true;
327 }
328 if (value.isBool()) {
329 sq_pushbool(vm, value.asBool() ? SQTrue : SQFalse);
330 return true;
331 }
332 if (value.isInt64()) {
333 const auto integer = value.asInt();
334 if (integer < static_cast<std::int64_t>(std::numeric_limits<SQInteger>::min()) ||
335 integer > static_cast<std::int64_t>(std::numeric_limits<SQInteger>::max())) {
336 diagnostic = Diagnostic::error(DiagnosticCode::InvalidArgument, "Int64 value does not fit Squirrel integer",
337 path, {}, options.source);
338 return false;
339 }
340 sq_pushinteger(vm, static_cast<SQInteger>(integer));
341 return true;
342 }
343 if (value.isDouble()) {
344 sq_pushfloat(vm, static_cast<SQFloat>(value.asDouble()));
345 return true;
346 }
347 if (value.isString()) {
348 const std::string& string = value.asString();
349 sq_pushstring(vm, string.c_str(), static_cast<SQInteger>(string.size()));
350 return true;
351 }
352 if (depth >= options.maxDepth) {
354 "Value exceeds maximum Squirrel conversion depth", path, {}, options.source);
355 return false;
356 }
357 if (value.isArray()) {
358 sq_newarray(vm, 0);
359 for (std::size_t index = 0; index < value.arraySize(); ++index) {
360 if (!pushImpl(vm, value.at(index), options, depth + 1, childPath(path, index), diagnostic)) return false;
361 if (SQ_FAILED(sq_arrayappend(vm, -2))) {
362 diagnostic =
363 Diagnostic::error(DiagnosticCode::Failed, "failed to append Value array element to Squirrel",
364 childPath(path, index), {}, options.source);
365 return false;
366 }
367 }
368 return true;
369 }
370 sq_newtable(vm);
371 for (const auto& [key, member] : *value.getIf<Value::Object>()) {
372 sq_pushstring(vm, key.c_str(), static_cast<SQInteger>(key.size()));
373 if (!pushImpl(vm, member, options, depth + 1, memberPath(path, key), diagnostic)) return false;
374 if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) {
375 diagnostic = Diagnostic::error(DiagnosticCode::Failed, "failed to add Value object member to Squirrel",
376 memberPath(path, key), {}, options.source);
377 return false;
378 }
379 }
380 return true;
381}
382
383ssq::Object objectFromTop(HSQUIRRELVM vm, SQInteger top) {
384 ssq::Object output(vm);
385 if (SQ_SUCCEEDED(sq_getstackobj(vm, -1, &output.getRaw()))) {
386 sq_addref(vm, &output.getRaw());
387 }
388 sq_settop(vm, top);
389 return output;
390}
391
392ssq::Object projectValueObject(HSQUIRRELVM vm, const Value& value) {
393 if (!vm) return ssq::Object();
394 const SQInteger top = sq_gettop(vm);
395 auto pushed = pushValue(vm, value);
396 if (!pushed.ok()) {
397 pushed.ignore("Result projection could not push its value payload");
398 sq_settop(vm, top);
399 return ssq::Object(vm);
400 }
401 return objectFromTop(vm, top);
402}
403
404bool readBoolField(const ssq::Object& object, const char* name) {
405 if (object.getType() != ssq::Type::TABLE) return false;
406 const ssq::Object field = object.find(name);
407 return field.getType() == ssq::Type::BOOL && field.toBool();
408}
409
410} // namespace
411
413 if (!vm)
414 return Result<Value>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "Squirrel VM must not be null",
415 "$", {}, options.source));
416 const SQInteger top = sq_gettop(vm);
417 ConversionContext context{options, 0, {}};
419 Diagnostic diagnostic;
420 const bool converted = convertAt(vm, index, context, "$", output, diagnostic);
421 sq_settop(vm, top);
422 if (!converted) return Result<Value>::failure(std::move(diagnostic));
423 return Result<Value>::success(std::move(output));
424}
425
427 const HSQUIRRELVM vm = object.getHandle();
428 if (!vm || object.isEmpty())
430 Diagnostic::error(DiagnosticCode::InvalidArgument, "Squirrel object is empty", "$", {}, options.source));
431 const SQInteger top = sq_gettop(vm);
432 sq_pushobject(vm, object.getRaw());
433 auto result = valueFromSquirrel(vm, -1, options);
434 // valueFromSquirrel restores to the height observed after this push.
435 sq_settop(vm, top);
436 return result;
437}
438
440 if (!vm)
441 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "Squirrel VM must not be null",
442 "$", {}, options.source));
443 std::size_t elements = 0;
444 Diagnostic diagnostic;
445 if (!validateValue(value, options, 0, elements, "$", diagnostic))
446 return Result<void>::failure(std::move(diagnostic));
447
448 const SQInteger top = sq_gettop(vm);
449 if (!pushImpl(vm, value, options, 0, "$", diagnostic)) {
450 sq_settop(vm, top);
451 return Result<void>::failure(std::move(diagnostic));
452 }
453 return Result<void>::success();
454}
455
456ssq::Table projectDiagnostic(HSQUIRRELVM vm, const Diagnostic& diagnostic) {
457 ssq::Table result(vm);
458 result.set("code", std::string(diagnosticCodeName(diagnostic.code())));
459 result.set("severity", [&diagnostic] {
460 switch (diagnostic.severity()) {
461 case Severity::Info: return std::string("info");
462 case Severity::Warning: return std::string("warning");
463 case Severity::Error: return std::string("error");
464 case Severity::Fatal: return std::string("fatal");
465 }
466 return std::string("unknown");
467 }());
468 result.set("message", diagnostic.message());
469 result.set("path", diagnostic.path());
470 result.set("source", diagnostic.source());
471 ssq::Table details(vm);
472 for (const auto& [key, value] : diagnostic.details()) details.set(key.c_str(), value);
473 result.set("details", details);
474 return result;
475}
476
477namespace {
478
480ssq::Array projectDiagnostics(HSQUIRRELVM vm, const Status& status) {
481 ssq::Array diagnostics(vm);
482 for (const Diagnostic& diagnostic : status.diagnostics()) diagnostics.push(projectDiagnostic(vm, diagnostic));
483 return diagnostics;
484}
485
487ssq::Table buildStatusTable(HSQUIRRELVM vm, const Status& status, const ssq::Array& diagnostics) {
488 ssq::Table result(vm);
489 result.set("ok", status.isSuccess());
490 result.set("code", std::string(statusCodeName(status.code())));
491 result.set("summary", status.describe());
492 result.set("diagnostics", diagnostics);
493 result.set("diagnosticCount", static_cast<std::int64_t>(status.diagnostics().size()));
494 return result;
495}
496
503ssq::Table buildResultTable(HSQUIRRELVM vm, const Status& status, bool hasValue) {
504 ssq::Table result(vm);
505 result.set("ok", status.isSuccess());
506 result.set("code", std::string(statusCodeName(status.code())));
507 result.set("hasValue", hasValue);
508 result.set("checked", true);
509 result.set("ignored", false);
510 result.set("ignoreReason", std::string{});
511 ssq::Array diagnostics = projectDiagnostics(vm, status);
512 result.set("status", buildStatusTable(vm, status, diagnostics));
513 result.set("diagnostics", diagnostics);
514 if (!hasValue) result.set("value", ssq::Object(vm));
515 return result;
516}
517
518} // namespace
519
521 return buildStatusTable(vm, status, projectDiagnostics(vm, status));
522}
523
524ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status& status) { return buildResultTable(vm, status, false); }
525
527 ssq::Table result = buildResultTable(vm, status, true);
528 result.set("value", projectValueObject(vm, value));
529 return result;
530}
531
532ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status& status, ssq::Object value) {
533 ssq::Table result = buildResultTable(vm, status, true);
534 result.set("value", std::move(value));
535 return result;
536}
537
538void markResultHasValue(ssq::Table& result) { result.set("hasValue", true); }
539
541 const Status status = result.status();
543}
544
545bool ignoreResult(const ssq::Object& result, const std::string& reason) {
546 if (result.getType() != ssq::Type::TABLE || reason.empty()) return false;
547 HSQUIRRELVM vm = result.getHandle();
548 if (!vm) return false;
549 const SQInteger top = sq_gettop(vm);
550 sq_pushobject(vm, result.getRaw());
551 sq_pushstring(vm, "ignored", -1);
552 sq_pushbool(vm, SQTrue);
553 if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) {
554 sq_settop(vm, top);
555 return false;
556 }
557 sq_pushstring(vm, "ignoreReason", -1);
558 sq_pushstring(vm, reason.c_str(), static_cast<SQInteger>(reason.size()));
559 if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) {
560 sq_settop(vm, top);
561 return false;
562 }
563 // A result table is already consumed by C++; this flag makes the script
564 // intent explicit without pretending that ignored means successful.
565 sq_pushstring(vm, "checked", -1);
566 sq_pushbool(vm, SQTrue);
567 if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) {
568 sq_settop(vm, top);
569 return false;
570 }
571 sq_settop(vm, top);
572 return true;
573}
574
575namespace {
576
578Value providerHandleValue(ScriptProviderHandle handle) {
579 return Value::object({{"name", Value::string(std::move(handle.name))},
580 {"generation", Value::integer(static_cast<std::int64_t>(handle.generation))}});
581}
582
583} // namespace
584
585void exposeResultBindings(ssq::Table& eveTable) {
586 ssq::Table result = eveTable.addTable("result");
587 result.addFunc("ignore", [](ssq::Object value, const std::string& reason) { return ignoreResult(value, reason); });
588 result.addFunc("isChecked", [](ssq::Object value) { return readBoolField(value, "checked"); });
589 result.addFunc("isIgnored", [](ssq::Object value) { return readBoolField(value, "ignored"); });
590
591 // Use the canonical owning Value codec for large script documents instead
592 // of quadratic VM string concatenation. Conversion retains its normal
593 // depth/element bounds and never borrows script storage across this call.
594 auto json = eveTable.addTable("json");
595 const auto vm = eveTable.getHandle();
596 json.addFunc("encode", [vm](const ssq::Object& object) {
597 auto value = valueFromSquirrel(object);
598 if (!value.ok()) return projectStatusResult(vm, value.status());
599 return projectResult(vm, value.value().toJson(), [](std::string text) { return Value(std::move(text)); });
600 });
601 json.addFunc("decode", [vm](const std::string& text) {
602 return projectResult(vm, Value::fromJson(text), [](Value value) { return value; });
603 });
604}
605
606void exposeProviderBindings(ssq::Table& eveTable) {
607 ssq::Table provider = eveTable.addTable("provider");
608 HSQUIRRELVM vm = eveTable.getHandle();
609
610 provider.addFunc("publish", [vm](const std::string& name, const ssq::Object& instance) {
611 return projectResult(vm, ScriptProviderRegistry::instance().publish(name, instance), providerHandleValue);
612 });
613
614 provider.addFunc("publishFor", [vm](const std::string& capability, const std::string& name,
615 const ssq::Object& instance) {
616 return projectResult(vm, ScriptProviderRegistry::instance().publishForCapability(capability, name, instance),
617 providerHandleValue);
618 });
619
620 // Script addresses a provider by name: the registry keeps exactly one live
621 // generation per name, so a script-held numeric generation would add no safety
622 // over the registry's own check. Native callers keep the generation-qualified
623 // handle, which is what protects them across a supersede.
624 provider.addFunc("unload", [vm](const std::string& name) {
626 return projectResult(vm, registry.unload(ScriptProviderHandle{name, registry.generationOf(name)}));
627 });
628
629 provider.addFunc("contains",
630 [](const std::string& name) { return ScriptProviderRegistry::instance().contains(name); });
631
632 provider.addFunc("generationOf", [](const std::string& name) {
633 return static_cast<std::int64_t>(ScriptProviderRegistry::instance().generationOf(name));
634 });
635
636 // Zero-argument lambdas spell out `()`: the binding-contract generator reads a
637 // binding's parameters from the lambda's own parameter list, so `[] { ... }` is
638 // reported as an unresolved binding (and fails the strict generation step).
639 provider.addFunc("size", []() { return static_cast<std::int64_t>(ScriptProviderRegistry::instance().size()); });
640
641 provider.addFunc("capabilities", [vm]() {
642 ssq::Array out(vm);
643 for (const std::string& name : ScriptProviderRegistry::instance().capabilities()) out.push(name);
644 return out;
645 });
646
647 provider.addFunc("published", [vm]() {
648 ssq::Array out(vm);
650 ssq::Table entry(vm);
651 entry.set("name", info.name);
652 entry.set("generation", static_cast<std::int64_t>(info.generation));
653 entry.set("capability", info.capability);
654 out.push(entry);
655 }
656 return out;
657 });
658}
659
660} // namespace eve::script
EVEngine assertion entry point, backed by zeroerr.
std::string output
std::mutex mutex
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
std::string message
DiagnosticCode code
wgpu::PopErrorScopeStatus status
const ColourScope * parent
Poco::JSON::Object::Ptr result
std::string path
Definition PlayHost.cpp:111
PrimitiveHandle handle
std::set< std::string > values
const eve::script::BindingContractRegistry & registry
Object::Ptr value
std::string text
ScriptStructure out
std::string_view source
std::string element
std::vector< const void * > activeContainers
std::size_t elements
const SquirrelValueOptions & options
int count
std::string lastMessage
The single Squirrel projection for common Result, Status and Value.
float size
Definition TreeMesh.cpp:158
float(ui::Theme::* member)[4]
std::uint32_t depth
uint32_t index
const VegetationPresetContext & context
uint64_t identity
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
DiagnosticCode code() const noexcept
Stable machine-readable code.
Definition Diagnostic.h:143
const std::string & path() const noexcept
Optional field, property, URI, or logical path.
Definition Diagnostic.h:149
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Severity severity() const noexcept
Severity for logging and presentation.
Definition Diagnostic.h:145
const std::string & message() const noexcept
Human-readable explanation.
Definition Diagnostic.h:147
const DiagnosticDetails & details() const noexcept
Structured context in insertion order.
Definition Diagnostic.h:151
const std::string & source() const noexcept
Optional producer or subsystem that emitted the diagnostic.
Definition Diagnostic.h:153
Move-only operation result carrying either a value or Status.
Definition Result.h:176
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
Named script objects that native systems drive through named methods.
static ScriptProviderRegistry & instance()
Process-lifetime registry; clearAll is the only reset.
std::vector< ScriptProviderInfo > published() const
Every currently published provider.
bool contains(std::string_view name) const
Whether name currently has a published provider.
std::vector< std::string > capabilities() const
Names of every capability that has a declared script binding seam.
std::size_t size() const
Number of published providers.
std::uint64_t generationOf(std::string_view name) const
Current published generation for name, or 0 when absent.
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
std::vector< Value > Array
Definition Value.h:33
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
SQInteger top
Definition Snapshot.cpp:115
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
eve::Diagnostic Diagnostic
SQRESULT callFunction(HSQUIRRELVM vm, const HSQOBJECT &fn, const Receiver &receiver, Args &&... args)
Call a script function, deriving sq_call's parameter count from the pushes.
bool ignoreResult(const ssq::Object &result, const std::string &reason)
Mark a projected Result table as intentionally ignored by script.
std::string formatScriptError(const ScriptErrorContext &ctx)
Formats a context into a human-readable multi-line report.
int reportedScriptCallFailures()
Count of engine-to-script call failures reported since the last reset.
ScriptErrorContext takeLastScriptError(HSQUIRRELVM vm)
Consumes and clears the last recorded error for a VM.
SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char *name, const char *source)
Compile and run one embedded script chunk against the root table.
ssq::Table projectDiagnostic(HSQUIRRELVM vm, const Diagnostic &diagnostic)
Project one Diagnostic using the common script table schema.
void resetReportedScriptCallFailures()
Clear the failure count and the deduplication memory (diagnostics and tests).
Result< void > pushValue(HSQUIRRELVM vm, const Value &value, const SquirrelValueOptions &options)
Push a canonical Value into the active Squirrel VM.
void markResultHasValue(ssq::Table &result)
Declare that a projected table received a payload the caller attached itself.
ssq::Table projectStatus(HSQUIRRELVM vm, const Status &status)
Project one Status using the common script table schema.
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ScriptErrorContext captureCompileError(HSQUIRRELVM vm)
Captures the last compilation error recorded by the VM.
constexpr RootTable rootTable
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
void exposeProviderBindings(ssq::Table &eveTable)
Expose eve.provider: script-published providers and capability seams.
std::string reportScriptCallFailure(HSQUIRRELVM vm, const char *what)
Report a failed engine-to-script call, consuming the VM's script error.
void exposeResultBindings(ssq::Table &eveTable)
Expose the common script helpers under eve.result.
Result< Value > valueFromSquirrel(HSQUIRRELVM vm, SQInteger index, const SquirrelValueOptions &options)
Convert one Squirrel value into the canonical owning Value tree.
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
constexpr std::string_view statusCodeName(StatusCode code) noexcept
Return the stable protocol spelling of a status code.
Definition Status.h:46
constexpr std::string_view diagnosticCodeName(DiagnosticCode code) noexcept
Return a stable, compact spelling useful for logs and diagnostics.
Definition Diagnostic.h:176
Generation-qualified identity of one published script provider.
Read-only view of one published provider, for discovery and diagnostics.
Structured snapshot of a script error: message, throw site and stack.
Definition ScriptError.h:46
Limits and provenance used by the Squirrel ↔ Value adapter.
glm::uvec4 info