summaryrefslogtreecommitdiff
path: root/src/libluaglue/LuaVM.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libluaglue/LuaVM.cpp')
-rw-r--r--src/libluaglue/LuaVM.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/libluaglue/LuaVM.cpp b/src/libluaglue/LuaVM.cpp
index 7b165f1..d3e2122 100644
--- a/src/libluaglue/LuaVM.cpp
+++ b/src/libluaglue/LuaVM.cpp
@@ -1,4 +1,5 @@
#include "LuaVM.hpp"
+#include "StringUtils.hpp"
#include <stdexcept>
#include <sstream>
@@ -87,3 +88,47 @@ void LuaVM::dumpState( )
lua_pop( m_lua, 1 );
}
}
+
+string LuaVM::getString( const string &key )
+{
+ vector<string> parts = split( key, "." );
+
+ string res;
+
+ if( parts.size( ) == 1 ) {
+ lua_getglobal( m_lua, parts[0].c_str( ) );
+
+ if( lua_isnil( m_lua, -1 ) ) {
+ lua_pop( m_lua, 1 );
+ return "";
+ }
+
+ res = lua_tostring( m_lua, -1 );
+ lua_pop( m_lua, 1 );
+
+ return res;
+ }
+
+ lua_getglobal( m_lua, parts[0].c_str( ) );
+ for( size_t i = 1; i <= parts.size( ) - 1; i++ ) {
+ if( !lua_istable( m_lua, -1 ) ) {
+ lua_pop( m_lua, i );
+ ostringstream ss;
+ ss << "table expected in '" << key << "' when derefencing field '" << parts[i] << "'";
+ throw runtime_error( ss.str( ) );
+ }
+ lua_getfield( m_lua, -1, parts[i].c_str( ) );
+ }
+
+ if( lua_isnil( m_lua, -1 ) ) {
+ lua_pop( m_lua, parts.size( ) );
+ ostringstream ss;
+ ss << "key '" << key << "' refers to an empty element";
+ throw runtime_error( ss.str( ) );
+ }
+
+ res = lua_tostring( m_lua, -1 );
+ lua_pop( m_lua, parts.size( ) );
+
+ return res;
+}