gd_function.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* gd_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 GD_FUNCTION_H
  31. #define GD_FUNCTION_H
  32. #include "os/thread.h"
  33. #include "pair.h"
  34. #include "reference.h"
  35. #include "self_list.h"
  36. #include "string_db.h"
  37. #include "variant.h"
  38. class GDInstance;
  39. class GDScript;
  40. class GDFunction {
  41. public:
  42. enum Opcode {
  43. OPCODE_OPERATOR,
  44. OPCODE_EXTENDS_TEST,
  45. OPCODE_SET,
  46. OPCODE_GET,
  47. OPCODE_SET_NAMED,
  48. OPCODE_GET_NAMED,
  49. OPCODE_ASSIGN,
  50. OPCODE_ASSIGN_TRUE,
  51. OPCODE_ASSIGN_FALSE,
  52. OPCODE_CONSTRUCT, //only for basic types!!
  53. OPCODE_CONSTRUCT_ARRAY,
  54. OPCODE_CONSTRUCT_DICTIONARY,
  55. OPCODE_CALL,
  56. OPCODE_CALL_RETURN,
  57. OPCODE_CALL_BUILT_IN,
  58. OPCODE_CALL_SELF,
  59. OPCODE_CALL_SELF_BASE,
  60. OPCODE_YIELD,
  61. OPCODE_YIELD_SIGNAL,
  62. OPCODE_YIELD_RESUME,
  63. OPCODE_JUMP,
  64. OPCODE_JUMP_IF,
  65. OPCODE_JUMP_IF_NOT,
  66. OPCODE_JUMP_TO_DEF_ARGUMENT,
  67. OPCODE_RETURN,
  68. OPCODE_ITERATE_BEGIN,
  69. OPCODE_ITERATE,
  70. OPCODE_ASSERT,
  71. OPCODE_BREAKPOINT,
  72. OPCODE_LINE,
  73. OPCODE_END
  74. };
  75. enum Address {
  76. ADDR_BITS = 24,
  77. ADDR_MASK = ((1 << ADDR_BITS) - 1),
  78. ADDR_TYPE_MASK = ~ADDR_MASK,
  79. ADDR_TYPE_SELF = 0,
  80. ADDR_TYPE_CLASS = 1,
  81. ADDR_TYPE_MEMBER = 2,
  82. ADDR_TYPE_CLASS_CONSTANT = 3,
  83. ADDR_TYPE_LOCAL_CONSTANT = 4,
  84. ADDR_TYPE_STACK = 5,
  85. ADDR_TYPE_STACK_VARIABLE = 6,
  86. ADDR_TYPE_GLOBAL = 7,
  87. ADDR_TYPE_NIL = 8
  88. };
  89. struct StackDebug {
  90. int line;
  91. int pos;
  92. bool added;
  93. StringName identifier;
  94. };
  95. private:
  96. friend class GDCompiler;
  97. StringName source;
  98. mutable Variant nil;
  99. mutable Variant *_constants_ptr;
  100. int _constant_count;
  101. const StringName *_global_names_ptr;
  102. int _global_names_count;
  103. const int *_default_arg_ptr;
  104. int _default_arg_count;
  105. const int *_code_ptr;
  106. int _code_size;
  107. int _argument_count;
  108. int _stack_size;
  109. int _call_size;
  110. int _initial_line;
  111. bool _static;
  112. GDScript *_script;
  113. StringName name;
  114. Vector<Variant> constants;
  115. Vector<StringName> global_names;
  116. Vector<int> default_arguments;
  117. Vector<int> code;
  118. #ifdef TOOLS_ENABLED
  119. Vector<StringName> arg_names;
  120. #endif
  121. List<StackDebug> stack_debug;
  122. _FORCE_INLINE_ Variant *_get_variant(int p_address, GDInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const;
  123. _FORCE_INLINE_ String _get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const;
  124. friend class GDScriptLanguage;
  125. SelfList<GDFunction> function_list;
  126. #ifdef DEBUG_ENABLED
  127. CharString func_cname;
  128. const char *_func_cname;
  129. struct Profile {
  130. StringName signature;
  131. uint64_t call_count;
  132. uint64_t self_time;
  133. uint64_t total_time;
  134. uint64_t frame_call_count;
  135. uint64_t frame_self_time;
  136. uint64_t frame_total_time;
  137. uint64_t last_frame_call_count;
  138. uint64_t last_frame_self_time;
  139. uint64_t last_frame_total_time;
  140. } profile;
  141. #endif
  142. public:
  143. struct CallState {
  144. ObjectID instance_id; //by debug only
  145. ObjectID script_id;
  146. GDInstance *instance;
  147. Vector<uint8_t> stack;
  148. int stack_size;
  149. Variant self;
  150. uint32_t alloca_size;
  151. GDScript *_class;
  152. int ip;
  153. int line;
  154. int defarg;
  155. Variant result;
  156. };
  157. _FORCE_INLINE_ bool is_static() const { return _static; }
  158. const int *get_code() const; //used for debug
  159. int get_code_size() const;
  160. Variant get_constant(int p_idx) const;
  161. StringName get_global_name(int p_idx) const;
  162. StringName get_name() const;
  163. int get_max_stack_size() const;
  164. int get_default_argument_count() const;
  165. int get_default_argument_addr(int p_idx) const;
  166. GDScript *get_script() const { return _script; }
  167. StringName get_source() const { return source; }
  168. void debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const;
  169. _FORCE_INLINE_ bool is_empty() const { return _code_size == 0; }
  170. int get_argument_count() const { return _argument_count; }
  171. StringName get_argument_name(int p_idx) const {
  172. #ifdef TOOLS_ENABLED
  173. ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
  174. return arg_names[p_idx];
  175. #endif
  176. return StringName();
  177. }
  178. Variant get_default_argument(int p_idx) const {
  179. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
  180. return default_arguments[p_idx];
  181. }
  182. Variant call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = NULL);
  183. GDFunction();
  184. ~GDFunction();
  185. };
  186. class GDFunctionState : public Reference {
  187. OBJ_TYPE(GDFunctionState, Reference);
  188. friend class GDFunction;
  189. GDFunction *function;
  190. GDFunction::CallState state;
  191. Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  192. protected:
  193. static void _bind_methods();
  194. public:
  195. bool is_valid(bool p_extended_check = false) const;
  196. Variant resume(const Variant &p_arg = Variant());
  197. GDFunctionState();
  198. ~GDFunctionState();
  199. };
  200. #endif // GD_FUNCTION_H