gdscript_function.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/os/thread.h"
  33. #include "core/pair.h"
  34. #include "core/reference.h"
  35. #include "core/script_language.h"
  36. #include "core/self_list.h"
  37. #include "core/string_name.h"
  38. #include "core/variant.h"
  39. class GDScriptInstance;
  40. class GDScript;
  41. struct GDScriptDataType {
  42. bool has_type;
  43. enum {
  44. UNINITIALIZED,
  45. BUILTIN,
  46. NATIVE,
  47. SCRIPT,
  48. GDSCRIPT,
  49. } kind;
  50. Variant::Type builtin_type;
  51. StringName native_type;
  52. Script *script_type;
  53. Ref<Script> script_type_ref;
  54. bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
  55. if (!has_type)
  56. return true; // Can't type check
  57. switch (kind) {
  58. case UNINITIALIZED:
  59. break;
  60. case BUILTIN: {
  61. Variant::Type var_type = p_variant.get_type();
  62. bool valid = builtin_type == var_type;
  63. if (!valid && p_allow_implicit_conversion) {
  64. valid = Variant::can_convert_strict(var_type, builtin_type);
  65. }
  66. return valid;
  67. } break;
  68. case NATIVE: {
  69. if (p_variant.get_type() == Variant::NIL) {
  70. return true;
  71. }
  72. if (p_variant.get_type() != Variant::OBJECT) {
  73. return false;
  74. }
  75. Object *obj = p_variant.operator Object *();
  76. if (!obj || !ObjectDB::instance_validate(obj)) {
  77. return false;
  78. }
  79. if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) {
  80. // Try with underscore prefix
  81. StringName underscore_native_type = "_" + native_type;
  82. if (!ClassDB::is_parent_class(obj->get_class_name(), underscore_native_type)) {
  83. return false;
  84. }
  85. }
  86. return true;
  87. } break;
  88. case SCRIPT:
  89. case GDSCRIPT: {
  90. if (p_variant.get_type() == Variant::NIL) {
  91. return true;
  92. }
  93. if (p_variant.get_type() != Variant::OBJECT) {
  94. return false;
  95. }
  96. Object *obj = p_variant.operator Object *();
  97. if (!obj || !ObjectDB::instance_validate(obj)) {
  98. return false;
  99. }
  100. Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : NULL;
  101. bool valid = false;
  102. while (base.is_valid()) {
  103. if (base == script_type) {
  104. valid = true;
  105. break;
  106. }
  107. base = base->get_base_script();
  108. }
  109. return valid;
  110. } break;
  111. }
  112. return false;
  113. }
  114. operator PropertyInfo() const {
  115. PropertyInfo info;
  116. if (has_type) {
  117. switch (kind) {
  118. case UNINITIALIZED:
  119. break;
  120. case BUILTIN: {
  121. info.type = builtin_type;
  122. } break;
  123. case NATIVE: {
  124. info.type = Variant::OBJECT;
  125. info.class_name = native_type;
  126. } break;
  127. case SCRIPT:
  128. case GDSCRIPT: {
  129. info.type = Variant::OBJECT;
  130. info.class_name = script_type->get_instance_base_type();
  131. } break;
  132. }
  133. } else {
  134. info.type = Variant::NIL;
  135. info.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  136. }
  137. return info;
  138. }
  139. GDScriptDataType() :
  140. has_type(false),
  141. kind(UNINITIALIZED),
  142. builtin_type(Variant::NIL),
  143. script_type(NULL) {}
  144. };
  145. class GDScriptFunction {
  146. public:
  147. enum Opcode {
  148. OPCODE_OPERATOR,
  149. OPCODE_EXTENDS_TEST,
  150. OPCODE_IS_BUILTIN,
  151. OPCODE_SET,
  152. OPCODE_GET,
  153. OPCODE_SET_NAMED,
  154. OPCODE_GET_NAMED,
  155. OPCODE_SET_MEMBER,
  156. OPCODE_GET_MEMBER,
  157. OPCODE_ASSIGN,
  158. OPCODE_ASSIGN_TRUE,
  159. OPCODE_ASSIGN_FALSE,
  160. OPCODE_ASSIGN_TYPED_BUILTIN,
  161. OPCODE_ASSIGN_TYPED_NATIVE,
  162. OPCODE_ASSIGN_TYPED_SCRIPT,
  163. OPCODE_CAST_TO_BUILTIN,
  164. OPCODE_CAST_TO_NATIVE,
  165. OPCODE_CAST_TO_SCRIPT,
  166. OPCODE_CONSTRUCT, //only for basic types!!
  167. OPCODE_CONSTRUCT_ARRAY,
  168. OPCODE_CONSTRUCT_DICTIONARY,
  169. OPCODE_CALL,
  170. OPCODE_CALL_RETURN,
  171. OPCODE_CALL_BUILT_IN,
  172. OPCODE_CALL_SELF,
  173. OPCODE_CALL_SELF_BASE,
  174. OPCODE_YIELD,
  175. OPCODE_YIELD_SIGNAL,
  176. OPCODE_YIELD_RESUME,
  177. OPCODE_JUMP,
  178. OPCODE_JUMP_IF,
  179. OPCODE_JUMP_IF_NOT,
  180. OPCODE_JUMP_TO_DEF_ARGUMENT,
  181. OPCODE_RETURN,
  182. OPCODE_ITERATE_BEGIN,
  183. OPCODE_ITERATE,
  184. OPCODE_ASSERT,
  185. OPCODE_BREAKPOINT,
  186. OPCODE_LINE,
  187. OPCODE_END
  188. };
  189. enum Address {
  190. ADDR_BITS = 24,
  191. ADDR_MASK = ((1 << ADDR_BITS) - 1),
  192. ADDR_TYPE_MASK = ~ADDR_MASK,
  193. ADDR_TYPE_SELF = 0,
  194. ADDR_TYPE_CLASS = 1,
  195. ADDR_TYPE_MEMBER = 2,
  196. ADDR_TYPE_CLASS_CONSTANT = 3,
  197. ADDR_TYPE_LOCAL_CONSTANT = 4,
  198. ADDR_TYPE_STACK = 5,
  199. ADDR_TYPE_STACK_VARIABLE = 6,
  200. ADDR_TYPE_GLOBAL = 7,
  201. ADDR_TYPE_NAMED_GLOBAL = 8,
  202. ADDR_TYPE_NIL = 9
  203. };
  204. struct StackDebug {
  205. int line;
  206. int pos;
  207. bool added;
  208. StringName identifier;
  209. };
  210. private:
  211. friend class GDScriptCompiler;
  212. StringName source;
  213. mutable Variant nil;
  214. mutable Variant *_constants_ptr;
  215. int _constant_count;
  216. const StringName *_global_names_ptr;
  217. int _global_names_count;
  218. #ifdef TOOLS_ENABLED
  219. const StringName *_named_globals_ptr;
  220. int _named_globals_count;
  221. #endif
  222. const int *_default_arg_ptr;
  223. int _default_arg_count;
  224. const int *_code_ptr;
  225. int _code_size;
  226. int _argument_count;
  227. int _stack_size;
  228. int _call_size;
  229. int _initial_line;
  230. bool _static;
  231. MultiplayerAPI::RPCMode rpc_mode;
  232. GDScript *_script;
  233. StringName name;
  234. Vector<Variant> constants;
  235. Vector<StringName> global_names;
  236. #ifdef TOOLS_ENABLED
  237. Vector<StringName> named_globals;
  238. #endif
  239. Vector<int> default_arguments;
  240. Vector<int> code;
  241. Vector<GDScriptDataType> argument_types;
  242. GDScriptDataType return_type;
  243. #ifdef TOOLS_ENABLED
  244. Vector<StringName> arg_names;
  245. #endif
  246. List<StackDebug> stack_debug;
  247. _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;
  248. _FORCE_INLINE_ String _get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const;
  249. friend class GDScriptLanguage;
  250. SelfList<GDScriptFunction> function_list;
  251. #ifdef DEBUG_ENABLED
  252. CharString func_cname;
  253. const char *_func_cname;
  254. struct Profile {
  255. StringName signature;
  256. uint64_t call_count;
  257. uint64_t self_time;
  258. uint64_t total_time;
  259. uint64_t frame_call_count;
  260. uint64_t frame_self_time;
  261. uint64_t frame_total_time;
  262. uint64_t last_frame_call_count;
  263. uint64_t last_frame_self_time;
  264. uint64_t last_frame_total_time;
  265. } profile;
  266. #endif
  267. public:
  268. struct CallState {
  269. GDScript *script;
  270. GDScriptInstance *instance;
  271. #ifdef DEBUG_ENABLED
  272. StringName function_name;
  273. String script_path;
  274. #endif
  275. Vector<uint8_t> stack;
  276. int stack_size;
  277. Variant self;
  278. uint32_t alloca_size;
  279. int ip;
  280. int line;
  281. int defarg;
  282. Variant result;
  283. };
  284. _FORCE_INLINE_ bool is_static() const { return _static; }
  285. const int *get_code() const; //used for debug
  286. int get_code_size() const;
  287. Variant get_constant(int p_idx) const;
  288. StringName get_global_name(int p_idx) const;
  289. StringName get_name() const;
  290. int get_max_stack_size() const;
  291. int get_default_argument_count() const;
  292. int get_default_argument_addr(int p_idx) const;
  293. GDScriptDataType get_return_type() const;
  294. GDScriptDataType get_argument_type(int p_idx) const;
  295. GDScript *get_script() const { return _script; }
  296. StringName get_source() const { return source; }
  297. void debug_get_stack_member_state(int p_line, List<Pair<StringName, int>> *r_stackvars) const;
  298. _FORCE_INLINE_ bool is_empty() const { return _code_size == 0; }
  299. int get_argument_count() const { return _argument_count; }
  300. StringName get_argument_name(int p_idx) const {
  301. #ifdef TOOLS_ENABLED
  302. ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
  303. return arg_names[p_idx];
  304. #else
  305. return StringName();
  306. #endif
  307. }
  308. Variant get_default_argument(int p_idx) const {
  309. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
  310. return default_arguments[p_idx];
  311. }
  312. Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = NULL);
  313. _FORCE_INLINE_ MultiplayerAPI::RPCMode get_rpc_mode() const { return rpc_mode; }
  314. GDScriptFunction();
  315. ~GDScriptFunction();
  316. };
  317. class GDScriptFunctionState : public Reference {
  318. GDCLASS(GDScriptFunctionState, Reference);
  319. friend class GDScriptFunction;
  320. GDScriptFunction *function;
  321. GDScriptFunction::CallState state;
  322. Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  323. Ref<GDScriptFunctionState> first_state;
  324. SelfList<GDScriptFunctionState> scripts_list;
  325. SelfList<GDScriptFunctionState> instances_list;
  326. protected:
  327. static void _bind_methods();
  328. public:
  329. bool is_valid(bool p_extended_check = false) const;
  330. Variant resume(const Variant &p_arg = Variant());
  331. void _clear_stack();
  332. GDScriptFunctionState();
  333. ~GDScriptFunctionState();
  334. };
  335. #endif // GDSCRIPT_FUNCTION_H