gdscript_function.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*************************************************************************/
  2. /* gdscript_function.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "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 GDScriptInstance;
  40. class GDScript;
  41. class GDScriptFunction {
  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_NAMED_GLOBAL = 8,
  91. ADDR_TYPE_NIL = 9
  92. };
  93. enum RPCMode {
  94. RPC_DISABLED,
  95. RPC_ENABLED,
  96. RPC_SYNC,
  97. RPC_SYNC_MASTER,
  98. RPC_SYNC_SLAVE
  99. };
  100. struct StackDebug {
  101. int line;
  102. int pos;
  103. bool added;
  104. StringName identifier;
  105. };
  106. private:
  107. friend class GDScriptCompiler;
  108. StringName source;
  109. mutable Variant nil;
  110. mutable Variant *_constants_ptr;
  111. int _constant_count;
  112. const StringName *_global_names_ptr;
  113. int _global_names_count;
  114. #ifdef TOOLS_ENABLED
  115. const StringName *_named_globals_ptr;
  116. int _named_globals_count;
  117. #endif
  118. const int *_default_arg_ptr;
  119. int _default_arg_count;
  120. const int *_code_ptr;
  121. int _code_size;
  122. int _argument_count;
  123. int _stack_size;
  124. int _call_size;
  125. int _initial_line;
  126. bool _static;
  127. ScriptInstance::RPCMode rpc_mode;
  128. GDScript *_script;
  129. StringName name;
  130. Vector<Variant> constants;
  131. Vector<StringName> global_names;
  132. #ifdef TOOLS_ENABLED
  133. Vector<StringName> named_globals;
  134. #endif
  135. Vector<int> default_arguments;
  136. Vector<int> code;
  137. #ifdef TOOLS_ENABLED
  138. Vector<StringName> arg_names;
  139. #endif
  140. List<StackDebug> stack_debug;
  141. _FORCE_INLINE_ Variant *_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const;
  142. _FORCE_INLINE_ String _get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const;
  143. friend class GDScriptLanguage;
  144. SelfList<GDScriptFunction> function_list;
  145. #ifdef DEBUG_ENABLED
  146. CharString func_cname;
  147. const char *_func_cname;
  148. struct Profile {
  149. StringName signature;
  150. uint64_t call_count;
  151. uint64_t self_time;
  152. uint64_t total_time;
  153. uint64_t frame_call_count;
  154. uint64_t frame_self_time;
  155. uint64_t frame_total_time;
  156. uint64_t last_frame_call_count;
  157. uint64_t last_frame_self_time;
  158. uint64_t last_frame_total_time;
  159. } profile;
  160. #endif
  161. public:
  162. struct CallState {
  163. ObjectID instance_id; //by debug only
  164. ObjectID script_id;
  165. GDScriptInstance *instance;
  166. Vector<uint8_t> stack;
  167. int stack_size;
  168. Variant self;
  169. uint32_t alloca_size;
  170. GDScript *_class;
  171. int ip;
  172. int line;
  173. int defarg;
  174. Variant result;
  175. };
  176. _FORCE_INLINE_ bool is_static() const { return _static; }
  177. const int *get_code() const; //used for debug
  178. int get_code_size() const;
  179. Variant get_constant(int p_idx) const;
  180. StringName get_global_name(int p_idx) const;
  181. StringName get_name() const;
  182. int get_max_stack_size() const;
  183. int get_default_argument_count() const;
  184. int get_default_argument_addr(int p_idx) const;
  185. GDScript *get_script() const { return _script; }
  186. StringName get_source() const { return source; }
  187. void debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const;
  188. _FORCE_INLINE_ bool is_empty() const { return _code_size == 0; }
  189. int get_argument_count() const { return _argument_count; }
  190. StringName get_argument_name(int p_idx) const {
  191. #ifdef TOOLS_ENABLED
  192. ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
  193. return arg_names[p_idx];
  194. #else
  195. return StringName();
  196. #endif
  197. }
  198. Variant get_default_argument(int p_idx) const {
  199. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
  200. return default_arguments[p_idx];
  201. }
  202. Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = NULL);
  203. _FORCE_INLINE_ ScriptInstance::RPCMode get_rpc_mode() const { return rpc_mode; }
  204. GDScriptFunction();
  205. ~GDScriptFunction();
  206. };
  207. class GDScriptFunctionState : public Reference {
  208. GDCLASS(GDScriptFunctionState, Reference);
  209. friend class GDScriptFunction;
  210. GDScriptFunction *function;
  211. GDScriptFunction::CallState state;
  212. Variant _signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  213. Ref<GDScriptFunctionState> previous_state;
  214. protected:
  215. static void _bind_methods();
  216. public:
  217. bool is_valid(bool p_extended_check = false) const;
  218. Variant resume(const Variant &p_arg = Variant());
  219. GDScriptFunctionState();
  220. ~GDScriptFunctionState();
  221. };
  222. #endif // GDSCRIPT_FUNCTION_H