Er.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Er_H
  16. #define Er_H
  17. #include "memory/Allocator.h"
  18. #include "util/Gcc.h"
  19. #include "util/Js.h"
  20. #include "util/Linker.h"
  21. Linker_require("exception/Er.c")
  22. struct Er_Ret
  23. {
  24. const char* message;
  25. };
  26. Js({ this.Er_JS = require("../exception/Er.js").create(); })
  27. #define Er_DEFUN(...) \
  28. Gcc_USE_RET Js_or({ return this.Er_JS.defun(Js_Q __VA_ARGS__ Js_Q) }, __VA_ARGS__)
  29. Gcc_PRINTF(4, 5)
  30. Gcc_USE_RET
  31. struct Er_Ret* Er__raise(char* file, int line, struct Allocator* alloc, char* format, ...);
  32. #define Er_raise(...) \
  33. do { \
  34. struct Er_Ret* Er_ret = Er__raise(Gcc_SHORT_FILE, Gcc_LINE, __VA_ARGS__); \
  35. Js_or({ return 'return Er_ret;' }, Er__assertFail(Er_ret)); \
  36. } while (0)
  37. // CHECKFILES_IGNORE missing ;
  38. #define Er(expr) Js_or({ return this.Er_JS.er(Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
  39. #define Er_assert(expr) \
  40. Js_or({ return this.Er_JS.assert(Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
  41. #define Er_check(ret, expr) \
  42. Js_or({ return this.Er_JS.check(#ret, Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
  43. #define Er_ret(val) Js_or({ return this.Er_JS.ret(Js_Q val Js_Q); }, return val)
  44. static inline struct Er_Ret* Er_unwind(const char* file, int line, struct Er_Ret* ret)
  45. {
  46. return ret;
  47. }
  48. void Er__assertFail(struct Er_Ret* er);
  49. #endif