8 static void jsB_stacktrace(js_State *J, int skip)
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;
17 snprintf(buf, sizeof buf, "\n\t%s:%d: in function '%s'", file, line, name);
19 snprintf(buf, sizeof buf, "\n\t%s: in function '%s'", file, name);
20 js_pushstring(J, buf);
21 if (n < J->tracetop - skip)
26 static void Ep_toString(js_State *J)
29 const char *name = "Error";
30 const char *message = "";
32 if (!js_isobject(J, -1))
33 js_typeerror(J, "not an object");
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);
40 snprintf(buf, sizeof buf, "%s: %s", name, message);
41 js_pushstring(J, buf);
43 if (js_hasproperty(J, 0, "stackTrace"))
47 static int jsB_ErrorX(js_State *J, js_Object *prototype)
49 unsigned int top = js_gettop(J);
50 js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
52 js_pushstring(J, js_tostring(J, 1));
53 js_setproperty(J, -2, "message");
56 js_setproperty(J, -2, "stackTrace");
60 static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
62 js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
63 js_pushstring(J, message);
64 js_setproperty(J, -2, "message");
66 js_setproperty(J, -2, "stackTrace");
69 #define DERROR(name, Name) \
70 static void jsB_##Name(js_State *J) { \
71 jsB_ErrorX(J, J->Name##_prototype); \
73 void js_new##name(js_State *J, const char *s) { \
74 js_newerrorx(J, s, J->Name##_prototype); \
76 void js_##name(js_State *J, const char *fmt, ...) { \
80 vsnprintf(buf, sizeof buf, fmt, ap); \
82 js_newerrorx(J, buf, J->Name##_prototype); \
87 DERROR(evalerror, EvalError)
88 DERROR(rangeerror, RangeError)
89 DERROR(referenceerror, ReferenceError)
90 DERROR(syntaxerror, SyntaxError)
91 DERROR(typeerror, TypeError)
92 DERROR(urierror, URIError)
96 void jsB_initerror(js_State *J)
98 js_pushobject(J, J->Error_prototype);
100 jsB_props(J, "name", "Error");
101 jsB_props(J, "message", "an error has occurred");
102 jsB_propf(J, "toString", Ep_toString, 0);
104 js_newcconstructor(J, jsB_Error, jsB_Error, "Error", 1);
105 js_defglobal(J, "Error", JS_DONTENUM);
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);
115 IERROR(ReferenceError);