From 81fdf41e3b04925dae7fe4629b5fd2b3913eb3cc Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 26 Nov 2011 05:10:44 +0200 Subject: [PATCH] Lua: Add table _SYSTEM --- manual.lyx | 9 +++++++++ src/core/lua.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/manual.lyx b/manual.lyx index 8a05e35c..8398605a 100644 --- a/manual.lyx +++ b/manual.lyx @@ -2583,6 +2583,15 @@ Writes the specified value (negative values undergo 2's complement) to specified \end_layout \begin_layout Subsection +Table _SYSTEM +\end_layout + +\begin_layout Standard +Contains copy of global variables from time of Lua initialization. + Non-writeable. +\end_layout + +\begin_layout Subsection Callbacks \end_layout diff --git a/src/core/lua.cpp b/src/core/lua.cpp index 26def20c..9d28f428 100644 --- a/src/core/lua.cpp +++ b/src/core/lua.cpp @@ -283,6 +283,34 @@ namespace command::invokeC("repaint"); } } + + int system_write_error(lua_State* L) + { + lua_pushstring(L, "_SYSTEM is write-protected"); + lua_error(L); + return 0; + } + + void copy_system_tables(lua_State* L) + { + lua_newtable(L); + lua_pushnil(L); + while(lua_next(L, LUA_GLOBALSINDEX)) { + //Stack: _SYSTEM, KEY, VALUE + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + //Stack: _SYSTEM, KEY, VALUE, KEY, VALUE + lua_rawset(L, -5); + //Stack: _SYSTEM, KEY, VALUE + lua_pop(L, 1); + //Stack: _SYSTEM, KEY + } + lua_newtable(L); + lua_pushcfunction(L, system_write_error); + lua_setfield(L, -2, "__newindex"); + lua_setmetatable(L, -2); + lua_setglobal(L, "_SYSTEM"); + } } void lua_callback_do_paint(struct lua_render_context* ctx) throw() @@ -438,6 +466,7 @@ void init_lua() throw() luaL_openlibs(L); register_lua_functions(L); + copy_system_tables(L); } bool lua_requests_repaint = false; -- 2.11.4.GIT