gdscript_function.h 7.6 KB

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