gdscript_function.h 19 KB

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