gdscript_function.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*************************************************************************/
  2. /* gdscript_function.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/reference.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. struct GDScriptDataType {
  43. enum Kind {
  44. UNINITIALIZED,
  45. BUILTIN,
  46. NATIVE,
  47. SCRIPT,
  48. GDSCRIPT,
  49. };
  50. Kind kind = UNINITIALIZED;
  51. bool has_type = false;
  52. Variant::Type builtin_type = Variant::NIL;
  53. StringName native_type;
  54. Script *script_type = nullptr;
  55. Ref<Script> script_type_ref;
  56. bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
  57. if (!has_type) {
  58. return true; // Can't type check
  59. }
  60. switch (kind) {
  61. case UNINITIALIZED:
  62. break;
  63. case BUILTIN: {
  64. Variant::Type var_type = p_variant.get_type();
  65. bool valid = builtin_type == var_type;
  66. if (!valid && p_allow_implicit_conversion) {
  67. valid = Variant::can_convert_strict(var_type, builtin_type);
  68. }
  69. return valid;
  70. } break;
  71. case NATIVE: {
  72. if (p_variant.get_type() == Variant::NIL) {
  73. return true;
  74. }
  75. if (p_variant.get_type() != Variant::OBJECT) {
  76. return false;
  77. }
  78. Object *obj = p_variant.get_validated_object();
  79. if (!obj) {
  80. return false;
  81. }
  82. if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) {
  83. // Try with underscore prefix
  84. StringName underscore_native_type = "_" + native_type;
  85. if (!ClassDB::is_parent_class(obj->get_class_name(), underscore_native_type)) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. } break;
  91. case SCRIPT:
  92. case GDSCRIPT: {
  93. if (p_variant.get_type() == Variant::NIL) {
  94. return true;
  95. }
  96. if (p_variant.get_type() != Variant::OBJECT) {
  97. return false;
  98. }
  99. Object *obj = p_variant.get_validated_object();
  100. if (!obj) {
  101. return false;
  102. }
  103. Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : nullptr;
  104. bool valid = false;
  105. while (base.is_valid()) {
  106. if (base == script_type) {
  107. valid = true;
  108. break;
  109. }
  110. base = base->get_base_script();
  111. }
  112. return valid;
  113. } break;
  114. }
  115. return false;
  116. }
  117. operator PropertyInfo() const {
  118. PropertyInfo info;
  119. if (has_type) {
  120. switch (kind) {
  121. case UNINITIALIZED:
  122. break;
  123. case BUILTIN: {
  124. info.type = builtin_type;
  125. } break;
  126. case NATIVE: {
  127. info.type = Variant::OBJECT;
  128. info.class_name = native_type;
  129. } break;
  130. case SCRIPT:
  131. case GDSCRIPT: {
  132. info.type = Variant::OBJECT;
  133. info.class_name = script_type->get_instance_base_type();
  134. } break;
  135. }
  136. } else {
  137. info.type = Variant::NIL;
  138. info.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  139. }
  140. return info;
  141. }
  142. GDScriptDataType() {}
  143. };
  144. class GDScriptFunction {
  145. public:
  146. enum Opcode {
  147. OPCODE_OPERATOR,
  148. OPCODE_OPERATOR_VALIDATED,
  149. OPCODE_EXTENDS_TEST,
  150. OPCODE_IS_BUILTIN,
  151. OPCODE_SET_KEYED,
  152. OPCODE_SET_KEYED_VALIDATED,
  153. OPCODE_SET_INDEXED_VALIDATED,
  154. OPCODE_GET_KEYED,
  155. OPCODE_GET_KEYED_VALIDATED,
  156. OPCODE_GET_INDEXED_VALIDATED,
  157. OPCODE_SET_NAMED,
  158. OPCODE_SET_NAMED_VALIDATED,
  159. OPCODE_GET_NAMED,
  160. OPCODE_GET_NAMED_VALIDATED,
  161. OPCODE_SET_MEMBER,
  162. OPCODE_GET_MEMBER,
  163. OPCODE_ASSIGN,
  164. OPCODE_ASSIGN_TRUE,
  165. OPCODE_ASSIGN_FALSE,
  166. OPCODE_ASSIGN_TYPED_BUILTIN,
  167. OPCODE_ASSIGN_TYPED_NATIVE,
  168. OPCODE_ASSIGN_TYPED_SCRIPT,
  169. OPCODE_CAST_TO_BUILTIN,
  170. OPCODE_CAST_TO_NATIVE,
  171. OPCODE_CAST_TO_SCRIPT,
  172. OPCODE_CONSTRUCT, // Only for basic types!
  173. OPCODE_CONSTRUCT_VALIDATED, // Only for basic types!
  174. OPCODE_CONSTRUCT_ARRAY,
  175. OPCODE_CONSTRUCT_DICTIONARY,
  176. OPCODE_CALL,
  177. OPCODE_CALL_RETURN,
  178. OPCODE_CALL_ASYNC,
  179. OPCODE_CALL_UTILITY,
  180. OPCODE_CALL_UTILITY_VALIDATED,
  181. OPCODE_CALL_GDSCRIPT_UTILITY,
  182. OPCODE_CALL_BUILTIN_TYPE_VALIDATED,
  183. OPCODE_CALL_SELF_BASE,
  184. OPCODE_CALL_METHOD_BIND,
  185. OPCODE_CALL_METHOD_BIND_RET,
  186. // ptrcall have one instruction per return type.
  187. OPCODE_CALL_PTRCALL_NO_RETURN,
  188. OPCODE_CALL_PTRCALL_BOOL,
  189. OPCODE_CALL_PTRCALL_INT,
  190. OPCODE_CALL_PTRCALL_FLOAT,
  191. OPCODE_CALL_PTRCALL_STRING,
  192. OPCODE_CALL_PTRCALL_VECTOR2,
  193. OPCODE_CALL_PTRCALL_VECTOR2I,
  194. OPCODE_CALL_PTRCALL_RECT2,
  195. OPCODE_CALL_PTRCALL_RECT2I,
  196. OPCODE_CALL_PTRCALL_VECTOR3,
  197. OPCODE_CALL_PTRCALL_VECTOR3I,
  198. OPCODE_CALL_PTRCALL_TRANSFORM2D,
  199. OPCODE_CALL_PTRCALL_PLANE,
  200. OPCODE_CALL_PTRCALL_QUAT,
  201. OPCODE_CALL_PTRCALL_AABB,
  202. OPCODE_CALL_PTRCALL_BASIS,
  203. OPCODE_CALL_PTRCALL_TRANSFORM,
  204. OPCODE_CALL_PTRCALL_COLOR,
  205. OPCODE_CALL_PTRCALL_STRING_NAME,
  206. OPCODE_CALL_PTRCALL_NODE_PATH,
  207. OPCODE_CALL_PTRCALL_RID,
  208. OPCODE_CALL_PTRCALL_OBJECT,
  209. OPCODE_CALL_PTRCALL_CALLABLE,
  210. OPCODE_CALL_PTRCALL_SIGNAL,
  211. OPCODE_CALL_PTRCALL_DICTIONARY,
  212. OPCODE_CALL_PTRCALL_ARRAY,
  213. OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY,
  214. OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY,
  215. OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY,
  216. OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY,
  217. OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY,
  218. OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY,
  219. OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY,
  220. OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY,
  221. OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY,
  222. OPCODE_AWAIT,
  223. OPCODE_AWAIT_RESUME,
  224. OPCODE_JUMP,
  225. OPCODE_JUMP_IF,
  226. OPCODE_JUMP_IF_NOT,
  227. OPCODE_JUMP_TO_DEF_ARGUMENT,
  228. OPCODE_RETURN,
  229. OPCODE_ITERATE_BEGIN,
  230. OPCODE_ITERATE_BEGIN_INT,
  231. OPCODE_ITERATE_BEGIN_FLOAT,
  232. OPCODE_ITERATE_BEGIN_VECTOR2,
  233. OPCODE_ITERATE_BEGIN_VECTOR2I,
  234. OPCODE_ITERATE_BEGIN_VECTOR3,
  235. OPCODE_ITERATE_BEGIN_VECTOR3I,
  236. OPCODE_ITERATE_BEGIN_STRING,
  237. OPCODE_ITERATE_BEGIN_DICTIONARY,
  238. OPCODE_ITERATE_BEGIN_ARRAY,
  239. OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY,
  240. OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY,
  241. OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY,
  242. OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY,
  243. OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY,
  244. OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY,
  245. OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY,
  246. OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY,
  247. OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY,
  248. OPCODE_ITERATE_BEGIN_OBJECT,
  249. OPCODE_ITERATE,
  250. OPCODE_ITERATE_INT,
  251. OPCODE_ITERATE_FLOAT,
  252. OPCODE_ITERATE_VECTOR2,
  253. OPCODE_ITERATE_VECTOR2I,
  254. OPCODE_ITERATE_VECTOR3,
  255. OPCODE_ITERATE_VECTOR3I,
  256. OPCODE_ITERATE_STRING,
  257. OPCODE_ITERATE_DICTIONARY,
  258. OPCODE_ITERATE_ARRAY,
  259. OPCODE_ITERATE_PACKED_BYTE_ARRAY,
  260. OPCODE_ITERATE_PACKED_INT32_ARRAY,
  261. OPCODE_ITERATE_PACKED_INT64_ARRAY,
  262. OPCODE_ITERATE_PACKED_FLOAT32_ARRAY,
  263. OPCODE_ITERATE_PACKED_FLOAT64_ARRAY,
  264. OPCODE_ITERATE_PACKED_STRING_ARRAY,
  265. OPCODE_ITERATE_PACKED_VECTOR2_ARRAY,
  266. OPCODE_ITERATE_PACKED_VECTOR3_ARRAY,
  267. OPCODE_ITERATE_PACKED_COLOR_ARRAY,
  268. OPCODE_ITERATE_OBJECT,
  269. OPCODE_ASSERT,
  270. OPCODE_BREAKPOINT,
  271. OPCODE_LINE,
  272. OPCODE_END
  273. };
  274. enum Address {
  275. ADDR_BITS = 24,
  276. ADDR_MASK = ((1 << ADDR_BITS) - 1),
  277. ADDR_TYPE_MASK = ~ADDR_MASK,
  278. ADDR_TYPE_SELF = 0,
  279. ADDR_TYPE_CLASS = 1,
  280. ADDR_TYPE_MEMBER = 2,
  281. ADDR_TYPE_CLASS_CONSTANT = 3,
  282. ADDR_TYPE_LOCAL_CONSTANT = 4,
  283. ADDR_TYPE_STACK = 5,
  284. ADDR_TYPE_STACK_VARIABLE = 6,
  285. ADDR_TYPE_GLOBAL = 7,
  286. ADDR_TYPE_NAMED_GLOBAL = 8,
  287. ADDR_TYPE_NIL = 9
  288. };
  289. enum Instruction {
  290. INSTR_BITS = 20,
  291. INSTR_MASK = ((1 << INSTR_BITS) - 1),
  292. INSTR_ARGS_MASK = ~INSTR_MASK,
  293. };
  294. struct StackDebug {
  295. int line;
  296. int pos;
  297. bool added;
  298. StringName identifier;
  299. };
  300. private:
  301. friend class GDScriptCompiler;
  302. friend class GDScriptByteCodeGenerator;
  303. StringName source;
  304. mutable Variant nil;
  305. mutable Variant *_constants_ptr = nullptr;
  306. int _constant_count = 0;
  307. const StringName *_global_names_ptr = nullptr;
  308. int _global_names_count = 0;
  309. const int *_default_arg_ptr = nullptr;
  310. int _default_arg_count = 0;
  311. int _operator_funcs_count = 0;
  312. const Variant::ValidatedOperatorEvaluator *_operator_funcs_ptr = nullptr;
  313. int _setters_count = 0;
  314. const Variant::ValidatedSetter *_setters_ptr = nullptr;
  315. int _getters_count = 0;
  316. const Variant::ValidatedGetter *_getters_ptr = nullptr;
  317. int _keyed_setters_count = 0;
  318. const Variant::ValidatedKeyedSetter *_keyed_setters_ptr = nullptr;
  319. int _keyed_getters_count = 0;
  320. const Variant::ValidatedKeyedGetter *_keyed_getters_ptr = nullptr;
  321. int _indexed_setters_count = 0;
  322. const Variant::ValidatedIndexedSetter *_indexed_setters_ptr = nullptr;
  323. int _indexed_getters_count = 0;
  324. const Variant::ValidatedIndexedGetter *_indexed_getters_ptr = nullptr;
  325. int _builtin_methods_count = 0;
  326. const Variant::ValidatedBuiltInMethod *_builtin_methods_ptr = nullptr;
  327. int _constructors_count = 0;
  328. const Variant::ValidatedConstructor *_constructors_ptr = nullptr;
  329. int _utilities_count = 0;
  330. const Variant::ValidatedUtilityFunction *_utilities_ptr = nullptr;
  331. int _gds_utilities_count = 0;
  332. const GDScriptUtilityFunctions::FunctionPtr *_gds_utilities_ptr = nullptr;
  333. int _methods_count = 0;
  334. MethodBind **_methods_ptr = nullptr;
  335. const int *_code_ptr = nullptr;
  336. int _code_size = 0;
  337. int _argument_count = 0;
  338. int _stack_size = 0;
  339. int _instruction_args_size = 0;
  340. int _ptrcall_args_size = 0;
  341. int _initial_line = 0;
  342. bool _static = false;
  343. MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  344. GDScript *_script = nullptr;
  345. StringName name;
  346. Vector<Variant> constants;
  347. Vector<StringName> global_names;
  348. Vector<int> default_arguments;
  349. Vector<Variant::ValidatedOperatorEvaluator> operator_funcs;
  350. Vector<Variant::ValidatedSetter> setters;
  351. Vector<Variant::ValidatedGetter> getters;
  352. Vector<Variant::ValidatedKeyedSetter> keyed_setters;
  353. Vector<Variant::ValidatedKeyedGetter> keyed_getters;
  354. Vector<Variant::ValidatedIndexedSetter> indexed_setters;
  355. Vector<Variant::ValidatedIndexedGetter> indexed_getters;
  356. Vector<Variant::ValidatedBuiltInMethod> builtin_methods;
  357. Vector<Variant::ValidatedConstructor> constructors;
  358. Vector<Variant::ValidatedUtilityFunction> utilities;
  359. Vector<GDScriptUtilityFunctions::FunctionPtr> gds_utilities;
  360. Vector<MethodBind *> methods;
  361. Vector<int> code;
  362. Vector<GDScriptDataType> argument_types;
  363. GDScriptDataType return_type;
  364. #ifdef TOOLS_ENABLED
  365. Vector<StringName> arg_names;
  366. Vector<Variant> default_arg_values;
  367. #endif
  368. List<StackDebug> stack_debug;
  369. _FORCE_INLINE_ Variant *_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant &static_ref, Variant *p_stack, String &r_error) const;
  370. _FORCE_INLINE_ String _get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const;
  371. friend class GDScriptLanguage;
  372. SelfList<GDScriptFunction> function_list{ this };
  373. #ifdef DEBUG_ENABLED
  374. CharString func_cname;
  375. const char *_func_cname = nullptr;
  376. struct Profile {
  377. StringName signature;
  378. uint64_t call_count = 0;
  379. uint64_t self_time = 0;
  380. uint64_t total_time = 0;
  381. uint64_t frame_call_count = 0;
  382. uint64_t frame_self_time = 0;
  383. uint64_t frame_total_time = 0;
  384. uint64_t last_frame_call_count = 0;
  385. uint64_t last_frame_self_time = 0;
  386. uint64_t last_frame_total_time = 0;
  387. } profile;
  388. #endif
  389. public:
  390. struct CallState {
  391. GDScript *script = nullptr;
  392. GDScriptInstance *instance = nullptr;
  393. #ifdef DEBUG_ENABLED
  394. StringName function_name;
  395. String script_path;
  396. #endif
  397. Vector<uint8_t> stack;
  398. int stack_size = 0;
  399. Variant self;
  400. uint32_t alloca_size = 0;
  401. int ip = 0;
  402. int line = 0;
  403. int defarg = 0;
  404. Variant result;
  405. };
  406. _FORCE_INLINE_ bool is_static() const { return _static; }
  407. const int *get_code() const; //used for debug
  408. int get_code_size() const;
  409. Variant get_constant(int p_idx) const;
  410. StringName get_global_name(int p_idx) const;
  411. StringName get_name() const;
  412. int get_max_stack_size() const;
  413. int get_default_argument_count() const;
  414. int get_default_argument_addr(int p_idx) const;
  415. GDScriptDataType get_return_type() const;
  416. GDScriptDataType get_argument_type(int p_idx) const;
  417. GDScript *get_script() const { return _script; }
  418. StringName get_source() const { return source; }
  419. void debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const;
  420. _FORCE_INLINE_ bool is_empty() const { return _code_size == 0; }
  421. int get_argument_count() const { return _argument_count; }
  422. StringName get_argument_name(int p_idx) const {
  423. #ifdef TOOLS_ENABLED
  424. ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
  425. return arg_names[p_idx];
  426. #else
  427. return StringName();
  428. #endif
  429. }
  430. Variant get_default_argument(int p_idx) const {
  431. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
  432. return default_arguments[p_idx];
  433. }
  434. #ifdef TOOLS_ENABLED
  435. const Vector<Variant> &get_default_arg_values() const {
  436. return default_arg_values;
  437. }
  438. #endif // TOOLS_ENABLED
  439. Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state = nullptr);
  440. #ifdef DEBUG_ENABLED
  441. void disassemble(const Vector<String> &p_code_lines) const;
  442. #endif
  443. _FORCE_INLINE_ MultiplayerAPI::RPCMode get_rpc_mode() const { return rpc_mode; }
  444. GDScriptFunction();
  445. ~GDScriptFunction();
  446. };
  447. class GDScriptFunctionState : public Reference {
  448. GDCLASS(GDScriptFunctionState, Reference);
  449. friend class GDScriptFunction;
  450. GDScriptFunction *function = nullptr;
  451. GDScriptFunction::CallState state;
  452. Variant _signal_callback(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  453. Ref<GDScriptFunctionState> first_state;
  454. SelfList<GDScriptFunctionState> scripts_list;
  455. SelfList<GDScriptFunctionState> instances_list;
  456. protected:
  457. static void _bind_methods();
  458. public:
  459. bool is_valid(bool p_extended_check = false) const;
  460. Variant resume(const Variant &p_arg = Variant());
  461. void _clear_stack();
  462. GDScriptFunctionState();
  463. ~GDScriptFunctionState();
  464. };
  465. #endif // GDSCRIPT_FUNCTION_H