2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#ifndef LUAINTERFACE_H
|
|
|
|
#define LUAINTERFACE_H
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include "declarations.h"
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
struct lua_State;
|
|
|
|
typedef int (*LuaCFunction) (lua_State *L);
|
|
|
|
|
|
|
|
/// Class that manages LUA stuff
|
|
|
|
class LuaInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LuaInterface();
|
|
|
|
~LuaInterface();
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void terminate();
|
|
|
|
|
|
|
|
/// Register core script functions
|
|
|
|
void registerFunctions();
|
|
|
|
|
|
|
|
// functions that will register all script stuff in lua global environment
|
2012-06-17 17:21:46 +02:00
|
|
|
void registerSingletonClass(const std::string& className);
|
2011-08-14 04:09:11 +02:00
|
|
|
void registerClass(const std::string& className, const std::string& baseClass = "LuaObject");
|
|
|
|
|
|
|
|
void registerClassStaticFunction(const std::string& className,
|
|
|
|
const std::string& functionName,
|
|
|
|
const LuaCppFunction& function);
|
|
|
|
|
|
|
|
void registerClassMemberFunction(const std::string& className,
|
|
|
|
const std::string& functionName,
|
|
|
|
const LuaCppFunction& function);
|
|
|
|
|
|
|
|
void registerClassMemberField(const std::string& className,
|
|
|
|
const std::string& field,
|
|
|
|
const LuaCppFunction& getFunction,
|
|
|
|
const LuaCppFunction& setFunction);
|
|
|
|
|
|
|
|
void registerGlobalFunction(const std::string& functionName,
|
|
|
|
const LuaCppFunction& function);
|
|
|
|
|
|
|
|
// register shortcuts using templates
|
|
|
|
template<class C, class B = LuaObject>
|
|
|
|
void registerClass() {
|
2013-11-12 19:31:51 +01:00
|
|
|
registerClass(stdext::demangle_class<C>(), stdext::demangle_class<B>());
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
void registerClassStaticFunction(const std::string& functionName, const LuaCppFunction& function) {
|
2013-11-12 19:31:51 +01:00
|
|
|
registerClassStaticFunction(stdext::demangle_class<C>(), functionName, function);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
void registerClassMemberFunction(const std::string& functionName, const LuaCppFunction& function) {
|
2013-11-12 19:31:51 +01:00
|
|
|
registerClassMemberFunction(stdext::demangle_class<C>(), functionName, function);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
void registerClassMemberField(const std::string& field,
|
|
|
|
const LuaCppFunction& getFunction,
|
|
|
|
const LuaCppFunction& setFunction) {
|
2013-11-12 19:31:51 +01:00
|
|
|
registerClassMemberField(stdext::demangle_class<C>(), field, getFunction, setFunction);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// methods for binding functions
|
2012-06-17 17:21:46 +02:00
|
|
|
template<class C, typename F>
|
|
|
|
void bindSingletonFunction(const std::string& functionName, F C::*function, C *instance);
|
|
|
|
template<class C, typename F>
|
|
|
|
void bindSingletonFunction(const std::string& className, const std::string& functionName, F C::*function, C *instance);
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<class C, typename F>
|
|
|
|
void bindClassStaticFunction(const std::string& functionName, const F& function);
|
2011-12-03 22:41:37 +01:00
|
|
|
template<typename F>
|
|
|
|
void bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberFunction(const std::string& functionName, F FC::*function);
|
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function);
|
|
|
|
|
|
|
|
template<class C, typename F1, typename F2, class FC>
|
|
|
|
void bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
|
|
|
|
template<class C, typename F1, typename F2, class FC>
|
|
|
|
void bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction);
|
|
|
|
|
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction);
|
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction);
|
|
|
|
|
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction);
|
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void bindGlobalFunction(const std::string& functionName, const F& function);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Metamethod that will retrieve fields values (that include functions) from the object when using '.' or ':'
|
|
|
|
static int luaObjectGetEvent(LuaInterface* lua);
|
|
|
|
/// Metamethod that is called when setting a field of the object by using the keyword '='
|
|
|
|
static int luaObjectSetEvent(LuaInterface* lua);
|
|
|
|
/// Metamethod that will check equality of objects by using the keyword '=='
|
|
|
|
static int luaObjectEqualEvent(LuaInterface* lua);
|
|
|
|
/// Metamethod that is called every two lua garbage collections
|
|
|
|
/// for any LuaObject that have no references left in lua environment
|
|
|
|
/// anymore, thus this creates the possibility of holding an object
|
|
|
|
/// existence by lua until it got no references left
|
|
|
|
static int luaObjectCollectEvent(LuaInterface* lua);
|
|
|
|
|
|
|
|
public:
|
2012-06-20 02:15:56 +02:00
|
|
|
/// Loads and runs a script, any errors are printed to stdout and returns false
|
|
|
|
bool safeRunScript(const std::string& fileName);
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Loads and runs a script
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
void runScript(const std::string& fileName);
|
|
|
|
|
|
|
|
/// Loads and runs the script from buffer
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
void runBuffer(const std::string& buffer, const std::string& source);
|
|
|
|
|
|
|
|
/// Loads a script file and pushes it's main function onto stack,
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
void loadScript(const std::string& fileName);
|
|
|
|
|
|
|
|
/// Loads a function from buffer and pushes it onto stack,
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
void loadFunction(const std::string& buffer, const std::string& source = "lua function buffer");
|
|
|
|
|
|
|
|
/// Evaluates a lua expression and pushes the result value onto the stack
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
void evaluateExpression(const std::string& expression, const std::string& source = "lua expression");
|
|
|
|
|
|
|
|
/// Generates a traceback message for the current call stack
|
|
|
|
/// @param errorMessage is an additional error message
|
|
|
|
/// @param level is the level of the traceback, 0 means trace from calling function
|
|
|
|
/// @return the generated traceback message
|
|
|
|
std::string traceback(const std::string& errorMessage = "", int level = 0);
|
|
|
|
|
2012-06-06 16:10:35 +02:00
|
|
|
/// Throw a lua error if inside a lua call or generates an C++ stdext::exception
|
|
|
|
/// @param message is the error message wich will be displayed before the error traceback
|
|
|
|
/// @exception stdext::exception is thrown with the error message if the error is not captured by lua
|
|
|
|
void throwError(const std::string& message);
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Searches for the source of the current running function
|
2011-12-30 05:50:19 +01:00
|
|
|
std::string getCurrentSourcePath(int level = 0);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// @brief Calls a function
|
|
|
|
/// The function and arguments must be on top of the stack in order,
|
|
|
|
/// results are pushed onto the stack.
|
|
|
|
/// @exception LuaException is thrown on any lua error
|
|
|
|
/// @return number of results
|
2012-07-19 11:12:17 +02:00
|
|
|
int safeCall(int numArgs = 0, int numRets = -1);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// Same as safeCall but catches exceptions and can also calls a table of functions,
|
|
|
|
/// if any error occurs it will be reported to stdout and returns 0 results
|
|
|
|
/// @param requestedResults is the number of results requested to pushes onto the stack,
|
|
|
|
/// if supplied, the call will always pushes that number of results, even if it fails
|
2012-07-19 11:12:17 +02:00
|
|
|
int signalCall(int numArgs = 0, int numRets = -1);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// @brief Creates a new environment table
|
|
|
|
/// The new environment table is redirected to the global environment (aka _G),
|
|
|
|
/// this allows to access global variables from _G in the new environment and
|
|
|
|
/// prevents new variables in this new environment to be set on the global environment
|
2012-07-24 02:22:38 +02:00
|
|
|
int newSandboxEnv();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
template<typename... T>
|
2013-02-10 05:05:02 +01:00
|
|
|
int luaCallGlobalField(const std::string& global, const std::string& field, const T&... args);
|
|
|
|
|
|
|
|
template<typename... T>
|
|
|
|
void callGlobalField(const std::string& global, const std::string& field, const T&... args);
|
2011-08-29 16:14:21 +02:00
|
|
|
|
|
|
|
template<typename R, typename... T>
|
|
|
|
R callGlobalField(const std::string& global, const std::string& field, const T&... args);
|
|
|
|
|
2012-01-06 20:25:27 +01:00
|
|
|
bool isInCppCallback() { return m_cppCallbackDepth != 0; }
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
private:
|
|
|
|
/// Load scripts requested by lua 'require'
|
|
|
|
static int luaScriptLoader(lua_State* L);
|
2012-02-06 05:39:52 +01:00
|
|
|
/// Run scripts requested by lua 'dofile'
|
2012-07-24 02:22:38 +02:00
|
|
|
static int lua_dofile(lua_State* L);
|
2012-04-26 17:11:03 +02:00
|
|
|
/// Run scripts requested by lua 'dofiles'
|
2012-07-24 02:22:38 +02:00
|
|
|
static int lua_dofiles(lua_State* L);
|
|
|
|
/// Run scripts requested by lua 'dofiles'
|
|
|
|
static int lua_loadfile(lua_State* L);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Handle lua errors from safeCall
|
|
|
|
static int luaErrorHandler(lua_State* L);
|
|
|
|
/// Handle bound cpp functions callbacks
|
|
|
|
static int luaCppFunctionCallback(lua_State* L);
|
|
|
|
/// Collect bound cpp function pointers
|
|
|
|
static int luaCollectCppFunction(lua_State* L);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void createLuaState();
|
|
|
|
void closeLuaState();
|
|
|
|
|
|
|
|
void collectGarbage();
|
|
|
|
|
|
|
|
void loadBuffer(const std::string& buffer, const std::string& source);
|
|
|
|
|
|
|
|
int pcall(int numArgs = 0, int numRets = 0, int errorFuncIndex = 0);
|
|
|
|
void call(int numArgs = 0, int numRets = 0);
|
2012-06-06 16:10:35 +02:00
|
|
|
void error();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
int ref();
|
|
|
|
int weakRef();
|
|
|
|
void unref(int ref);
|
|
|
|
void useValue() { pushValue(); ref(); }
|
|
|
|
|
|
|
|
const char* typeName(int index = -1);
|
|
|
|
std::string functionSourcePath();
|
|
|
|
|
|
|
|
void insert(int index);
|
|
|
|
void remove(int index);
|
|
|
|
bool next(int index = -2);
|
|
|
|
|
2012-06-06 05:44:05 +02:00
|
|
|
void checkStack() { assert(getTop() <= 20); }
|
2011-08-14 04:09:11 +02:00
|
|
|
void getStackFunction(int level = 0);
|
|
|
|
|
|
|
|
void getRef(int ref);
|
|
|
|
void getWeakRef(int weakRef);
|
|
|
|
|
2012-07-24 02:22:38 +02:00
|
|
|
int getGlobalEnvironment() { return m_globalEnv; }
|
|
|
|
void setGlobalEnvironment(int env);
|
|
|
|
void resetGlobalEnvironment() { setGlobalEnvironment(m_globalEnv); }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
void setMetatable(int index = -2);
|
|
|
|
void getMetatable(int index = -1);
|
|
|
|
|
|
|
|
void getField(const char* key, int index = -1);
|
|
|
|
void getField(const std::string& key, int index = -1) { return getField(key.c_str(), index); }
|
|
|
|
void setField(const char* key, int index = -2);
|
|
|
|
void setField(const std::string& key, int index = -2) { return setField(key.c_str(), index); }
|
|
|
|
|
|
|
|
void getTable(int index = -2);
|
|
|
|
void setTable(int index = -3);
|
2012-07-24 02:22:38 +02:00
|
|
|
void clearTable(int index = -1);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
void getEnv(int index = -1);
|
|
|
|
void setEnv(int index = -2);
|
|
|
|
|
|
|
|
void getGlobal(const std::string& key);
|
2011-08-29 16:14:21 +02:00
|
|
|
void getGlobalField(const std::string& globalKey, const std::string& fieldKey);
|
2011-08-14 04:09:11 +02:00
|
|
|
void setGlobal(const std::string& key);
|
|
|
|
|
|
|
|
void rawGet(int index = -1);
|
|
|
|
void rawGeti(int n, int index = -1);
|
|
|
|
void rawSet(int index = -3);
|
|
|
|
void rawSeti(int n, int index = -2);
|
|
|
|
|
|
|
|
void newTable();
|
lua: now we have a function to static allocate an array
lua_newtable() definition:
#define lua_newtable(L) lua_createtable(L, 0, 0)
This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.
This function statically allocates an array, so it's ready to have
X fields into it.
Performance tests:
lua_newtable
1428161
1426992
1413513
lua_createtable
2004544
1974117
1957533
These tests were done on an AMD 8350fx CPU, single thread used.
narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.
This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.
All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
2013-12-21 15:13:24 +01:00
|
|
|
void createTable(int narr, int nrec);
|
2011-08-14 04:09:11 +02:00
|
|
|
void* newUserdata(int size);
|
|
|
|
|
|
|
|
void pop(int n = 1);
|
2012-07-23 06:33:08 +02:00
|
|
|
long popInteger();
|
2011-08-14 04:09:11 +02:00
|
|
|
double popNumber();
|
|
|
|
bool popBoolean();
|
|
|
|
std::string popString();
|
|
|
|
void* popUserdata();
|
|
|
|
void* popUpvalueUserdata();
|
|
|
|
LuaObjectPtr popObject();
|
|
|
|
|
|
|
|
void pushNil();
|
2012-07-23 06:33:08 +02:00
|
|
|
void pushInteger(long v);
|
2011-08-14 04:09:11 +02:00
|
|
|
void pushNumber(double v);
|
|
|
|
void pushBoolean(bool v);
|
|
|
|
void pushCString(const char* v);
|
2012-05-14 23:36:54 +02:00
|
|
|
void pushString(const std::string& v);
|
2011-08-14 04:09:11 +02:00
|
|
|
void pushLightUserdata(void* p);
|
|
|
|
void pushThread();
|
|
|
|
void pushValue(int index = -1);
|
|
|
|
void pushObject(const LuaObjectPtr& obj);
|
|
|
|
void pushCFunction(LuaCFunction func, int n = 0);
|
|
|
|
void pushCppFunction(const LuaCppFunction& func);
|
|
|
|
|
|
|
|
bool isNil(int index = -1);
|
|
|
|
bool isBoolean(int index = -1);
|
|
|
|
bool isNumber(int index = -1);
|
|
|
|
bool isString(int index = -1);
|
|
|
|
bool isTable(int index = -1);
|
|
|
|
bool isFunction(int index = -1);
|
|
|
|
bool isCFunction(int index = -1);
|
|
|
|
bool isLuaFunction(int index = -1) { return (isFunction() && !isCFunction()); }
|
|
|
|
bool isUserdata(int index = -1);
|
|
|
|
|
|
|
|
bool toBoolean(int index = -1);
|
|
|
|
int toInteger(int index = -1);
|
|
|
|
double toNumber(int index = -1);
|
|
|
|
const char* toCString(int index = -1);
|
|
|
|
std::string toString(int index = -1);
|
|
|
|
void* toUserdata(int index = -1);
|
|
|
|
LuaObjectPtr toObject(int index = -1);
|
|
|
|
|
|
|
|
int getTop();
|
|
|
|
int stackSize() { return getTop(); }
|
|
|
|
void clearStack() { pop(stackSize()); }
|
|
|
|
bool hasIndex(int index) { return (stackSize() >= (index < 0 ? -index : index) && index != 0); }
|
|
|
|
|
2013-05-12 11:16:43 +02:00
|
|
|
void loadFiles(std::string directory, bool recursive = false, std::string contains = "");
|
2013-05-12 07:00:52 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Pushes any type onto the stack
|
|
|
|
template<typename T, typename... Args>
|
2012-08-01 09:49:09 +02:00
|
|
|
int polymorphicPush(const T& v, const Args&... args);
|
2012-07-05 14:38:48 +02:00
|
|
|
int polymorphicPush() { return 0; }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
/// Casts a value from stack to any type
|
|
|
|
/// @exception LuaBadValueCastException thrown if the cast fails
|
|
|
|
template<class T>
|
|
|
|
T castValue(int index = -1);
|
|
|
|
|
|
|
|
/// Same as castValue but also pops
|
|
|
|
template<class T>
|
|
|
|
T polymorphicPop() { T v = castValue<T>(); pop(1); return v; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
lua_State* L;
|
|
|
|
int m_weakTableRef;
|
2012-01-06 20:25:27 +01:00
|
|
|
int m_cppCallbackDepth;
|
2012-06-18 10:13:52 +02:00
|
|
|
int m_totalObjRefs;
|
|
|
|
int m_totalFuncRefs;
|
2012-07-24 02:22:38 +02:00
|
|
|
int m_globalEnv;
|
2011-08-14 04:09:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern LuaInterface g_lua;
|
|
|
|
|
|
|
|
// must be included after, because they need LuaInterface fully declared
|
|
|
|
#include "luaexception.h"
|
|
|
|
#include "luabinder.h"
|
|
|
|
#include "luavaluecasts.h"
|
|
|
|
|
2011-12-19 18:26:19 +01:00
|
|
|
template<typename T, typename... Args>
|
2012-08-01 09:49:09 +02:00
|
|
|
int LuaInterface::polymorphicPush(const T& v, const Args&... args) {
|
2012-07-05 14:38:48 +02:00
|
|
|
int r = push_luavalue(v);
|
|
|
|
return r + polymorphicPush(args...);
|
2011-12-19 18:26:19 +01:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// next templates must be defined after above includes
|
2012-06-17 17:21:46 +02:00
|
|
|
|
|
|
|
template<class C, typename F>
|
|
|
|
void LuaInterface::bindSingletonFunction(const std::string& functionName, F C::*function, C *instance) {
|
|
|
|
registerClassStaticFunction<C>(functionName, luabinder::bind_singleton_mem_fun(function, instance));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class C, typename F>
|
|
|
|
void LuaInterface::bindSingletonFunction(const std::string& className, const std::string& functionName, F C::*function, C *instance) {
|
|
|
|
registerClassStaticFunction(className, functionName, luabinder::bind_singleton_mem_fun(function, instance));
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<class C, typename F>
|
|
|
|
void LuaInterface::bindClassStaticFunction(const std::string& functionName, const F& function) {
|
|
|
|
registerClassStaticFunction<C>(functionName, luabinder::bind_fun(function));
|
|
|
|
}
|
2011-12-03 22:41:37 +01:00
|
|
|
template<typename F>
|
|
|
|
void LuaInterface::bindClassStaticFunction(const std::string& className, const std::string& functionName, const F& function) {
|
|
|
|
registerClassStaticFunction(className, functionName, luabinder::bind_fun(function));
|
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberFunction(const std::string& functionName, F FC::*function) {
|
|
|
|
registerClassMemberFunction<C>(functionName, luabinder::bind_mem_fun<C>(function));
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberFunction(const std::string& className, const std::string& functionName, F FC::*function) {
|
|
|
|
registerClassMemberFunction(className, functionName, luabinder::bind_mem_fun<C>(function));
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F1, typename F2, class FC>
|
|
|
|
void LuaInterface::bindClassMemberField(const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
|
|
|
|
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F1, typename F2, class FC>
|
|
|
|
void LuaInterface::bindClassMemberField(const std::string& className, const std::string& fieldName, F1 FC::*getFunction, F2 FC::*setFunction) {
|
|
|
|
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), luabinder::bind_mem_fun<C>(setFunction));
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberGetField(const std::string& fieldName, F FC::*getFunction) {
|
|
|
|
registerClassMemberField<C>(fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberGetField(const std::string& className, const std::string& fieldName, F FC::*getFunction) {
|
|
|
|
registerClassMemberField(className, fieldName, luabinder::bind_mem_fun<C>(getFunction), LuaCppFunction());
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberSetField(const std::string& fieldName, F FC::*setFunction) {
|
|
|
|
registerClassMemberField<C>(fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
2012-01-06 04:29:26 +01:00
|
|
|
template<class C, typename F, class FC>
|
|
|
|
void LuaInterface::bindClassMemberSetField(const std::string& className, const std::string& fieldName, F FC::*setFunction) {
|
|
|
|
registerClassMemberField(className, fieldName, LuaCppFunction(), luabinder::bind_mem_fun<C>(setFunction));
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void LuaInterface::bindGlobalFunction(const std::string& functionName, const F& function) {
|
|
|
|
registerGlobalFunction(functionName, luabinder::bind_fun(function));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T LuaInterface::castValue(int index) {
|
|
|
|
T o;
|
|
|
|
if(!luavalue_cast(index, o))
|
2012-05-28 15:06:26 +02:00
|
|
|
throw LuaBadValueCastException(typeName(index), stdext::demangle_type<T>());
|
2011-08-14 04:09:11 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
template<typename... T>
|
2013-02-10 05:05:02 +01:00
|
|
|
int LuaInterface::luaCallGlobalField(const std::string& global, const std::string& field, const T&... args) {
|
2011-08-29 16:14:21 +02:00
|
|
|
g_lua.getGlobalField(global, field);
|
|
|
|
if(!g_lua.isNil()) {
|
2012-07-05 14:38:48 +02:00
|
|
|
int numArgs = g_lua.polymorphicPush(args...);
|
|
|
|
return g_lua.signalCall(numArgs);
|
2011-08-29 16:14:21 +02:00
|
|
|
} else
|
|
|
|
g_lua.pop(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-10 05:05:02 +01:00
|
|
|
template<typename... T>
|
|
|
|
void LuaInterface::callGlobalField(const std::string& global, const std::string& field, const T&... args) {
|
|
|
|
int rets = luaCallGlobalField(global, field, args...);
|
|
|
|
if(rets > 0)
|
|
|
|
pop(rets);
|
|
|
|
}
|
|
|
|
|
2011-08-29 16:14:21 +02:00
|
|
|
template<typename R, typename... T>
|
|
|
|
R LuaInterface::callGlobalField(const std::string& global, const std::string& field, const T&... args) {
|
|
|
|
R result;
|
2013-02-10 05:05:02 +01:00
|
|
|
int rets = luaCallGlobalField(global, field, args...);
|
2011-08-29 16:14:21 +02:00
|
|
|
if(rets > 0) {
|
|
|
|
assert(rets == 1);
|
|
|
|
result = g_lua.polymorphicPop<R>();
|
|
|
|
} else
|
|
|
|
result = R();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#endif
|