]> git.r.bdr.sh - rbdr/pico-engine/blob - LuaThis.h
Update project so it compiles
[rbdr/pico-engine] / LuaThis.h
1 // ---------------------------------------------------------------------------
2 // FILE NAME : LuaThis.h
3 // ---------------------------------------------------------------------------
4 // DESCRIPTION :
5 //
6 // Controls the "this" table
7 //
8 // ---------------------------------------------------------------------------
9 // VERSION : 1.01
10 // DATE : 1-Sep-2005
11 // AUTHOR : Richard Shephard
12 // ---------------------------------------------------------------------------
13 // LIBRARY INCLUDE FILES
14 #ifndef __LUA_THIS_H__
15 #define __LUA_THIS_H__
16
17 #include "luainc.h"
18 #include "luavirtualmachine.h"
19
20 // Sets the "this" global table that scripts use
21
22 class CLuaThis
23 {
24 public:
25 CLuaThis (CLuaVirtualMachine& vm, int iRef) : m_iOldRef (0), m_vm (vm)
26 {
27 lua_State *state = (lua_State *) vm;
28 if (vm.Ok ())
29 {
30 // Save the old "this" table
31 lua_getglobal (state, "this");
32 m_iOldRef = luaL_ref (state, LUA_REGISTRYINDEX);
33
34 // replace it with our new one
35 lua_rawgeti(state, LUA_REGISTRYINDEX, iRef);
36 lua_setglobal (state, "this");
37 }
38 }
39
40 virtual ~CLuaThis (void)
41 {
42 lua_State *state = (lua_State *) m_vm;
43 if (m_iOldRef > 0 && m_vm.Ok ())
44 {
45 // Replace the old "this" table
46 lua_rawgeti(state, LUA_REGISTRYINDEX, m_iOldRef);
47 lua_setglobal (state, "this");
48 luaL_unref (state, LUA_REGISTRYINDEX, m_iOldRef);
49 }
50 }
51
52
53 protected:
54 int m_iOldRef;
55 CLuaVirtualMachine& m_vm;
56 };
57
58 #endif // __LUA_THIS_H__