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