gdscript_function.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. HashMap<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()->mutex);
  123. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  124. }
  125. #endif
  126. }
  127. GDScriptFunction::~GDScriptFunction() {
  128. get_script()->member_functions.erase(name);
  129. for (int i = 0; i < lambdas.size(); i++) {
  130. memdelete(lambdas[i]);
  131. }
  132. for (int i = 0; i < argument_types.size(); i++) {
  133. argument_types.write[i].script_type_ref = Ref<Script>();
  134. }
  135. return_type.script_type_ref = Ref<Script>();
  136. #ifdef DEBUG_ENABLED
  137. MutexLock lock(GDScriptLanguage::get_singleton()->mutex);
  138. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  139. #endif
  140. }
  141. /////////////////////
  142. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  143. Variant arg;
  144. r_error.error = Callable::CallError::CALL_OK;
  145. if (p_argcount == 0) {
  146. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  147. r_error.argument = 1;
  148. return Variant();
  149. } else if (p_argcount == 1) {
  150. //noooneee
  151. } else if (p_argcount == 2) {
  152. arg = *p_args[0];
  153. } else {
  154. Array extra_args;
  155. for (int i = 0; i < p_argcount - 1; i++) {
  156. extra_args.push_back(*p_args[i]);
  157. }
  158. arg = extra_args;
  159. }
  160. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  161. if (self.is_null()) {
  162. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  163. r_error.argument = p_argcount - 1;
  164. r_error.expected = Variant::OBJECT;
  165. return Variant();
  166. }
  167. return resume(arg);
  168. }
  169. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  170. if (function == nullptr) {
  171. return false;
  172. }
  173. if (p_extended_check) {
  174. MutexLock lock(GDScriptLanguage::get_singleton()->mutex);
  175. // Script gone?
  176. if (!scripts_list.in_list()) {
  177. return false;
  178. }
  179. // Class instance gone? (if not static function)
  180. if (state.instance && !instances_list.in_list()) {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  187. ERR_FAIL_COND_V(!function, Variant());
  188. {
  189. MutexLock lock(GDScriptLanguage::singleton->mutex);
  190. if (!scripts_list.in_list()) {
  191. #ifdef DEBUG_ENABLED
  192. ERR_FAIL_V_MSG(Variant(), "Resumed function '" + state.function_name + "()' after await, but script is gone. At script: " + state.script_path + ":" + itos(state.line));
  193. #else
  194. return Variant();
  195. #endif
  196. }
  197. if (state.instance && !instances_list.in_list()) {
  198. #ifdef DEBUG_ENABLED
  199. 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));
  200. #else
  201. return Variant();
  202. #endif
  203. }
  204. // Do these now to avoid locking again after the call
  205. scripts_list.remove_from_list();
  206. instances_list.remove_from_list();
  207. }
  208. state.result = p_arg;
  209. Callable::CallError err;
  210. Variant ret = function->call(nullptr, nullptr, 0, err, &state);
  211. bool completed = true;
  212. // If the return value is a GDScriptFunctionState reference,
  213. // then the function did await again after resuming.
  214. if (ret.is_ref_counted()) {
  215. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  216. if (gdfs && gdfs->function == function) {
  217. completed = false;
  218. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  219. }
  220. }
  221. function = nullptr; //cleaned up;
  222. state.result = Variant();
  223. if (completed) {
  224. if (first_state.is_valid()) {
  225. first_state->emit_signal(SNAME("completed"), ret);
  226. } else {
  227. emit_signal(SNAME("completed"), ret);
  228. }
  229. #ifdef DEBUG_ENABLED
  230. if (EngineDebugger::is_active()) {
  231. GDScriptLanguage::get_singleton()->exit_function();
  232. }
  233. _clear_stack();
  234. #endif
  235. }
  236. return ret;
  237. }
  238. void GDScriptFunctionState::_clear_stack() {
  239. if (state.stack_size) {
  240. Variant *stack = (Variant *)state.stack.ptr();
  241. // The first 3 are special addresses and not copied to the state, so we skip them here.
  242. for (int i = 3; i < state.stack_size; i++) {
  243. stack[i].~Variant();
  244. }
  245. state.stack_size = 0;
  246. }
  247. }
  248. void GDScriptFunctionState::_bind_methods() {
  249. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  250. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  251. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  252. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  253. }
  254. GDScriptFunctionState::GDScriptFunctionState() :
  255. scripts_list(this),
  256. instances_list(this) {
  257. }
  258. GDScriptFunctionState::~GDScriptFunctionState() {
  259. {
  260. MutexLock lock(GDScriptLanguage::singleton->mutex);
  261. scripts_list.remove_from_list();
  262. instances_list.remove_from_list();
  263. }
  264. }