diff --git a/src/binding.cc b/src/binding.cc index 6894bbb2..f1274fb1 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -546,6 +546,10 @@ int v8__Message__GetLineNumber(const v8::Message& self, v8::Context* context) { } } +v8::StackTrace* v8__Message__GetStackTrace(v8::Message& self) { + return local_to_ptr(self.GetStackTrace()); +} + int v8__Message__GetStartPosition(const v8::Message& self) { return self.GetStartPosition(); } @@ -668,6 +672,45 @@ int v8__StackTrace__GetFrameCount(v8::StackTrace* self) { return self->GetFrameCount(); } +v8::StackFrame* v8__StackTrace__GetFrame(v8::StackTrace& self, + v8::Isolate* isolate, uint32_t index) { + return local_to_ptr(self.GetFrame(isolate, index)); +} + +int v8__StackFrame__GetLineNumber(v8::StackFrame& self) { + return self.GetLineNumber(); +} + +int v8__StackFrame__GetColumn(v8::StackFrame& self) { return self.GetColumn(); } + +int v8__StackFrame__GetScriptId(v8::StackFrame& self) { + return self.GetScriptId(); +} + +v8::String* v8__StackFrame__GetScriptName(v8::StackFrame& self) { + return local_to_ptr(self.GetScriptName()); +} + +v8::String* v8__StackFrame__GetScriptNameOrSourceURL(v8::StackFrame& self) { + return local_to_ptr(self.GetScriptNameOrSourceURL()); +} + +v8::String* v8__StackFrame__GetFunctionName(v8::StackFrame& self) { + return local_to_ptr(self.GetFunctionName()); +} + +bool v8__StackFrame__IsEval(v8::StackFrame& self) { return self.IsEval(); } + +bool v8__StackFrame__IsConstructor(v8::StackFrame& self) { + return self.IsConstructor(); +} + +bool v8__StackFrame__IsWasm(v8::StackFrame& self) { return self.IsWasm(); } + +bool v8__StackFrame__IsUserJavaScript(v8::StackFrame& self) { + return self.IsUserJavaScript(); +} + void v8__TryCatch__CONSTRUCT(uninit_t& buf, v8::Isolate* isolate) { construct_in_place(buf, isolate); diff --git a/src/exception.rs b/src/exception.rs index 4fbe7733..395f9723 100644 --- a/src/exception.rs +++ b/src/exception.rs @@ -29,8 +29,27 @@ extern "C" { fn v8__Message__GetEndColumn(message: &Message) -> int; fn v8__Message__IsSharedCrossOrigin(message: &Message) -> bool; fn v8__Message__IsOpaque(message: &Message) -> bool; + fn v8__Message__GetStackTrace(message: &Message) -> *mut StackTrace; - fn v8__StackTrace__GetFrameCount(stack_trace: *mut StackTrace) -> int; + fn v8__StackTrace__GetFrameCount(self_: &StackTrace) -> int; + fn v8__StackTrace__GetFrame( + self_: &StackTrace, + isolate: *mut Isolate, + index: u32, + ) -> *mut StackFrame; + + fn v8__StackFrame__GetLineNumber(self_: &StackFrame) -> int; + fn v8__StackFrame__GetColumn(self_: &StackFrame) -> int; + fn v8__StackFrame__GetScriptId(self_: &StackFrame) -> int; + fn v8__StackFrame__GetScriptName(self_: &StackFrame) -> *mut String; + fn v8__StackFrame__GetScriptNameOrSourceURL( + self_: &StackFrame, + ) -> *mut String; + fn v8__StackFrame__GetFunctionName(self_: &StackFrame) -> *mut String; + fn v8__StackFrame__IsEval(self_: &StackFrame) -> bool; + fn v8__StackFrame__IsConstructor(self_: &StackFrame) -> bool; + fn v8__StackFrame__IsWasm(self_: &StackFrame) -> bool; + fn v8__StackFrame__IsUserJavaScript(self_: &StackFrame) -> bool; fn v8__Exception__RangeError(message: *mut String) -> *mut Value; fn v8__Exception__ReferenceError(message: *mut String) -> *mut Value; @@ -54,9 +73,102 @@ pub struct StackTrace(Opaque); impl StackTrace { /// Returns the number of StackFrames. - pub fn get_frame_count(&mut self) -> usize { + pub fn get_frame_count(&self) -> usize { unsafe { v8__StackTrace__GetFrameCount(self) as usize } } + + /// Returns a StackFrame at a particular index. + pub fn get_frame<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + index: usize, + ) -> Option> { + let isolate = scope.isolate(); + unsafe { + Local::from_raw(v8__StackTrace__GetFrame(self, isolate, index as u32)) + } + } +} + +/// A single JavaScript stack frame. +#[repr(C)] +pub struct StackFrame(Opaque); + +impl StackFrame { + /// Returns the number, 1-based, of the line for the associate function call. + /// This method will return Message::kNoLineNumberInfo if it is unable to + /// retrieve the line number, or if kLineNumber was not passed as an option + /// when capturing the StackTrace. + pub fn get_line_number(&self) -> usize { + unsafe { v8__StackFrame__GetLineNumber(self) as usize } + } + + /// Returns the 1-based column offset on the line for the associated function + /// call. + /// This method will return Message::kNoColumnInfo if it is unable to retrieve + /// the column number, or if kColumnOffset was not passed as an option when + /// capturing the StackTrace. + pub fn get_column(&self) -> usize { + unsafe { v8__StackFrame__GetColumn(self) as usize } + } + + /// Returns the id of the script for the function for this StackFrame. + /// This method will return Message::kNoScriptIdInfo if it is unable to + /// retrieve the script id, or if kScriptId was not passed as an option when + /// capturing the StackTrace. + pub fn get_script_id(&self) -> usize { + unsafe { v8__StackFrame__GetScriptId(self) as usize } + } + + /// Returns the name of the resource that contains the script for the + /// function for this StackFrame. + pub fn get_script_name<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + ) -> Option> { + unsafe { scope.to_local(v8__StackFrame__GetScriptName(self)) } + } + + /// Returns the name of the resource that contains the script for the + /// function for this StackFrame or sourceURL value if the script name + /// is undefined and its source ends with //# sourceURL=... string or + /// deprecated //@ sourceURL=... string. + pub fn get_script_name_or_source_url<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + ) -> Option> { + unsafe { scope.to_local(v8__StackFrame__GetScriptNameOrSourceURL(self)) } + } + + /// Returns the name of the function associated with this stack frame. + pub fn get_function_name<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + ) -> Option> { + unsafe { scope.to_local(v8__StackFrame__GetFunctionName(self)) } + } + + /// Returns whether or not the associated function is compiled via a call to + /// eval(). + pub fn is_eval(&self) -> bool { + unsafe { v8__StackFrame__IsEval(self) } + } + + /// Returns whether or not the associated function is called as a + /// constructor via "new". + pub fn is_constructor(&self) -> bool { + unsafe { v8__StackFrame__IsConstructor(self) } + } + + /// Returns whether or not the associated functions is defined in wasm. + pub fn is_wasm(&self) -> bool { + unsafe { v8__StackFrame__IsWasm(self) } + } + + /// Returns whether or not the associated function is defined by the user. + pub fn is_user_javascript(&self) -> bool { + unsafe { v8__StackFrame__IsUserJavaScript(self) } + } } /// An error message. @@ -72,6 +184,16 @@ impl Message { unsafe { v8__Message__GetIsolate(self) } } + /// Exception stack trace. By default stack traces are not captured for + /// uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows + /// to change this option. + pub fn get_stack_trace<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + ) -> Option> { + unsafe { scope.to_local(v8__Message__GetStackTrace(self)) } + } + pub fn get_source_line<'s>( &self, scope: &mut impl ToLocal<'s>, diff --git a/tests/test_api.rs b/tests/test_api.rs index f9a12d9a..035913b2 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -469,6 +469,19 @@ fn add_message_listener() { assert_eq!(message.get_end_column(), 1); assert!(!message.is_shared_cross_origin()); assert!(!message.is_opaque()); + let stack_trace = message.get_stack_trace(scope).unwrap(); + assert_eq!(1, stack_trace.get_frame_count()); + let frame = stack_trace.get_frame(scope, 0).unwrap(); + assert_eq!(1, frame.get_line_number()); + assert_eq!(1, frame.get_column()); + assert_eq!(3, frame.get_script_id()); + assert!(frame.get_script_name(scope).is_none()); + assert!(frame.get_script_name_or_source_url(scope).is_none()); + assert!(frame.get_function_name(scope).is_none()); + assert_eq!(false, frame.is_eval()); + assert_eq!(false, frame.is_constructor()); + assert_eq!(false, frame.is_wasm()); + assert_eq!(true, frame.is_user_javascript()); CALL_COUNT.fetch_add(1, Ordering::SeqCst); } isolate.add_message_listener(check_message_0);