gd_function.h 4.5 KB

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