#ifndef __LUA_CALLBACK_H__ #define __LUA_CALLBACK_H__ #include #include #include #include namespace OrxScript { class LuaVirtualMachine; /** * @brief This function accually executes a method called by Lua. * * @param lua Since this function gets called by lua it has to have a lua_State as parameter * * @return the number of return values. (which can be found on the stack) * * For Lua doesn't know how to handle C++ objects it pushes the Objecttable (including the objectpointer) on top of the stack and * calls this function. * * * @todo handle an acces to a nonexistent (deleted) object */ static int luaCallback (lua_State *lua) { // Locate the psudo-index for the function number int methodNumber = lua_upvalueindex (1); int returnCount = 0; bool functionSuccess = false; // Check for the "this" table if (lua_istable (lua, 1)) { // Found the "this" table. The object pointer is at the index 0 lua_rawgeti (lua, 1, 0); if (lua_islightuserdata (lua, -1)) { // Found the pointer, need to cast it Scriptable* thisPointer = (Scriptable*) lua_touserdata (lua, -1); // Get the method index int methodIndex = (int) lua_tonumber (lua, methodNumber); // Reformat the stack so our parameters are correct // Clean up the "this" table lua_remove (lua, 1); // Clean up the thisPointer pointer lua_remove (lua, -1); //debug //std::cout<whatIsThis()<whatIsThis()<getVirtualMachine(); //debug //std::cout<<"test "<< thisPointer->methods(virtualMachine)<methods(virtualMachine) )); // Call the class returnCount = thisPointer->scriptCalling ( virtualMachine, thisPointer->getFunctionAtIndex(methodIndex,virtualMachine)); functionSuccess = true; } } if (functionSuccess == false) { lua_pushstring (lua, "luaCallback -> Failed to call the class function"); lua_error (lua); } // Number of return variables return returnCount; } } #endif /* __LUA_CALLBACK_H__*/