PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib_duktape/src-separate/duk_error_macros.c

https://bitbucket.org/xixs/lua
C | 130 lines | 98 code | 20 blank | 12 comment | 0 complexity | d3764ae3e7ee69f2f1ce1742ffb8b072 MD5 | raw file
Possible License(s): Zlib, BSD-3-Clause, CC0-1.0, GPL-3.0, GPL-2.0, CPL-1.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, LGPL-2.1, LGPL-3.0, 0BSD, Cube
  1. /*
  2. * Error, fatal, and panic handling.
  3. */
  4. #include "duk_internal.h"
  5. #define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */
  6. #ifdef DUK_USE_VERBOSE_ERRORS
  7. #ifdef DUK_USE_VARIADIC_MACROS
  8. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  9. va_list ap;
  10. char msg[DUK__ERRFMT_BUFSIZE];
  11. va_start(ap, fmt);
  12. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  13. msg[sizeof(msg) - 1] = (char) 0;
  14. duk_err_create_and_throw(thr, code, msg, filename, line);
  15. va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */
  16. }
  17. #else /* DUK_USE_VARIADIC_MACROS */
  18. DUK_INTERNAL const char *duk_err_file_stash = NULL;
  19. DUK_INTERNAL duk_int_t duk_err_line_stash = 0;
  20. DUK_NORETURN(DUK_LOCAL_DECL void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap));
  21. DUK_LOCAL void duk__handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, va_list ap) {
  22. char msg[DUK__ERRFMT_BUFSIZE];
  23. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  24. msg[sizeof(msg) - 1] = (char) 0;
  25. duk_err_create_and_throw(thr, code, msg, filename, line);
  26. }
  27. DUK_INTERNAL void duk_err_handle_error(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  28. va_list ap;
  29. va_start(ap, fmt);
  30. duk__handle_error(filename, line, thr, code, fmt, ap);
  31. va_end(ap); /* dead code */
  32. }
  33. DUK_INTERNAL void duk_err_handle_error_stash(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  34. va_list ap;
  35. va_start(ap, fmt);
  36. duk__handle_error(duk_err_file_stash, duk_err_line_stash, thr, code, fmt, ap);
  37. va_end(ap); /* dead code */
  38. }
  39. #endif /* DUK_USE_VARIADIC_MACROS */
  40. #else /* DUK_USE_VERBOSE_ERRORS */
  41. #ifdef DUK_USE_VARIADIC_MACROS
  42. DUK_INTERNAL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) {
  43. duk_err_create_and_throw(thr, code);
  44. }
  45. #else /* DUK_USE_VARIADIC_MACROS */
  46. DUK_INTERNAL void duk_err_handle_error_nonverbose1(duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  47. DUK_UNREF(fmt);
  48. duk_err_create_and_throw(thr, code);
  49. }
  50. DUK_INTERNAL void duk_err_handle_error_nonverbose2(const char *filename, duk_int_t line, duk_hthread *thr, duk_errcode_t code, const char *fmt, ...) {
  51. DUK_UNREF(filename);
  52. DUK_UNREF(line);
  53. DUK_UNREF(fmt);
  54. duk_err_create_and_throw(thr, code);
  55. }
  56. #endif /* DUK_USE_VARIADIC_MACROS */
  57. #endif /* DUK_USE_VERBOSE_ERRORS */
  58. /*
  59. * Default fatal error handler
  60. */
  61. DUK_INTERNAL void duk_default_fatal_handler(duk_context *ctx, duk_errcode_t code, const char *msg) {
  62. DUK_UNREF(ctx);
  63. #ifdef DUK_USE_FILE_IO
  64. DUK_FPRINTF(DUK_STDERR, "FATAL %ld: %s\n", (long) code, (const char *) (msg ? msg : "null"));
  65. DUK_FFLUSH(DUK_STDERR);
  66. #else
  67. /* omit print */
  68. #endif
  69. DUK_D(DUK_DPRINT("default fatal handler called, code %ld -> calling DUK_PANIC()", (long) code));
  70. DUK_PANIC(code, msg);
  71. DUK_UNREACHABLE();
  72. }
  73. /*
  74. * Default panic handler
  75. */
  76. #if !defined(DUK_USE_PANIC_HANDLER)
  77. DUK_INTERNAL void duk_default_panic_handler(duk_errcode_t code, const char *msg) {
  78. #ifdef DUK_USE_FILE_IO
  79. DUK_FPRINTF(DUK_STDERR, "PANIC %ld: %s ("
  80. #if defined(DUK_USE_PANIC_ABORT)
  81. "calling abort"
  82. #elif defined(DUK_USE_PANIC_EXIT)
  83. "calling exit"
  84. #elif defined(DUK_USE_PANIC_SEGFAULT)
  85. "segfaulting on purpose"
  86. #else
  87. #error no DUK_USE_PANIC_xxx macro defined
  88. #endif
  89. ")\n", (long) code, (const char *) (msg ? msg : "null"));
  90. DUK_FFLUSH(DUK_STDERR);
  91. #else
  92. /* omit print */
  93. DUK_UNREF(code);
  94. DUK_UNREF(msg);
  95. #endif
  96. #if defined(DUK_USE_PANIC_ABORT)
  97. DUK_ABORT();
  98. #elif defined(DUK_USE_PANIC_EXIT)
  99. DUK_EXIT(-1);
  100. #elif defined(DUK_USE_PANIC_SEGFAULT)
  101. /* exit() afterwards to satisfy "noreturn" */
  102. DUK_CAUSE_SEGFAULT(); /* SCANBUILD: "Dereference of null pointer", normal */
  103. DUK_EXIT(-1);
  104. #else
  105. #error no DUK_USE_PANIC_xxx macro defined
  106. #endif
  107. DUK_UNREACHABLE();
  108. }
  109. #endif /* !DUK_USE_PANIC_HANDLER */
  110. #undef DUK__ERRFMT_BUFSIZE