comparison mupdf-source/thirdparty/mujs/jserror.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 #include "jsi.h"
2
3 #define QQ(X) #X
4 #define Q(X) QQ(X)
5
6 static int jsB_stacktrace(js_State *J, int skip)
7 {
8 char buf[256];
9 int n = J->tracetop - skip;
10 if (n <= 0)
11 return 0;
12 for (; n > 0; --n) {
13 const char *name = J->trace[n].name;
14 const char *file = J->trace[n].file;
15 int line = J->trace[n].line;
16 if (line > 0) {
17 if (name[0])
18 snprintf(buf, sizeof buf, "\n\tat %s (%s:%d)", name, file, line);
19 else
20 snprintf(buf, sizeof buf, "\n\tat %s:%d", file, line);
21 } else
22 snprintf(buf, sizeof buf, "\n\tat %s (%s)", name, file);
23 js_pushstring(J, buf);
24 if (n < J->tracetop - skip)
25 js_concat(J);
26 }
27 return 1;
28 }
29
30 static void Ep_toString(js_State *J)
31 {
32 const char *name = "Error";
33 const char *message = "";
34
35 if (!js_isobject(J, -1))
36 js_typeerror(J, "not an object");
37
38 if (js_hasproperty(J, 0, "name"))
39 name = js_tostring(J, -1);
40 if (js_hasproperty(J, 0, "message"))
41 message = js_tostring(J, -1);
42
43 if (name[0] == 0)
44 js_pushstring(J, message);
45 else if (message[0] == 0)
46 js_pushstring(J, name);
47 else {
48 js_pushstring(J, name);
49 js_pushstring(J, ": ");
50 js_concat(J);
51 js_pushstring(J, message);
52 js_concat(J);
53 }
54 }
55
56 static int jsB_ErrorX(js_State *J, js_Object *prototype)
57 {
58 js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
59 if (js_isdefined(J, 1)) {
60 js_pushstring(J, js_tostring(J, 1));
61 js_defproperty(J, -2, "message", JS_DONTENUM);
62 }
63 if (jsB_stacktrace(J, 1))
64 js_defproperty(J, -2, "stackTrace", JS_DONTENUM);
65 return 1;
66 }
67
68 static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
69 {
70 js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
71 js_pushstring(J, message);
72 js_setproperty(J, -2, "message");
73 if (jsB_stacktrace(J, 0))
74 js_setproperty(J, -2, "stackTrace");
75 }
76
77 #define DERROR(name, Name) \
78 static void jsB_##Name(js_State *J) { \
79 jsB_ErrorX(J, J->Name##_prototype); \
80 } \
81 void js_new##name(js_State *J, const char *s) { \
82 js_newerrorx(J, s, J->Name##_prototype); \
83 } \
84 void js_##name(js_State *J, const char *fmt, ...) { \
85 va_list ap; \
86 char buf[256]; \
87 va_start(ap, fmt); \
88 vsnprintf(buf, sizeof buf, fmt, ap); \
89 va_end(ap); \
90 js_newerrorx(J, buf, J->Name##_prototype); \
91 js_throw(J); \
92 }
93
94 DERROR(error, Error)
95 DERROR(evalerror, EvalError)
96 DERROR(rangeerror, RangeError)
97 DERROR(referenceerror, ReferenceError)
98 DERROR(syntaxerror, SyntaxError)
99 DERROR(typeerror, TypeError)
100 DERROR(urierror, URIError)
101
102 #undef DERROR
103
104 void jsB_initerror(js_State *J)
105 {
106 js_pushobject(J, J->Error_prototype);
107 {
108 jsB_props(J, "name", "Error");
109 jsB_propf(J, "Error.prototype.toString", Ep_toString, 0);
110 }
111 js_newcconstructor(J, jsB_Error, jsB_Error, "Error", 1);
112 js_defglobal(J, "Error", JS_DONTENUM);
113
114 #define IERROR(NAME) \
115 js_pushobject(J, J->NAME##_prototype); \
116 jsB_props(J, "name", Q(NAME)); \
117 js_newcconstructor(J, jsB_##NAME, jsB_##NAME, Q(NAME), 1); \
118 js_defglobal(J, Q(NAME), JS_DONTENUM);
119
120 IERROR(EvalError);
121 IERROR(RangeError);
122 IERROR(ReferenceError);
123 IERROR(SyntaxError);
124 IERROR(TypeError);
125 IERROR(URIError);
126
127 #undef IERROR
128 }