gdscript_function.h 10 KB

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