gdscript_function.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**************************************************************************/
  2. /* gdscript_function.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. #ifndef GDSCRIPT_FUNCTION_H
  31. #define GDSCRIPT_FUNCTION_H
  32. #include "gdscript_utility_functions.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/object/script_language.h"
  35. #include "core/os/thread.h"
  36. #include "core/string/string_name.h"
  37. #include "core/templates/pair.h"
  38. #include "core/templates/self_list.h"
  39. #include "core/variant/variant.h"
  40. class GDScriptInstance;
  41. class GDScript;
  42. class GDScriptDataType {
  43. public:
  44. Vector<GDScriptDataType> container_element_types;
  45. enum Kind {
  46. UNINITIALIZED,
  47. BUILTIN,
  48. NATIVE,
  49. SCRIPT,
  50. GDSCRIPT,
  51. };
  52. Kind kind = UNINITIALIZED;
  53. bool has_type = false;
  54. Variant::Type builtin_type = Variant::NIL;
  55. StringName native_type;
  56. Script *script_type = nullptr;
  57. Ref<Script> script_type_ref;
  58. bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
  59. if (!has_type) {
  60. return true; // Can't type check
  61. }
  62. switch (kind) {
  63. case UNINITIALIZED:
  64. break;
  65. case BUILTIN: {
  66. Variant::Type var_type = p_variant.get_type();
  67. bool valid = builtin_type == var_type;
  68. if (valid && builtin_type == Variant::ARRAY && has_container_element_type(0)) {
  69. Array array = p_variant;
  70. if (array.is_typed()) {
  71. GDScriptDataType array_container_type = get_container_element_type(0);
  72. Variant::Type array_builtin_type = (Variant::Type)array.get_typed_builtin();
  73. StringName array_native_type = array.get_typed_class_name();
  74. Ref<Script> array_script_type_ref = array.get_typed_script();
  75. if (array_script_type_ref.is_valid()) {
  76. valid = (array_container_type.kind == SCRIPT || array_container_type.kind == GDSCRIPT) && array_container_type.script_type == array_script_type_ref.ptr();
  77. } else if (array_native_type != StringName()) {
  78. valid = array_container_type.kind == NATIVE && array_container_type.native_type == array_native_type;
  79. } else {
  80. valid = array_container_type.kind == BUILTIN && array_container_type.builtin_type == array_builtin_type;
  81. }
  82. } else {
  83. valid = false;
  84. }
  85. } else if (!valid && p_allow_implicit_conversion) {
  86. valid = Variant::can_convert_strict(var_type, builtin_type);
  87. }
  88. return valid;
  89. } break;
  90. case NATIVE: {
  91. if (p_variant.get_type() == Variant::NIL) {
  92. return true;
  93. }
  94. if (p_variant.get_type() != Variant::OBJECT) {
  95. return false;
  96. }
  97. bool was_freed = false;
  98. Object *obj = p_variant.get_validated_object_with_check(was_freed);
  99. if (!obj) {
  100. return !was_freed;
  101. }
  102. if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) {
  103. return false;
  104. }
  105. return true;
  106. } break;
  107. case SCRIPT:
  108. case GDSCRIPT: {
  109. if (p_variant.get_type() == Variant::NIL) {
  110. return true;
  111. }
  112. if (p_variant.get_type() != Variant::OBJECT) {
  113. return false;
  114. }
  115. bool was_freed = false;
  116. Object *obj = p_variant.get_validated_object_with_check(was_freed);
  117. if (!obj) {
  118. return !was_freed;
  119. }
  120. Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : nullptr;
  121. bool valid = false;
  122. while (base.is_valid()) {
  123. if (base == script_type) {
  124. valid = true;
  125. break;
  126. }
  127. base = base->get_base_script();
  128. }
  129. return valid;
  130. } break;
  131. }
  132. return false;
  133. }
  134. void set_container_element_type(int p_index, const GDScriptDataType &p_element_type) {
  135. ERR_FAIL_COND(p_index < 0);
  136. while (p_index >= container_element_types.size()) {
  137. container_element_types.push_back(GDScriptDataType());
  138. }
  139. container_element_types.write[p_index] = GDScriptDataType(p_element_type);
  140. }
  141. GDScriptDataType get_container_element_type(int p_index) const {
  142. ERR_FAIL_INDEX_V(p_index, container_element_types.size(), GDScriptDataType());
  143. return container_element_types[p_index];
  144. }
  145. GDScriptDataType get_container_element_type_or_variant(int p_index) const {
  146. if (p_index < 0 || p_index >= container_element_types.size()) {
  147. return GDScriptDataType();
  148. }
  149. return container_element_types[p_index];
  150. }
  151. bool has_container_element_type(int p_index) const {
  152. return p_index >= 0 && p_index < container_element_types.size();
  153. }
  154. bool has_container_element_types() const {
  155. return !container_element_types.is_empty();
  156. }
  157. GDScriptDataType() = default;
  158. void operator=(const GDScriptDataType &p_other) {
  159. kind = p_other.kind;
  160. has_type = p_other.has_type;
  161. builtin_type = p_other.builtin_type;
  162. native_type = p_other.native_type;
  163. script_type = p_other.script_type;
  164. script_type_ref = p_other.script_type_ref;
  165. container_element_types = p_other.container_element_types;
  166. }
  167. GDScriptDataType(const GDScriptDataType &p_other) {
  168. *this = p_other;
  169. }
  170. ~GDScriptDataType() {}
  171. };
  172. class GDScriptFunction {
  173. public:
  174. enum Opcode {
  175. OPCODE_OPERATOR,
  176. OPCODE_OPERATOR_VALIDATED,
  177. OPCODE_TYPE_TEST_BUILTIN,
  178. OPCODE_TYPE_TEST_ARRAY,
  179. OPCODE_TYPE_TEST_NATIVE,
  180. OPCODE_TYPE_TEST_SCRIPT,
  181. OPCODE_SET_KEYED,
  182. OPCODE_SET_KEYED_VALIDATED,
  183. OPCODE_SET_INDEXED_VALIDATED,
  184. OPCODE_GET_KEYED,
  185. OPCODE_GET_KEYED_VALIDATED,
  186. OPCODE_GET_INDEXED_VALIDATED,
  187. OPCODE_SET_NAMED,
  188. OPCODE_SET_NAMED_VALIDATED,
  189. OPCODE_GET_NAMED,
  190. OPCODE_GET_NAMED_VALIDATED,
  191. OPCODE_SET_MEMBER,
  192. OPCODE_GET_MEMBER,
  193. OPCODE_SET_STATIC_VARIABLE, // Only for GDScript.
  194. OPCODE_GET_STATIC_VARIABLE, // Only for GDScript.
  195. OPCODE_ASSIGN,
  196. OPCODE_ASSIGN_TRUE,
  197. OPCODE_ASSIGN_FALSE,
  198. OPCODE_ASSIGN_TYPED_BUILTIN,
  199. OPCODE_ASSIGN_TYPED_ARRAY,
  200. OPCODE_ASSIGN_TYPED_NATIVE,
  201. OPCODE_ASSIGN_TYPED_SCRIPT,
  202. OPCODE_CAST_TO_BUILTIN,
  203. OPCODE_CAST_TO_NATIVE,
  204. OPCODE_CAST_TO_SCRIPT,
  205. OPCODE_CONSTRUCT, // Only for basic types!
  206. OPCODE_CONSTRUCT_VALIDATED, // Only for basic types!
  207. OPCODE_CONSTRUCT_ARRAY,
  208. OPCODE_CONSTRUCT_TYPED_ARRAY,
  209. OPCODE_CONSTRUCT_DICTIONARY,
  210. OPCODE_CALL,
  211. OPCODE_CALL_RETURN,
  212. OPCODE_CALL_ASYNC,
  213. OPCODE_CALL_UTILITY,
  214. OPCODE_CALL_UTILITY_VALIDATED,
  215. OPCODE_CALL_GDSCRIPT_UTILITY,
  216. OPCODE_CALL_BUILTIN_TYPE_VALIDATED,
  217. OPCODE_CALL_SELF_BASE,
  218. OPCODE_CALL_METHOD_BIND,
  219. OPCODE_CALL_METHOD_BIND_RET,
  220. OPCODE_CALL_BUILTIN_STATIC,
  221. OPCODE_CALL_NATIVE_STATIC,
  222. OPCODE_CALL_METHOD_BIND_VALIDATED_RETURN,
  223. OPCODE_CALL_METHOD_BIND_VALIDATED_NO_RETURN,
  224. OPCODE_AWAIT,
  225. OPCODE_AWAIT_RESUME,
  226. OPCODE_CREATE_LAMBDA,
  227. OPCODE_CREATE_SELF_LAMBDA,
  228. OPCODE_JUMP,
  229. OPCODE_JUMP_IF,
  230. OPCODE_JUMP_IF_NOT,
  231. OPCODE_JUMP_TO_DEF_ARGUMENT,
  232. OPCODE_JUMP_IF_SHARED,
  233. OPCODE_RETURN,
  234. OPCODE_RETURN_TYPED_BUILTIN,
  235. OPCODE_RETURN_TYPED_ARRAY,
  236. OPCODE_RETURN_TYPED_NATIVE,
  237. OPCODE_RETURN_TYPED_SCRIPT,
  238. OPCODE_ITERATE_BEGIN,
  239. OPCODE_ITERATE_BEGIN_INT,
  240. OPCODE_ITERATE_BEGIN_FLOAT,
  241. OPCODE_ITERATE_BEGIN_VECTOR2,
  242. OPCODE_ITERATE_BEGIN_VECTOR2I,
  243. OPCODE_ITERATE_BEGIN_VECTOR3,
  244. OPCODE_ITERATE_BEGIN_VECTOR3I,
  245. OPCODE_ITERATE_BEGIN_STRING,
  246. OPCODE_ITERATE_BEGIN_DICTIONARY,
  247. OPCODE_ITERATE_BEGIN_ARRAY,
  248. OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY,
  249. OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY,
  250. OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY,
  251. OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY,
  252. OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY,
  253. OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY,
  254. OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY,
  255. OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY,
  256. OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY,
  257. OPCODE_ITERATE_BEGIN_OBJECT,
  258. OPCODE_ITERATE,
  259. OPCODE_ITERATE_INT,
  260. OPCODE_ITERATE_FLOAT,
  261. OPCODE_ITERATE_VECTOR2,
  262. OPCODE_ITERATE_VECTOR2I,
  263. OPCODE_ITERATE_VECTOR3,
  264. OPCODE_ITERATE_VECTOR3I,
  265. OPCODE_ITERATE_STRING,
  266. OPCODE_ITERATE_DICTIONARY,
  267. OPCODE_ITERATE_ARRAY,
  268. OPCODE_ITERATE_PACKED_BYTE_ARRAY,
  269. OPCODE_ITERATE_PACKED_INT32_ARRAY,
  270. OPCODE_ITERATE_PACKED_INT64_ARRAY,
  271. OPCODE_ITERATE_PACKED_FLOAT32_ARRAY,
  272. OPCODE_ITERATE_PACKED_FLOAT64_ARRAY,
  273. OPCODE_ITERATE_PACKED_STRING_ARRAY,
  274. OPCODE_ITERATE_PACKED_VECTOR2_ARRAY,
  275. OPCODE_ITERATE_PACKED_VECTOR3_ARRAY,
  276. OPCODE_ITERATE_PACKED_COLOR_ARRAY,
  277. OPCODE_ITERATE_OBJECT,
  278. OPCODE_STORE_GLOBAL,
  279. OPCODE_STORE_NAMED_GLOBAL,
  280. OPCODE_TYPE_ADJUST_BOOL,
  281. OPCODE_TYPE_ADJUST_INT,
  282. OPCODE_TYPE_ADJUST_FLOAT,
  283. OPCODE_TYPE_ADJUST_STRING,
  284. OPCODE_TYPE_ADJUST_VECTOR2,
  285. OPCODE_TYPE_ADJUST_VECTOR2I,
  286. OPCODE_TYPE_ADJUST_RECT2,
  287. OPCODE_TYPE_ADJUST_RECT2I,
  288. OPCODE_TYPE_ADJUST_VECTOR3,
  289. OPCODE_TYPE_ADJUST_VECTOR3I,
  290. OPCODE_TYPE_ADJUST_TRANSFORM2D,
  291. OPCODE_TYPE_ADJUST_VECTOR4,
  292. OPCODE_TYPE_ADJUST_VECTOR4I,
  293. OPCODE_TYPE_ADJUST_PLANE,
  294. OPCODE_TYPE_ADJUST_QUATERNION,
  295. OPCODE_TYPE_ADJUST_AABB,
  296. OPCODE_TYPE_ADJUST_BASIS,
  297. OPCODE_TYPE_ADJUST_TRANSFORM3D,
  298. OPCODE_TYPE_ADJUST_PROJECTION,
  299. OPCODE_TYPE_ADJUST_COLOR,
  300. OPCODE_TYPE_ADJUST_STRING_NAME,
  301. OPCODE_TYPE_ADJUST_NODE_PATH,
  302. OPCODE_TYPE_ADJUST_RID,
  303. OPCODE_TYPE_ADJUST_OBJECT,
  304. OPCODE_TYPE_ADJUST_CALLABLE,
  305. OPCODE_TYPE_ADJUST_SIGNAL,
  306. OPCODE_TYPE_ADJUST_DICTIONARY,
  307. OPCODE_TYPE_ADJUST_ARRAY,
  308. OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY,
  309. OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY,
  310. OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY,
  311. OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY,
  312. OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY,
  313. OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY,
  314. OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY,
  315. OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY,
  316. OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY,
  317. OPCODE_ASSERT,
  318. OPCODE_BREAKPOINT,
  319. OPCODE_LINE,
  320. OPCODE_END
  321. };
  322. enum Address {
  323. ADDR_BITS = 24,
  324. ADDR_MASK = ((1 << ADDR_BITS) - 1),
  325. ADDR_TYPE_MASK = ~ADDR_MASK,
  326. ADDR_TYPE_STACK = 0,
  327. ADDR_TYPE_CONSTANT = 1,
  328. ADDR_TYPE_MEMBER = 2,
  329. ADDR_TYPE_MAX = 3,
  330. };
  331. enum FixedAddresses {
  332. ADDR_STACK_SELF = 0,
  333. ADDR_STACK_CLASS = 1,
  334. ADDR_STACK_NIL = 2,
  335. FIXED_ADDRESSES_MAX = 3,
  336. ADDR_SELF = ADDR_STACK_SELF | (ADDR_TYPE_STACK << ADDR_BITS),
  337. ADDR_CLASS = ADDR_STACK_CLASS | (ADDR_TYPE_STACK << ADDR_BITS),
  338. ADDR_NIL = ADDR_STACK_NIL | (ADDR_TYPE_STACK << ADDR_BITS),
  339. };
  340. struct StackDebug {
  341. int line;
  342. int pos;
  343. bool added;
  344. StringName identifier;
  345. };
  346. private:
  347. friend class GDScript;
  348. friend class GDScriptCompiler;
  349. friend class GDScriptByteCodeGenerator;
  350. friend class GDScriptLanguage;
  351. StringName name;
  352. StringName source;
  353. bool _static = false;
  354. Vector<GDScriptDataType> argument_types;
  355. GDScriptDataType return_type;
  356. MethodInfo method_info;
  357. Variant rpc_config;
  358. GDScript *_script = nullptr;
  359. int _initial_line = 0;
  360. int _argument_count = 0;
  361. int _stack_size = 0;
  362. int _instruction_args_size = 0;
  363. SelfList<GDScriptFunction> function_list{ this };
  364. mutable Variant nil;
  365. HashMap<int, Variant::Type> temporary_slots;
  366. List<StackDebug> stack_debug;
  367. Vector<int> code;
  368. Vector<int> default_arguments;
  369. Vector<Variant> constants;
  370. Vector<StringName> global_names;
  371. Vector<Variant::ValidatedOperatorEvaluator> operator_funcs;
  372. Vector<Variant::ValidatedSetter> setters;
  373. Vector<Variant::ValidatedGetter> getters;
  374. Vector<Variant::ValidatedKeyedSetter> keyed_setters;
  375. Vector<Variant::ValidatedKeyedGetter> keyed_getters;
  376. Vector<Variant::ValidatedIndexedSetter> indexed_setters;
  377. Vector<Variant::ValidatedIndexedGetter> indexed_getters;
  378. Vector<Variant::ValidatedBuiltInMethod> builtin_methods;
  379. Vector<Variant::ValidatedConstructor> constructors;
  380. Vector<Variant::ValidatedUtilityFunction> utilities;
  381. Vector<GDScriptUtilityFunctions::FunctionPtr> gds_utilities;
  382. Vector<MethodBind *> methods;
  383. Vector<GDScriptFunction *> lambdas;
  384. int _code_size = 0;
  385. int _default_arg_count = 0;
  386. int _constant_count = 0;
  387. int _global_names_count = 0;
  388. int _operator_funcs_count = 0;
  389. int _setters_count = 0;
  390. int _getters_count = 0;
  391. int _keyed_setters_count = 0;
  392. int _keyed_getters_count = 0;
  393. int _indexed_setters_count = 0;
  394. int _indexed_getters_count = 0;
  395. int _builtin_methods_count = 0;
  396. int _constructors_count = 0;
  397. int _utilities_count = 0;
  398. int _gds_utilities_count = 0;
  399. int _methods_count = 0;
  400. int _lambdas_count = 0;
  401. int *_code_ptr = nullptr;
  402. const int *_default_arg_ptr = nullptr;
  403. mutable Variant *_constants_ptr = nullptr;
  404. const StringName *_global_names_ptr = nullptr;
  405. const Variant::ValidatedOperatorEvaluator *_operator_funcs_ptr = nullptr;
  406. const Variant::ValidatedSetter *_setters_ptr = nullptr;
  407. const Variant::ValidatedGetter *_getters_ptr = nullptr;
  408. const Variant::ValidatedKeyedSetter *_keyed_setters_ptr = nullptr;
  409. const Variant::ValidatedKeyedGetter *_keyed_getters_ptr = nullptr;
  410. const Variant::ValidatedIndexedSetter *_indexed_setters_ptr = nullptr;
  411. const Variant::ValidatedIndexedGetter *_indexed_getters_ptr = nullptr;
  412. const Variant::ValidatedBuiltInMethod *_builtin_methods_ptr = nullptr;
  413. const Variant::ValidatedConstructor *_constructors_ptr = nullptr;
  414. const Variant::ValidatedUtilityFunction *_utilities_ptr = nullptr;
  415. const GDScriptUtilityFunctions::FunctionPtr *_gds_utilities_ptr = nullptr;
  416. MethodBind **_methods_ptr = nullptr;
  417. GDScriptFunction **_lambdas_ptr = nullptr;
  418. #ifdef DEBUG_ENABLED
  419. CharString func_cname;
  420. const char *_func_cname = nullptr;
  421. Vector<String> operator_names;
  422. Vector<String> setter_names;
  423. Vector<String> getter_names;
  424. Vector<String> builtin_methods_names;
  425. Vector<String> constructors_names;
  426. Vector<String> utilities_names;
  427. Vector<String> gds_utilities_names;
  428. struct Profile {
  429. StringName signature;
  430. SafeNumeric<uint64_t> call_count;
  431. SafeNumeric<uint64_t> self_time;
  432. SafeNumeric<uint64_t> total_time;
  433. SafeNumeric<uint64_t> frame_call_count;
  434. SafeNumeric<uint64_t> frame_self_time;
  435. SafeNumeric<uint64_t> frame_total_time;
  436. uint64_t last_frame_call_count = 0;
  437. uint64_t last_frame_self_time = 0;
  438. uint64_t last_frame_total_time = 0;
  439. typedef struct NativeProfile {
  440. uint64_t call_count;
  441. uint64_t total_time;
  442. String signature;
  443. } NativeProfile;
  444. HashMap<String, NativeProfile> native_calls;
  445. HashMap<String, NativeProfile> last_native_calls;
  446. } profile;
  447. #endif
  448. _FORCE_INLINE_ String _get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const;
  449. Variant _get_default_variant_for_data_type(const GDScriptDataType &p_data_type);
  450. public:
  451. static constexpr int MAX_CALL_DEPTH = 2048; // Limit to try to avoid crash because of a stack overflow.
  452. struct CallState {
  453. GDScript *script = nullptr;
  454. GDScriptInstance *instance = nullptr;
  455. #ifdef DEBUG_ENABLED
  456. StringName function_name;
  457. String script_path;
  458. #endif
  459. Vector<uint8_t> stack;
  460. int stack_size = 0;
  461. uint32_t alloca_size = 0;
  462. int ip = 0;
  463. int line = 0;
  464. int defarg = 0;
  465. Variant result;
  466. };
  467. _FORCE_INLINE_ StringName get_name() const { return name; }
  468. _FORCE_INLINE_ StringName get_source() const { return source; }
  469. _FORCE_INLINE_ GDScript *get_script() const { return _script; }
  470. _FORCE_INLINE_ bool is_static() const { return _static; }
  471. _FORCE_INLINE_ MethodInfo get_method_info() const { return method_info; }
  472. _FORCE_INLINE_ int get_argument_count() const { return _argument_count; }
  473. _FORCE_INLINE_ Variant get_rpc_config() const { return rpc_config; }
  474. _FORCE_INLINE_ int get_max_stack_size() const { return _stack_size; }
  475. Variant get_constant(int p_idx) const;
  476. StringName get_global_name(int p_idx) const;
  477. Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state = nullptr);
  478. void debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const;
  479. #ifdef DEBUG_ENABLED
  480. void _profile_native_call(uint64_t p_t_taken, const String &p_function_name, const String &p_instance_class_name = String());
  481. void disassemble(const Vector<String> &p_code_lines) const;
  482. #endif
  483. GDScriptFunction();
  484. ~GDScriptFunction();
  485. };
  486. class GDScriptFunctionState : public RefCounted {
  487. GDCLASS(GDScriptFunctionState, RefCounted);
  488. friend class GDScriptFunction;
  489. GDScriptFunction *function = nullptr;
  490. GDScriptFunction::CallState state;
  491. Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  492. Ref<GDScriptFunctionState> first_state;
  493. SelfList<GDScriptFunctionState> scripts_list;
  494. SelfList<GDScriptFunctionState> instances_list;
  495. protected:
  496. static void _bind_methods();
  497. public:
  498. bool is_valid(bool p_extended_check = false) const;
  499. Variant resume(const Variant &p_arg = Variant());
  500. void _clear_stack();
  501. void _clear_connections();
  502. GDScriptFunctionState();
  503. ~GDScriptFunctionState();
  504. };
  505. #endif // GDSCRIPT_FUNCTION_H