gdscript_function.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gdscript_function.h"
  31. #include "gdscript.h"
  32. const int *GDScriptFunction::get_code() const {
  33. return _code_ptr;
  34. }
  35. int GDScriptFunction::get_code_size() const {
  36. return _code_size;
  37. }
  38. Variant GDScriptFunction::get_constant(int p_idx) const {
  39. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  40. return constants[p_idx];
  41. }
  42. StringName GDScriptFunction::get_global_name(int p_idx) const {
  43. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  44. return global_names[p_idx];
  45. }
  46. int GDScriptFunction::get_default_argument_count() const {
  47. return _default_arg_count;
  48. }
  49. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  50. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  51. return default_arguments[p_idx];
  52. }
  53. GDScriptDataType GDScriptFunction::get_return_type() const {
  54. return return_type;
  55. }
  56. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  57. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  58. return argument_types[p_idx];
  59. }
  60. StringName GDScriptFunction::get_name() const {
  61. return name;
  62. }
  63. int GDScriptFunction::get_max_stack_size() const {
  64. return _stack_size;
  65. }
  66. struct _GDFKC {
  67. int order = 0;
  68. List<int> pos;
  69. };
  70. struct _GDFKCS {
  71. int order = 0;
  72. StringName id;
  73. int pos = 0;
  74. bool operator<(const _GDFKCS &p_r) const {
  75. return order < p_r.order;
  76. }
  77. };
  78. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const {
  79. int oc = 0;
  80. Map<StringName, _GDFKC> sdmap;
  81. for (const StackDebug &sd : stack_debug) {
  82. if (sd.line > p_line) {
  83. break;
  84. }
  85. if (sd.added) {
  86. if (!sdmap.has(sd.identifier)) {
  87. _GDFKC d;
  88. d.order = oc++;
  89. d.pos.push_back(sd.pos);
  90. sdmap[sd.identifier] = d;
  91. } else {
  92. sdmap[sd.identifier].pos.push_back(sd.pos);
  93. }
  94. } else {
  95. ERR_CONTINUE(!sdmap.has(sd.identifier));
  96. sdmap[sd.identifier].pos.pop_back();
  97. if (sdmap[sd.identifier].pos.is_empty()) {
  98. sdmap.erase(sd.identifier);
  99. }
  100. }
  101. }
  102. List<_GDFKCS> stackpositions;
  103. for (const KeyValue<StringName, _GDFKC> &E : sdmap) {
  104. _GDFKCS spp;
  105. spp.id = E.key;
  106. spp.order = E.value.order;
  107. spp.pos = E.value.pos.back()->get();
  108. stackpositions.push_back(spp);
  109. }
  110. stackpositions.sort();
  111. for (_GDFKCS &E : stackpositions) {
  112. Pair<StringName, int> p;
  113. p.first = E.id;
  114. p.second = E.pos;
  115. r_stackvars->push_back(p);
  116. }
  117. }
  118. GDScriptFunction::GDScriptFunction() {
  119. name = "<anonymous>";
  120. #ifdef DEBUG_ENABLED
  121. {
  122. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  123. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  124. }
  125. #endif
  126. }
  127. GDScriptFunction::~GDScriptFunction() {
  128. for (int i = 0; i < lambdas.size(); i++) {
  129. memdelete(lambdas[i]);
  130. }
  131. #ifdef DEBUG_ENABLED
  132. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  133. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  134. #endif
  135. }
  136. /////////////////////
  137. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  138. Variant arg;
  139. r_error.error = Callable::CallError::CALL_OK;
  140. if (p_argcount == 0) {
  141. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  142. r_error.argument = 1;
  143. return Variant();
  144. } else if (p_argcount == 1) {
  145. //noooneee
  146. } else if (p_argcount == 2) {
  147. arg = *p_args[0];
  148. } else {
  149. Array extra_args;
  150. for (int i = 0; i < p_argcount - 1; i++) {
  151. extra_args.push_back(*p_args[i]);
  152. }
  153. arg = extra_args;
  154. }
  155. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  156. if (self.is_null()) {
  157. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  158. r_error.argument = p_argcount - 1;
  159. r_error.expected = Variant::OBJECT;
  160. return Variant();
  161. }
  162. return resume(arg);
  163. }
  164. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  165. if (function == nullptr) {
  166. return false;
  167. }
  168. if (p_extended_check) {
  169. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  170. // Script gone?
  171. if (!scripts_list.in_list()) {
  172. return false;
  173. }
  174. // Class instance gone? (if not static function)
  175. if (state.instance && !instances_list.in_list()) {
  176. return false;
  177. }
  178. }
  179. return true;
  180. }
  181. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  182. ERR_FAIL_COND_V(!function, Variant());
  183. {
  184. MutexLock lock(GDScriptLanguage::singleton->lock);
  185. if (!scripts_list.in_list()) {
  186. #ifdef DEBUG_ENABLED
  187. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
  188. #else
  189. return Variant();
  190. #endif
  191. }
  192. if (state.instance && !instances_list.in_list()) {
  193. #ifdef DEBUG_ENABLED
  194. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but class instance is gone. At script: " + state.script_path + ":" + itos(state.line));
  195. #else
  196. return Variant();
  197. #endif
  198. }
  199. // Do these now to avoid locking again after the call
  200. scripts_list.remove_from_list();
  201. instances_list.remove_from_list();
  202. }
  203. state.result = p_arg;
  204. Callable::CallError err;
  205. Variant ret = function->call(nullptr, nullptr, 0, err, &state);
  206. bool completed = true;
  207. // If the return value is a GDScriptFunctionState reference,
  208. // then the function did await again after resuming.
  209. if (ret.is_ref_counted()) {
  210. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  211. if (gdfs && gdfs->function == function) {
  212. completed = false;
  213. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  214. }
  215. }
  216. function = nullptr; //cleaned up;
  217. state.result = Variant();
  218. if (completed) {
  219. if (first_state.is_valid()) {
  220. first_state->emit_signal(SNAME("completed"), ret);
  221. } else {
  222. emit_signal(SNAME("completed"), ret);
  223. }
  224. #ifdef DEBUG_ENABLED
  225. if (EngineDebugger::is_active()) {
  226. GDScriptLanguage::get_singleton()->exit_function();
  227. }
  228. #endif
  229. }
  230. return ret;
  231. }
  232. void GDScriptFunctionState::_clear_stack() {
  233. if (state.stack_size) {
  234. Variant *stack = (Variant *)state.stack.ptr();
  235. for (int i = 0; i < state.stack_size; i++) {
  236. stack[i].~Variant();
  237. }
  238. state.stack_size = 0;
  239. }
  240. }
  241. void GDScriptFunctionState::_bind_methods() {
  242. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  243. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  244. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  245. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  246. }
  247. GDScriptFunctionState::GDScriptFunctionState() :
  248. scripts_list(this),
  249. instances_list(this) {
  250. }
  251. GDScriptFunctionState::~GDScriptFunctionState() {
  252. _clear_stack();
  253. {
  254. MutexLock lock(GDScriptLanguage::singleton->lock);
  255. scripts_list.remove_from_list();
  256. instances_list.remove_from_list();
  257. }
  258. }