gd_script.h 16 KB

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