@@ -457,27 +457,47 @@ Ref<LuaError> LuaState::pushVariant(lua_State *state, Variant var) {
457
457
return nullptr ;
458
458
}
459
459
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
+
460
486
// gets a variant at a given index
461
487
Variant LuaState::getVariant (lua_State *state, int index ) {
462
- Variant result;
463
-
464
488
int type = lua_type (state, index );
465
489
switch (type) {
466
490
case LUA_TSTRING: {
467
491
String utf8_str;
468
492
utf8_str.parse_utf8 (lua_tostring (state, index ));
469
- result = utf8_str;
470
- break ;
493
+ return utf8_str;
471
494
}
472
495
case LUA_TNUMBER:
473
- result = lua_tonumber (state, index );
474
- break ;
496
+ return lua_tonumber (state, index );
475
497
case LUA_TBOOLEAN:
476
- result = (bool )lua_toboolean (state, index );
477
- break ;
498
+ return (bool )lua_toboolean (state, index );
478
499
case LUA_TUSERDATA:
479
- result = *(Variant *)lua_touserdata (state, index );
480
- break ;
500
+ return *(Variant *)lua_touserdata (state, index );
481
501
case LUA_TTABLE: {
482
502
#ifndef LAPI_LUAJIT
483
503
lua_len (state, index );
@@ -487,64 +507,36 @@ Variant LuaState::getVariant(lua_State *state, int index) {
487
507
488
508
int len = lua_tointeger (state, -1 );
489
509
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 );
516
511
}
517
512
case LUA_TFUNCTION: {
518
513
Ref<LuaAPI> api = getAPI (state);
519
514
// Put function on the top of the stack and get a ref to it. This will create a copy of the function.
520
515
lua_pushvalue (state, index );
521
516
if (api->getUseCallables ()) {
522
517
LuaCallable *callable = memnew (LuaCallable (api, luaL_ref (state, LUA_REGISTRYINDEX), state));
523
- result = Callable (callable);
518
+ return Callable (callable);
524
519
} else {
525
520
Ref<LuaFunctionRef> funcRef;
526
521
funcRef.instantiate ();
527
522
funcRef->setRef (luaL_ref (state, LUA_REGISTRYINDEX));
528
523
funcRef->setLuaState (state);
529
- result = funcRef;
524
+ return funcRef;
530
525
}
531
- break ;
532
526
}
533
527
case LUA_TTHREAD: {
534
528
lua_State *tState = lua_tothread (state, index );
535
529
Ref<LuaCoroutine> thread;
536
530
thread.instantiate ();
537
531
thread->bindExisting (getAPI (state), tState);
538
- result = thread;
539
- break ;
532
+ return thread;
540
533
}
541
534
case LUA_TNIL: {
542
- break ;
535
+ return {} ;
543
536
}
544
537
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);
546
539
}
547
- return result;
548
540
}
549
541
550
542
// Assumes there is a error in the top of the stack. Pops it.
0 commit comments