forked from flier/pyv8
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathException.h
More file actions
200 lines (153 loc) · 6.52 KB
/
Copy pathException.h
File metadata and controls
200 lines (153 loc) · 6.52 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once
#include <cassert>
#include <stdexcept>
#include <boost/shared_ptr.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include "Config.h"
#include "Utils.h"
#define BEGIN_HANDLE_PYTHON_EXCEPTION try
#define END_HANDLE_PYTHON_EXCEPTION \
catch (const std::exception& ex) { v8::Isolate::GetCurrent()->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), ex.what()))); } \
catch (const py::error_already_set&) { CPythonObject::ThrowIf(v8::Isolate::GetCurrent()); } \
catch (...) { v8::Isolate::GetCurrent()->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "unknown exception"))); }
#define BEGIN_HANDLE_JAVASCRIPT_EXCEPTION v8::TryCatch try_catch;
#define END_HANDLE_JAVASCRIPT_EXCEPTION if (try_catch.HasCaught()) CJavascriptException::ThrowIf(v8::Isolate::GetCurrent(), try_catch);
class CJavascriptException;
class CJavascriptTimeoutException;
struct ExceptionTranslator
{
static void Translate(CJavascriptException const& ex);
static void TranslateTimeout(CJavascriptTimeoutException const& ex);
static void *Convertible(PyObject* obj);
static void Construct(PyObject* obj, py::converter::rvalue_from_python_stage1_data* data);
};
class CJavascriptStackTrace;
class CJavascriptStackFrame;
typedef boost::shared_ptr<CJavascriptStackTrace> CJavascriptStackTracePtr;
typedef boost::shared_ptr<CJavascriptStackFrame> CJavascriptStackFramePtr;
class CJavascriptStackTrace
{
v8::Isolate *m_isolate;
v8::Persistent<v8::StackTrace> m_st;
public:
CJavascriptStackTrace(v8::Isolate *isolate, v8::Handle<v8::StackTrace> st)
: m_isolate(isolate), m_st(isolate, st)
{
}
CJavascriptStackTrace(const CJavascriptStackTrace& st)
: m_isolate(st.m_isolate)
{
v8::HandleScope handle_scope(m_isolate);
m_st.Reset(m_isolate, st.Handle());
}
v8::Handle<v8::StackTrace> Handle() const { return v8::Local<v8::StackTrace>::New(m_isolate, m_st); }
int GetFrameCount() const { v8::HandleScope handle_scope(m_isolate); return Handle()->GetFrameCount(); }
CJavascriptStackFramePtr GetFrame(size_t idx) const;
static CJavascriptStackTracePtr GetCurrentStackTrace(v8::Isolate *isolate, int frame_limit,
v8::StackTrace::StackTraceOptions options = v8::StackTrace::kOverview);
void Dump(std::ostream& os) const;
class FrameIterator
: public boost::iterator_facade<FrameIterator, CJavascriptStackFramePtr const, boost::forward_traversal_tag, CJavascriptStackFramePtr>
{
CJavascriptStackTrace *m_st;
size_t m_idx;
public:
FrameIterator(CJavascriptStackTrace *st, size_t idx)
: m_st(st), m_idx(idx)
{
}
void increment() { m_idx++; }
bool equal(FrameIterator const& other) const { return m_st == other.m_st && m_idx == other.m_idx; }
reference dereference() const { return m_st->GetFrame(m_idx); }
};
FrameIterator begin(void) { return FrameIterator(this, 0);}
FrameIterator end(void) { return FrameIterator(this, GetFrameCount());}
};
class CJavascriptStackFrame
{
v8::Isolate *m_isolate;
v8::Persistent<v8::StackFrame> m_frame;
public:
CJavascriptStackFrame(v8::Isolate *isolate, v8::Handle<v8::StackFrame> frame)
: m_isolate(isolate), m_frame(isolate, frame)
{
}
CJavascriptStackFrame(const CJavascriptStackFrame& frame)
: m_isolate(frame.m_isolate)
{
v8::HandleScope handle_scope(m_isolate);
m_frame.Reset(m_isolate, frame.Handle());
}
v8::Handle<v8::StackFrame> Handle() const { return v8::Local<v8::StackFrame>::New(m_isolate, m_frame); }
int GetLineNumber() const { v8::HandleScope handle_scope(m_isolate); return Handle()->GetLineNumber(); }
int GetColumn() const { v8::HandleScope handle_scope(m_isolate); return Handle()->GetColumn(); }
const std::string GetScriptName() const;
const std::string GetFunctionName() const;
bool IsEval() const { v8::HandleScope handle_scope(m_isolate); return Handle()->IsEval(); }
bool IsConstructor() const { v8::HandleScope handle_scope(m_isolate); return Handle()->IsConstructor(); }
};
class CJavascriptException : public std::runtime_error
{
v8::Isolate *m_isolate;
PyObject *m_type;
v8::Persistent<v8::Value> m_exc, m_stack;
v8::Persistent<v8::Message> m_msg;
friend struct ExceptionTranslator;
static const std::string Extract(v8::Isolate *isolate, v8::TryCatch& try_catch);
protected:
CJavascriptException(v8::Isolate *isolate, v8::TryCatch& try_catch, PyObject *type)
: std::runtime_error(Extract(isolate, try_catch)), m_isolate(isolate), m_type(type)
{
v8::HandleScope handle_scope(m_isolate);
m_exc.Reset(m_isolate, try_catch.Exception());
m_stack.Reset(m_isolate, try_catch.StackTrace());
m_msg.Reset(m_isolate, try_catch.Message());
}
public:
CJavascriptException(const std::string& msg, PyObject *type = NULL)
: std::runtime_error(msg), m_isolate(v8::Isolate::GetCurrent()), m_type(type)
{
}
CJavascriptException(const CJavascriptException& ex)
: std::runtime_error(ex.what()), m_isolate(ex.m_isolate), m_type(ex.m_type)
{
v8::HandleScope handle_scope(m_isolate);
m_exc.Reset(m_isolate, ex.Exception());
m_stack.Reset(m_isolate, ex.Stack());
m_msg.Reset(m_isolate, ex.Message());
}
~CJavascriptException() throw()
{
if (!m_exc.IsEmpty()) m_exc.Reset();
if (!m_msg.IsEmpty()) m_msg.Reset();
}
v8::Handle<v8::Value> Exception() const { return v8::Local<v8::Value>::New(m_isolate, m_exc); }
v8::Handle<v8::Value> Stack() const { return v8::Local<v8::Value>::New(m_isolate, m_stack); }
v8::Handle<v8::Message> Message() const { return v8::Local<v8::Message>::New(m_isolate, m_msg); }
const std::string GetName(void);
const std::string GetMessage(void);
const std::string GetScriptName(void);
int GetLineNumber(void);
int GetStartPosition(void);
int GetEndPosition(void);
int GetStartColumn(void);
int GetEndColumn(void);
const std::string GetSourceLine(void);
const std::string GetStackTrace(void);
void PrintCallStack(py::object file);
static void ThrowIf(v8::Isolate *isolate, v8::TryCatch& try_catch);
static void Expose(void);
};
class CJavascriptTimeoutException : public std::runtime_error
{
public:
CJavascriptTimeoutException(const std::string& msg) :
std::runtime_error(msg)
{
}
CJavascriptTimeoutException(const CJavascriptTimeoutException& ex)
: std::runtime_error(ex.what())
{
}
static void Expose(void);
};