gd_script.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*************************************************************************/
  2. /* gd_script.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef GD_SCRIPT_H
  30. #define GD_SCRIPT_H
  31. #include "script_language.h"
  32. #include "io/resource_loader.h"
  33. #include "io/resource_saver.h"
  34. #include "gd_function.h"
  35. class GDNativeClass : public Reference {
  36. OBJ_TYPE(GDNativeClass,Reference);
  37. StringName name;
  38. protected:
  39. bool _get(const StringName& p_name,Variant &r_ret) const;
  40. static void _bind_methods();
  41. public:
  42. _FORCE_INLINE_ const StringName& get_name() const { return name; }
  43. Variant _new();
  44. Object *instance();
  45. GDNativeClass(const StringName& p_name);
  46. };
  47. class GDScript : public Script {
  48. OBJ_TYPE(GDScript,Script);
  49. bool tool;
  50. bool valid;
  51. struct MemberInfo {
  52. int index;
  53. StringName setter;
  54. StringName getter;
  55. ScriptInstance::RPCMode rpc_mode;
  56. };
  57. friend class GDInstance;
  58. friend class GDFunction;
  59. friend class GDCompiler;
  60. friend class GDFunctions;
  61. friend class GDScriptLanguage;
  62. Variant _static_ref; //used for static call
  63. Ref<GDNativeClass> native;
  64. Ref<GDScript> base;
  65. GDScript *_base; //fast pointer access
  66. GDScript *_owner; //for subclasses
  67. Set<StringName> members; //members are just indices to the instanced script.
  68. Map<StringName,Variant> constants;
  69. Map<StringName,GDFunction*> member_functions;
  70. Map<StringName,MemberInfo> member_indices; //members are just indices to the instanced script.
  71. Map<StringName,Ref<GDScript> > subclasses;
  72. Map<StringName,Vector<StringName> > _signals;
  73. #ifdef TOOLS_ENABLED
  74. Map<StringName,Variant> member_default_values;
  75. List<PropertyInfo> members_cache;
  76. Map<StringName,Variant> member_default_values_cache;
  77. Ref<GDScript> base_cache;
  78. Set<ObjectID> inheriters_cache;
  79. bool source_changed_cache;
  80. void _update_exports_values(Map<StringName,Variant>& values, List<PropertyInfo> &propnames);
  81. #endif
  82. Map<StringName,PropertyInfo> member_info;
  83. GDFunction *initializer; //direct pointer to _init , faster to locate
  84. int subclass_count;
  85. Set<Object*> instances;
  86. //exported members
  87. String source;
  88. String path;
  89. String name;
  90. SelfList<GDScript> script_list;
  91. GDInstance* _create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError &r_error);
  92. void _set_subclass_path(Ref<GDScript>& p_sc,const String& p_path);
  93. #ifdef TOOLS_ENABLED
  94. Set<PlaceHolderScriptInstance*> placeholders;
  95. //void _update_placeholder(PlaceHolderScriptInstance *p_placeholder);
  96. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
  97. #endif
  98. #ifdef DEBUG_ENABLED
  99. Map<ObjectID,List<Pair<StringName,Variant> > > pending_reload_state;
  100. #endif
  101. bool _update_exports();
  102. protected:
  103. bool _get(const StringName& p_name,Variant &r_ret) const;
  104. bool _set(const StringName& p_name, const Variant& p_value);
  105. void _get_property_list(List<PropertyInfo> *p_properties) const;
  106. Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  107. // void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  108. static void _bind_methods();
  109. public:
  110. bool is_valid() const { return valid; }
  111. const Map<StringName,Ref<GDScript> >& get_subclasses() const { return subclasses; }
  112. const Map<StringName,Variant >& get_constants() const { return constants; }
  113. const Set<StringName>& get_members() const { return members; }
  114. const Map<StringName,GDFunction*>& get_member_functions() const { return member_functions; }
  115. const Ref<GDNativeClass>& get_native() const { return native; }
  116. virtual bool has_script_signal(const StringName& p_signal) const;
  117. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const;
  118. bool is_tool() const { return tool; }
  119. Ref<GDScript> get_base() const;
  120. const Map<StringName,MemberInfo>& debug_get_member_indices() const { return member_indices; }
  121. const Map<StringName,GDFunction*>& debug_get_member_functions() const; //this is debug only
  122. StringName debug_get_member_by_index(int p_idx) const;
  123. Variant _new(const Variant** p_args,int p_argcount,Variant::CallError& r_error);
  124. virtual bool can_instance() const;
  125. virtual StringName get_instance_base_type() const; // this may not work in all scripts, will return empty if so
  126. virtual ScriptInstance* instance_create(Object *p_this);
  127. virtual bool instance_has(const Object *p_this) const;
  128. virtual bool has_source_code() const;
  129. virtual String get_source_code() const;
  130. virtual void set_source_code(const String& p_code);
  131. virtual void update_exports();
  132. virtual Error reload(bool p_keep_state=false);
  133. virtual String get_node_type() const;
  134. void set_script_path(const String& p_path) { path=p_path; } //because subclasses need a path too...
  135. Error load_source_code(const String& p_path);
  136. Error load_byte_code(const String& p_path);
  137. Vector<uint8_t> get_as_byte_code() const;
  138. bool get_property_default_value(const StringName& p_property,Variant& r_value) const;
  139. virtual void get_script_method_list(List<MethodInfo> *p_list) const;
  140. virtual bool has_method(const StringName& p_method) const;
  141. virtual MethodInfo get_method_info(const StringName& p_method) const;
  142. virtual void get_script_property_list(List<PropertyInfo> *p_list) const;
  143. virtual ScriptLanguage *get_language() const;
  144. GDScript();
  145. ~GDScript();
  146. };
  147. class GDInstance : public ScriptInstance {
  148. friend class GDScript;
  149. friend class GDFunction;
  150. friend class GDFunctions;
  151. friend class GDCompiler;
  152. Object *owner;
  153. Ref<GDScript> script;
  154. #ifdef DEBUG_ENABLED
  155. Map<StringName,int> member_indices_cache; //used only for hot script reloading
  156. #endif
  157. Vector<Variant> members;
  158. bool base_ref;
  159. void _ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount);
  160. public:
  161. _FORCE_INLINE_ Object* get_owner() { return owner; }
  162. virtual bool set(const StringName& p_name, const Variant& p_value);
  163. virtual bool get(const StringName& p_name, Variant &r_ret) const;
  164. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  165. virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const;
  166. virtual void get_method_list(List<MethodInfo> *p_list) const;
  167. virtual bool has_method(const StringName& p_method) const;
  168. virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  169. virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  170. virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
  171. Variant debug_get_member_by_index(int p_idx) const { return members[p_idx]; }
  172. virtual void notification(int p_notification);
  173. virtual Ref<Script> get_script() const;
  174. virtual ScriptLanguage *get_language();
  175. void set_path(const String& p_path);
  176. void reload_members();
  177. virtual RPCMode get_rpc_mode(const StringName& p_method) const;
  178. virtual RPCMode get_rset_mode(const StringName& p_variable) const;
  179. GDInstance();
  180. ~GDInstance();
  181. };
  182. class GDScriptLanguage : public ScriptLanguage {
  183. static GDScriptLanguage *singleton;
  184. Variant* _global_array;
  185. Vector<Variant> global_array;
  186. Map<StringName,int> globals;
  187. struct CallLevel {
  188. Variant *stack;
  189. GDFunction *function;
  190. GDInstance *instance;
  191. int *ip;
  192. int *line;
  193. };
  194. int _debug_parse_err_line;
  195. String _debug_parse_err_file;
  196. String _debug_error;
  197. int _debug_call_stack_pos;
  198. int _debug_max_call_stack;
  199. CallLevel *_call_stack;
  200. void _add_global(const StringName& p_name,const Variant& p_value);
  201. Mutex *lock;
  202. friend class GDScript;
  203. SelfList<GDScript>::List script_list;
  204. friend class GDFunction;
  205. SelfList<GDFunction>::List function_list;
  206. bool profiling;
  207. uint64_t script_frame_time;
  208. public:
  209. int calls;
  210. bool debug_break(const String& p_error,bool p_allow_continue=true);
  211. bool debug_break_parse(const String& p_file, int p_line,const String& p_error);
  212. _FORCE_INLINE_ void enter_function(GDInstance *p_instance,GDFunction *p_function, Variant *p_stack, int *p_ip, int *p_line) {
  213. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  214. return; //no support for other threads than main for now
  215. if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
  216. ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() +1 );
  217. if (_debug_call_stack_pos >= _debug_max_call_stack) {
  218. //stack overflow
  219. _debug_error="Stack Overflow (Stack Size: "+itos(_debug_max_call_stack)+")";
  220. ScriptDebugger::get_singleton()->debug(this);
  221. return;
  222. }
  223. _call_stack[_debug_call_stack_pos].stack=p_stack;
  224. _call_stack[_debug_call_stack_pos].instance=p_instance;
  225. _call_stack[_debug_call_stack_pos].function=p_function;
  226. _call_stack[_debug_call_stack_pos].ip=p_ip;
  227. _call_stack[_debug_call_stack_pos].line=p_line;
  228. _debug_call_stack_pos++;
  229. }
  230. _FORCE_INLINE_ void exit_function() {
  231. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  232. return; //no support for other threads than main for now
  233. if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
  234. ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() -1 );
  235. if (_debug_call_stack_pos==0) {
  236. _debug_error="Stack Underflow (Engine Bug)";
  237. ScriptDebugger::get_singleton()->debug(this);
  238. return;
  239. }
  240. _debug_call_stack_pos--;
  241. }
  242. virtual Vector<StackInfo> debug_get_current_stack_info() {
  243. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  244. return Vector<StackInfo>();
  245. Vector<StackInfo> csi;
  246. csi.resize(_debug_call_stack_pos);
  247. for(int i=0;i<_debug_call_stack_pos;i++) {
  248. csi[_debug_call_stack_pos-i-1].line=_call_stack[i].line?*_call_stack[i].line:0;
  249. csi[_debug_call_stack_pos-i-1].script=Ref<GDScript>(_call_stack[i].function->get_script());
  250. }
  251. return csi;
  252. }
  253. struct {
  254. StringName _init;
  255. StringName _notification;
  256. StringName _set;
  257. StringName _get;
  258. StringName _get_property_list;
  259. StringName _script_source;
  260. } strings;
  261. _FORCE_INLINE_ int get_global_array_size() const { return global_array.size(); }
  262. _FORCE_INLINE_ Variant* get_global_array() { return _global_array; }
  263. _FORCE_INLINE_ const Map<StringName,int>& get_global_map() { return globals; }
  264. _FORCE_INLINE_ static GDScriptLanguage *get_singleton() { return singleton; }
  265. virtual String get_name() const;
  266. /* LANGUAGE FUNCTIONS */
  267. virtual void init();
  268. virtual String get_type() const;
  269. virtual String get_extension() const;
  270. virtual Error execute_file(const String& p_path) ;
  271. virtual void finish();
  272. /* EDITOR FUNCTIONS */
  273. virtual void get_reserved_words(List<String> *p_words) const;
  274. virtual void get_comment_delimiters(List<String> *p_delimiters) const;
  275. virtual void get_string_delimiters(List<String> *p_delimiters) const;
  276. virtual Ref<Script> get_template(const String& p_class_name, const String& p_base_class_name) const;
  277. virtual bool validate(const String& p_script,int &r_line_error,int &r_col_error,String& r_test_error, const String& p_path="",List<String> *r_functions=NULL) const;
  278. virtual Script *create_script() const;
  279. virtual bool has_named_classes() const;
  280. virtual int find_function(const String& p_function,const String& p_code) const;
  281. virtual String make_function(const String& p_class,const String& p_name,const StringArray& p_args) const;
  282. virtual Error complete_code(const String& p_code, const String& p_base_path, Object*p_owner,List<String>* r_options,String& r_call_hint);
  283. virtual void auto_indent_code(String& p_code,int p_from_line,int p_to_line) const;
  284. virtual void add_global_constant(const StringName& p_variable,const Variant& p_value);
  285. /* DEBUGGER FUNCTIONS */
  286. virtual String debug_get_error() const;
  287. virtual int debug_get_stack_level_count() const;
  288. virtual int debug_get_stack_level_line(int p_level) const;
  289. virtual String debug_get_stack_level_function(int p_level) const;
  290. virtual String debug_get_stack_level_source(int p_level) const;
  291. virtual void debug_get_stack_level_locals(int p_level,List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1);
  292. virtual void debug_get_stack_level_members(int p_level,List<String> *p_members, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1);
  293. virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1);
  294. virtual String debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems=-1,int p_max_depth=-1);
  295. virtual void reload_all_scripts();
  296. virtual void reload_tool_script(const Ref<Script>& p_script,bool p_soft_reload);
  297. virtual void frame();
  298. virtual void get_public_functions(List<MethodInfo> *p_functions) const;
  299. virtual void get_public_constants(List<Pair<String,Variant> > *p_constants) const;
  300. virtual void profiling_start();
  301. virtual void profiling_stop();
  302. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max);
  303. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max);
  304. /* LOADER FUNCTIONS */
  305. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  306. GDScriptLanguage();
  307. ~GDScriptLanguage();
  308. };
  309. class ResourceFormatLoaderGDScript : public ResourceFormatLoader {
  310. public:
  311. virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL);
  312. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  313. virtual bool handles_type(const String& p_type) const;
  314. virtual String get_resource_type(const String &p_path) const;
  315. };
  316. class ResourceFormatSaverGDScript : public ResourceFormatSaver {
  317. public:
  318. virtual Error save(const String &p_path,const RES& p_resource,uint32_t p_flags=0);
  319. virtual void get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const;
  320. virtual bool recognize(const RES& p_resource) const;
  321. };
  322. #endif // GD_SCRIPT_H