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