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
|
Rev | Line | |
---|
[7645] | 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 |
---|
[7653] | 8 | namespace OrxScript |
---|
| 9 | { |
---|
[7645] | 10 | |
---|
[7653] | 11 | class LuaThis |
---|
| 12 | { |
---|
[7645] | 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 | { |
---|
[7653] | 19 | // Save the old "this" table |
---|
[7645] | 20 | lua_getglobal (state, "this"); |
---|
| 21 | oldReference = luaL_ref (state, LUA_REGISTRYINDEX); |
---|
| 22 | |
---|
[7653] | 23 | // replace it with our new one |
---|
[7645] | 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 | { |
---|
[7653] | 34 | // Replace the old "this" table |
---|
[7645] | 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; |
---|
[7653] | 45 | }; |
---|
| 46 | } |
---|
[7645] | 47 | #endif // __THIS_H__ |
---|
Note: See
TracBrowser
for help on using the repository browser.