2015-11-01 09:06:54 +01:00
|
|
|
//
|
|
|
|
// sqratFunction: Squirrel Function Wrapper
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copyright (c) 2009 Brandon Jones
|
|
|
|
// Copyirght 2011 Li-Cheng (Andy) Tai
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would be
|
|
|
|
// appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
// misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if !defined(_SCRAT_SQFUNC_H_)
|
|
|
|
#define _SCRAT_SQFUNC_H_
|
|
|
|
|
2016-02-23 16:48:30 +01:00
|
|
|
#ifdef SQMOD_PLUGIN_API
|
2016-02-27 10:57:10 +01:00
|
|
|
#include <SqAPI.h>
|
2016-02-23 16:48:30 +01:00
|
|
|
#else
|
|
|
|
#include <squirrel.h>
|
|
|
|
#endif // SQMOD_PLUGIN_API
|
|
|
|
|
2015-11-01 09:06:54 +01:00
|
|
|
#include "sqratObject.h"
|
|
|
|
|
|
|
|
namespace Sqrat {
|
|
|
|
|
2018-07-31 16:41:46 +02:00
|
|
|
// Represents a function in Squirrel
|
2018-08-02 17:33:52 +02:00
|
|
|
struct Function {
|
2015-11-01 09:06:54 +01:00
|
|
|
|
|
|
|
friend class TableBase;
|
|
|
|
friend class Table;
|
|
|
|
friend class ArrayBase;
|
|
|
|
friend struct Var<Function>;
|
|
|
|
|
2018-08-02 17:33:52 +02:00
|
|
|
HSQOBJECT mEnv, mObj;
|
2015-11-01 09:06:54 +01:00
|
|
|
|
2018-07-31 16:41:46 +02:00
|
|
|
// Default constructor (null)
|
2018-10-23 20:29:28 +02:00
|
|
|
Function() {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_resetobject(&mEnv);
|
|
|
|
sq_resetobject(&mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Copy constructor
|
2018-10-23 20:29:28 +02:00
|
|
|
Function(const Function& sf) : mEnv(sf.mEnv), mObj(sf.mObj) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_addref(DefaultVM::Get_(), &mEnv);
|
|
|
|
sq_addref(DefaultVM::Get_(), &mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Move constructor
|
2018-10-23 20:29:28 +02:00
|
|
|
Function(Function&& sf) : mEnv(sf.mEnv), mObj(sf.mObj) {
|
2016-06-12 12:43:29 +02:00
|
|
|
sq_resetobject(&sf.GetEnv());
|
|
|
|
sq_resetobject(&sf.GetFunc());
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Constructs a Function from a slot in an Object
|
2018-08-02 17:33:52 +02:00
|
|
|
Function(const Object& e, const SQChar* slot) : mEnv(e.GetObject()) {
|
|
|
|
sq_addref(DefaultVM::Get_(), &mEnv);
|
2015-11-01 09:06:54 +01:00
|
|
|
Object so = e.GetSlot(slot);
|
2018-08-02 17:33:52 +02:00
|
|
|
mObj = so.GetObject();
|
|
|
|
sq_addref(DefaultVM::Get_(), &mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
|
|
|
SQObjectType value_type = so.GetType();
|
2018-07-31 16:41:46 +02:00
|
|
|
if (value_type != OT_CLOSURE && value_type != OT_NATIVECLOSURE) {
|
2015-11-01 09:06:54 +01:00
|
|
|
// Note that classes can also be considered functions in Squirrel
|
2018-07-31 16:41:46 +02:00
|
|
|
SQTHROW(DefaultVM::Get_(), _SC("function not found in slot"));
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Constructs a Function from a value off the stack at the specified index
|
|
|
|
// Assumes the Function environment is at index 1.
|
|
|
|
Function(HSQUIRRELVM vm, SQInteger idx) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_getstackobj(vm, 1, &mEnv);
|
|
|
|
sq_getstackobj(vm, idx, &mObj);
|
|
|
|
sq_addref(vm, &mEnv);
|
|
|
|
sq_addref(vm, &mObj);
|
2018-07-31 16:41:46 +02:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
|
|
|
SQObjectType value_type = sq_gettype(vm, idx);
|
|
|
|
if (value_type != OT_CLOSURE && value_type != OT_NATIVECLOSURE) {
|
|
|
|
SQTHROW(vm, FormatTypeError(vm, idx, _SC("closure")));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// Constructs a Function from two Squirrel objects (one is the environment object and the other is the function object)
|
2018-10-23 20:29:28 +02:00
|
|
|
Function(HSQUIRRELVM vm, HSQOBJECT e, HSQOBJECT o) : mEnv(e), mObj(o) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_addref(vm, &mEnv);
|
|
|
|
sq_addref(vm, &mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Destructor
|
2015-11-01 09:06:54 +01:00
|
|
|
~Function() {
|
|
|
|
Release();
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Copy Assignment operator
|
2018-10-23 20:29:28 +02:00
|
|
|
Function& operator=(const Function& sf) {
|
2015-11-01 09:06:54 +01:00
|
|
|
Release();
|
2018-08-02 17:33:52 +02:00
|
|
|
mEnv = sf.mEnv;
|
|
|
|
mObj = sf.mObj;
|
2018-07-31 16:41:46 +02:00
|
|
|
if (!sf.IsNull()) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_addref(DefaultVM::Get_(), &mEnv);
|
|
|
|
sq_addref(DefaultVM::Get_(), &mObj);
|
2018-07-31 16:41:46 +02:00
|
|
|
}
|
2015-11-01 09:06:54 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Move Assignment operator
|
2018-10-23 20:29:28 +02:00
|
|
|
Function& operator=(Function&& sf) {
|
2016-06-12 12:43:29 +02:00
|
|
|
Release();
|
2018-08-02 17:33:52 +02:00
|
|
|
mEnv = sf.mEnv;
|
|
|
|
mObj = sf.mObj;
|
2016-06-12 12:43:29 +02:00
|
|
|
sq_resetobject(&sf.GetEnv());
|
|
|
|
sq_resetobject(&sf.GetFunc());
|
|
|
|
return *this;
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Checks whether the Function is null
|
2018-10-23 20:29:28 +02:00
|
|
|
bool IsNull() const {
|
2018-08-02 17:33:52 +02:00
|
|
|
return sq_isnull(mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Gets the Squirrel environment object for this Function (copy)
|
2018-10-23 20:29:28 +02:00
|
|
|
HSQOBJECT GetEnv() const {
|
2018-08-02 17:33:52 +02:00
|
|
|
return mEnv;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Gets the Squirrel environment object for this Function (reference)
|
2018-10-23 20:29:28 +02:00
|
|
|
HSQOBJECT& GetEnv() {
|
2018-08-02 17:33:52 +02:00
|
|
|
return mEnv;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Gets the Squirrel function object for this Function (copy)
|
2018-10-23 20:29:28 +02:00
|
|
|
HSQOBJECT GetFunc() const {
|
2018-08-02 17:33:52 +02:00
|
|
|
return mObj;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Gets the Squirrel function object for this Function (reference)
|
2018-10-23 20:29:28 +02:00
|
|
|
HSQOBJECT& GetFunc() {
|
2018-08-02 17:33:52 +02:00
|
|
|
return mObj;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Gets the Squirrel VM for this Function
|
2018-10-23 20:29:28 +02:00
|
|
|
HSQUIRRELVM GetVM() const {
|
2018-07-31 16:41:46 +02:00
|
|
|
return DefaultVM::Get_();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Sets the Function to null (removing its references to underlying Squirrel objects)
|
2018-10-23 20:29:28 +02:00
|
|
|
void Release() {
|
2015-11-01 09:06:54 +01:00
|
|
|
if(!IsNull()) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_release(DefaultVM::Get_(), &mEnv);
|
|
|
|
sq_release(DefaultVM::Get_(), &mObj);
|
|
|
|
sq_resetobject(&mEnv);
|
|
|
|
sq_resetobject(&mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Sets the Function to null (removing its references to underlying Squirrel objects)
|
|
|
|
// This doesn't call release if the reference count is 1.
|
|
|
|
// Workaround for some weird squirrel behavior that generates an assert in debug mode.
|
2018-10-23 20:29:28 +02:00
|
|
|
void ReleaseGently() {
|
2015-11-01 09:06:54 +01:00
|
|
|
if(!IsNull()) {
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_release(DefaultVM::Get_(), &mEnv);
|
|
|
|
if (sq_getrefcount(DefaultVM::Get_(), &mObj) > 1) {
|
|
|
|
sq_release(DefaultVM::Get_(), &mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_resetobject(&mEnv);
|
|
|
|
sq_resetobject(&mObj);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Runs the Function and returns its value as a SharedPtr
|
|
|
|
template<class R, class... Args> SharedPtr<R> Evaluate(Args &&... args) {
|
2018-07-29 13:36:52 +02:00
|
|
|
static constexpr unsigned ARGC = sizeof...(Args) + 1; // + environment
|
2018-07-31 16:41:46 +02:00
|
|
|
HSQUIRRELVM vm = DefaultVM::Get_();
|
2015-11-01 09:06:54 +01:00
|
|
|
SQInteger top = sq_gettop(vm);
|
2018-07-31 16:41:46 +02:00
|
|
|
// Push the environment followed by the function
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_pushobject(vm, mObj);
|
|
|
|
sq_pushobject(vm, mEnv);
|
2018-07-31 16:41:46 +02:00
|
|
|
// Validate the funtion parameter count
|
2015-11-01 09:06:54 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-10-25 19:08:31 +02:00
|
|
|
SQInteger nparams;
|
|
|
|
SQInteger nfreevars;
|
2018-07-29 13:36:52 +02:00
|
|
|
if (SQ_SUCCEEDED(sq_getclosureinfo(vm, -2, &nparams, &nfreevars)) && (nparams != ARGC)) {
|
2015-11-01 09:06:54 +01:00
|
|
|
sq_pop(vm, 2);
|
|
|
|
SQTHROW(vm, _SC("wrong number of parameters"));
|
|
|
|
return SharedPtr<R>();
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-31 16:41:46 +02:00
|
|
|
// Push the arguments
|
|
|
|
PushVars(vm, std::forward<Args>(args)...);
|
2015-11-01 09:06:54 +01:00
|
|
|
|
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-07-29 13:36:52 +02:00
|
|
|
SQRESULT result = sq_call(vm, ARGC, true, ErrorHandling::IsEnabled());
|
2015-11-01 09:06:54 +01:00
|
|
|
|
|
|
|
//handle an error: pop the stack and throw the exception
|
|
|
|
if (SQ_FAILED(result)) {
|
|
|
|
sq_settop(vm, top);
|
|
|
|
SQTHROW(vm, LastErrorString(vm));
|
|
|
|
return SharedPtr<R>();
|
|
|
|
}
|
|
|
|
#else
|
2018-07-29 13:36:52 +02:00
|
|
|
sq_call(vm, ARGC, true, ErrorHandling::IsEnabled());
|
2015-11-01 09:06:54 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SharedPtr<R> ret = Var<SharedPtr<R> >(vm, -1).value;
|
|
|
|
sq_settop(vm, top);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Runs the Function
|
|
|
|
template< class... Args >
|
2018-07-29 13:36:52 +02:00
|
|
|
void Execute(Args &&... args) {
|
|
|
|
static constexpr unsigned ARGC = sizeof...(Args) + 1; // + environment
|
2018-07-31 16:41:46 +02:00
|
|
|
HSQUIRRELVM vm = DefaultVM::Get_();
|
2015-11-01 09:06:54 +01:00
|
|
|
SQInteger top = sq_gettop(vm);
|
2018-07-31 16:41:46 +02:00
|
|
|
// Push the environment followed by the function
|
2018-08-02 17:33:52 +02:00
|
|
|
sq_pushobject(vm, mObj);
|
|
|
|
sq_pushobject(vm, mEnv);
|
2018-07-31 16:41:46 +02:00
|
|
|
// Validate the funtion parameter count
|
2015-11-01 09:06:54 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-10-25 19:08:31 +02:00
|
|
|
SQInteger nparams;
|
|
|
|
SQInteger nfreevars;
|
2018-07-29 13:36:52 +02:00
|
|
|
if (SQ_SUCCEEDED(sq_getclosureinfo(vm, -2, &nparams, &nfreevars)) && (nparams != ARGC)) {
|
2015-11-01 09:06:54 +01:00
|
|
|
sq_pop(vm, 2);
|
|
|
|
SQTHROW(vm, _SC("wrong number of parameters"));
|
2018-07-29 13:36:52 +02:00
|
|
|
return;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-31 16:41:46 +02:00
|
|
|
PushVars(vm, std::forward<Args>(args)...);
|
2015-11-01 09:06:54 +01:00
|
|
|
|
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-07-29 13:36:52 +02:00
|
|
|
SQRESULT result = sq_call(vm, ARGC, false, ErrorHandling::IsEnabled());
|
2015-11-01 09:06:54 +01:00
|
|
|
sq_settop(vm, top);
|
|
|
|
|
2018-07-29 13:36:52 +02:00
|
|
|
//handle an error: throw the exception
|
2015-11-01 09:06:54 +01:00
|
|
|
if (SQ_FAILED(result)) {
|
|
|
|
SQTHROW(vm, LastErrorString(vm));
|
2018-07-29 13:36:52 +02:00
|
|
|
return;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
#else
|
2018-07-29 13:36:52 +02:00
|
|
|
sq_call(vm, ARGC, false, ErrorHandling::IsEnabled());
|
2015-11-01 09:06:54 +01:00
|
|
|
sq_settop(vm, top);
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Runs the Function
|
|
|
|
template< class... Args > void operator() (Args &&... args) {
|
|
|
|
Execute(std::forward<Args>(args)...);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-31 16:41:46 +02:00
|
|
|
// Used to get and push Function instances to and from the stack as references (functions are always references in Squirrel)
|
|
|
|
template<> struct Var<Function> {
|
|
|
|
/// The actual value of get operations
|
|
|
|
Function value;
|
|
|
|
// Attempts to get the value off the stack at idx as a Function
|
|
|
|
// Assumes the Function environment is at index 1.
|
|
|
|
Var(HSQUIRRELVM vm, SQInteger idx) : value(vm, idx) {
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
2018-07-31 16:41:46 +02:00
|
|
|
// Called by Sqrat::PushVar to put a Function on the stack
|
2018-10-23 20:29:28 +02:00
|
|
|
static void push(HSQUIRRELVM vm, const Function& value) {
|
2015-11-01 09:06:54 +01:00
|
|
|
sq_pushobject(vm, value.GetFunc());
|
|
|
|
}
|
|
|
|
};
|
2018-07-31 16:41:46 +02:00
|
|
|
// Used to get and push Function instances to and from the stack as references (functions are always references in Squirrel)
|
|
|
|
template<> struct Var<Function&> : public Var<Function> {
|
|
|
|
Var(HSQUIRRELVM vm, SQInteger idx) : Var<Function>(vm, idx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Used to get and push Function instances to and from the stack as references (functions are always references in Squirrel)
|
|
|
|
template<> struct Var<const Function&> : public Var<Function> {
|
|
|
|
Var(HSQUIRRELVM vm, SQInteger idx) : Var<Function>(vm, idx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2015-11-01 09:06:54 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|