gdscript_function.h 13 KB

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