lua_scripts.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #pragma once
  14. #include <AP_Common/AP_Common.h>
  15. #include <AP_Param/AP_Param.h>
  16. #include <setjmp.h>
  17. #include <AP_Filesystem/posix_compat.h>
  18. #include "lua_bindings.h"
  19. class lua_scripts
  20. {
  21. public:
  22. lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_level);
  23. /* Do not allow copies */
  24. lua_scripts(const lua_scripts &other) = delete;
  25. lua_scripts &operator=(const lua_scripts&) = delete;
  26. // run scripts, does not return unless an error occured
  27. void run(void);
  28. static bool overtime; // script exceeded it's execution slot, and we are bailing out
  29. private:
  30. typedef struct script_info {
  31. int lua_ref; // reference to the loaded script object
  32. uint64_t next_run_ms; // time (in milliseconds) the script should next be run at
  33. char *name; // filename for the script // FIXME: This information should be available from Lua
  34. script_info *next;
  35. } script_info;
  36. script_info *load_script(lua_State *L, char *filename);
  37. void load_all_scripts_in_dir(lua_State *L, const char *dirname);
  38. void run_next_script(lua_State *L);
  39. void remove_script(lua_State *L, script_info *script);
  40. // reschedule the script for execution. It is assumed the script is not in the list already
  41. void reschedule_script(script_info *script);
  42. script_info *scripts; // linked list of scripts to be run, sorted by next run time (soonest first)
  43. // hook will be run when CPU time for a script is exceeded
  44. // it must be static to be passed to the C API
  45. static void hook(lua_State *L, lua_Debug *ar);
  46. // lua panic handler, will jump back to the start of run
  47. static int atpanic(lua_State *L);
  48. static jmp_buf panic_jmp;
  49. lua_State *lua_state;
  50. const AP_Int32 & _vm_steps;
  51. const AP_Int8 & _debug_level;
  52. static void *alloc(void *ud, void *ptr, size_t osize, size_t nsize);
  53. static void *_heap;
  54. };