gdscript_function.h 9.6 KB

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