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<HSQOBJECT> {
static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res);
return 1; } };
44 template <
typename T>
requires std::is_enum_v<T>
struct Return<T> {
45 static inline int Set(HSQUIRRELVM vm, T res)
52 template <ConvertibleThroughBase T>
struct Return<T> {
53 static inline int Set(HSQUIRRELVM vm, T res)
55 sq_pushinteger(vm, res.base());
60 template <>
struct Return<std::optional<std::string>> {
61 static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res)
63 if (res.has_value()) {
64 sq_pushstring(vm, res.value());
76 template <
typename T>
struct Param;
78 template <>
struct Param<uint8_t> {
static inline uint8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
79 template <>
struct Param<uint16_t> {
static inline uint16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
80 template <>
struct Param<uint32_t> {
static inline uint32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
81 template <>
struct Param<int8_t> {
static inline int8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
82 template <>
struct Param<int16_t> {
static inline int16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
83 template <>
struct Param<int32_t> {
static inline int32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
84 template <>
struct Param<int64_t> {
static inline int64_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
85 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); } };
86 template <>
struct Param<bool> {
static inline bool Get(HSQUIRRELVM vm,
int index) { SQBool tmp; sq_getbool (vm, index, &tmp);
return tmp != 0; } };
87 template <>
struct Param<const char *> { };
89 template <
typename T>
requires std::is_enum_v<T>
struct Param<T> {
90 static inline T Get(HSQUIRRELVM vm,
int index)
93 sq_getinteger(vm, index, &tmp);
94 return static_cast<T
>(tmp);
98 template <ConvertibleThroughBase T>
struct Param<T> {
99 static inline T Get(HSQUIRRELVM vm,
int index)
102 sq_getinteger(vm, index, &tmp);
103 return T{
static_cast<T::BaseType
>(tmp)};
107 template <>
struct Param<const std::string &> {
108 static inline const std::string Get(HSQUIRRELVM vm,
int index)
111 sq_tostring(vm, index);
113 std::string_view view;
114 sq_getstring(vm, -1, view);
121 template <
typename Titem>
123 static inline Array<Titem> Get(HSQUIRRELVM vm,
int index)
126 if (sq_getsize(vm, index) > UINT16_MAX)
throw sq_throwerror(vm,
"an array used as parameter to a function is too large");
129 sq_getstackobj(vm, index, &obj);
130 sq_pushobject(vm, obj);
135 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
155 template <
typename Tretval,
typename... Targs>
157 static int SQCall(
void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm)
159 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
163 template <
size_t... i>
164 static int SQCall(
void *, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
166 if constexpr (std::is_void_v<Tretval>) {
172 Tretval ret = (*func)(
183 template <
class Tcls,
typename Tretval,
typename... Targs>
185 static int SQCall(Tcls *instance,
auto func, HSQUIRRELVM vm)
187 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
190 static Tcls *SQConstruct(HSQUIRRELVM vm)
192 return SQConstruct(vm, std::index_sequence_for<Targs...>{});
196 template <
size_t... i>
197 static int SQCall(Tcls *instance,
auto func, [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
199 if constexpr (std::is_void_v<Tretval>) {
205 Tretval ret = (instance->*func)(
212 template <
size_t... i>
213 static Tcls *SQConstruct([[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
215 Tcls *inst =
new Tcls(
226 template <
class Tcls,
typename Tretval,
typename... Targs>
227 struct HelperT<Tretval(Tcls:: *)(Targs...) const> :
HelperT<Tretval(Tcls:: *)(Targs...)> {};
235 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
239 int nparam = sq_gettop(vm);
240 SQUserPointer ptr =
nullptr;
241 SQUserPointer real_instance =
nullptr;
248 sq_pushroottable(vm);
249 PushClassName<Tcls, Ttype>(vm);
251 sq_pushobject(vm, instance);
252 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
256 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
258 sq_getuserdata(vm, nparam, &ptr,
nullptr);
259 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
265 auto cls_instance =
static_cast<Tcls *
>(real_instance);
266 auto method = *
static_cast<Tmethod *
>(ptr);
268 }
catch (SQInteger &e) {
278 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
282 int nparam = sq_gettop(vm);
283 SQUserPointer ptr =
nullptr;
284 SQUserPointer real_instance =
nullptr;
291 sq_pushroottable(vm);
292 PushClassName<Tcls, Ttype>(vm);
294 sq_pushobject(vm, instance);
295 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
299 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
301 sq_getuserdata(vm, nparam, &ptr,
nullptr);
302 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
308 auto cls_instance =
static_cast<Tcls *
>(real_instance);
309 auto method = *
static_cast<Tmethod *
>(ptr);
310 return static_cast<SQInteger
>((cls_instance->*method)(vm));
311 }
catch (SQInteger &e) {
321 template <
typename Tcls,
typename Tmethod>
325 int nparam = sq_gettop(vm);
326 SQUserPointer ptr =
nullptr;
329 sq_getuserdata(vm, nparam, &ptr,
nullptr);
333 auto cls_instance =
static_cast<Tcls *
>(
nullptr);
334 auto method = *
static_cast<Tmethod *
>(ptr);
336 }
catch (SQInteger &e) {
347 template <
typename Tcls,
typename Tmethod>
351 int nparam = sq_gettop(vm);
352 SQUserPointer ptr =
nullptr;
355 sq_getuserdata(vm, nparam, &ptr,
nullptr);
361 auto method = *
static_cast<Tmethod *
>(ptr);
362 return static_cast<SQInteger
>((*method)(vm));
363 }
catch (SQInteger &e) {
372 template <
typename Tcls>
376 if (p !=
nullptr) ((Tcls *)p)->Release();
385 template <
typename Tcls,
typename Tmethod>
390 int nparam = sq_gettop(vm);
394 sq_setinstanceup(vm, -nparam, instance);
395 sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
398 }
catch (SQInteger &e) {
407 template <
typename Tcls>
412 int nparam = sq_gettop(vm);
415 Tcls *instance =
new Tcls(vm);
416 sq_setinstanceup(vm, -nparam, instance);
417 sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
420 }
catch (SQInteger &e) {