gdscript_function.h 21 KB

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