debugger_marshalls.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 (int i = 0; i < frames.size(); i++) {
  38. arr.push_back(frames[i].file);
  39. arr.push_back(frames[i].line);
  40. arr.push_back(frames[i].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. Variant var = value;
  65. if (value.get_type() == Variant::OBJECT && value.get_validated_object() == nullptr) {
  66. var = Variant();
  67. }
  68. int len = 0;
  69. Error err = encode_variant(var, nullptr, len, true);
  70. if (err != OK) {
  71. ERR_PRINT("Failed to encode variant.");
  72. }
  73. if (len > max_size) {
  74. arr.push_back(Variant());
  75. } else {
  76. arr.push_back(var);
  77. }
  78. return arr;
  79. }
  80. bool DebuggerMarshalls::ScriptStackVariable::deserialize(const Array &p_arr) {
  81. CHECK_SIZE(p_arr, 3, "ScriptStackVariable");
  82. name = p_arr[0];
  83. type = p_arr[1];
  84. value = p_arr[2];
  85. CHECK_END(p_arr, 3, "ScriptStackVariable");
  86. return true;
  87. }
  88. Array DebuggerMarshalls::OutputError::serialize() {
  89. Array arr;
  90. arr.push_back(hr);
  91. arr.push_back(min);
  92. arr.push_back(sec);
  93. arr.push_back(msec);
  94. arr.push_back(source_file);
  95. arr.push_back(source_func);
  96. arr.push_back(source_line);
  97. arr.push_back(error);
  98. arr.push_back(error_descr);
  99. arr.push_back(warning);
  100. unsigned int size = callstack.size();
  101. const ScriptLanguage::StackInfo *r = callstack.ptr();
  102. arr.push_back(size * 3);
  103. for (int i = 0; i < callstack.size(); i++) {
  104. arr.push_back(r[i].file);
  105. arr.push_back(r[i].func);
  106. arr.push_back(r[i].line);
  107. }
  108. return arr;
  109. }
  110. bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {
  111. CHECK_SIZE(p_arr, 11, "OutputError");
  112. hr = p_arr[0];
  113. min = p_arr[1];
  114. sec = p_arr[2];
  115. msec = p_arr[3];
  116. source_file = p_arr[4];
  117. source_func = p_arr[5];
  118. source_line = p_arr[6];
  119. error = p_arr[7];
  120. error_descr = p_arr[8];
  121. warning = p_arr[9];
  122. unsigned int stack_size = p_arr[10];
  123. CHECK_SIZE(p_arr, stack_size, "OutputError");
  124. int idx = 11;
  125. callstack.resize(stack_size / 3);
  126. ScriptLanguage::StackInfo *w = callstack.ptrw();
  127. for (unsigned int i = 0; i < stack_size / 3; i++) {
  128. w[i].file = p_arr[idx];
  129. w[i].func = p_arr[idx + 1];
  130. w[i].line = p_arr[idx + 2];
  131. idx += 3;
  132. }
  133. CHECK_END(p_arr, idx, "OutputError");
  134. return true;
  135. }