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 pub fn get_frame_count(&self) -> usize {
72 unsafe { v8__StackTrace__GetFrameCount(self) as usize }
73 }
74
75 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 pub fn get_line_number(&self) -> usize {
95 unsafe { v8__StackFrame__GetLineNumber(self) as usize }
96 }
97
98 pub fn get_column(&self) -> usize {
104 unsafe { v8__StackFrame__GetColumn(self) as usize }
105 }
106
107 pub fn get_script_id(&self) -> usize {
112 unsafe { v8__StackFrame__GetScriptId(self) as usize }
113 }
114
115 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 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 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 pub fn is_eval(&self) -> bool {
148 unsafe { v8__StackFrame__IsEval(self) }
149 }
150
151 pub fn is_constructor(&self) -> bool {
154 unsafe { v8__StackFrame__IsConstructor(self) }
155 }
156
157 pub fn is_wasm(&self) -> bool {
159 unsafe { v8__StackFrame__IsWasm(self) }
160 }
161
162 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 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 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 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 pub fn get_start_position(&self) -> int {
218 unsafe { v8__Message__GetStartPosition(self) }
219 }
220
221 pub fn get_end_position(&self) -> int {
224 unsafe { v8__Message__GetEndPosition(self) }
225 }
226
227 pub fn get_wasm_function_index(&self) -> int {
230 unsafe { v8__Message__GetWasmFunctionIndex(self) }
231 }
232
233 pub fn error_level(&self) -> int {
235 unsafe { v8__Message__ErrorLevel(self) }
236 }
237
238 pub fn get_start_column(&self) -> usize {
241 unsafe { v8__Message__GetStartColumn(self) as usize }
242 }
243
244 pub fn get_end_column(&self) -> usize {
247 unsafe { v8__Message__GetEndColumn(self) as usize }
248 }
249
250 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#[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 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 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 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}