Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/LuaCallback.h @ 7645

Last change on this file since 7645 was 7645, checked in by bensch, 18 years ago

added script_engine

File size: 2.7 KB
Line 
1#ifndef __LUA_CALLBACK_H__
2#define __LUA_CALLBACK_H__
3
4#include <assert.h>
5#include <string>
6#include <iostream>
7#include <stdio.h>
8
9class LuaVirtualMachine;
10
11
12/**
13 * @brief This function accually executes a method called by Lua.
14 *
15 * @param lua Since this function gets called by lua it has to have a lua_State as parameter
16 *
17 * @return the number of return values. (which can be found on the stack)
18 *
19 * For Lua doesn't know how to handle C++ objects it pushes the Objecttable (including the objectpointer) on top of the stack and
20 * calls this function.
21 *
22 *
23 * @todo handle an acces to a nonexistent (deleted) object
24 */
25
26
27static int luaCallback (lua_State *lua)
28{
29
30 // Locate the psudo-index for the function number
31  int methodNumber = lua_upvalueindex (1);
32  int returnCount = 0;
33
34  bool functionSuccess = false;
35
36 // Check for the "this" table
37  if (lua_istable (lua, 1))
38  {
39   // Found the "this" table. The object pointer is at the index 0
40    lua_rawgeti (lua, 1, 0);
41
42    if (lua_islightuserdata (lua, -1))
43    {
44     // Found the pointer, need to cast it
45      Scriptable* thisPointer = (Scriptable*) lua_touserdata (lua, -1);
46
47      // Get the method index
48      int methodIndex = (int) lua_tonumber (lua, methodNumber);
49
50     // Reformat the stack so our parameters are correct
51     // Clean up the "this" table
52      lua_remove (lua, 1);
53     // Clean up the thisPointer pointer
54      lua_remove (lua, -1);
55
56      //debug
57      //std::cout<<thisPointer->whatIsThis()<<std::endl;
58
59      LuaScript* originScript = NULL;
60
61      lua_getglobal (lua, "this");
62
63      if (lua_istable (lua, 1))
64      {
65   // Found the "this" table. The object pointer is at the index 0
66        lua_rawgeti (lua, 1, 0);
67
68        if (lua_islightuserdata (lua, -1))
69        {
70     // Found the pointer, need to cast it
71           originScript = (LuaScript *) lua_touserdata (lua, -1);
72        }
73      }
74
75      if(originScript == NULL)
76      {//do something usefull
77      }
78      //debug
79     // std::cout<<originScript->whatIsThis()<<std::endl;
80
81      LuaVirtualMachine virtualMachine = originScript->getVirtualMachine();
82      //debug
83      //std::cout<<"test "<< thisPointer->methods(virtualMachine)<<std::endl;
84      // Check that the method is correct index
85      assert ((methodIndex <= thisPointer->methods(virtualMachine) ));
86      // Call the class
87      returnCount = thisPointer->scriptCalling ( virtualMachine, thisPointer->getFunctionAtIndex(methodIndex,virtualMachine));
88
89      functionSuccess = true;
90    }
91  }
92
93  if (functionSuccess == false)
94  {
95    lua_pushstring (lua, "luaCallback -> Failed to call the class function");
96    lua_error (lua);
97  }
98
99   // Number of return variables
100  return returnCount;
101}
102
103#endif /* __LUA_CALLBACK_H__*/
Note: See TracBrowser for help on using the repository browser.