Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/This.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: 1.1 KB
Line 
1#ifndef __THIS_H__
2#define __THIS_H__
3
4#include "VirtualMachine.h"
5#include "luaincl.h"
6
7// Sets the "this" global table that scripts use
8namespace OrxScript
9{
10
11  class LuaThis
12  {
13  public:
14    LuaThis (LuaVirtualMachine& vm, int iRef) : oldReference (0), virtualMachine (vm)
15    {
16      lua_State *state = (lua_State *) vm;
17      if (vm.isOk ())
18      {
19        // Save the old "this" table
20        lua_getglobal (state, "this");
21        oldReference = luaL_ref (state, LUA_REGISTRYINDEX);
22
23        // replace it with our new one
24        lua_rawgeti(state, LUA_REGISTRYINDEX, iRef);
25        lua_setglobal (state, "this");
26      }
27    }
28
29    virtual ~LuaThis (void)
30    {
31      lua_State *state = (lua_State *) virtualMachine;
32      if (oldReference > 0 && virtualMachine.isOk ())
33      {
34        // Replace the old "this" table
35        lua_rawgeti(state, LUA_REGISTRYINDEX, oldReference);
36        lua_setglobal (state, "this");
37        luaL_unref (state, LUA_REGISTRYINDEX, oldReference);
38      }
39    }
40
41
42  protected:
43    int oldReference;
44    LuaVirtualMachine& virtualMachine;
45  };
46}
47#endif // __THIS_H__
Note: See TracBrowser for help on using the repository browser.