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