|
|
|
@ -26,6 +26,7 @@
|
|
|
|
|
#include <script/luascript.h>
|
|
|
|
|
#include <script/luafunctions.h>
|
|
|
|
|
#include <core/resources.h>
|
|
|
|
|
#include <lua.hpp>
|
|
|
|
|
|
|
|
|
|
LuaScript g_lua;
|
|
|
|
|
|
|
|
|
@ -82,6 +83,7 @@ bool LuaScript::loadBuffer(const std::string& text, const std::string& what)
|
|
|
|
|
|
|
|
|
|
// check if is loaded as a function
|
|
|
|
|
if(lua_isfunction(L, -1) == 0) {
|
|
|
|
|
pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -95,20 +97,20 @@ bool LuaScript::loadBuffer(const std::string& text, const std::string& what)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaScript::loadBufferAsFunction(const std::string& text, const std::string& what)
|
|
|
|
|
bool LuaScript::loadBufferAsFunction(const std::string& text, const std::string& what)
|
|
|
|
|
{
|
|
|
|
|
int ret = luaL_loadbuffer(L, text.c_str(), text.length(), what.c_str());
|
|
|
|
|
if(ret != 0){
|
|
|
|
|
reportError(popString());
|
|
|
|
|
return LUA_REFNIL;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if is loaded as a function
|
|
|
|
|
if(lua_isfunction(L, -1) == 0) {
|
|
|
|
|
return LUA_REFNIL;
|
|
|
|
|
if(!isFunction()) {
|
|
|
|
|
pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return popFunction();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::reportError(const std::string& errorDesc, const char *funcName)
|
|
|
|
@ -117,7 +119,7 @@ void LuaScript::reportError(const std::string& errorDesc, const char *funcName)
|
|
|
|
|
ss << "LUA Script ERROR: ";
|
|
|
|
|
if(funcName)
|
|
|
|
|
ss << " in " << funcName << "(): ";
|
|
|
|
|
ss << errorDesc << std::endl;
|
|
|
|
|
ss << errorDesc;
|
|
|
|
|
logError(ss.str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -144,19 +146,156 @@ void LuaScript::insert(int index)
|
|
|
|
|
lua_insert(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaScript::ref()
|
|
|
|
|
void LuaScript::remove(int index)
|
|
|
|
|
{
|
|
|
|
|
return luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
lua_remove(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::unref(int ref)
|
|
|
|
|
void LuaScript::releaseRef(int ref)
|
|
|
|
|
{
|
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, ref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::getRef(int ref)
|
|
|
|
|
void LuaScript::newTable()
|
|
|
|
|
{
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setTable(int index)
|
|
|
|
|
{
|
|
|
|
|
lua_settable(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *LuaScript::newUserdata(int size)
|
|
|
|
|
{
|
|
|
|
|
return lua_newuserdata(L, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::newMetatable(const std::string& name)
|
|
|
|
|
{
|
|
|
|
|
luaL_newmetatable(L, name.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setMetatable(const std::string& name, int index)
|
|
|
|
|
{
|
|
|
|
|
luaL_getmetatable(L, name.c_str());
|
|
|
|
|
if(isNil())
|
|
|
|
|
reportError(f("could not retrive metatable %d", name));
|
|
|
|
|
else
|
|
|
|
|
lua_setmetatable(L, index < 0 ? index-1 : index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::rawGet(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
pushString(key);
|
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::rawSet(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
pushString(key);
|
|
|
|
|
insert(-2);
|
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::getField(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
lua_getfield(L, -1, key.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setField(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
lua_setfield(L, -2, key.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::getScriptableField(const ScriptablePtr& scriptable, const std::string& field)
|
|
|
|
|
{
|
|
|
|
|
if(scriptable) {
|
|
|
|
|
pushRef(scriptable->getLuaTableRef());
|
|
|
|
|
getField(field);
|
|
|
|
|
remove(-2);
|
|
|
|
|
} else
|
|
|
|
|
pushNil();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setScriptableField(const ScriptablePtr& scriptable, const std::string& field)
|
|
|
|
|
{
|
|
|
|
|
if(scriptable) {
|
|
|
|
|
pushRef(scriptable->getLuaTableRef());
|
|
|
|
|
insert(-2);
|
|
|
|
|
setField(field);
|
|
|
|
|
}
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::rawGetGlobalTableField(const std::string& globalTable, const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
getGlobal(globalTable);
|
|
|
|
|
rawGet(key);
|
|
|
|
|
remove(-2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::rawSetGlobalTableField(const std::string& globalTable, const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
getGlobal(globalTable);
|
|
|
|
|
insert(-2);
|
|
|
|
|
rawSet(key);
|
|
|
|
|
pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::getGlobal(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, key.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setGlobal(const std::string& key)
|
|
|
|
|
{
|
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, key.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isNil(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isnil(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isBoolean(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isboolean(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isNumber(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isnumber(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isString(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isstring(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isTable(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_istable(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isUserdata(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isuserdata(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isFunction(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_isfunction(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isCFunction(int index)
|
|
|
|
|
{
|
|
|
|
|
return lua_iscfunction(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaScript::isLuaFunction(int index)
|
|
|
|
|
{
|
|
|
|
|
return isFunction(index) && !isCFunction(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pop(int n)
|
|
|
|
@ -171,7 +310,7 @@ bool LuaScript::popBoolean()
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t LuaScript::popInteger()
|
|
|
|
|
int LuaScript::popInteger()
|
|
|
|
|
{
|
|
|
|
|
double d = lua_tonumber(L, -1);
|
|
|
|
|
pop();
|
|
|
|
@ -187,6 +326,24 @@ std::string LuaScript::popString()
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptablePtr LuaScript::popClassInstance()
|
|
|
|
|
{
|
|
|
|
|
ScriptablePtr object;
|
|
|
|
|
if(isUserdata()) { // instances are store as userdata
|
|
|
|
|
object = *((ScriptablePtr *)lua_touserdata(L, -1));
|
|
|
|
|
if(!object)
|
|
|
|
|
reportErrorWithTraceback("attempt to retrive class instance from a object that is already expired");
|
|
|
|
|
} else if(!isNil()) // we accept nil values
|
|
|
|
|
reportErrorWithTraceback("couldn't retrive class instance, the value is not a scriptable type");
|
|
|
|
|
pop();
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaScript::popRef()
|
|
|
|
|
{
|
|
|
|
|
return luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pushNil()
|
|
|
|
|
{
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
@ -207,55 +364,23 @@ void LuaScript::pushString(const std::string& str)
|
|
|
|
|
lua_pushstring(L, str.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pushUserdata(void* ptr)
|
|
|
|
|
{
|
|
|
|
|
lua_pushlightuserdata(L, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pushValue(int index)
|
|
|
|
|
{
|
|
|
|
|
lua_pushvalue(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pushClassInstance(const ScriptablePtr& object)
|
|
|
|
|
{
|
|
|
|
|
if(object) {
|
|
|
|
|
// create weak_ptr to the scriptable object stored as userdata
|
|
|
|
|
new(lua_newuserdata(L, sizeof(ScriptablePtr))) ScriptablePtr(object);
|
|
|
|
|
// set object metatable
|
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, (std::string(object->getScriptableName()) + "_mt").c_str());
|
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
} else {
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptablePtr LuaScript::popClassInstance()
|
|
|
|
|
{
|
|
|
|
|
ScriptablePtr object;
|
|
|
|
|
if(lua_isuserdata(L, -1)) { // instances are store as userdata
|
|
|
|
|
object = *((ScriptablePtr *)lua_touserdata(L, -1));
|
|
|
|
|
if(!object)
|
|
|
|
|
reportErrorWithTraceback("attempt to retrive class instance from a object that is already expired");
|
|
|
|
|
} else if(!lua_isnil(L, -1)) // we accept nil values
|
|
|
|
|
reportErrorWithTraceback("couldn't retrive class instance, the value is not a scriptable type");
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::pushFunction(int functionRef)
|
|
|
|
|
{
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, functionRef);
|
|
|
|
|
new(newUserdata(sizeof(ScriptablePtr))) ScriptablePtr(object);
|
|
|
|
|
setMetatable(std::string(object->getScriptableName()) + "_mt");
|
|
|
|
|
} else
|
|
|
|
|
pushNil();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaScript::popFunction()
|
|
|
|
|
void LuaScript::pushValue(int index)
|
|
|
|
|
{
|
|
|
|
|
return ref();
|
|
|
|
|
lua_pushvalue(L, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::releaseFunction(int functionRef)
|
|
|
|
|
void LuaScript::pushRef(int ref)
|
|
|
|
|
{
|
|
|
|
|
unref(functionRef);
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::callFunction(int numArgs)
|
|
|
|
@ -277,53 +402,33 @@ void LuaScript::callFunction(int numArgs)
|
|
|
|
|
reportError("stack size changed!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimpleCallback LuaScript::createSimpleFuncCallback(int funcRef)
|
|
|
|
|
void LuaScript::callModuleField(const std::string& module, const std::string& field)
|
|
|
|
|
{
|
|
|
|
|
return [this, funcRef]() {
|
|
|
|
|
pushFunction(funcRef);
|
|
|
|
|
callFunction();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boost::function<void(ScriptablePtr)> LuaScript::createScriptableSelfFuncCallback(int funcRef)
|
|
|
|
|
{
|
|
|
|
|
return [this, funcRef](ScriptablePtr scriptable) {
|
|
|
|
|
pushClassInstance(scriptable);
|
|
|
|
|
setGlobal("self");
|
|
|
|
|
|
|
|
|
|
pushFunction(funcRef);
|
|
|
|
|
callFunction();
|
|
|
|
|
|
|
|
|
|
pushNil();
|
|
|
|
|
setGlobal("self");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setGlobal(const char *varName)
|
|
|
|
|
{
|
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, varName);
|
|
|
|
|
g_lua.rawGetGlobalTableField(module, field);
|
|
|
|
|
g_lua.callFunction();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::setupPackageLoader()
|
|
|
|
|
{
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "package");
|
|
|
|
|
lua_getfield(L, -1, "loaders");
|
|
|
|
|
lua_remove(L, -2);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "package"); // package
|
|
|
|
|
lua_getfield(L, -1, "loaders"); // package.loaders, package
|
|
|
|
|
lua_remove(L, -2); // package.loaders
|
|
|
|
|
lua_pushnil(L); // nil, package.loaders
|
|
|
|
|
int numLoaders = 0;
|
|
|
|
|
while(lua_next(L, -2) != 0) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
// value, key, package.loaders
|
|
|
|
|
lua_pop(L, 1); // key, package.loaders
|
|
|
|
|
numLoaders++;
|
|
|
|
|
}
|
|
|
|
|
lua_pushinteger(L, numLoaders + 1);
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaPackageLoader);
|
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_pushinteger(L, numLoaders + 1); // at, package.loaders
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaPackageLoader); // luaPackageLoader, at, package.loaders
|
|
|
|
|
lua_rawset(L, -3); // package.loaders
|
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::registerClass(const std::string& klass, const std::string& baseClass)
|
|
|
|
|
{
|
|
|
|
|
// klass_mt = {}
|
|
|
|
|
// mt = {}
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, (klass + "_mt").c_str());
|
|
|
|
@ -344,35 +449,60 @@ void LuaScript::registerClass(const std::string& klass, const std::string& baseC
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaGarbageCollectMetaMethod);
|
|
|
|
|
lua_setfield(L, -2, "__gc");
|
|
|
|
|
|
|
|
|
|
// klass_mt.methods = { }
|
|
|
|
|
// klass = { }
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, klass.c_str());
|
|
|
|
|
|
|
|
|
|
// mt.methods = klass
|
|
|
|
|
lua_setfield(L, -2, "methods");
|
|
|
|
|
|
|
|
|
|
// klass_mt.fieldmethods = { }
|
|
|
|
|
// klass_fieldmethods = { }
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
|
|
|
|
|
|
|
|
|
// mt.fieldmethods = klass_fieldmethods
|
|
|
|
|
lua_setfield(L, -2, "fieldmethods");
|
|
|
|
|
|
|
|
|
|
if(baseClass.length()) {
|
|
|
|
|
// klass_mt.base = baseClass_mt
|
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, (baseClass + "_mt").c_str());
|
|
|
|
|
lua_setfield(L, -2, "base");
|
|
|
|
|
// get klass
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, klass.c_str());
|
|
|
|
|
|
|
|
|
|
// redirect = { __index = baseClass }
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, baseClass.c_str());
|
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
|
|
|
|
|
|
// setmetatable(klass, redirect)
|
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
|
|
// get klass_fieldmethods
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
|
|
|
|
|
|
|
|
|
// redirect = { __index = baseClass_fieldmethods }
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (baseClass + "_fieldmethods").c_str());
|
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
|
|
|
|
|
|
// setmetatable(klass_fieldmethods, redirect)
|
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
|
|
m_currentClass = klass;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::registerMemberField(const std::string& klass, const std::string& field, LuaScript::LuaCFunction getFunction, LuaScript::LuaCFunction setFunction)
|
|
|
|
|
void LuaScript::registerMemberField(const std::string& field, LuaScript::LuaCFunction getFunction, LuaScript::LuaCFunction setFunction)
|
|
|
|
|
{
|
|
|
|
|
if(getFunction) {
|
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
|
m_functions.push_back(getFunction);
|
|
|
|
|
|
|
|
|
|
// push the class table
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (m_currentClass + "_fieldmethods").c_str());
|
|
|
|
|
// push the function id
|
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
|
// store id in the closure
|
|
|
|
@ -388,7 +518,7 @@ void LuaScript::registerMemberField(const std::string& klass, const std::string&
|
|
|
|
|
m_functions.push_back(setFunction);
|
|
|
|
|
|
|
|
|
|
// push the class table
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (m_currentClass + "_fieldmethods").c_str());
|
|
|
|
|
// push the function id
|
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
|
// store id in the closure
|
|
|
|
@ -400,13 +530,13 @@ void LuaScript::registerMemberField(const std::string& klass, const std::string&
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaScript::registerMemberFunction(const std::string& klass, const std::string& functionName, LuaCFunction function)
|
|
|
|
|
void LuaScript::registerMemberFunction(const std::string& functionName, LuaCFunction function)
|
|
|
|
|
{
|
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
|
m_functions.push_back(function);
|
|
|
|
|
|
|
|
|
|
// push the class table
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, klass.c_str());
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, m_currentClass.c_str());
|
|
|
|
|
// push the function id
|
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
|
// store id in the closure
|
|
|
|
@ -432,9 +562,11 @@ void LuaScript::registerGlobalFunction(const std::string& functionName, LuaCFunc
|
|
|
|
|
|
|
|
|
|
void LuaScript::registerModule(const std::string& module)
|
|
|
|
|
{
|
|
|
|
|
// module = {}
|
|
|
|
|
lua_newtable(L); // module table
|
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, module.c_str()); // register at globals index
|
|
|
|
|
registerClass(module);
|
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, module.c_str());
|
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, (module + "_mt").c_str());
|
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
lua_pop(L, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaScript::luaPackageLoader(lua_State* L)
|
|
|
|
@ -452,50 +584,52 @@ int LuaScript::luaPackageLoader(lua_State* L)
|
|
|
|
|
|
|
|
|
|
int LuaScript::luaIndexMetaMethod(lua_State* L)
|
|
|
|
|
{
|
|
|
|
|
// stack: key, obj
|
|
|
|
|
std::string key = lua_tostring(L, -1); // key, obj
|
|
|
|
|
lua_pop(L, 1); // obj
|
|
|
|
|
lua_getmetatable(L, -1); // mt, obj
|
|
|
|
|
while(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_getfield(L, -1, "fieldmethods"); // mt.fieldmethods, mt, obj
|
|
|
|
|
lua_getfield(L, -1, ("get_" + key).c_str()); // mt.fieldmethods[get_key], mt.fieldmethods, mt, obj
|
|
|
|
|
lua_remove(L, -2); // mt.fieldmethods[get_key], mt, obj
|
|
|
|
|
lua_getfield(L, -1, "fieldmethods"); // fieldmethods, mt, obj
|
|
|
|
|
lua_getfield(L, -1, ("get_" + key).c_str()); // get_method, fieldmethods, mt, obj
|
|
|
|
|
lua_remove(L, -2); // get_method, mt, obj
|
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_remove(L, -2); // get_method, obj
|
|
|
|
|
lua_insert(L, -2); // obj, get_method
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, obj,get_method
|
|
|
|
|
lua_insert(L, -3); // obj, get_method, errorfunc
|
|
|
|
|
int ret = lua_pcall(L, 1, 1, -3); // ret, errorfunc
|
|
|
|
|
if(ret != 0) {
|
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
|
lua_pushnil(L); // nil
|
|
|
|
|
} else {
|
|
|
|
|
lua_remove(L, -2); // ret
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(L, 1); // mt, obj
|
|
|
|
|
lua_getfield(L, -1, "methods"); // methods, mt, obj
|
|
|
|
|
lua_getfield(L, -1, key.c_str()); // method, methods, mt, obj
|
|
|
|
|
lua_remove(L, -2); // method, mt, obj
|
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_remove(L, -2); // mt.fieldmethods[get_key], obj
|
|
|
|
|
lua_insert(L, -2); // obj, mt.fieldmethods[get_key]
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, obj, mt.fieldmethods[get_key]
|
|
|
|
|
lua_insert(L, -3); // obj, mt.fieldmethods[get_key], errorfunc
|
|
|
|
|
int ret = lua_pcall(L, 1, 1, -3); // ret, errorfunc
|
|
|
|
|
if(ret != 0) {
|
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
|
lua_pushnil(L); // nil
|
|
|
|
|
} else {
|
|
|
|
|
lua_remove(L, -2); // ret
|
|
|
|
|
}
|
|
|
|
|
lua_insert(L, -3); // obj, mt, method
|
|
|
|
|
lua_pop(L, 2); // method
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(L, 1); // mt, obj
|
|
|
|
|
lua_getfield(L, -1, "methods"); // mt.methods, mt, obj
|
|
|
|
|
lua_getfield(L, -1, key.c_str()); // mt.methods[key], mt.methods, mt, obj
|
|
|
|
|
lua_remove(L, -2); // mt.methods[key], mt, obj
|
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_insert(L, -3);
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
lua_getfield(L, -1, "base"); // mt.base, mt, obj
|
|
|
|
|
lua_remove(L, -2); // mt.base, obj
|
|
|
|
|
lua_pop(L, 1); // mt, obj
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_pop(L, 1); // obj
|
|
|
|
|
|
|
|
|
|
ScriptablePtr scriptable = g_lua.popClassInstance();
|
|
|
|
|
int refId = scriptable->getLuaRef(key);
|
|
|
|
|
if(refId != -1)
|
|
|
|
|
g_lua.getRef(refId);
|
|
|
|
|
else
|
|
|
|
|
// stack: obj
|
|
|
|
|
if(g_lua.isTable()) {
|
|
|
|
|
g_lua.rawGet(key);
|
|
|
|
|
g_lua.remove(-2);
|
|
|
|
|
} else if(g_lua.isUserdata()) {
|
|
|
|
|
g_lua.getScriptableField(g_lua.popClassInstance(), key);
|
|
|
|
|
} else {
|
|
|
|
|
g_lua.pop();
|
|
|
|
|
g_lua.pushNil();
|
|
|
|
|
g_lua.reportErrorWithTraceback("unknown type in newindex metamethod");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -503,39 +637,38 @@ int LuaScript::luaIndexMetaMethod(lua_State* L)
|
|
|
|
|
int LuaScript::luaNewIndexMetaMethod(lua_State* L)
|
|
|
|
|
{
|
|
|
|
|
// stack: value, key, obj
|
|
|
|
|
lua_insert(L, -2);
|
|
|
|
|
lua_insert(L, -2); // key, value, obj
|
|
|
|
|
std::string key = lua_tostring(L, -1);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_getmetatable(L, -2);
|
|
|
|
|
// stack: mt, value, obj
|
|
|
|
|
while(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_getfield(L, -1, "fieldmethods");
|
|
|
|
|
lua_getfield(L, -1, ("set_" + key).c_str());
|
|
|
|
|
lua_remove(L, -2); // stack: set method, mt, value, obj
|
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_remove(L, -2); // mt.fieldmethods[get_key], value, obj
|
|
|
|
|
lua_insert(L, -3); // value, obj, mt.fieldmethods[get_key]
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, value, obj, mt.fieldmethods[get_key]
|
|
|
|
|
lua_insert(L, -4); // value, obj, mt.fieldmethods[get_key], errorfunc
|
|
|
|
|
int ret = lua_pcall(L, 2, 0, -4); // errorfunc
|
|
|
|
|
if(ret != 0)
|
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1); // mt, value, obj
|
|
|
|
|
lua_getfield(L, -1, "base"); // mt.base, mt, value, obj
|
|
|
|
|
lua_remove(L, -2); // mt.base, value, obj
|
|
|
|
|
lua_pop(L, 1); // value, obj
|
|
|
|
|
lua_getmetatable(L, -2); // mt, value, obj
|
|
|
|
|
lua_getfield(L, -1, "fieldmethods"); // fieldmethods, mt, value, obj
|
|
|
|
|
lua_getfield(L, -1, ("set_" + key).c_str()); // set_method, fieldmethods, mt, value, obj
|
|
|
|
|
lua_remove(L, -2); // set_method, mt, value, obj
|
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
|
lua_remove(L, -2); // set_method, value, obj
|
|
|
|
|
lua_insert(L, -3); // value, obj, set_method
|
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, value, obj, set_method
|
|
|
|
|
lua_insert(L, -4); // value, obj, set_method, errorfunc
|
|
|
|
|
int ret = lua_pcall(L, 2, 0, -4); // errorfunc
|
|
|
|
|
if(ret != 0)
|
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 2); // mt, value, obj
|
|
|
|
|
|
|
|
|
|
// stack: value, obj
|
|
|
|
|
g_lua.insert(-2);
|
|
|
|
|
if(g_lua.isTable()) {
|
|
|
|
|
g_lua.insert(-2);
|
|
|
|
|
g_lua.rawSet(key);
|
|
|
|
|
g_lua.pop();
|
|
|
|
|
} else if(g_lua.isUserdata()) {
|
|
|
|
|
g_lua.setScriptableField(g_lua.popClassInstance(), key);
|
|
|
|
|
} else {
|
|
|
|
|
g_lua.pop(2);
|
|
|
|
|
g_lua.reportErrorWithTraceback("unknown type in index metamethod");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stack:
|
|
|
|
|
g_lua.pop(); // value, obj
|
|
|
|
|
g_lua.insert(-2); // obj, value
|
|
|
|
|
ScriptablePtr scriptable = g_lua.popClassInstance();
|
|
|
|
|
g_lua.pushValue(); // value, value
|
|
|
|
|
int refId = g_lua.ref();
|
|
|
|
|
scriptable->associateLuaRef(key, refId);
|
|
|
|
|
g_lua.pop();
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -559,8 +692,10 @@ int LuaScript::luaEqualMetaMethod(lua_State* L)
|
|
|
|
|
|
|
|
|
|
int LuaScript::luaGarbageCollectMetaMethod(lua_State* L)
|
|
|
|
|
{
|
|
|
|
|
ScriptablePtr *objectRef = (ScriptablePtr *)lua_touserdata(L, -1);
|
|
|
|
|
objectRef->reset();
|
|
|
|
|
if(lua_isuserdata(L, -1)) {
|
|
|
|
|
ScriptablePtr *objectRef = (ScriptablePtr *)lua_touserdata(L, -1);
|
|
|
|
|
objectRef->reset();
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|