Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/OLD/LuaCallback.cc @ 8230

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

merged the scriptengine back to the trunk

File size: 3.3 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[7654]12   main-programmer: Silvan Nellen
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[7654]18#include "Script.h"
19#include "LuaCallback.h"
20#include "luaincl.h"
21#include "scriptable.h"
[1853]22
[7654]23#include <cassert>
24#include <string>
25#include <iostream>
26#include <stdio.h>
[1853]27
[7654]28namespace OrxScript
[3365]29{
[4320]30
[7654]31  /**
32   * @brief This function accually executes a method called by Lua.
33   * @param lua Since this function gets called by lua it has to have a lua_State as parameter
34   * @return the number of return values. (which can be found on the stack)
35   *
36   * For Lua doesn't know how to handle C++ objects it pushes the Objecttable (including the objectpointer) on top of the stack and
37   * calls this function.
38   *
39   * @todo handle an acces to a nonexistent (deleted) object
[4320]40   */
[7654]41  int luaCallback (lua_State *lua)
42  {
[1853]43
[7654]44    // Locate the psudo-index for the function number
45    int methodNumber = lua_upvalueindex (1);
46    int returnCount = 0;
[1853]47
[7654]48    bool functionSuccess = false;
49
50    // Check for the "this" table
51    if (lua_istable (lua, 1))
52    {
53      // Found the "this" table. The object pointer is at the index 0
54      lua_rawgeti (lua, 1, 0);
55
56      if (lua_islightuserdata (lua, -1))
57      {
58        // Found the pointer, need to cast it
59        Scriptable* thisPointer = (Scriptable*) lua_touserdata (lua, -1);
60
61        // Get the method index
62        int methodIndex = (int) lua_tonumber (lua, methodNumber);
63
64        // Reformat the stack so our parameters are correct
65        // Clean up the "this" table
66        lua_remove (lua, 1);
67        // Clean up the thisPointer pointer
68        lua_remove (lua, -1);
69
70        //debug
71        //std::cout<<thisPointer->whatIsThis()<<std::endl;
72
73        LuaScript* originScript = NULL;
74
75        lua_getglobal (lua, "this");
76
77        if (lua_istable (lua, 1))
78        {
79          // Found the "this" table. The object pointer is at the index 0
80          lua_rawgeti (lua, 1, 0);
81
82          if (lua_islightuserdata (lua, -1))
83          {
84            // Found the pointer, need to cast it
85            originScript = (LuaScript *) lua_touserdata (lua, -1);
86          }
87        }
88
89        if(originScript == NULL)
90        {//do something usefull
91        }
92        //debug
93        // std::cout<<originScript->whatIsThis()<<std::endl;
94
95        LuaVirtualMachine virtualMachine = originScript->getVirtualMachine();
96        //debug
97        //std::cout<<"test "<< thisPointer->methods(virtualMachine)<<std::endl;
98        // Check that the method is correct index
99        assert ((methodIndex <= thisPointer->methods(virtualMachine) ));
100        // Call the class
101        returnCount = thisPointer->scriptCalling ( virtualMachine, thisPointer->getFunctionAtIndex(methodIndex,virtualMachine));
102
103        functionSuccess = true;
104      }
105    }
106
107    if (functionSuccess == false)
108    {
109      lua_pushstring (lua, "luaCallback -> Failed to call the class function");
110      lua_error (lua);
111    }
112
113    // Number of return variables
114    return returnCount;
115  }
[3543]116}
Note: See TracBrowser for help on using the repository browser.