gdscript.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**************************************************************************/
  2. /* gdscript.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "gdscript_function.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/debugger/script_debugger.h"
  34. #include "core/doc_data.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/io/resource_saver.h"
  37. #include "core/object/script_language.h"
  38. #include "core/templates/rb_set.h"
  39. class GDScriptNativeClass : public RefCounted {
  40. GDCLASS(GDScriptNativeClass, RefCounted);
  41. StringName name;
  42. protected:
  43. bool _get(const StringName &p_name, Variant &r_ret) const;
  44. static void _bind_methods();
  45. public:
  46. _FORCE_INLINE_ const StringName &get_name() const { return name; }
  47. Variant _new();
  48. Object *instantiate();
  49. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
  50. GDScriptNativeClass(const StringName &p_name);
  51. };
  52. class GDScript : public Script {
  53. GDCLASS(GDScript, Script);
  54. bool tool = false;
  55. bool valid = false;
  56. bool reloading = false;
  57. bool _is_abstract = false;
  58. struct MemberInfo {
  59. int index = 0;
  60. StringName setter;
  61. StringName getter;
  62. GDScriptDataType data_type;
  63. PropertyInfo property_info;
  64. };
  65. struct ClearData {
  66. RBSet<GDScriptFunction *> functions;
  67. RBSet<Ref<Script>> scripts;
  68. void clear() {
  69. functions.clear();
  70. scripts.clear();
  71. }
  72. };
  73. friend class GDScriptInstance;
  74. friend class GDScriptFunction;
  75. friend class GDScriptAnalyzer;
  76. friend class GDScriptCompiler;
  77. friend class GDScriptDocGen;
  78. friend class GDScriptLambdaCallable;
  79. friend class GDScriptLambdaSelfCallable;
  80. friend class GDScriptLanguage;
  81. friend struct GDScriptUtilityFunctionsDefinitions;
  82. Ref<GDScriptNativeClass> native;
  83. Ref<GDScript> base;
  84. GDScript *_owner = nullptr; //for subclasses
  85. // Members are just indices to the instantiated script.
  86. HashMap<StringName, MemberInfo> member_indices; // Includes member info of all base GDScript classes.
  87. HashSet<StringName> members; // Only members of the current class.
  88. // Only static variables of the current class.
  89. HashMap<StringName, MemberInfo> static_variables_indices;
  90. Vector<Variant> static_variables; // Static variable values.
  91. HashMap<StringName, Variant> constants;
  92. HashMap<StringName, GDScriptFunction *> member_functions;
  93. HashMap<StringName, Ref<GDScript>> subclasses;
  94. HashMap<StringName, MethodInfo> _signals;
  95. Dictionary rpc_config;
  96. public:
  97. struct LambdaInfo {
  98. int capture_count;
  99. bool use_self;
  100. };
  101. private:
  102. HashMap<GDScriptFunction *, LambdaInfo> lambda_info;
  103. public:
  104. class UpdatableFuncPtr {
  105. friend class GDScript;
  106. GDScriptFunction *ptr = nullptr;
  107. GDScript *script = nullptr;
  108. List<UpdatableFuncPtr *>::Element *list_element = nullptr;
  109. public:
  110. GDScriptFunction *operator->() const { return ptr; }
  111. operator GDScriptFunction *() const { return ptr; }
  112. UpdatableFuncPtr(GDScriptFunction *p_function);
  113. ~UpdatableFuncPtr();
  114. };
  115. private:
  116. // List is used here because a ptr to elements are stored, so the memory locations need to be stable
  117. List<UpdatableFuncPtr *> func_ptrs_to_update;
  118. Mutex func_ptrs_to_update_mutex;
  119. void _recurse_replace_function_ptrs(const HashMap<GDScriptFunction *, GDScriptFunction *> &p_replacements) const;
  120. #ifdef TOOLS_ENABLED
  121. // For static data storage during hot-reloading.
  122. HashMap<StringName, MemberInfo> old_static_variables_indices;
  123. Vector<Variant> old_static_variables;
  124. void _save_old_static_data();
  125. void _restore_old_static_data();
  126. HashMap<StringName, int> member_lines;
  127. HashMap<StringName, Variant> member_default_values;
  128. List<PropertyInfo> members_cache;
  129. HashMap<StringName, Variant> member_default_values_cache;
  130. Ref<GDScript> base_cache;
  131. HashSet<ObjectID> inheriters_cache;
  132. bool source_changed_cache = false;
  133. bool placeholder_fallback_enabled = false;
  134. void _update_exports_values(HashMap<StringName, Variant> &values, List<PropertyInfo> &propnames);
  135. StringName doc_class_name;
  136. DocData::ClassDoc doc;
  137. Vector<DocData::ClassDoc> docs;
  138. void _add_doc(const DocData::ClassDoc &p_doc);
  139. void _clear_doc();
  140. #endif
  141. GDScriptFunction *initializer = nullptr; // Direct pointer to `new()`/`_init()` member function, faster to locate.
  142. GDScriptFunction *implicit_initializer = nullptr; // `@implicit_new()` special function.
  143. GDScriptFunction *implicit_ready = nullptr; // `@implicit_ready()` special function.
  144. GDScriptFunction *static_initializer = nullptr; // `@static_initializer()` special function.
  145. Error _static_init();
  146. void _static_default_init(); // Initialize static variables with default values based on their types.
  147. RBSet<Object *> instances;
  148. bool destructing = false;
  149. bool clearing = false;
  150. //exported members
  151. String source;
  152. Vector<uint8_t> binary_tokens;
  153. String path;
  154. bool path_valid = false; // False if using default path.
  155. StringName local_name; // Inner class identifier or `class_name`.
  156. StringName global_name; // `class_name`.
  157. String fully_qualified_name;
  158. String simplified_icon_path;
  159. SelfList<GDScript> script_list;
  160. SelfList<GDScriptFunctionState>::List pending_func_states;
  161. GDScriptFunction *_super_constructor(GDScript *p_script);
  162. void _super_implicit_constructor(GDScript *p_script, GDScriptInstance *p_instance, Callable::CallError &r_error);
  163. GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Callable::CallError &r_error);
  164. String _get_debug_path() const;
  165. #ifdef TOOLS_ENABLED
  166. HashSet<PlaceHolderScriptInstance *> placeholders;
  167. //void _update_placeholder(PlaceHolderScriptInstance *p_placeholder);
  168. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override;
  169. void _update_exports_down(bool p_base_exports_changed);
  170. #endif
  171. #ifdef DEBUG_ENABLED
  172. HashMap<ObjectID, List<Pair<StringName, Variant>>> pending_reload_state;
  173. #endif
  174. bool _update_exports(bool *r_err = nullptr, bool p_recursive_call = false, PlaceHolderScriptInstance *p_instance_to_update = nullptr, bool p_base_exports_changed = false);
  175. void _save_orphaned_subclasses();
  176. void _get_script_property_list(List<PropertyInfo> *r_list, bool p_include_base) const;
  177. void _get_script_method_list(List<MethodInfo> *r_list, bool p_include_base) const;
  178. void _get_script_signal_list(List<MethodInfo> *r_list, bool p_include_base) const;
  179. protected:
  180. bool _get(const StringName &p_name, Variant &r_ret) const;
  181. bool _set(const StringName &p_name, const Variant &p_value);
  182. void _get_property_list(List<PropertyInfo> *p_properties) const;
  183. Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
  184. static void _bind_methods();
  185. public:
  186. #ifdef DEBUG_ENABLED
  187. static String debug_get_script_name(const Ref<Script> &p_script);
  188. #endif
  189. static String canonicalize_path(const String &p_path);
  190. _FORCE_INLINE_ static bool is_canonically_equal_paths(const String &p_path_a, const String &p_path_b) {
  191. return canonicalize_path(p_path_a) == canonicalize_path(p_path_b);
  192. }
  193. _FORCE_INLINE_ StringName get_local_name() const { return local_name; }
  194. void clear();
  195. // Cancels all functions of the script that are are waiting to be resumed after using await.
  196. void cancel_pending_functions(bool warn);
  197. virtual bool is_valid() const override { return valid; }
  198. bool inherits_script(const Ref<Script> &p_script) const override;
  199. GDScript *find_class(const String &p_qualified_name);
  200. bool has_class(const GDScript *p_script);
  201. GDScript *get_root_script();
  202. bool is_root_script() const { return _owner == nullptr; }
  203. String get_fully_qualified_name() const { return fully_qualified_name; }
  204. const HashMap<StringName, Ref<GDScript>> &get_subclasses() const { return subclasses; }
  205. const HashMap<StringName, Variant> &get_constants() const { return constants; }
  206. const HashSet<StringName> &get_members() const { return members; }
  207. const GDScriptDataType &get_member_type(const StringName &p_member) const {
  208. CRASH_COND(!member_indices.has(p_member));
  209. return member_indices[p_member].data_type;
  210. }
  211. const Ref<GDScriptNativeClass> &get_native() const { return native; }
  212. _FORCE_INLINE_ const HashMap<StringName, GDScriptFunction *> &get_member_functions() const { return member_functions; }
  213. _FORCE_INLINE_ const HashMap<GDScriptFunction *, LambdaInfo> &get_lambda_info() const { return lambda_info; }
  214. _FORCE_INLINE_ const GDScriptFunction *get_implicit_initializer() const { return implicit_initializer; }
  215. _FORCE_INLINE_ const GDScriptFunction *get_implicit_ready() const { return implicit_ready; }
  216. _FORCE_INLINE_ const GDScriptFunction *get_static_initializer() const { return static_initializer; }
  217. virtual bool has_script_signal(const StringName &p_signal) const override;
  218. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const override;
  219. bool is_tool() const override { return tool; }
  220. bool is_abstract() const override { return _is_abstract; }
  221. Ref<GDScript> get_base() const;
  222. const HashMap<StringName, MemberInfo> &debug_get_member_indices() const { return member_indices; }
  223. const HashMap<StringName, GDScriptFunction *> &debug_get_member_functions() const; //this is debug only
  224. StringName debug_get_member_by_index(int p_idx) const;
  225. StringName debug_get_static_var_by_index(int p_idx) const;
  226. Variant _new(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  227. virtual bool can_instantiate() const override;
  228. virtual Ref<Script> get_base_script() const override;
  229. virtual StringName get_global_name() const override;
  230. virtual StringName get_instance_base_type() const override; // this may not work in all scripts, will return empty if so
  231. virtual ScriptInstance *instance_create(Object *p_this) override;
  232. virtual PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override;
  233. virtual bool instance_has(const Object *p_this) const override;
  234. virtual bool has_source_code() const override;
  235. virtual String get_source_code() const override;
  236. virtual void set_source_code(const String &p_code) override;
  237. virtual void update_exports() override;
  238. #ifdef TOOLS_ENABLED
  239. virtual StringName get_doc_class_name() const override { return doc_class_name; }
  240. virtual Vector<DocData::ClassDoc> get_documentation() const override { return docs; }
  241. virtual String get_class_icon_path() const override;
  242. #endif // TOOLS_ENABLED
  243. virtual Error reload(bool p_keep_state = false) override;
  244. virtual void set_path_cache(const String &p_path) override;
  245. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  246. String get_script_path() const;
  247. Error load_source_code(const String &p_path);
  248. void set_binary_tokens_source(const Vector<uint8_t> &p_binary_tokens);
  249. const Vector<uint8_t> &get_binary_tokens_source() const;
  250. Vector<uint8_t> get_as_binary_tokens() const;
  251. bool get_property_default_value(const StringName &p_property, Variant &r_value) const override;
  252. virtual void get_script_method_list(List<MethodInfo> *p_list) const override;
  253. virtual bool has_method(const StringName &p_method) const override;
  254. virtual bool has_static_method(const StringName &p_method) const override;
  255. virtual int get_script_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override;
  256. virtual MethodInfo get_method_info(const StringName &p_method) const override;
  257. virtual void get_script_property_list(List<PropertyInfo> *p_list) const override;
  258. virtual ScriptLanguage *get_language() const override;
  259. virtual int get_member_line(const StringName &p_member) const override {
  260. #ifdef TOOLS_ENABLED
  261. if (member_lines.has(p_member)) {
  262. return member_lines[p_member];
  263. }
  264. #endif
  265. return -1;
  266. }
  267. virtual void get_constants(HashMap<StringName, Variant> *p_constants) override;
  268. virtual void get_members(HashSet<StringName> *p_members) override;
  269. virtual const Variant get_rpc_config() const override;
  270. void unload_static() const;
  271. #ifdef TOOLS_ENABLED
  272. virtual bool is_placeholder_fallback_enabled() const override { return placeholder_fallback_enabled; }
  273. #endif
  274. GDScript();
  275. ~GDScript();
  276. };
  277. class GDScriptInstance : public ScriptInstance {
  278. friend class GDScript;
  279. friend class GDScriptFunction;
  280. friend class GDScriptLambdaCallable;
  281. friend class GDScriptLambdaSelfCallable;
  282. friend class GDScriptCompiler;
  283. friend class GDScriptCache;
  284. friend struct GDScriptUtilityFunctionsDefinitions;
  285. ObjectID owner_id;
  286. Object *owner = nullptr;
  287. Ref<GDScript> script;
  288. #ifdef DEBUG_ENABLED
  289. HashMap<StringName, int> member_indices_cache; //used only for hot script reloading
  290. #endif
  291. Vector<Variant> members;
  292. SelfList<GDScriptFunctionState>::List pending_func_states;
  293. void _call_implicit_ready_recursively(GDScript *p_script);
  294. public:
  295. virtual Object *get_owner() { return owner; }
  296. virtual bool set(const StringName &p_name, const Variant &p_value);
  297. virtual bool get(const StringName &p_name, Variant &r_ret) const;
  298. virtual void get_property_list(List<PropertyInfo> *p_properties) const;
  299. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const;
  300. virtual void validate_property(PropertyInfo &p_property) const;
  301. virtual bool property_can_revert(const StringName &p_name) const;
  302. virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const;
  303. virtual void get_method_list(List<MethodInfo> *p_list) const;
  304. virtual bool has_method(const StringName &p_method) const;
  305. virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const;
  306. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  307. Variant debug_get_member_by_index(int p_idx) const { return members[p_idx]; }
  308. virtual void notification(int p_notification, bool p_reversed = false);
  309. String to_string(bool *r_valid);
  310. virtual Ref<Script> get_script() const;
  311. virtual ScriptLanguage *get_language();
  312. void set_path(const String &p_path);
  313. void reload_members();
  314. virtual const Variant get_rpc_config() const;
  315. GDScriptInstance() {}
  316. ~GDScriptInstance();
  317. };
  318. class GDScriptLanguage : public ScriptLanguage {
  319. friend class GDScriptFunctionState;
  320. static GDScriptLanguage *singleton;
  321. bool finishing = false;
  322. Variant *_global_array = nullptr;
  323. Vector<Variant> global_array;
  324. HashMap<StringName, int> globals;
  325. HashMap<StringName, Variant> named_globals;
  326. Vector<int> global_array_empty_indexes;
  327. struct CallLevel {
  328. Variant *stack = nullptr;
  329. GDScriptFunction *function = nullptr;
  330. GDScriptInstance *instance = nullptr;
  331. int *ip = nullptr;
  332. int *line = nullptr;
  333. CallLevel *prev = nullptr; // Reverse linked list (stack).
  334. };
  335. static thread_local int _debug_parse_err_line;
  336. static thread_local String _debug_parse_err_file;
  337. static thread_local String _debug_error;
  338. static thread_local CallLevel *_call_stack;
  339. static thread_local uint32_t _call_stack_size;
  340. uint32_t _debug_max_call_stack = 0;
  341. bool track_call_stack = false;
  342. bool track_locals = false;
  343. static CallLevel *_get_stack_level(uint32_t p_level);
  344. void _add_global(const StringName &p_name, const Variant &p_value);
  345. void _remove_global(const StringName &p_name);
  346. String _get_global_class_name(const String &p_path, String *r_base_type, String *r_icon_path, bool *r_is_abstract, bool *r_is_tool, LocalVector<String> &r_visited) const;
  347. friend class GDScriptInstance;
  348. Mutex mutex;
  349. friend class GDScript;
  350. SelfList<GDScript>::List script_list;
  351. friend class GDScriptFunction;
  352. SelfList<GDScriptFunction>::List function_list;
  353. #ifdef DEBUG_ENABLED
  354. bool profiling;
  355. bool profile_native_calls;
  356. uint64_t script_frame_time;
  357. #endif
  358. HashMap<String, ObjectID> orphan_subclasses;
  359. #ifdef TOOLS_ENABLED
  360. void _extension_loaded(const Ref<GDExtension> &p_extension);
  361. void _extension_unloading(const Ref<GDExtension> &p_extension);
  362. #endif
  363. public:
  364. bool debug_break(const String &p_error, bool p_allow_continue = true);
  365. bool debug_break_parse(const String &p_file, int p_line, const String &p_error);
  366. _FORCE_INLINE_ void enter_function(CallLevel *call_level, GDScriptInstance *p_instance, GDScriptFunction *p_function, Variant *p_stack, int *p_ip, int *p_line) {
  367. if (!track_call_stack) {
  368. return;
  369. }
  370. #ifdef DEBUG_ENABLED
  371. ScriptDebugger *script_debugger = EngineDebugger::get_script_debugger();
  372. if (script_debugger != nullptr && script_debugger->get_lines_left() > 0 && script_debugger->get_depth() >= 0) {
  373. script_debugger->set_depth(script_debugger->get_depth() + 1);
  374. }
  375. #endif
  376. if (unlikely(_call_stack_size >= _debug_max_call_stack)) {
  377. _debug_error = vformat("Stack overflow (stack size: %s). Check for infinite recursion in your script.", _debug_max_call_stack);
  378. #ifdef DEBUG_ENABLED
  379. if (script_debugger != nullptr) {
  380. script_debugger->debug(this);
  381. }
  382. #endif
  383. return;
  384. }
  385. call_level->prev = _call_stack;
  386. _call_stack = call_level;
  387. call_level->stack = p_stack;
  388. call_level->instance = p_instance;
  389. call_level->function = p_function;
  390. call_level->ip = p_ip;
  391. call_level->line = p_line;
  392. _call_stack_size++;
  393. }
  394. _FORCE_INLINE_ void exit_function() {
  395. if (!track_call_stack) {
  396. return;
  397. }
  398. #ifdef DEBUG_ENABLED
  399. ScriptDebugger *script_debugger = EngineDebugger::get_script_debugger();
  400. if (script_debugger && script_debugger->get_lines_left() > 0 && script_debugger->get_depth() >= 0) {
  401. script_debugger->set_depth(script_debugger->get_depth() - 1);
  402. }
  403. #endif
  404. if (unlikely(_call_stack_size == 0)) {
  405. #ifdef DEBUG_ENABLED
  406. if (script_debugger) {
  407. _debug_error = "Stack Underflow (Engine Bug)";
  408. script_debugger->debug(this);
  409. } else {
  410. ERR_PRINT("Stack underflow! (Engine Bug)");
  411. }
  412. #else // !DEBUG_ENABLED
  413. ERR_PRINT("Stack underflow! (Engine Bug)");
  414. #endif
  415. return;
  416. }
  417. _call_stack_size--;
  418. _call_stack = _call_stack->prev;
  419. }
  420. virtual Vector<StackInfo> debug_get_current_stack_info() override {
  421. Vector<StackInfo> csi;
  422. csi.resize(_call_stack_size);
  423. CallLevel *cl = _call_stack;
  424. uint32_t idx = 0;
  425. while (cl) {
  426. csi.write[idx].line = *cl->line;
  427. if (cl->function) {
  428. csi.write[idx].func = cl->function->get_name();
  429. csi.write[idx].file = cl->function->get_script()->get_script_path();
  430. }
  431. idx++;
  432. cl = cl->prev;
  433. }
  434. return csi;
  435. }
  436. struct {
  437. StringName _init;
  438. StringName _static_init;
  439. StringName _notification;
  440. StringName _set;
  441. StringName _get;
  442. StringName _get_property_list;
  443. StringName _validate_property;
  444. StringName _property_can_revert;
  445. StringName _property_get_revert;
  446. StringName _script_source;
  447. } strings;
  448. _FORCE_INLINE_ bool should_track_call_stack() const { return track_call_stack; }
  449. _FORCE_INLINE_ bool should_track_locals() const { return track_locals; }
  450. _FORCE_INLINE_ int get_global_array_size() const { return global_array.size(); }
  451. _FORCE_INLINE_ Variant *get_global_array() { return _global_array; }
  452. _FORCE_INLINE_ const HashMap<StringName, int> &get_global_map() const { return globals; }
  453. _FORCE_INLINE_ const HashMap<StringName, Variant> &get_named_globals_map() const { return named_globals; }
  454. // These two functions should be used when behavior needs to be consistent between in-editor and running the scene
  455. bool has_any_global_constant(const StringName &p_name) { return named_globals.has(p_name) || globals.has(p_name); }
  456. Variant get_any_global_constant(const StringName &p_name);
  457. _FORCE_INLINE_ static GDScriptLanguage *get_singleton() { return singleton; }
  458. virtual String get_name() const override;
  459. /* LANGUAGE FUNCTIONS */
  460. virtual void init() override;
  461. virtual String get_type() const override;
  462. virtual String get_extension() const override;
  463. virtual void finish() override;
  464. /* EDITOR FUNCTIONS */
  465. virtual Vector<String> get_reserved_words() const override;
  466. virtual bool is_control_flow_keyword(const String &p_keywords) const override;
  467. virtual Vector<String> get_comment_delimiters() const override;
  468. virtual Vector<String> get_doc_comment_delimiters() const override;
  469. virtual Vector<String> get_string_delimiters() const override;
  470. virtual bool is_using_templates() override;
  471. virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
  472. virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) override;
  473. virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const override;
  474. virtual Script *create_script() const override;
  475. virtual bool supports_builtin_mode() const override;
  476. virtual bool supports_documentation() const override;
  477. virtual bool can_inherit_from_file() const override { return true; }
  478. virtual int find_function(const String &p_function, const String &p_code) const override;
  479. virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
  480. virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) override;
  481. #ifdef TOOLS_ENABLED
  482. virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override;
  483. #endif
  484. virtual String _get_indentation() const;
  485. virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override;
  486. virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) override;
  487. virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value) override;
  488. virtual void remove_named_global_constant(const StringName &p_name) override;
  489. /* DEBUGGER FUNCTIONS */
  490. virtual String debug_get_error() const override;
  491. virtual int debug_get_stack_level_count() const override;
  492. virtual int debug_get_stack_level_line(int p_level) const override;
  493. virtual String debug_get_stack_level_function(int p_level) const override;
  494. virtual String debug_get_stack_level_source(int p_level) const override;
  495. 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) override;
  496. 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) override;
  497. virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override;
  498. virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
  499. virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1) override;
  500. virtual void reload_all_scripts() override;
  501. virtual void reload_scripts(const Array &p_scripts, bool p_soft_reload) override;
  502. virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) override;
  503. virtual void frame() override;
  504. virtual void get_public_functions(List<MethodInfo> *p_functions) const override;
  505. virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override;
  506. virtual void get_public_annotations(List<MethodInfo> *p_annotations) const override;
  507. virtual void profiling_start() override;
  508. virtual void profiling_stop() override;
  509. virtual void profiling_set_save_native_calls(bool p_enable) override;
  510. void profiling_collate_native_call_data(bool p_accumulated);
  511. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override;
  512. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override;
  513. /* LOADER FUNCTIONS */
  514. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  515. /* GLOBAL CLASSES */
  516. virtual bool handles_global_class_type(const String &p_type) const override;
  517. virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr, bool *r_is_abstract = nullptr, bool *r_is_tool = nullptr) const override;
  518. void add_orphan_subclass(const String &p_qualified_name, const ObjectID &p_subclass);
  519. Ref<GDScript> get_orphan_subclass(const String &p_qualified_name);
  520. Ref<GDScript> get_script_by_fully_qualified_name(const String &p_name);
  521. GDScriptLanguage();
  522. ~GDScriptLanguage();
  523. };
  524. class ResourceFormatLoaderGDScript : public ResourceFormatLoader {
  525. GDSOFTCLASS(ResourceFormatLoaderGDScript, ResourceFormatLoader);
  526. public:
  527. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  528. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  529. virtual bool handles_type(const String &p_type) const override;
  530. virtual String get_resource_type(const String &p_path) const override;
  531. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
  532. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override;
  533. };
  534. class ResourceFormatSaverGDScript : public ResourceFormatSaver {
  535. GDSOFTCLASS(ResourceFormatSaverGDScript, ResourceFormatSaver);
  536. public:
  537. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
  538. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
  539. virtual bool recognize(const Ref<Resource> &p_resource) const override;
  540. };