Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit 100a3db

Browse files
committed
Some cleanup
1 parent edf4afe commit 100a3db

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

src/luaState.cpp

+36-44
Original file line numberDiff line numberDiff line change
@@ -457,27 +457,47 @@ Ref<LuaError> LuaState::pushVariant(lua_State *state, Variant var) {
457457
return nullptr;
458458
}
459459

460+
Variant LuaState::getArrayVariant(lua_State *state, int len, int index) {
461+
Array array;
462+
for (int i = 1; i <= len; i++) {
463+
#ifndef LAPI_LUAJIT
464+
lua_geti(state, index, i);
465+
#else
466+
lua_rawgeti(state, index, i);
467+
#endif
468+
array.push_back(getVariant(state, -1));
469+
lua_pop(state, 1);
470+
}
471+
return array;
472+
}
473+
474+
Variant LuaState::getDictionaryVariant(lua_State *state, int index) {
475+
lua_pushnil(state); /* first key */
476+
Dictionary dict;
477+
while (lua_next(state, (index < 0) ? (index - 1) : (index)) != 0) {
478+
Variant key = getVariant(state, -2);
479+
Variant value = getVariant(state, -1);
480+
dict[key] = value;
481+
lua_pop(state, 1);
482+
}
483+
return dict;
484+
}
485+
460486
// gets a variant at a given index
461487
Variant LuaState::getVariant(lua_State *state, int index) {
462-
Variant result;
463-
464488
int type = lua_type(state, index);
465489
switch (type) {
466490
case LUA_TSTRING: {
467491
String utf8_str;
468492
utf8_str.parse_utf8(lua_tostring(state, index));
469-
result = utf8_str;
470-
break;
493+
return utf8_str;
471494
}
472495
case LUA_TNUMBER:
473-
result = lua_tonumber(state, index);
474-
break;
496+
return lua_tonumber(state, index);
475497
case LUA_TBOOLEAN:
476-
result = (bool)lua_toboolean(state, index);
477-
break;
498+
return (bool)lua_toboolean(state, index);
478499
case LUA_TUSERDATA:
479-
result = *(Variant *)lua_touserdata(state, index);
480-
break;
500+
return *(Variant *)lua_touserdata(state, index);
481501
case LUA_TTABLE: {
482502
#ifndef LAPI_LUAJIT
483503
lua_len(state, index);
@@ -487,64 +507,36 @@ Variant LuaState::getVariant(lua_State *state, int index) {
487507

488508
int len = lua_tointeger(state, -1);
489509
lua_pop(state, 1);
490-
// len should be 0 if the type is table and not a array
491-
if (len) {
492-
Array array;
493-
for (int i = 1; i <= len; i++) {
494-
#ifndef LAPI_LUAJIT
495-
lua_geti(state, index, i);
496-
#else
497-
lua_rawgeti(state, index, i);
498-
#endif
499-
array.push_back(getVariant(state, -1));
500-
lua_pop(state, 1);
501-
}
502-
result = array;
503-
break;
504-
}
505-
506-
lua_pushnil(state); /* first key */
507-
Dictionary dict;
508-
while (lua_next(state, (index < 0) ? (index - 1) : (index)) != 0) {
509-
Variant key = getVariant(state, -2);
510-
Variant value = getVariant(state, -1);
511-
dict[key] = value;
512-
lua_pop(state, 1);
513-
}
514-
result = dict;
515-
break;
510+
return len ? getArrayVariant(state, len, index) : getDictionaryVariant(state, index);
516511
}
517512
case LUA_TFUNCTION: {
518513
Ref<LuaAPI> api = getAPI(state);
519514
// Put function on the top of the stack and get a ref to it. This will create a copy of the function.
520515
lua_pushvalue(state, index);
521516
if (api->getUseCallables()) {
522517
LuaCallable *callable = memnew(LuaCallable(api, luaL_ref(state, LUA_REGISTRYINDEX), state));
523-
result = Callable(callable);
518+
return Callable(callable);
524519
} else {
525520
Ref<LuaFunctionRef> funcRef;
526521
funcRef.instantiate();
527522
funcRef->setRef(luaL_ref(state, LUA_REGISTRYINDEX));
528523
funcRef->setLuaState(state);
529-
result = funcRef;
524+
return funcRef;
530525
}
531-
break;
532526
}
533527
case LUA_TTHREAD: {
534528
lua_State *tState = lua_tothread(state, index);
535529
Ref<LuaCoroutine> thread;
536530
thread.instantiate();
537531
thread->bindExisting(getAPI(state), tState);
538-
result = thread;
539-
break;
532+
return thread;
540533
}
541534
case LUA_TNIL: {
542-
break;
535+
return {};
543536
}
544537
default:
545-
result = LuaError::newError(vformat("Unsupported lua type '%d' in LuaState::getVariant", type), LuaError::ERR_RUNTIME);
538+
return LuaError::newError(vformat("Unsupported lua type '%d' in LuaState::getVariant", type), LuaError::ERR_RUNTIME);
546539
}
547-
return result;
548540
}
549541

550542
// Assumes there is a error in the top of the stack. Pops it.

src/luaState.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class LuaState {
6363
// Helper functions for recursive indexing
6464
void indexForReading(String name); // Puts the object on the stack
6565
String indexForWriting(String name); // Puts the table on the stack and gives the last name. (Please make sure the table is not nil.)
66+
static Variant getArrayVariant(lua_State *state, int len, int index);
67+
static Variant getDictionaryVariant(lua_State *state, int index);
6668

6769
void exposeConstructors();
6870
void createVector2Metatable();

0 commit comments

Comments
 (0)