-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy patherror.c
87 lines (70 loc) · 2.11 KB
/
error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright 2009 Paul J. Davis <[email protected]>
*
* This file is part of the python-spidermonkey package released
* under the MIT license.
*
*/
#include "spidermonkey.h"
#include "frameobject.h" // Python
#include "traceback.h" // Python
void
add_frame(const char* srcfile, const char* funcname, int linenum)
{
PyObject* src = NULL;
PyObject* func = NULL;
PyObject* glbl = NULL;
PyObject* tpl = NULL;
PyObject* str = NULL;
PyCodeObject* code = NULL;
PyFrameObject* frame = NULL;
src = PyBytes_FromString(srcfile);
if(src == NULL) goto error;
func = PyBytes_FromString(funcname);
if(func == NULL) goto error;
glbl = PyModule_GetDict(SpidermonkeyModule);
if(glbl == NULL) goto error;
tpl = PyTuple_New(0);
if(tpl == NULL) goto error;
str = PyBytes_FromString("");
if(str == NULL) goto error;
code = PyCode_NewEmpty(
src, /*co_filename*/
func, /*co_name*/
linenum /*co_firstlineno*/
);
if(code == NULL) goto error;
frame = PyFrame_New(PyThreadState_Get(), code, glbl, NULL);
if(frame == NULL) goto error;
frame->f_lineno = linenum;
PyTraceBack_Here(frame);
goto success;
error:
success:
Py_XDECREF(func);
Py_XDECREF(src);
Py_XDECREF(tpl);
Py_XDECREF(str);
Py_XDECREF(code);
Py_XDECREF(frame);
}
void
report_error_cb(JSContext* cx, const char* message, JSErrorReport* report)
{
/* Subtle note about JSREPORT_EXCEPTION: it triggers whenever exceptions
* are raised, even if they're caught and the Mozilla docs say you can
* ignore it.
*/
/* TODO What should we do about warnings? A callback somehow? */
if (report->flags & JSREPORT_WARNING)
return;
const char* srcfile = report->filename;
const char* mesg = message;
if(srcfile == NULL) srcfile = "<JavaScript>";
if(mesg == NULL) mesg = "Unknown JavaScript execution error";
if(!PyErr_Occurred())
{
PyErr_SetString(JSError, mesg);
}
add_frame(srcfile, "JavaScript code", report->lineno);
}