script_backtrace.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**************************************************************************/
  2. /* script_backtrace.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/object/ref_counted.h"
  32. class ScriptLanguage;
  33. class ScriptBacktrace : public RefCounted {
  34. GDCLASS(ScriptBacktrace, RefCounted);
  35. struct StackVariable {
  36. String name;
  37. Variant value;
  38. };
  39. struct StackFrame {
  40. LocalVector<StackVariable> local_variables;
  41. LocalVector<StackVariable> member_variables;
  42. String function;
  43. String file;
  44. int line = 0;
  45. };
  46. LocalVector<StackFrame> stack_frames;
  47. LocalVector<StackVariable> global_variables;
  48. String language_name;
  49. static void _store_variables(const List<String> &p_names, const List<Variant> &p_values, LocalVector<StackVariable> &r_variables);
  50. protected:
  51. static void _bind_methods();
  52. public:
  53. ScriptBacktrace() = default;
  54. ScriptBacktrace(ScriptLanguage *p_language, bool p_include_variables = false);
  55. String get_language_name() const { return language_name; }
  56. bool is_empty() const { return stack_frames.is_empty(); }
  57. int get_frame_count() const { return stack_frames.size(); }
  58. String get_frame_function(int p_index) const;
  59. String get_frame_file(int p_index) const;
  60. int get_frame_line(int p_index) const;
  61. int get_global_variable_count() const { return global_variables.size(); }
  62. String get_global_variable_name(int p_variable_index) const;
  63. Variant get_global_variable_value(int p_variable_index) const;
  64. int get_local_variable_count(int p_frame_index) const;
  65. String get_local_variable_name(int p_frame_index, int p_variable_index) const;
  66. Variant get_local_variable_value(int p_frame_index, int p_variable_index) const;
  67. int get_member_variable_count(int p_frame_index) const;
  68. String get_member_variable_name(int p_frame_index, int p_variable_index) const;
  69. Variant get_member_variable_value(int p_frame_index, int p_variable_index) const;
  70. String format(int p_indent_all = 0, int p_indent_frames = 4) const;
  71. virtual String _to_string() override { return format(); }
  72. };