2
0

gd_function.h 4.7 KB

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