载入中...
搜索中...
未找到
SquirrelBinding.h
浏览该文件的文档.
1#pragma once
2
13#include "common/Export.h"
14#include "common/Result.h"
15#include "common/Value.h"
16
17#include <squirrel.h>
18#include <simplesquirrel/simplesquirrel.hpp>
19
20#include <cstddef>
21#include <functional>
22#include <string>
23#include <type_traits>
24#include <utility>
25
26namespace eve::script {
27
37 std::size_t maxDepth = 64;
38 std::size_t maxElements = 100000;
39 std::string source = "squirrel.binding";
40};
41
52 const SquirrelValueOptions& options = {});
53
60[[nodiscard]] EVENGINE_API_FOUNDATION Result<Value> valueFromSquirrel(const ssq::Object& object,
61 const SquirrelValueOptions& options = {});
62
71[[nodiscard]] EVENGINE_API_FOUNDATION Result<void> pushValue(HSQUIRRELVM vm, const Value& value,
72 const SquirrelValueOptions& options = {});
73
75[[nodiscard]] EVENGINE_API_FOUNDATION ssq::Table projectDiagnostic(HSQUIRRELVM vm, const Diagnostic& diagnostic);
76
78[[nodiscard]] EVENGINE_API_FOUNDATION ssq::Table projectStatus(HSQUIRRELVM vm, const Status& status);
79
87struct RootTable {};
88inline constexpr RootTable rootTable{};
89
91template <typename Receiver>
92void pushReceiver(HSQUIRRELVM vm, const Receiver& receiver) {
93 if constexpr (std::is_same_v<std::decay_t<Receiver>, RootTable>) {
94 sq_pushroottable(vm);
95 } else {
96 ssq::detail::pushValue(vm, receiver);
97 }
98}
99
116template <typename Arg>
117decltype(auto) scriptArgument(const Arg& arg) {
118 using Bare = std::remove_cv_t<std::remove_reference_t<Arg>>;
119 if constexpr (std::is_array_v<Bare> || std::is_same_v<Bare, char*> || std::is_same_v<Bare, const char*>) {
120 return std::string(arg);
121 } else {
122 return (arg);
123 }
124}
125
154
157
160
180EVENGINE_API_FOUNDATION SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char* name, const char* source);
181
211template <typename Receiver, typename... Args>
212SQRESULT callFunction(HSQUIRRELVM vm, const HSQOBJECT& fn, const Receiver& receiver, Args&&... args) {
213 const SQInteger top = sq_gettop(vm);
214 sq_pushobject(vm, fn);
215 pushReceiver(vm, receiver);
217 // Everything above the callable: the receiver plus the arguments. The callable itself is not
218 // part of the count (sq_call takes the callee at -(params)-1).
219 const SQInteger params = sq_gettop(vm) - top - 1;
220 const SQRESULT result = sq_call(vm, params, SQFalse, SQTrue);
221 if (SQ_FAILED(result)) reportScriptCallFailure(vm, "callFunction");
222 sq_settop(vm, top);
223 return result;
224}
225
239template <typename Receiver, typename... Args>
240SQRESULT callFunctionResult(HSQUIRRELVM vm, const HSQOBJECT& fn, const Receiver& receiver, Args&&... args) {
241 const SQInteger top = sq_gettop(vm);
242 sq_pushobject(vm, fn);
243 pushReceiver(vm, receiver);
245 // Everything above the callable: the receiver plus the arguments. The callable itself is not
246 // part of the count (sq_call takes the callee at -(params)-1).
247 const SQInteger params = sq_gettop(vm) - top - 1;
248 const SQRESULT result = sq_call(vm, params, SQTrue, SQTrue);
249 if (SQ_FAILED(result)) {
250 reportScriptCallFailure(vm, "callFunctionResult");
251 sq_settop(vm, top);
252 }
253 return result;
254}
255
265
274 const Value& value);
275
285 ssq::Object value);
286
295
307template <class T, class Projector>
309[[nodiscard]] ssq::Table projectResult(HSQUIRRELVM vm, Result<T>&& result, Projector&& projector) {
310 if (!result.ok()) return projectStatusResult(vm, result.status());
311
312 const Status status = result.status();
313 T payload = std::move(result).takeValue();
315 return projectStatusResult(vm, status, std::invoke(std::forward<Projector>(projector), std::move(payload)));
316}
317
320
327[[nodiscard]] EVENGINE_API_FOUNDATION bool ignoreResult(const ssq::Object& result, const std::string& reason);
328
335EVENGINE_API_FOUNDATION void exposeResultBindings(ssq::Table& eveTable);
336
350EVENGINE_API_FOUNDATION void exposeProviderBindings(ssq::Table& eveTable);
351
352} // namespace eve::script
Value::Object payload
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
wgpu::PopErrorScopeStatus status
Poco::JSON::Object::Ptr result
std::vector< RecipeParam > params
Object::Ptr value
Move-only, checked operation results for the common layer.
std::string_view source
const SquirrelValueOptions & options
uint32_t index
Owning, renderer-independent dynamic values.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
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
SQInteger top
Definition Snapshot.cpp:115
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.
int reportedScriptCallFailures()
Count of engine-to-script call failures reported since the last reset.
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.
SQRESULT callFunctionResult(HSQUIRRELVM vm, const HSQOBJECT &fn, const Receiver &receiver, Args &&... args)
callFunction() that keeps the return value on top for the caller to read.
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.
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 pushReceiver(HSQUIRRELVM vm, const Receiver &receiver)
Push one callFunction() receiver (the root-table marker or a value).
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.
decltype(auto) scriptArgument(const Arg &arg)
Prepare one callFunction() argument, converting C strings to std::string.
void pushValue(HSQUIRRELVM vm, const eve::rx::Value &v)
Definition Rx.cpp:93
SettlementPipeline::Stage fn
Receiver marker for callFunction(): bind the VM's root table as this.
Limits and provenance used by the Squirrel ↔ Value adapter.