You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
4.4 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. /******************************************************************************/
  17. /******************************************************************************/
  18. /* WARNING!!!! do NOT add this header in any include file or any code file */
  19. /* not being a modapi file!!!!!!!! */
  20. /******************************************************************************/
  21. /******************************************************************************/
  22. #ifndef C_INTERNAL_H_
  23. #define C_INTERNAL_H_
  24. extern "C" {
  25. #include <lua.h>
  26. #include <lauxlib.h>
  27. }
  28. #include "common/c_types.h"
  29. /*
  30. Define our custom indices into the Lua registry table.
  31. Lua 5.2 and above define the LUA_RIDX_LAST macro. Only numbers above that
  32. may be used for custom indices, anything else is reserved.
  33. Lua 5.1 / LuaJIT do not use any numeric indices (only string indices),
  34. so we can use numeric indices freely.
  35. */
  36. #ifdef LUA_RIDX_LAST
  37. #define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST)+1)
  38. #else
  39. #define CUSTOM_RIDX_BASE 1
  40. #endif
  41. #define CUSTOM_RIDX_SCRIPTAPI (CUSTOM_RIDX_BASE)
  42. #define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
  43. #define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
  44. #define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3)
  45. // Pushes the error handler onto the stack and returns its index
  46. #define PUSH_ERROR_HANDLER(L) \
  47. (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE), lua_gettop((L)))
  48. #define PCALL_RESL(L, RES) do { \
  49. int result_ = (RES); \
  50. if (result_ != 0) { \
  51. script_error((L), result_, NULL, __FUNCTION__); \
  52. } \
  53. } while (0)
  54. #define script_run_callbacks(L, nargs, mode) \
  55. script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__)
  56. // What script_run_callbacks does with the return values of callbacks.
  57. // Regardless of the mode, if only one callback is defined,
  58. // its return value is the total return value.
  59. // Modes only affect the case where 0 or >= 2 callbacks are defined.
  60. enum RunCallbacksMode
  61. {
  62. // Returns the return value of the first callback
  63. // Returns nil if list of callbacks is empty
  64. RUN_CALLBACKS_MODE_FIRST,
  65. // Returns the return value of the last callback
  66. // Returns nil if list of callbacks is empty
  67. RUN_CALLBACKS_MODE_LAST,
  68. // If any callback returns a false value, the first such is returned
  69. // Otherwise, the first callback's return value (trueish) is returned
  70. // Returns true if list of callbacks is empty
  71. RUN_CALLBACKS_MODE_AND,
  72. // Like above, but stops calling callbacks (short circuit)
  73. // after seeing the first false value
  74. RUN_CALLBACKS_MODE_AND_SC,
  75. // If any callback returns a true value, the first such is returned
  76. // Otherwise, the first callback's return value (falseish) is returned
  77. // Returns false if list of callbacks is empty
  78. RUN_CALLBACKS_MODE_OR,
  79. // Like above, but stops calling callbacks (short circuit)
  80. // after seeing the first true value
  81. RUN_CALLBACKS_MODE_OR_SC,
  82. // Note: "a true value" and "a false value" refer to values that
  83. // are converted by readParam<bool> to true or false, respectively.
  84. };
  85. std::string script_get_backtrace(lua_State *L);
  86. int script_exception_wrapper(lua_State *L, lua_CFunction f);
  87. void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
  88. void script_run_callbacks_f(lua_State *L, int nargs,
  89. RunCallbacksMode mode, const char *fxn);
  90. void log_deprecated(lua_State *L, const std::string &message);
  91. #endif /* C_INTERNAL_H_ */