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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::fmt;
use v8_sys as v8;
use context;
use isolate;
use util;
use value;
error_chain! {
errors {
Javascript(message: String, stack_trace: CapturedStackTrace) {
description("Javascript exception")
display("Javascript exception: {}\n{}", message, stack_trace)
}
}
}
#[derive(Clone, Debug)]
pub struct CapturedStackTrace {
pub frames: Vec<CapturedStackFrame>,
}
#[derive(Clone, Debug)]
pub struct CapturedStackFrame {
pub line: u32,
pub column: u32,
pub script_name: Option<String>,
pub function_name: Option<String>,
pub is_eval: bool,
pub is_constructor: bool,
}
#[derive(Debug)]
pub struct Message(isolate::Isolate, v8::MessageRef);
#[derive(Debug)]
pub struct StackTrace(isolate::Isolate, v8::StackTraceRef);
#[derive(Debug)]
pub struct StackFrame(isolate::Isolate, v8::StackFrameRef);
impl Message {
pub fn get(&self, context: &context::Context) -> value::String {
let _g = context.make_current();
unsafe {
value::String::from_raw(&self.0,
util::invoke_ctx(&self.0,
context,
|c| v8::v8_Message_Get(c, self.1))
.unwrap())
}
}
pub fn get_stack_trace(&self) -> StackTrace {
let raw =
unsafe { util::invoke(&self.0, |c| v8::v8_Message_GetStackTrace(c, self.1)).unwrap() };
StackTrace(self.0.clone(), raw)
}
pub unsafe fn from_raw(isolate: &isolate::Isolate, raw: v8::MessageRef) -> Message {
Message(isolate.clone(), raw)
}
}
impl StackTrace {
pub fn get_frames(&self) -> Vec<StackFrame> {
let count =
unsafe { util::invoke(&self.0, |c| v8::v8_StackTrace_GetFrameCount(c, self.1)).unwrap() };
let mut result = Vec::with_capacity(count as usize);
for i in 0..count {
let raw_frame = unsafe {
util::invoke(&self.0, |c| v8::v8_StackTrace_GetFrame(c, self.1, i as u32)).unwrap()
};
let frame = StackFrame(self.0.clone(), raw_frame);
result.push(frame);
}
result
}
pub fn to_captured(&self) -> CapturedStackTrace {
CapturedStackTrace {
frames: self.get_frames()
.iter()
.map(StackFrame::to_captured)
.collect(),
}
}
}
impl StackFrame {
pub fn get_line_number(&self) -> u32 {
unsafe {
util::invoke(&self.0, |c| v8::v8_StackFrame_GetLineNumber(c, self.1)).unwrap() as u32
}
}
pub fn get_column(&self) -> u32 {
unsafe { util::invoke(&self.0, |c| v8::v8_StackFrame_GetColumn(c, self.1)).unwrap() as u32 }
}
pub fn get_script_name(&self) -> Option<value::String> {
unsafe {
let raw = util::invoke(&self.0, |c| v8::v8_StackFrame_GetScriptName(c, self.1)).unwrap();
if raw.is_null() {
None
} else {
Some(value::String::from_raw(&self.0, raw))
}
}
}
pub fn get_function_name(&self) -> value::String {
unsafe {
let raw = util::invoke(&self.0, |c| v8::v8_StackFrame_GetFunctionName(c, self.1)).unwrap();
value::String::from_raw(&self.0, raw)
}
}
pub fn is_eval(&self) -> bool {
unsafe { util::invoke(&self.0, |c| v8::v8_StackFrame_IsEval(c, self.1)).unwrap() }
}
pub fn is_constructor(&self) -> bool {
unsafe { util::invoke(&self.0, |c| v8::v8_StackFrame_IsConstructor(c, self.1)).unwrap() }
}
pub fn to_captured(&self) -> CapturedStackFrame {
let function_name = self.get_function_name().value();
CapturedStackFrame {
line: self.get_line_number(),
column: self.get_column(),
script_name: self.get_script_name().map(|ref s| s.value()),
function_name: if function_name.is_empty() {
None
} else {
Some(function_name)
},
is_eval: self.is_eval(),
is_constructor: self.is_constructor(),
}
}
}
impl fmt::Display for CapturedStackTrace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for frame in self.frames.iter() {
try!(writeln!(f, "{}", frame));
}
Ok(())
}
}
impl fmt::Display for CapturedStackFrame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, " at "));
if self.is_constructor {
try!(write!(f, "new "));
}
if let Some(ref function_name) = self.function_name {
try!(write!(f, "{} (", function_name));
if self.is_eval {
try!(write!(f, "eval "));
}
try!(write!(f,
"{}:{}:{})",
self.script_name.as_ref().map(|n| n.as_str()).unwrap_or("<anon>"),
self.line,
self.column));
} else {
if self.is_eval {
try!(write!(f, "eval "));
}
try!(write!(f,
"{}:{}:{}",
self.script_name.as_ref().map(|n| n.as_str()).unwrap_or("<anon>"),
self.line,
self.column));
}
Ok(())
}
}
reference!(Message, v8::v8_Message_CloneRef, v8::v8_Message_DestroyRef);
reference!(StackTrace,
v8::v8_StackTrace_CloneRef,
v8::v8_StackTrace_DestroyRef);
reference!(StackFrame,
v8::v8_StackFrame_CloneRef,
v8::v8_StackFrame_DestroyRef);