2
0

gdscript_function.h 21 KB

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