载入中...
搜索中...
未找到
SquirrelOwnership.cpp
浏览该文件的文档.
1
12
13#include <squirrel.h>
14#include <simplesquirrel/simplesquirrel.hpp>
15
16#include <exception>
17#include <string>
18
19namespace eve::script::detail {
20
23 "owned Squirrel instance requires a VM and non-null object", {}, {}, "squirrel.ownership");
24}
25
26Result<ssq::Object> makeOwnedSquirrelInstanceRaw(HSQUIRRELVM vm, void* object, SquirrelReleaseHook releaseHook,
27 OwnedInstanceDestroy destroy, std::size_t typeHash) {
28 const SQInteger top = sq_gettop(vm);
29 bool transferred = false;
30 try {
31 const HSQOBJECT& classObject = ssq::detail::getClassObj(vm, typeHash);
32 sq_pushobject(vm, classObject);
33 if (SQ_FAILED(sq_createinstance(vm, -1)))
34 throw ssq::RuntimeException("failed to create owned Squirrel instance");
35 sq_remove(vm, -2);
36 if (SQ_FAILED(sq_setinstanceup(vm, -1, static_cast<SQUserPointer>(object))))
37 throw ssq::RuntimeException("failed to attach owned Squirrel instance");
38 sq_settypetag(vm, -1, reinterpret_cast<SQUserPointer>(typeHash));
39 sq_setreleasehook(vm, -1, releaseHook);
40 // From here the live instance owns the object: its release hook is
41 // installed, so a later failure must not destroy it here.
42 transferred = true;
43
44 ssq::Object result(vm);
45 if (SQ_FAILED(sq_getstackobj(vm, -1, &result.getRaw())))
46 throw ssq::RuntimeException("failed to root owned Squirrel instance");
47 sq_addref(vm, &result.getRaw());
48 sq_settop(vm, top);
49 return Result<ssq::Object>::success(std::move(result));
50 } catch (const std::exception& error) {
51 sq_settop(vm, top);
52 if (!transferred) {
53 destroy(object);
55 DiagnosticCode::Failed, std::string("owned Squirrel instance creation failed: ") + error.what(), {}, {},
56 "squirrel.ownership"));
57 }
59 DiagnosticCode::Failed, std::string("owned Squirrel instance rooting failed: ") + error.what(), {}, {},
60 "squirrel.ownership"));
61 } catch (...) {
62 sq_settop(vm, top);
63 if (!transferred) destroy(object);
65 DiagnosticCode::Failed, "owned Squirrel instance creation failed", {}, {}, "squirrel.ownership"));
66 }
67}
68
69
70} // namespace eve::script::detail
SQInteger top
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
void(* destroy)(void *)
std::string error
Definition Package.cpp:60
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
void(*)(void *) noexcept OwnedInstanceDestroy
Destroys one owned C++ wrapper without knowing its type.
Result< ssq::Object > makeOwnedSquirrelInstanceRaw(HSQUIRRELVM vm, void *object, SquirrelReleaseHook releaseHook, OwnedInstanceDestroy destroy, std::size_t typeHash)
Non-template core of makeOwnedSquirrelInstance.
Diagnostic ownedInstanceArgumentDiagnostic()
Diagnostic for a null VM or null object, built once instead of per T.