| 475 | } |
| 476 | |
| 477 | void lua_scripts::run(void) { |
| 478 | bool succeeded_initial_load = false; |
| 479 | |
| 480 | if (!_heap.available()) { |
| 481 | GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Lua: Unable to allocate a heap"); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | // panic should be hooked first |
| 486 | if (setjmp(panic_jmp)) { |
| 487 | if (!succeeded_initial_load) { |
| 488 | return; |
| 489 | } |
| 490 | if (lua_state != nullptr) { |
| 491 | lua_close(lua_state); // shutdown the old state |
| 492 | } |
| 493 | // remove all the old scheduled scripts |
| 494 | for (script_info *script = scripts; script != nullptr; script = scripts) { |
| 495 | remove_script(nullptr, script); |
| 496 | } |
| 497 | scripts = nullptr; |
| 498 | overtime = false; |
| 499 | } |
| 500 | |
| 501 | lua_state = lua_newstate(alloc, NULL); |
| 502 | lua_State *L = lua_state; |
| 503 | if (L == nullptr) { |
| 504 | GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Lua: Couldn't allocate a lua state"); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | #ifndef HAL_CONSOLE_DISABLED |
| 509 | const int inital_mem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0); |
| 510 | #endif |
| 511 | |
| 512 | lua_atpanic(L, atpanic); |
| 513 | load_generated_bindings(L); |
| 514 | |
| 515 | // set up string metatable. we set up one for all scripts that no script has |
| 516 | // access to, as it's impossible to set up one per-script and we don't want |
| 517 | // any script to be able to mess with it. |
| 518 | lua_pushliteral(L, ""); /* dummy string */ |
| 519 | lua_createtable(L, 0, 1); /* table to be metatable for strings */ |
| 520 | luaopen_string(L); /* get string library */ |
| 521 | lua_setfield(L, -2, "__index"); /* metatable.__index = string */ |
| 522 | lua_setmetatable(L, -2); /* set table as metatable for strings */ |
| 523 | lua_pop(L, 1); /* pop dummy string */ |
| 524 | |
| 525 | #ifndef HAL_CONSOLE_DISABLED |
| 526 | const int loaded_mem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0); |
| 527 | DEV_PRINTF("Lua: State memory usage: %i + %i\n", inital_mem, loaded_mem - inital_mem); |
| 528 | #endif |
| 529 | |
| 530 | // Scan the filesystem in an appropriate manner and autostart scripts |
| 531 | // Skip those directores disabled with SCR_DIR_DISABLE param |
| 532 | uint16_t dir_disable = AP_Scripting::get_singleton()->get_disabled_dir(); |
| 533 | bool loaded = false; |
| 534 | if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::SCRIPTS)) == 0) { |
no test coverage detected