std::string NSCPlugin::getDescription() { char *buffer = new char[4096]; if (!getDescription_(buffer, 4095)) { throw NSPluginException(get_alias_or_name(), "Could not get description"); } std::string ret = buffer; delete[] buffer; return ret; }
bool _Wolframe::langbind::getDescription( lua_State *ls, int index, std::string& ret) { try { LuaExceptionHandlerScope exceptionHandler( ls); { getDescription_( ls, index, ret); } return true; } catch (std::bad_alloc) { } catch (std::bad_cast) { } return false; }
static void getDescription_( lua_State *ls, int index, std::string& rt) { int type = lua_type( ls, index); switch (type) { case LUA_TLIGHTUSERDATA: rt.append( "lightuserdata"); break; case LUA_TUSERDATA: rt.append( " userdata "); lua_pushvalue( ls, index); ///...STK: udata if (!lua_getmetatable( ls, -1)) ///...STK: udata mt { rt.append( "(lightuserdata)"); lua_pop( ls, 1); } else { lua_pushliteral( ls, "__tostring"); ///...STK: udata mt __tostring lua_rawget( ls, -2); ///...STK: udata mt mt[__tostring] if (lua_isnil( ls, -1)) { lua_pop( ls, 3); ///... STK: } else { rt.append( " "); lua_pushvalue( ls, -3); ///... STK: udata mt mt[__tostring] udata lua_call( ls, 1, 1); ///... STK: udata mt str rt.append( lua_tostring( ls, -1)); lua_pop( ls, 3); ///... STK: } } break; case LUA_TNIL: rt.append( "nil"); break; case LUA_TBOOLEAN: rt.append( lua_toboolean( ls, index) ? "true":"false"); break; case LUA_TSTRING: { std::size_t len; const char* ptr = lua_tolstring( ls, index, &len); rt.append( ptr, len); break; } case LUA_TNUMBER: rt.append( boost::lexical_cast<std::string>( lua_tonumber( ls, index))); break; case LUA_TFUNCTION: rt.append( "function"); break; case LUA_TTABLE: if (!lua_checkstack( ls, 10)) { throw std::runtime_error( "lua stack overflow"); } rt.append( "{ "); lua_pushvalue( ls, index); lua_pushnil( ls); while (lua_next( ls, -2)) { lua_pushvalue( ls, -2); const char *key = lua_tostring( ls, -1); if (key && std::memcmp( key, "__", 2) != 0) { rt.append( key); bool istable = (lua_type( ls, -2) == LUA_TTABLE); rt.append( istable?"=":"='"); getDescription_( ls, -2, rt); rt.append( istable?" ":"' "); } lua_pop( ls, 2); } lua_pop( ls, 1); rt.append( "}"); break; default: rt.append( "(none)"); } }
std::string _Wolframe::langbind::getDescription( lua_State *ls, int index) { std::string rt; getDescription_( ls, index, rt); return rt; }