| 452 | } |
| 453 | |
| 454 | int LuaStack::executeFunction(int numArgs) |
| 455 | { |
| 456 | int functionIndex = -(numArgs + 1); |
| 457 | if (!lua_isfunction(_state, functionIndex)) |
| 458 | { |
| 459 | AXLOGE("value at stack [{}] is not function", functionIndex); |
| 460 | lua_pop(_state, numArgs + 1); // remove function and arguments |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int traceback = 0; |
| 465 | lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */ |
| 466 | if (!lua_isfunction(_state, -1)) |
| 467 | { |
| 468 | lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */ |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */ |
| 473 | traceback = functionIndex - 1; |
| 474 | } |
| 475 | |
| 476 | int error = 0; |
| 477 | ++_callFromLua; |
| 478 | error = lua_pcall(_state, numArgs, 1, traceback); /* L: ... [G] ret */ |
| 479 | --_callFromLua; |
| 480 | if (error) |
| 481 | { |
| 482 | if (traceback == 0) |
| 483 | { |
| 484 | AXLOGE("[LUA ERROR] {}", lua_tostring(_state, -1)); /* L: ... error */ |
| 485 | lua_pop(_state, 1); // remove error message from stack |
| 486 | } |
| 487 | else /* L: ... G error */ |
| 488 | { |
| 489 | lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack |
| 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | // get return value |
| 495 | int ret = 0; |
| 496 | if (lua_isnumber(_state, -1)) |
| 497 | { |
| 498 | ret = (int)lua_tointeger(_state, -1); |
| 499 | } |
| 500 | else if (lua_isboolean(_state, -1)) |
| 501 | { |
| 502 | ret = (int)lua_toboolean(_state, -1); |
| 503 | } |
| 504 | // remove return value from stack |
| 505 | lua_pop(_state, 1); /* L: ... [G] */ |
| 506 | |
| 507 | if (traceback) |
| 508 | { |
| 509 | lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */ |
| 510 | } |
| 511 |
no test coverage detected