31 template <>
struct Return<uint8_t> {
static inline int Set(HSQUIRRELVM vm, uint8_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
32 template <>
struct Return<uint16_t> {
static inline int Set(HSQUIRRELVM vm, uint16_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
33 template <>
struct Return<uint32_t> {
static inline int Set(HSQUIRRELVM vm, uint32_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
34 template <>
struct Return<int8_t> {
static inline int Set(HSQUIRRELVM vm, int8_t res) { sq_pushinteger(vm, res);
return 1; } };
35 template <>
struct Return<int16_t> {
static inline int Set(HSQUIRRELVM vm, int16_t res) { sq_pushinteger(vm, res);
return 1; } };
36 template <>
struct Return<int32_t> {
static inline int Set(HSQUIRRELVM vm, int32_t res) { sq_pushinteger(vm, res);
return 1; } };
37 template <>
struct Return<int64_t> {
static inline int Set(HSQUIRRELVM vm, int64_t res) { sq_pushinteger(vm, res);
return 1; } };
38 template <>
struct Return<
TileIndex> {
static inline int Set(HSQUIRRELVM vm,
TileIndex res) { sq_pushinteger(vm, (int32_t)res.base());
return 1; } };
39 template <>
struct Return<bool> {
static inline int Set(HSQUIRRELVM vm,
bool res) { sq_pushbool (vm, res);
return 1; } };
40 template <>
struct Return<char *> { };
41 template <>
struct Return<const char *> { };
42 template <>
struct Return<void *> {
static inline int Set(HSQUIRRELVM vm,
void *res) { sq_pushuserpointer(vm, res);
return 1; } };
43 template <>
struct Return<HSQOBJECT> {
static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res);
return 1; } };
45 template <
typename T>
requires std::is_enum_v<T>
struct Return<T> {
46 static inline int Set(HSQUIRRELVM vm, T res)
53 template <ConvertibleThroughBase T>
struct Return<T> {
54 static inline int Set(HSQUIRRELVM vm, T res)
56 sq_pushinteger(vm, res.base());
61 template <>
struct Return<std::optional<std::string>> {
62 static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res)
64 if (res.has_value()) {
65 sq_pushstring(vm, res.value(), -1);
77 template <
typename T>
struct Param;
79 template <>
struct Param<uint8_t> {
static inline uint8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
80 template <>
struct Param<uint16_t> {
static inline uint16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
81 template <>
struct Param<uint32_t> {
static inline uint32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
82 template <>
struct Param<int8_t> {
static inline int8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
83 template <>
struct Param<int16_t> {
static inline int16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
84 template <>
struct Param<int32_t> {
static inline int32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
85 template <>
struct Param<int64_t> {
static inline int64_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
86 template <>
struct Param<
TileIndex> {
static inline TileIndex Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return TileIndex((uint32_t)(int32_t)tmp); } };
87 template <>
struct Param<bool> {
static inline bool Get(HSQUIRRELVM vm,
int index) { SQBool tmp; sq_getbool (vm, index, &tmp);
return tmp != 0; } };
88 template <>
struct Param<const char *> { };
89 template <>
struct Param<void *> {
static inline void *Get(HSQUIRRELVM vm,
int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp);
return tmp; } };
91 template <
typename T>
requires std::is_enum_v<T>
struct Param<T> {
92 static inline T Get(HSQUIRRELVM vm,
int index)
95 sq_getinteger(vm, index, &tmp);
96 return static_cast<T
>(tmp);
100 template <ConvertibleThroughBase T>
struct Param<T> {
101 static inline T Get(HSQUIRRELVM vm,
int index)
104 sq_getinteger(vm, index, &tmp);
105 return T{
static_cast<T::BaseType
>(tmp)};
109 template <>
struct Param<const std::string &> {
110 static inline const std::string Get(HSQUIRRELVM vm,
int index)
113 sq_tostring(vm, index);
116 sq_getstring(vm, -1, &tmp);
123 template <
typename Titem>
125 static inline Array<Titem> Get(HSQUIRRELVM vm,
int index)
128 if (sq_getsize(vm, index) > UINT16_MAX)
throw sq_throwerror(vm,
"an array used as parameter to a function is too large");
131 sq_getstackobj(vm, index, &obj);
132 sq_pushobject(vm, obj);
137 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
157 template <
typename Tretval,
typename... Targs>
159 static int SQCall(
void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm)
161 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
165 template <
size_t... i>
166 static int SQCall(
void *, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
168 if constexpr (std::is_void_v<Tretval>) {
174 Tretval ret = (*func)(
185 template <
class Tcls,
typename Tretval,
typename... Targs>
187 static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
189 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
192 static Tcls *SQConstruct(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
194 return SQConstruct(instance, func, vm, std::index_sequence_for<Targs...>{});
198 template <
size_t... i>
199 static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
201 if constexpr (std::is_void_v<Tretval>) {
207 Tretval ret = (instance->*func)(
214 template <
size_t... i>
215 static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
217 Tcls *inst =
new Tcls(
231 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
235 int nparam = sq_gettop(vm);
236 SQUserPointer ptr =
nullptr;
237 SQUserPointer real_instance =
nullptr;
244 sq_pushroottable(vm);
245 const char *className = GetClassName<Tcls, Ttype>();
246 sq_pushstring(vm, className, -1);
248 sq_pushobject(vm, instance);
249 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
253 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
255 sq_getuserdata(vm, nparam, &ptr,
nullptr);
256 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
263 }
catch (SQInteger &e) {
273 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
277 int nparam = sq_gettop(vm);
278 SQUserPointer ptr =
nullptr;
279 SQUserPointer real_instance =
nullptr;
286 sq_pushroottable(vm);
287 const char *className = GetClassName<Tcls, Ttype>();
288 sq_pushstring(vm, className, -1);
290 sq_pushobject(vm, instance);
291 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
295 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
297 sq_getuserdata(vm, nparam, &ptr,
nullptr);
298 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
303 return (SQInteger)(((Tcls *)real_instance)->*(*(Tmethod *)ptr))(vm);
311 template <
typename Tcls,
typename Tmethod>
315 int nparam = sq_gettop(vm);
316 SQUserPointer ptr =
nullptr;
319 sq_getuserdata(vm, nparam, &ptr,
nullptr);
324 }
catch (SQInteger &e) {
335 template <
typename Tcls,
typename Tmethod>
339 int nparam = sq_gettop(vm);
340 SQUserPointer ptr =
nullptr;
343 sq_getuserdata(vm, nparam, &ptr,
nullptr);
348 return (SQInteger)(*(*(Tmethod *)ptr))(vm);
355 template <
typename Tcls>
359 if (p !=
nullptr) ((Tcls *)p)->Release();
368 template <
typename Tcls,
typename Tmethod,
int Tnparam>
374 sq_setinstanceup(vm, -Tnparam, instance);
375 sq_setreleasehook(vm, -Tnparam, DefSQDestructorCallback<Tcls>);
378 }
catch (SQInteger &e) {
387 template <
typename Tcls>
392 int nparam = sq_gettop(vm);
395 Tcls *instance =
new Tcls(vm);
396 sq_setinstanceup(vm, -nparam, instance);
397 sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
400 }
catch (SQInteger &e) {