]>
git.r.bdr.sh - rbdr/pico-engine/blob - LuaThis.h
1 // ---------------------------------------------------------------------------
2 // FILE NAME : LuaThis.h
3 // ---------------------------------------------------------------------------
6 // Controls the "this" table
8 // ---------------------------------------------------------------------------
11 // AUTHOR : Richard Shephard
12 // ---------------------------------------------------------------------------
13 // LIBRARY INCLUDE FILES
14 #ifndef __LUA_THIS_H__
15 #define __LUA_THIS_H__
18 #include "luavirtualmachine.h"
20 // Sets the "this" global table that scripts use
25 CLuaThis (CLuaVirtualMachine
& vm
, int iRef
) : m_iOldRef (0), m_vm (vm
)
27 lua_State
*state
= (lua_State
*) vm
;
30 // Save the old "this" table
31 lua_getglobal (state
, "this");
32 m_iOldRef
= luaL_ref (state
, LUA_REGISTRYINDEX
);
34 // replace it with our new one
35 lua_rawgeti(state
, LUA_REGISTRYINDEX
, iRef
);
36 lua_setglobal (state
, "this");
40 virtual ~CLuaThis (void)
42 lua_State
*state
= (lua_State
*) m_vm
;
43 if (m_iOldRef
> 0 && m_vm
.Ok ())
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
);
55 CLuaVirtualMachine
& m_vm
;
58 #endif // __LUA_THIS_H__