gd_script.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 "os/thread.h"
  35. #include "pair.h"
  36. #include "self_list.h"
  37. class GDInstance;
  38. class GDScript;
  39. class GDFunction {
  40. public:
  41. enum Opcode {
  42. OPCODE_OPERATOR,
  43. OPCODE_EXTENDS_TEST,
  44. OPCODE_SET,
  45. OPCODE_GET,
  46. OPCODE_SET_NAMED,
  47. OPCODE_GET_NAMED,
  48. OPCODE_ASSIGN,
  49. OPCODE_ASSIGN_TRUE,
  50. OPCODE_ASSIGN_FALSE,
  51. OPCODE_CONSTRUCT, //only for basic types!!
  52. OPCODE_CONSTRUCT_ARRAY,
  53. OPCODE_CONSTRUCT_DICTIONARY,
  54. OPCODE_CALL,
  55. OPCODE_CALL_RETURN,
  56. OPCODE_CALL_BUILT_IN,
  57. OPCODE_CALL_SELF,
  58. OPCODE_CALL_SELF_BASE,
  59. OPCODE_YIELD,
  60. OPCODE_YIELD_SIGNAL,
  61. OPCODE_YIELD_RESUME,
  62. OPCODE_JUMP,
  63. OPCODE_JUMP_IF,
  64. OPCODE_JUMP_IF_NOT,
  65. OPCODE_JUMP_TO_DEF_ARGUMENT,
  66. OPCODE_RETURN,
  67. OPCODE_ITERATE_BEGIN,
  68. OPCODE_ITERATE,
  69. OPCODE_ASSERT,
  70. OPCODE_BREAKPOINT,
  71. OPCODE_LINE,
  72. OPCODE_END
  73. };
  74. enum Address {
  75. ADDR_BITS=24,
  76. ADDR_MASK=((1<<ADDR_BITS)-1),
  77. ADDR_TYPE_MASK=~ADDR_MASK,
  78. ADDR_TYPE_SELF=0,
  79. ADDR_TYPE_CLASS=1,
  80. ADDR_TYPE_MEMBER=2,
  81. ADDR_TYPE_CLASS_CONSTANT=3,
  82. ADDR_TYPE_LOCAL_CONSTANT=4,
  83. ADDR_TYPE_STACK=5,
  84. ADDR_TYPE_STACK_VARIABLE=6,
  85. ADDR_TYPE_GLOBAL=7,
  86. ADDR_TYPE_NIL=8
  87. };
  88. struct StackDebug {
  89. int line;
  90. int pos;
  91. bool added;
  92. StringName identifier;
  93. };
  94. private:
  95. friend class GDCompiler;
  96. StringName source;
  97. mutable Variant nil;
  98. mutable Variant *_constants_ptr;
  99. int _constant_count;
  100. const StringName *_global_names_ptr;
  101. int _global_names_count;
  102. const int *_default_arg_ptr;
  103. int _default_arg_count;
  104. const int *_code_ptr;
  105. int _code_size;
  106. int _argument_count;
  107. int _stack_size;
  108. int _call_size;
  109. int _initial_line;
  110. bool _static;
  111. GDScript *_script;
  112. StringName name;
  113. Vector<Variant> constants;
  114. Vector<StringName> global_names;
  115. Vector<int> default_arguments;
  116. Vector<int> code;
  117. #ifdef TOOLS_ENABLED
  118. Vector<StringName> arg_names;
  119. #endif
  120. List<StackDebug> stack_debug;
  121. _FORCE_INLINE_ Variant *_get_variant(int p_address,GDInstance *p_instance,GDScript *p_script,Variant &self,Variant *p_stack,String& r_error) const;
  122. _FORCE_INLINE_ String _get_call_error(const Variant::CallError& p_err, const String& p_where,const Variant**argptrs) const;
  123. friend class GDScriptLanguage;
  124. SelfList<GDFunction> function_list;
  125. #ifdef DEBUG_ENABLED
  126. CharString func_cname;
  127. const char*_func_cname;
  128. struct Profile {
  129. StringName signature;
  130. uint64_t call_count;
  131. uint64_t self_time;
  132. uint64_t total_time;
  133. uint64_t frame_call_count;
  134. uint64_t frame_self_time;
  135. uint64_t frame_total_time;
  136. uint64_t last_frame_call_count;
  137. uint64_t last_frame_self_time;
  138. uint64_t last_frame_total_time;
  139. } profile;
  140. #endif
  141. public:
  142. struct CallState {
  143. GDInstance *instance;
  144. Vector<uint8_t> stack;
  145. int stack_size;
  146. Variant self;
  147. uint32_t alloca_size;
  148. GDScript *_class;
  149. int ip;
  150. int line;
  151. int defarg;
  152. Variant result;
  153. };
  154. _FORCE_INLINE_ bool is_static() const { return _static; }
  155. const int* get_code() const; //used for debug
  156. int get_code_size() const;
  157. Variant get_constant(int p_idx) const;
  158. StringName get_global_name(int p_idx) const;
  159. StringName get_name() const;
  160. int get_max_stack_size() const;
  161. int get_default_argument_count() const;
  162. int get_default_argument_addr(int p_idx) const;
  163. GDScript *get_script() const { return _script; }
  164. void debug_get_stack_member_state(int p_line,List<Pair<StringName,int> > *r_stackvars) const;
  165. _FORCE_INLINE_ bool is_empty() const { return _code_size==0; }
  166. int get_argument_count() const { return _argument_count; }
  167. StringName get_argument_name(int p_idx) const {
  168. #ifdef TOOLS_ENABLED
  169. ERR_FAIL_INDEX_V(p_idx,arg_names.size(),StringName());
  170. return arg_names[p_idx];
  171. #endif
  172. return StringName();
  173. }
  174. Variant get_default_argument(int p_idx) const {
  175. ERR_FAIL_INDEX_V(p_idx,default_arguments.size(),Variant());
  176. return default_arguments[p_idx];
  177. }
  178. Variant call(GDInstance *p_instance,const Variant **p_args, int p_argcount,Variant::CallError& r_err,CallState *p_state=NULL);
  179. GDFunction();
  180. ~GDFunction();
  181. };
  182. class GDFunctionState : public Reference {
  183. OBJ_TYPE(GDFunctionState,Reference);
  184. friend class GDFunction;
  185. GDFunction *function;
  186. GDFunction::CallState state;
  187. Variant _signal_callback(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  188. protected:
  189. static void _bind_methods();
  190. public:
  191. bool is_valid() const;
  192. Variant resume(const Variant& p_arg=Variant());
  193. GDFunctionState();
  194. ~GDFunctionState();
  195. };
  196. class GDNativeClass : public Reference {
  197. OBJ_TYPE(GDNativeClass,Reference);
  198. StringName name;
  199. protected:
  200. bool _get(const StringName& p_name,Variant &r_ret) const;
  201. static void _bind_methods();
  202. public:
  203. _FORCE_INLINE_ const StringName& get_name() const { return name; }
  204. Variant _new();
  205. Object *instance();
  206. GDNativeClass(const StringName& p_name);
  207. };
  208. class GDScript : public Script {
  209. OBJ_TYPE(GDScript,Script);
  210. bool tool;
  211. bool valid;
  212. struct MemberInfo {
  213. int index;
  214. StringName setter;
  215. StringName getter;
  216. };
  217. friend class GDInstance;
  218. friend class GDFunction;
  219. friend class GDCompiler;
  220. friend class GDFunctions;
  221. friend class GDScriptLanguage;
  222. Variant _static_ref; //used for static call
  223. Ref<GDNativeClass> native;
  224. Ref<GDScript> base;
  225. GDScript *_base; //fast pointer access
  226. GDScript *_owner; //for subclasses
  227. Set<StringName> members; //members are just indices to the instanced script.
  228. Map<StringName,Variant> constants;
  229. Map<StringName,GDFunction*> member_functions;
  230. Map<StringName,MemberInfo> member_indices; //members are just indices to the instanced script.
  231. Map<StringName,Ref<GDScript> > subclasses;
  232. Map<StringName,Vector<StringName> > _signals;
  233. #ifdef TOOLS_ENABLED
  234. Map<StringName,Variant> member_default_values;
  235. List<PropertyInfo> members_cache;
  236. Map<StringName,Variant> member_default_values_cache;
  237. Ref<GDScript> base_cache;
  238. Set<ObjectID> inheriters_cache;
  239. bool source_changed_cache;
  240. void _update_exports_values(Map<StringName,Variant>& values, List<PropertyInfo> &propnames);
  241. #endif
  242. Map<StringName,PropertyInfo> member_info;
  243. GDFunction *initializer; //direct pointer to _init , faster to locate
  244. int subclass_count;
  245. Set<Object*> instances;
  246. //exported members
  247. String source;
  248. String path;
  249. String name;
  250. GDInstance* _create_instance(const Variant** p_args,int p_argcount,Object *p_owner,bool p_isref,Variant::CallError &r_error);
  251. void _set_subclass_path(Ref<GDScript>& p_sc,const String& p_path);
  252. #ifdef TOOLS_ENABLED
  253. Set<PlaceHolderScriptInstance*> placeholders;
  254. //void _update_placeholder(PlaceHolderScriptInstance *p_placeholder);
  255. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder);
  256. #endif
  257. bool _update_exports();
  258. protected:
  259. bool _get(const StringName& p_name,Variant &r_ret) const;
  260. bool _set(const StringName& p_name, const Variant& p_value);
  261. void _get_property_list(List<PropertyInfo> *p_properties) const;
  262. Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  263. // void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  264. static void _bind_methods();
  265. public:
  266. bool is_valid() const { return valid; }
  267. const Map<StringName,Ref<GDScript> >& get_subclasses() const { return subclasses; }
  268. const Map<StringName,Variant >& get_constants() const { return constants; }
  269. const Set<StringName>& get_members() const { return members; }
  270. const Map<StringName,GDFunction*>& get_member_functions() const { return member_functions; }
  271. const Ref<GDNativeClass>& get_native() const { return native; }
  272. virtual bool has_script_signal(const StringName& p_signal) const;
  273. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const;
  274. bool is_tool() const { return tool; }
  275. Ref<GDScript> get_base() const;
  276. const Map<StringName,MemberInfo>& debug_get_member_indices() const { return member_indices; }
  277. const Map<StringName,GDFunction*>& debug_get_member_functions() const; //this is debug only
  278. StringName debug_get_member_by_index(int p_idx) const;
  279. Variant _new(const Variant** p_args,int p_argcount,Variant::CallError& r_error);
  280. virtual bool can_instance() const;
  281. virtual StringName get_instance_base_type() const; // this may not work in all scripts, will return empty if so
  282. virtual ScriptInstance* instance_create(Object *p_this);
  283. virtual bool instance_has(const Object *p_this) const;
  284. virtual bool has_source_code() const;
  285. virtual String get_source_code() const;
  286. virtual void set_source_code(const String& p_code);
  287. virtual void update_exports();
  288. virtual Error reload();
  289. virtual String get_node_type() const;
  290. void set_script_path(const String& p_path) { path=p_path; } //because subclasses need a path too...
  291. Error load_source_code(const String& p_path);
  292. Error load_byte_code(const String& p_path);
  293. Vector<uint8_t> get_as_byte_code() const;
  294. bool get_property_default_value(const StringName& p_property,Variant& r_value) const;
  295. virtual ScriptLanguage *get_language() const;
  296. GDScript();
  297. ~GDScript();
  298. };
  299. class GDInstance : public ScriptInstance {
  300. friend class GDScript;
  301. friend class GDFunction;
  302. friend class GDFunctions;
  303. Object *owner;
  304. Ref<GDScript> script;
  305. Vector<Variant> members;
  306. bool base_ref;
  307. void _ml_call_reversed(GDScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount);
  308. public:
  309. virtual bool set(const StringName& p_name, const Variant& p_value);
  310. virtual bool get(const StringName& p_name, Variant &r_ret) const;
  311. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  312. virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const;
  313. virtual void get_method_list(List<MethodInfo> *p_list) const;
  314. virtual bool has_method(const StringName& p_method) const;
  315. virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  316. virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  317. virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
  318. Variant debug_get_member_by_index(int p_idx) const { return members[p_idx]; }
  319. virtual void notification(int p_notification);
  320. virtual Ref<Script> get_script() const;
  321. virtual ScriptLanguage *get_language();
  322. void set_path(const String& p_path);
  323. GDInstance();
  324. ~GDInstance();
  325. };
  326. class GDScriptLanguage : public ScriptLanguage {
  327. static GDScriptLanguage *singleton;
  328. Variant* _global_array;
  329. Vector<Variant> global_array;
  330. Map<StringName,int> globals;
  331. struct CallLevel {
  332. Variant *stack;
  333. GDFunction *function;
  334. GDInstance *instance;
  335. int *ip;
  336. int *line;
  337. };
  338. int _debug_parse_err_line;
  339. String _debug_parse_err_file;
  340. String _debug_error;
  341. int _debug_call_stack_pos;
  342. int _debug_max_call_stack;
  343. CallLevel *_call_stack;
  344. void _add_global(const StringName& p_name,const Variant& p_value);
  345. Mutex *lock;
  346. friend class GDFunction;
  347. SelfList<GDFunction>::List function_list;
  348. bool profiling;
  349. uint64_t script_frame_time;
  350. public:
  351. int calls;
  352. bool debug_break(const String& p_error,bool p_allow_continue=true);
  353. bool debug_break_parse(const String& p_file, int p_line,const String& p_error);
  354. _FORCE_INLINE_ void enter_function(GDInstance *p_instance,GDFunction *p_function, Variant *p_stack, int *p_ip, int *p_line) {
  355. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  356. return; //no support for other threads than main for now
  357. if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
  358. ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() +1 );
  359. if (_debug_call_stack_pos >= _debug_max_call_stack) {
  360. //stack overflow
  361. _debug_error="Stack Overflow (Stack Size: "+itos(_debug_max_call_stack)+")";
  362. ScriptDebugger::get_singleton()->debug(this);
  363. return;
  364. }
  365. _call_stack[_debug_call_stack_pos].stack=p_stack;
  366. _call_stack[_debug_call_stack_pos].instance=p_instance;
  367. _call_stack[_debug_call_stack_pos].function=p_function;
  368. _call_stack[_debug_call_stack_pos].ip=p_ip;
  369. _call_stack[_debug_call_stack_pos].line=p_line;
  370. _debug_call_stack_pos++;
  371. }
  372. _FORCE_INLINE_ void exit_function() {
  373. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  374. return; //no support for other threads than main for now
  375. if (ScriptDebugger::get_singleton()->get_lines_left()>0 && ScriptDebugger::get_singleton()->get_depth()>=0)
  376. ScriptDebugger::get_singleton()->set_depth( ScriptDebugger::get_singleton()->get_depth() -1 );
  377. if (_debug_call_stack_pos==0) {
  378. _debug_error="Stack Underflow (Engine Bug)";
  379. ScriptDebugger::get_singleton()->debug(this);
  380. return;
  381. }
  382. _debug_call_stack_pos--;
  383. }
  384. virtual Vector<StackInfo> debug_get_current_stack_info() {
  385. if (Thread::get_main_ID()!=Thread::get_caller_ID())
  386. return Vector<StackInfo>();
  387. Vector<StackInfo> csi;
  388. csi.resize(_debug_call_stack_pos);
  389. for(int i=0;i<_debug_call_stack_pos;i++) {
  390. csi[_debug_call_stack_pos-i-1].line=_call_stack[i].line?*_call_stack[i].line:0;
  391. csi[_debug_call_stack_pos-i-1].script=Ref<GDScript>(_call_stack[i].function->get_script());
  392. }
  393. return csi;
  394. }
  395. struct {
  396. StringName _init;
  397. StringName _notification;
  398. StringName _set;
  399. StringName _get;
  400. StringName _get_property_list;
  401. StringName _script_source;
  402. } strings;
  403. _FORCE_INLINE_ int get_global_array_size() const { return global_array.size(); }
  404. _FORCE_INLINE_ Variant* get_global_array() { return _global_array; }
  405. _FORCE_INLINE_ const Map<StringName,int>& get_global_map() { return globals; }
  406. _FORCE_INLINE_ static GDScriptLanguage *get_singleton() { return singleton; }
  407. virtual String get_name() const;
  408. /* LANGUAGE FUNCTIONS */
  409. virtual void init();
  410. virtual String get_type() const;
  411. virtual String get_extension() const;
  412. virtual Error execute_file(const String& p_path) ;
  413. virtual void finish();
  414. /* EDITOR FUNCTIONS */
  415. virtual void get_reserved_words(List<String> *p_words) const;
  416. virtual void get_comment_delimiters(List<String> *p_delimiters) const;
  417. virtual void get_string_delimiters(List<String> *p_delimiters) const;
  418. virtual String get_template(const String& p_class_name, const String& p_base_class_name) const;
  419. 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;
  420. virtual Script *create_script() const;
  421. virtual bool has_named_classes() const;
  422. virtual int find_function(const String& p_function,const String& p_code) const;
  423. virtual String make_function(const String& p_class,const String& p_name,const StringArray& p_args) const;
  424. virtual Error complete_code(const String& p_code, const String& p_base_path, Object*p_owner,List<String>* r_options,String& r_call_hint);
  425. virtual void auto_indent_code(String& p_code,int p_from_line,int p_to_line) const;
  426. virtual void add_global_constant(const StringName& p_variable,const Variant& p_value);
  427. /* DEBUGGER FUNCTIONS */
  428. virtual String debug_get_error() const;
  429. virtual int debug_get_stack_level_count() const;
  430. virtual int debug_get_stack_level_line(int p_level) const;
  431. virtual String debug_get_stack_level_function(int p_level) const;
  432. virtual String debug_get_stack_level_source(int p_level) const;
  433. 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);
  434. 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);
  435. virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1);
  436. virtual String debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems=-1,int p_max_depth=-1);
  437. virtual void frame();
  438. virtual void get_public_functions(List<MethodInfo> *p_functions) const;
  439. virtual void get_public_constants(List<Pair<String,Variant> > *p_constants) const;
  440. virtual void profiling_start();
  441. virtual void profiling_stop();
  442. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max);
  443. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max);
  444. /* LOADER FUNCTIONS */
  445. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  446. GDScriptLanguage();
  447. ~GDScriptLanguage();
  448. };
  449. class ResourceFormatLoaderGDScript : public ResourceFormatLoader {
  450. public:
  451. virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL);
  452. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  453. virtual bool handles_type(const String& p_type) const;
  454. virtual String get_resource_type(const String &p_path) const;
  455. };
  456. class ResourceFormatSaverGDScript : public ResourceFormatSaver {
  457. public:
  458. virtual Error save(const String &p_path,const RES& p_resource,uint32_t p_flags=0);
  459. virtual void get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const;
  460. virtual bool recognize(const RES& p_resource) const;
  461. };
  462. #endif // GD_SCRIPT_H