Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/script_engine: namespace OrxScript introduced

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