jserror.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "jsi.h"
  2. #define QQ(X) #X
  3. #define Q(X) QQ(X)
  4. static int jsB_stacktrace(js_State *J, int skip)
  5. {
  6. char buf[256];
  7. int n = J->tracetop - skip;
  8. if (n <= 0)
  9. return 0;
  10. for (; n > 0; --n) {
  11. const char *name = J->trace[n].name;
  12. const char *file = J->trace[n].file;
  13. int line = J->trace[n].line;
  14. if (line > 0) {
  15. if (name[0])
  16. snprintf(buf, sizeof buf, "\n\tat %s (%s:%d)", name, file, line);
  17. else
  18. snprintf(buf, sizeof buf, "\n\tat %s:%d", file, line);
  19. } else
  20. snprintf(buf, sizeof buf, "\n\tat %s (%s)", name, file);
  21. js_pushstring(J, buf);
  22. if (n < J->tracetop - skip)
  23. js_concat(J);
  24. }
  25. return 1;
  26. }
  27. static void Ep_toString(js_State *J)
  28. {
  29. const char *name = "Error";
  30. const char *message = "";
  31. if (!js_isobject(J, -1))
  32. js_typeerror(J, "not an object");
  33. if (js_hasproperty(J, 0, "name"))
  34. name = js_tostring(J, -1);
  35. if (js_hasproperty(J, 0, "message"))
  36. message = js_tostring(J, -1);
  37. if (name[0] == 0)
  38. js_pushstring(J, message);
  39. else if (message[0] == 0)
  40. js_pushstring(J, name);
  41. else {
  42. js_pushstring(J, name);
  43. js_pushstring(J, ": ");
  44. js_concat(J);
  45. js_pushstring(J, message);
  46. js_concat(J);
  47. }
  48. }
  49. static void Ep_get_stack(js_State *J)
  50. {
  51. Ep_toString(J);
  52. js_getproperty(J, 0, "stackTrace");
  53. js_concat(J);
  54. }
  55. static int jsB_ErrorX(js_State *J, js_Object *prototype)
  56. {
  57. js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
  58. if (js_isdefined(J, 1)) {
  59. js_pushstring(J, js_tostring(J, 1));
  60. js_defproperty(J, -2, "message", JS_DONTENUM);
  61. }
  62. if (jsB_stacktrace(J, 1))
  63. js_defproperty(J, -2, "stackTrace", JS_DONTENUM);
  64. return 1;
  65. }
  66. static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
  67. {
  68. js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
  69. js_pushstring(J, message);
  70. js_setproperty(J, -2, "message");
  71. if (jsB_stacktrace(J, 0))
  72. js_setproperty(J, -2, "stackTrace");
  73. }
  74. #define DERROR(name, Name) \
  75. static void jsB_##Name(js_State *J) { \
  76. jsB_ErrorX(J, J->Name##_prototype); \
  77. } \
  78. void js_new##name(js_State *J, const char *s) { \
  79. js_newerrorx(J, s, J->Name##_prototype); \
  80. } \
  81. void js_##name(js_State *J, const char *fmt, ...) { \
  82. va_list ap; \
  83. char buf[256]; \
  84. va_start(ap, fmt); \
  85. vsnprintf(buf, sizeof buf, fmt, ap); \
  86. va_end(ap); \
  87. js_newerrorx(J, buf, J->Name##_prototype); \
  88. js_throw(J); \
  89. }
  90. DERROR(error, Error)
  91. DERROR(evalerror, EvalError)
  92. DERROR(rangeerror, RangeError)
  93. DERROR(referenceerror, ReferenceError)
  94. DERROR(syntaxerror, SyntaxError)
  95. DERROR(typeerror, TypeError)
  96. DERROR(urierror, URIError)
  97. #undef DERROR
  98. void jsB_initerror(js_State *J)
  99. {
  100. js_pushobject(J, J->Error_prototype);
  101. {
  102. jsB_props(J, "name", "Error");
  103. jsB_propf(J, "Error.prototype.toString", Ep_toString, 0);
  104. jsB_props(J, "message", "");
  105. js_newcfunction(J, Ep_get_stack, "stack", 0);
  106. js_pushnull(J);
  107. js_defaccessor(J, -3, "stack", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
  108. }
  109. js_newcconstructor(J, jsB_Error, jsB_Error, "Error", 1);
  110. js_defglobal(J, "Error", JS_DONTENUM);
  111. #define IERROR(NAME) \
  112. js_pushobject(J, J->NAME##_prototype); \
  113. jsB_props(J, "name", Q(NAME)); \
  114. js_newcconstructor(J, jsB_##NAME, jsB_##NAME, Q(NAME), 1); \
  115. js_defglobal(J, Q(NAME), JS_DONTENUM);
  116. IERROR(EvalError);
  117. IERROR(RangeError);
  118. IERROR(ReferenceError);
  119. IERROR(SyntaxError);
  120. IERROR(TypeError);
  121. IERROR(URIError);
  122. #undef IERROR
  123. }