Skip to content

Commit 39f3d2d

Browse files
committed
dev: code review ListAudioDevice
1 parent 4bd46bf commit 39f3d2d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

LuaSTG/LuaSTG/LuaBinding/LW_Audio.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@ void luastg::binding::Audio::Register(lua_State* L)noexcept
1111
{
1212
struct Wrapper
1313
{
14-
static int ListAudioDevice(lua_State* L)
14+
static int ListAudioDevice(lua_State* vm)
1515
{
16+
lua::stack_t const ctx(vm);
1617
auto const audio_engine = LAPP.getAudioEngine();
1718
if (!audio_engine)
1819
{
19-
return luaL_error(L, "engine not initialized");
20+
return luaL_error(vm, "engine not initialized");
2021
}
21-
lua::stack_t S(L);
22-
auto const refresh = S.get_value<bool>(1);
23-
if (refresh) {
24-
audio_engine->refreshAudioEndpoint();
22+
if (auto const refresh = ctx.get_value<bool>(1); refresh) {
23+
std::ignore = audio_engine->refreshAudioEndpoint();
2524
}
2625
uint32_t const count = audio_engine->getAudioEndpointCount();
27-
S.create_array(count);
26+
auto const names = ctx.create_array(count);
2827
for (uint32_t i = 0; i < count; i += 1)
2928
{
3029
auto const name = audio_engine->getAudioEndpointName(i);
31-
S.set_array_value_zero_base<std::string_view>(i, name);
30+
ctx.set_array_value_zero_base<std::string_view>(names, i, name);
3231
}
3332
return 1;
3433
}

engine/lua/lua/plus.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ namespace lua {
163163
template<>
164164
inline void set_array_value_zero_base(size_t c_index, std::string_view value) { lua_pushlstring(L, value.data(), value.size()); lua_rawseti(L, -2, static_cast<int>(c_index + 1)); }
165165

166+
template<typename T>
167+
void set_array_value_zero_base(stack_index_t const array_index, stack_index_t const index, T const& value) const {
168+
lua_pushinteger(L, index.value + 1);
169+
push_value(value);
170+
lua_settable(L, array_index.value);
171+
}
172+
166173
template<typename T>
167174
inline void set_array_value(stack_index_t index, T value) { typename T::__invalid_type__ _{}; }
168175

0 commit comments

Comments
 (0)