debugger_marshalls.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**************************************************************************/
  2. /* debugger_marshalls.cpp */
  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. #include "debugger_marshalls.h"
  31. #include "core/io/marshalls.h"
  32. #define CHECK_SIZE(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() < (uint32_t)(expected), false, String("Malformed ") + what + " message from script debugger, message too short. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  33. #define CHECK_END(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() > (uint32_t)expected, false, String("Malformed ") + what + " message from script debugger, message too long. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  34. Array DebuggerMarshalls::ScriptStackDump::serialize() {
  35. Array arr;
  36. arr.push_back(frames.size() * 3);
  37. for (const ScriptLanguage::StackInfo &frame : frames) {
  38. arr.push_back(frame.file);
  39. arr.push_back(frame.line);
  40. arr.push_back(frame.func);
  41. }
  42. return arr;
  43. }
  44. bool DebuggerMarshalls::ScriptStackDump::deserialize(const Array &p_arr) {
  45. CHECK_SIZE(p_arr, 1, "ScriptStackDump");
  46. uint32_t size = p_arr[0];
  47. CHECK_SIZE(p_arr, size, "ScriptStackDump");
  48. int idx = 1;
  49. for (uint32_t i = 0; i < size / 3; i++) {
  50. ScriptLanguage::StackInfo sf;
  51. sf.file = p_arr[idx];
  52. sf.line = p_arr[idx + 1];
  53. sf.func = p_arr[idx + 2];
  54. frames.push_back(sf);
  55. idx += 3;
  56. }
  57. CHECK_END(p_arr, idx, "ScriptStackDump");
  58. return true;
  59. }
  60. Array DebuggerMarshalls::ScriptStackVariable::serialize(int max_size) {
  61. Array arr;
  62. arr.push_back(name);
  63. arr.push_back(type);
  64. arr.push_back(value.get_type());
  65. Variant var = value;
  66. if (value.get_type() == Variant::OBJECT && value.get_validated_object() == nullptr) {
  67. var = Variant();
  68. }
  69. int len = 0;
  70. Error err = encode_variant(var, nullptr, len, false);
  71. if (err != OK) {
  72. ERR_PRINT("Failed to encode variant.");
  73. }
  74. if (len > max_size) {
  75. arr.push_back(Variant());
  76. } else {
  77. arr.push_back(var);
  78. }
  79. return arr;
  80. }
  81. bool DebuggerMarshalls::ScriptStackVariable::deserialize(const Array &p_arr) {
  82. CHECK_SIZE(p_arr, 4, "ScriptStackVariable");
  83. name = p_arr[0];
  84. type = p_arr[1];
  85. var_type = p_arr[2];
  86. value = p_arr[3];
  87. CHECK_END(p_arr, 4, "ScriptStackVariable");
  88. return true;
  89. }
  90. Array DebuggerMarshalls::OutputError::serialize() {
  91. Array arr;
  92. arr.push_back(hr);
  93. arr.push_back(min);
  94. arr.push_back(sec);
  95. arr.push_back(msec);
  96. arr.push_back(source_file);
  97. arr.push_back(source_func);
  98. arr.push_back(source_line);
  99. arr.push_back(error);
  100. arr.push_back(error_descr);
  101. arr.push_back(warning);
  102. unsigned int size = callstack.size();
  103. const ScriptLanguage::StackInfo *r = callstack.ptr();
  104. arr.push_back(size * 3);
  105. for (int i = 0; i < callstack.size(); i++) {
  106. arr.push_back(r[i].file);
  107. arr.push_back(r[i].func);
  108. arr.push_back(r[i].line);
  109. }
  110. return arr;
  111. }
  112. bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {
  113. CHECK_SIZE(p_arr, 11, "OutputError");
  114. hr = p_arr[0];
  115. min = p_arr[1];
  116. sec = p_arr[2];
  117. msec = p_arr[3];
  118. source_file = p_arr[4];
  119. source_func = p_arr[5];
  120. source_line = p_arr[6];
  121. error = p_arr[7];
  122. error_descr = p_arr[8];
  123. warning = p_arr[9];
  124. unsigned int stack_size = p_arr[10];
  125. CHECK_SIZE(p_arr, stack_size, "OutputError");
  126. int idx = 11;
  127. callstack.resize(stack_size / 3);
  128. ScriptLanguage::StackInfo *w = callstack.ptrw();
  129. for (unsigned int i = 0; i < stack_size / 3; i++) {
  130. w[i].file = p_arr[idx];
  131. w[i].func = p_arr[idx + 1];
  132. w[i].line = p_arr[idx + 2];
  133. idx += 3;
  134. }
  135. CHECK_END(p_arr, idx, "OutputError");
  136. return true;
  137. }