rusty_v8/
exception.rs

1#![allow(non_snake_case)]
2
3use crate::isolate::Isolate;
4use crate::support::int;
5use crate::Context;
6use crate::HandleScope;
7use crate::Local;
8use crate::Message;
9use crate::StackFrame;
10use crate::StackTrace;
11use crate::String;
12use crate::Value;
13
14extern "C" {
15  fn v8__Message__Get(this: *const Message) -> *const String;
16  fn v8__Message__GetSourceLine(
17    this: *const Message,
18    context: *const Context,
19  ) -> *const String;
20  fn v8__Message__GetScriptResourceName(this: *const Message) -> *const Value;
21  fn v8__Message__GetLineNumber(
22    this: *const Message,
23    context: *const Context,
24  ) -> int;
25  fn v8__Message__GetStartPosition(this: *const Message) -> int;
26  fn v8__Message__GetEndPosition(this: *const Message) -> int;
27  fn v8__Message__GetWasmFunctionIndex(this: *const Message) -> int;
28  fn v8__Message__ErrorLevel(this: *const Message) -> int;
29  fn v8__Message__GetStartColumn(this: *const Message) -> int;
30  fn v8__Message__GetEndColumn(this: *const Message) -> int;
31  fn v8__Message__IsSharedCrossOrigin(this: *const Message) -> bool;
32  fn v8__Message__IsOpaque(this: *const Message) -> bool;
33  fn v8__Message__GetStackTrace(this: *const Message) -> *const StackTrace;
34
35  fn v8__StackTrace__GetFrameCount(this: *const StackTrace) -> int;
36  fn v8__StackTrace__GetFrame(
37    this: *const StackTrace,
38    isolate: *mut Isolate,
39    index: u32,
40  ) -> *const StackFrame;
41
42  fn v8__StackFrame__GetLineNumber(this: *const StackFrame) -> int;
43  fn v8__StackFrame__GetColumn(this: *const StackFrame) -> int;
44  fn v8__StackFrame__GetScriptId(this: *const StackFrame) -> int;
45  fn v8__StackFrame__GetScriptName(this: *const StackFrame) -> *const String;
46  fn v8__StackFrame__GetScriptNameOrSourceURL(
47    this: *const StackFrame,
48  ) -> *const String;
49  fn v8__StackFrame__GetFunctionName(this: *const StackFrame) -> *const String;
50  fn v8__StackFrame__IsEval(this: *const StackFrame) -> bool;
51  fn v8__StackFrame__IsConstructor(this: *const StackFrame) -> bool;
52  fn v8__StackFrame__IsWasm(this: *const StackFrame) -> bool;
53  fn v8__StackFrame__IsUserJavaScript(this: *const StackFrame) -> bool;
54
55  fn v8__Exception__Error(message: *const String) -> *const Value;
56  fn v8__Exception__RangeError(message: *const String) -> *const Value;
57  fn v8__Exception__ReferenceError(message: *const String) -> *const Value;
58  fn v8__Exception__SyntaxError(message: *const String) -> *const Value;
59  fn v8__Exception__TypeError(message: *const String) -> *const Value;
60
61  fn v8__Exception__CreateMessage(
62    isolate: *mut Isolate,
63    exception: *const Value,
64  ) -> *const Message;
65  fn v8__Exception__GetStackTrace(exception: *const Value)
66    -> *const StackTrace;
67}
68
69impl StackTrace {
70  /// Returns the number of StackFrames.
71  pub fn get_frame_count(&self) -> usize {
72    unsafe { v8__StackTrace__GetFrameCount(self) as usize }
73  }
74
75  /// Returns a StackFrame at a particular index.
76  pub fn get_frame<'s>(
77    &self,
78    scope: &mut HandleScope<'s>,
79    index: usize,
80  ) -> Option<Local<'s, StackFrame>> {
81    unsafe {
82      scope.cast_local(|sd| {
83        v8__StackTrace__GetFrame(self, sd.get_isolate_ptr(), index as u32)
84      })
85    }
86  }
87}
88
89impl StackFrame {
90  /// Returns the number, 1-based, of the line for the associated function call.
91  /// This method will return Message::kNoLineNumberInfo if it is unable to
92  /// retrieve the line number, or if kLineNumber was not passed as an option
93  /// when capturing the StackTrace.
94  pub fn get_line_number(&self) -> usize {
95    unsafe { v8__StackFrame__GetLineNumber(self) as usize }
96  }
97
98  /// Returns the 1-based column offset on the line for the associated function
99  /// call.
100  /// This method will return Message::kNoColumnInfo if it is unable to retrieve
101  /// the column number, or if kColumnOffset was not passed as an option when
102  /// capturing the StackTrace.
103  pub fn get_column(&self) -> usize {
104    unsafe { v8__StackFrame__GetColumn(self) as usize }
105  }
106
107  /// Returns the id of the script for the function for this StackFrame.
108  /// This method will return Message::kNoScriptIdInfo if it is unable to
109  /// retrieve the script id, or if kScriptId was not passed as an option when
110  /// capturing the StackTrace.
111  pub fn get_script_id(&self) -> usize {
112    unsafe { v8__StackFrame__GetScriptId(self) as usize }
113  }
114
115  /// Returns the name of the resource that contains the script for the
116  /// function for this StackFrame.
117  pub fn get_script_name<'s>(
118    &self,
119    scope: &mut HandleScope<'s>,
120  ) -> Option<Local<'s, String>> {
121    unsafe { scope.cast_local(|_| v8__StackFrame__GetScriptName(self)) }
122  }
123
124  /// Returns the name of the resource that contains the script for the
125  /// function for this StackFrame or sourceURL value if the script name
126  /// is undefined and its source ends with //# sourceURL=... string or
127  /// deprecated //@ sourceURL=... string.
128  pub fn get_script_name_or_source_url<'s>(
129    &self,
130    scope: &mut HandleScope<'s>,
131  ) -> Option<Local<'s, String>> {
132    unsafe {
133      scope.cast_local(|_| v8__StackFrame__GetScriptNameOrSourceURL(self))
134    }
135  }
136
137  /// Returns the name of the function associated with this stack frame.
138  pub fn get_function_name<'s>(
139    &self,
140    scope: &mut HandleScope<'s>,
141  ) -> Option<Local<'s, String>> {
142    unsafe { scope.cast_local(|_| v8__StackFrame__GetFunctionName(self)) }
143  }
144
145  /// Returns whether or not the associated function is compiled via a call to
146  /// eval().
147  pub fn is_eval(&self) -> bool {
148    unsafe { v8__StackFrame__IsEval(self) }
149  }
150
151  /// Returns whether or not the associated function is called as a
152  /// constructor via "new".
153  pub fn is_constructor(&self) -> bool {
154    unsafe { v8__StackFrame__IsConstructor(self) }
155  }
156
157  /// Returns whether or not the associated functions is defined in wasm.
158  pub fn is_wasm(&self) -> bool {
159    unsafe { v8__StackFrame__IsWasm(self) }
160  }
161
162  /// Returns whether or not the associated function is defined by the user.
163  pub fn is_user_javascript(&self) -> bool {
164    unsafe { v8__StackFrame__IsUserJavaScript(self) }
165  }
166}
167
168impl Message {
169  pub fn get<'s>(&self, scope: &mut HandleScope<'s>) -> Local<'s, String> {
170    unsafe { scope.cast_local(|_| v8__Message__Get(self)) }.unwrap()
171  }
172
173  /// Exception stack trace. By default stack traces are not captured for
174  /// uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
175  /// to change this option.
176  pub fn get_stack_trace<'s>(
177    &self,
178    scope: &mut HandleScope<'s>,
179  ) -> Option<Local<'s, StackTrace>> {
180    unsafe { scope.cast_local(|_| v8__Message__GetStackTrace(self)) }
181  }
182
183  pub fn get_source_line<'s>(
184    &self,
185    scope: &mut HandleScope<'s>,
186  ) -> Option<Local<'s, String>> {
187    unsafe {
188      scope.cast_local(|sd| {
189        v8__Message__GetSourceLine(self, sd.get_current_context())
190      })
191    }
192  }
193
194  /// Returns the resource name for the script from where the function causing
195  /// the error originates.
196  pub fn get_script_resource_name<'s>(
197    &self,
198    scope: &mut HandleScope<'s>,
199  ) -> Option<Local<'s, Value>> {
200    unsafe { scope.cast_local(|_| v8__Message__GetScriptResourceName(self)) }
201  }
202
203  /// Returns the number, 1-based, of the line where the error occurred.
204  pub fn get_line_number(&self, scope: &mut HandleScope) -> Option<usize> {
205    let i = unsafe {
206      v8__Message__GetLineNumber(self, &*scope.get_current_context())
207    };
208    if i < 0 {
209      None
210    } else {
211      Some(i as usize)
212    }
213  }
214
215  /// Returns the index within the script of the first character where
216  /// the error occurred.
217  pub fn get_start_position(&self) -> int {
218    unsafe { v8__Message__GetStartPosition(self) }
219  }
220
221  /// Returns the index within the script of the last character where
222  /// the error occurred.
223  pub fn get_end_position(&self) -> int {
224    unsafe { v8__Message__GetEndPosition(self) }
225  }
226
227  /// Returns the Wasm function index where the error occurred. Returns -1 if
228  /// message is not from a Wasm script.
229  pub fn get_wasm_function_index(&self) -> int {
230    unsafe { v8__Message__GetWasmFunctionIndex(self) }
231  }
232
233  /// Returns the error level of the message.
234  pub fn error_level(&self) -> int {
235    unsafe { v8__Message__ErrorLevel(self) }
236  }
237
238  /// Returns the index within the line of the first character where
239  /// the error occurred.
240  pub fn get_start_column(&self) -> usize {
241    unsafe { v8__Message__GetStartColumn(self) as usize }
242  }
243
244  /// Returns the index within the line of the last character where
245  /// the error occurred.
246  pub fn get_end_column(&self) -> usize {
247    unsafe { v8__Message__GetEndColumn(self) as usize }
248  }
249
250  /// Passes on the value set by the embedder when it fed the script from which
251  /// this Message was generated to V8.
252  pub fn is_shared_cross_origin(&self) -> bool {
253    unsafe { v8__Message__IsSharedCrossOrigin(self) }
254  }
255
256  pub fn is_opaque(&self) -> bool {
257    unsafe { v8__Message__IsOpaque(self) }
258  }
259}
260
261/// Create new error objects by calling the corresponding error object
262/// constructor with the message.
263#[derive(Debug)]
264pub struct Exception;
265
266impl Exception {
267  pub fn error<'s>(
268    scope: &mut HandleScope<'s>,
269    message: Local<String>,
270  ) -> Local<'s, Value> {
271    Self::new_error_with(scope, message, v8__Exception__Error)
272  }
273
274  pub fn range_error<'s>(
275    scope: &mut HandleScope<'s>,
276    message: Local<String>,
277  ) -> Local<'s, Value> {
278    Self::new_error_with(scope, message, v8__Exception__RangeError)
279  }
280
281  pub fn reference_error<'s>(
282    scope: &mut HandleScope<'s>,
283    message: Local<String>,
284  ) -> Local<'s, Value> {
285    Self::new_error_with(scope, message, v8__Exception__ReferenceError)
286  }
287
288  pub fn syntax_error<'s>(
289    scope: &mut HandleScope<'s>,
290    message: Local<String>,
291  ) -> Local<'s, Value> {
292    Self::new_error_with(scope, message, v8__Exception__SyntaxError)
293  }
294
295  pub fn type_error<'s>(
296    scope: &mut HandleScope<'s>,
297    message: Local<String>,
298  ) -> Local<'s, Value> {
299    Self::new_error_with(scope, message, v8__Exception__TypeError)
300  }
301
302  /// Internal helper to make the above error constructors less repetitive.
303  fn new_error_with<'s>(
304    scope: &mut HandleScope<'s>,
305    message: Local<String>,
306    contructor: unsafe extern "C" fn(*const String) -> *const Value,
307  ) -> Local<'s, Value> {
308    unsafe {
309      scope.enter();
310      let error = scope.cast_local(|_| (contructor)(&*message)).unwrap();
311      scope.exit();
312      error
313    }
314  }
315
316  /// Creates an error message for the given exception.
317  /// Will try to reconstruct the original stack trace from the exception value,
318  /// or capture the current stack trace if not available.
319  pub fn create_message<'s>(
320    scope: &mut HandleScope<'s>,
321    exception: Local<Value>,
322  ) -> Local<'s, Message> {
323    unsafe {
324      scope.cast_local(|sd| {
325        v8__Exception__CreateMessage(sd.get_isolate_ptr(), &*exception)
326      })
327    }
328    .unwrap()
329  }
330
331  /// Returns the original stack trace that was captured at the creation time
332  /// of a given exception, or an empty handle if not available.
333  pub fn get_stack_trace<'s>(
334    scope: &mut HandleScope<'s>,
335    exception: Local<Value>,
336  ) -> Option<Local<'s, StackTrace>> {
337    unsafe { scope.cast_local(|_| v8__Exception__GetStackTrace(&*exception)) }
338  }
339}