gdscript_function.h 9.8 KB

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