gd_function.h 7.0 KB

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