gdscript_function.h 19 KB

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