script_language_extension.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /**************************************************************************/
  2. /* script_language_extension.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. #ifndef SCRIPT_LANGUAGE_EXTENSION_H
  31. #define SCRIPT_LANGUAGE_EXTENSION_H
  32. #include "core/extension/ext_wrappers.gen.inc"
  33. #include "core/object/gdvirtual.gen.inc"
  34. #include "core/object/script_language.h"
  35. #include "core/variant/native_ptr.h"
  36. #include "core/variant/typed_array.h"
  37. class ScriptExtension : public Script {
  38. GDCLASS(ScriptExtension, Script)
  39. protected:
  40. EXBIND0R(bool, editor_can_reload_from_file)
  41. GDVIRTUAL1(_placeholder_erased, GDExtensionPtr<void>)
  42. virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override {
  43. GDVIRTUAL_CALL(_placeholder_erased, p_placeholder);
  44. }
  45. static void _bind_methods();
  46. public:
  47. EXBIND0RC(bool, can_instantiate)
  48. EXBIND0RC(Ref<Script>, get_base_script)
  49. EXBIND0RC(StringName, get_global_name)
  50. EXBIND1RC(bool, inherits_script, const Ref<Script> &)
  51. EXBIND0RC(StringName, get_instance_base_type)
  52. GDVIRTUAL1RC_REQUIRED(GDExtensionPtr<void>, _instance_create, Object *)
  53. virtual ScriptInstance *instance_create(Object *p_this) override {
  54. GDExtensionPtr<void> ret = nullptr;
  55. GDVIRTUAL_CALL(_instance_create, p_this, ret);
  56. return reinterpret_cast<ScriptInstance *>(ret.operator void *());
  57. }
  58. GDVIRTUAL1RC_REQUIRED(GDExtensionPtr<void>, _placeholder_instance_create, Object *)
  59. PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override {
  60. GDExtensionPtr<void> ret = nullptr;
  61. GDVIRTUAL_CALL(_placeholder_instance_create, p_this, ret);
  62. return reinterpret_cast<PlaceHolderScriptInstance *>(ret.operator void *());
  63. }
  64. EXBIND1RC(bool, instance_has, const Object *)
  65. EXBIND0RC(bool, has_source_code)
  66. EXBIND0RC(String, get_source_code)
  67. EXBIND1(set_source_code, const String &)
  68. EXBIND1R(Error, reload, bool)
  69. GDVIRTUAL0RC_REQUIRED(StringName, _get_doc_class_name)
  70. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_documentation)
  71. GDVIRTUAL0RC(String, _get_class_icon_path)
  72. #ifdef TOOLS_ENABLED
  73. virtual StringName get_doc_class_name() const override {
  74. StringName ret;
  75. GDVIRTUAL_CALL(_get_doc_class_name, ret);
  76. return ret;
  77. }
  78. virtual Vector<DocData::ClassDoc> get_documentation() const override {
  79. TypedArray<Dictionary> doc;
  80. GDVIRTUAL_CALL(_get_documentation, doc);
  81. Vector<DocData::ClassDoc> class_doc;
  82. for (int i = 0; i < doc.size(); i++) {
  83. class_doc.append(DocData::ClassDoc::from_dict(doc[i]));
  84. }
  85. return class_doc;
  86. }
  87. virtual String get_class_icon_path() const override {
  88. String ret;
  89. GDVIRTUAL_CALL(_get_class_icon_path, ret);
  90. return ret;
  91. }
  92. #endif // TOOLS_ENABLED
  93. EXBIND1RC(bool, has_method, const StringName &)
  94. EXBIND1RC(bool, has_static_method, const StringName &)
  95. GDVIRTUAL1RC(Variant, _get_script_method_argument_count, const StringName &)
  96. virtual int get_script_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override {
  97. Variant ret;
  98. if (GDVIRTUAL_CALL(_get_script_method_argument_count, p_method, ret) && ret.get_type() == Variant::INT) {
  99. if (r_is_valid) {
  100. *r_is_valid = true;
  101. }
  102. return ret.operator int();
  103. }
  104. // Fallback to default.
  105. return Script::get_script_method_argument_count(p_method, r_is_valid);
  106. }
  107. GDVIRTUAL1RC_REQUIRED(Dictionary, _get_method_info, const StringName &)
  108. virtual MethodInfo get_method_info(const StringName &p_method) const override {
  109. Dictionary mi;
  110. GDVIRTUAL_CALL(_get_method_info, p_method, mi);
  111. return MethodInfo::from_dict(mi);
  112. }
  113. EXBIND0RC(bool, is_tool)
  114. EXBIND0RC(bool, is_valid)
  115. virtual bool is_abstract() const override {
  116. bool abst;
  117. return GDVIRTUAL_CALL(_is_abstract, abst) && abst;
  118. }
  119. GDVIRTUAL0RC(bool, _is_abstract)
  120. EXBIND0RC(ScriptLanguage *, get_language)
  121. EXBIND1RC(bool, has_script_signal, const StringName &)
  122. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_signal_list)
  123. virtual void get_script_signal_list(List<MethodInfo> *r_signals) const override {
  124. TypedArray<Dictionary> sl;
  125. GDVIRTUAL_CALL(_get_script_signal_list, sl);
  126. for (int i = 0; i < sl.size(); i++) {
  127. r_signals->push_back(MethodInfo::from_dict(sl[i]));
  128. }
  129. }
  130. GDVIRTUAL1RC_REQUIRED(bool, _has_property_default_value, const StringName &)
  131. GDVIRTUAL1RC_REQUIRED(Variant, _get_property_default_value, const StringName &)
  132. virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const override {
  133. bool has_dv = false;
  134. if (!GDVIRTUAL_CALL(_has_property_default_value, p_property, has_dv) || !has_dv) {
  135. return false;
  136. }
  137. Variant ret;
  138. GDVIRTUAL_CALL(_get_property_default_value, p_property, ret);
  139. r_value = ret;
  140. return true;
  141. }
  142. EXBIND0(update_exports)
  143. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_method_list)
  144. virtual void get_script_method_list(List<MethodInfo> *r_methods) const override {
  145. TypedArray<Dictionary> sl;
  146. GDVIRTUAL_CALL(_get_script_method_list, sl);
  147. for (int i = 0; i < sl.size(); i++) {
  148. r_methods->push_back(MethodInfo::from_dict(sl[i]));
  149. }
  150. }
  151. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_property_list)
  152. virtual void get_script_property_list(List<PropertyInfo> *r_propertys) const override {
  153. TypedArray<Dictionary> sl;
  154. GDVIRTUAL_CALL(_get_script_property_list, sl);
  155. for (int i = 0; i < sl.size(); i++) {
  156. r_propertys->push_back(PropertyInfo::from_dict(sl[i]));
  157. }
  158. }
  159. EXBIND1RC(int, get_member_line, const StringName &)
  160. GDVIRTUAL0RC_REQUIRED(Dictionary, _get_constants)
  161. virtual void get_constants(HashMap<StringName, Variant> *p_constants) override {
  162. Dictionary constants;
  163. GDVIRTUAL_CALL(_get_constants, constants);
  164. List<Variant> keys;
  165. constants.get_key_list(&keys);
  166. for (const Variant &K : keys) {
  167. p_constants->insert(K, constants[K]);
  168. }
  169. }
  170. GDVIRTUAL0RC_REQUIRED(TypedArray<StringName>, _get_members)
  171. virtual void get_members(HashSet<StringName> *p_members) override {
  172. TypedArray<StringName> members;
  173. GDVIRTUAL_CALL(_get_members, members);
  174. for (int i = 0; i < members.size(); i++) {
  175. p_members->insert(members[i]);
  176. }
  177. }
  178. EXBIND0RC(bool, is_placeholder_fallback_enabled)
  179. GDVIRTUAL0RC_REQUIRED(Variant, _get_rpc_config)
  180. virtual Variant get_rpc_config() const override {
  181. Variant ret;
  182. GDVIRTUAL_CALL(_get_rpc_config, ret);
  183. return ret;
  184. }
  185. ScriptExtension() {}
  186. };
  187. typedef ScriptLanguage::ProfilingInfo ScriptLanguageExtensionProfilingInfo;
  188. GDVIRTUAL_NATIVE_PTR(ScriptLanguageExtensionProfilingInfo)
  189. class ScriptLanguageExtension : public ScriptLanguage {
  190. GDCLASS(ScriptLanguageExtension, ScriptLanguage)
  191. protected:
  192. static void _bind_methods();
  193. public:
  194. EXBIND0RC(String, get_name)
  195. EXBIND0(init)
  196. EXBIND0RC(String, get_type)
  197. EXBIND0RC(String, get_extension)
  198. EXBIND0(finish)
  199. /* EDITOR FUNCTIONS */
  200. GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_reserved_words)
  201. virtual void get_reserved_words(List<String> *p_words) const override {
  202. Vector<String> ret;
  203. GDVIRTUAL_CALL(_get_reserved_words, ret);
  204. for (int i = 0; i < ret.size(); i++) {
  205. p_words->push_back(ret[i]);
  206. }
  207. }
  208. EXBIND1RC(bool, is_control_flow_keyword, const String &)
  209. GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_comment_delimiters)
  210. virtual void get_comment_delimiters(List<String> *p_words) const override {
  211. Vector<String> ret;
  212. GDVIRTUAL_CALL(_get_comment_delimiters, ret);
  213. for (int i = 0; i < ret.size(); i++) {
  214. p_words->push_back(ret[i]);
  215. }
  216. }
  217. GDVIRTUAL0RC(Vector<String>, _get_doc_comment_delimiters)
  218. virtual void get_doc_comment_delimiters(List<String> *p_words) const override {
  219. Vector<String> ret;
  220. GDVIRTUAL_CALL(_get_doc_comment_delimiters, ret);
  221. for (int i = 0; i < ret.size(); i++) {
  222. p_words->push_back(ret[i]);
  223. }
  224. }
  225. GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_string_delimiters)
  226. virtual void get_string_delimiters(List<String> *p_words) const override {
  227. Vector<String> ret;
  228. GDVIRTUAL_CALL(_get_string_delimiters, ret);
  229. for (int i = 0; i < ret.size(); i++) {
  230. p_words->push_back(ret[i]);
  231. }
  232. }
  233. EXBIND3RC(Ref<Script>, make_template, const String &, const String &, const String &)
  234. GDVIRTUAL1RC_REQUIRED(TypedArray<Dictionary>, _get_built_in_templates, StringName)
  235. virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) override {
  236. TypedArray<Dictionary> ret;
  237. GDVIRTUAL_CALL(_get_built_in_templates, p_object, ret);
  238. Vector<ScriptTemplate> stret;
  239. for (int i = 0; i < ret.size(); i++) {
  240. Dictionary d = ret[i];
  241. ScriptTemplate st;
  242. ERR_CONTINUE(!d.has("inherit"));
  243. st.inherit = d["inherit"];
  244. ERR_CONTINUE(!d.has("name"));
  245. st.name = d["name"];
  246. ERR_CONTINUE(!d.has("description"));
  247. st.description = d["description"];
  248. ERR_CONTINUE(!d.has("content"));
  249. st.content = d["content"];
  250. ERR_CONTINUE(!d.has("id"));
  251. st.id = d["id"];
  252. ERR_CONTINUE(!d.has("origin"));
  253. st.origin = TemplateLocation(int(d["origin"]));
  254. stret.push_back(st);
  255. }
  256. return stret;
  257. }
  258. EXBIND0R(bool, is_using_templates)
  259. GDVIRTUAL6RC_REQUIRED(Dictionary, _validate, const String &, const String &, bool, bool, bool, bool)
  260. virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptError> *r_errors = nullptr, List<Warning> *r_warnings = nullptr, HashSet<int> *r_safe_lines = nullptr) const override {
  261. Dictionary ret;
  262. GDVIRTUAL_CALL(_validate, p_script, p_path, r_functions != nullptr, r_errors != nullptr, r_warnings != nullptr, r_safe_lines != nullptr, ret);
  263. if (!ret.has("valid")) {
  264. return false;
  265. }
  266. if (r_functions != nullptr && ret.has("functions")) {
  267. Vector<String> functions = ret["functions"];
  268. for (int i = 0; i < functions.size(); i++) {
  269. r_functions->push_back(functions[i]);
  270. }
  271. }
  272. if (r_errors != nullptr && ret.has("errors")) {
  273. Array errors = ret["errors"];
  274. for (const Variant &error : errors) {
  275. Dictionary err = error;
  276. ERR_CONTINUE(!err.has("line"));
  277. ERR_CONTINUE(!err.has("column"));
  278. ERR_CONTINUE(!err.has("message"));
  279. ScriptError serr;
  280. if (err.has("path")) {
  281. serr.path = err["path"];
  282. }
  283. serr.line = err["line"];
  284. serr.column = err["column"];
  285. serr.message = err["message"];
  286. r_errors->push_back(serr);
  287. }
  288. }
  289. if (r_warnings != nullptr && ret.has("warnings")) {
  290. ERR_FAIL_COND_V(!ret.has("warnings"), false);
  291. Array warnings = ret["warnings"];
  292. for (const Variant &warning : warnings) {
  293. Dictionary warn = warning;
  294. ERR_CONTINUE(!warn.has("start_line"));
  295. ERR_CONTINUE(!warn.has("end_line"));
  296. ERR_CONTINUE(!warn.has("leftmost_column"));
  297. ERR_CONTINUE(!warn.has("rightmost_column"));
  298. ERR_CONTINUE(!warn.has("code"));
  299. ERR_CONTINUE(!warn.has("string_code"));
  300. ERR_CONTINUE(!warn.has("message"));
  301. Warning swarn;
  302. swarn.start_line = warn["start_line"];
  303. swarn.end_line = warn["end_line"];
  304. swarn.leftmost_column = warn["leftmost_column"];
  305. swarn.rightmost_column = warn["rightmost_column"];
  306. swarn.code = warn["code"];
  307. swarn.string_code = warn["string_code"];
  308. swarn.message = warn["message"];
  309. r_warnings->push_back(swarn);
  310. }
  311. }
  312. if (r_safe_lines != nullptr && ret.has("safe_lines")) {
  313. PackedInt32Array safe_lines = ret["safe_lines"];
  314. for (int i = 0; i < safe_lines.size(); i++) {
  315. r_safe_lines->insert(safe_lines[i]);
  316. }
  317. }
  318. return ret["valid"];
  319. }
  320. EXBIND1RC(String, validate_path, const String &)
  321. GDVIRTUAL0RC_REQUIRED(Object *, _create_script)
  322. Script *create_script() const override {
  323. Object *ret = nullptr;
  324. GDVIRTUAL_CALL(_create_script, ret);
  325. return Object::cast_to<Script>(ret);
  326. }
  327. #ifndef DISABLE_DEPRECATED
  328. EXBIND0RC(bool, has_named_classes)
  329. #endif
  330. EXBIND0RC(bool, supports_builtin_mode)
  331. EXBIND0RC(bool, supports_documentation)
  332. EXBIND0RC(bool, can_inherit_from_file)
  333. EXBIND2RC(int, find_function, const String &, const String &)
  334. EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &)
  335. EXBIND0RC(bool, can_make_function)
  336. EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int)
  337. EXBIND0R(bool, overrides_external_editor)
  338. GDVIRTUAL0RC(ScriptNameCasing, _preferred_file_name_casing);
  339. virtual ScriptNameCasing preferred_file_name_casing() const override {
  340. ScriptNameCasing ret;
  341. if (GDVIRTUAL_CALL(_preferred_file_name_casing, ret)) {
  342. return ret;
  343. }
  344. return ScriptNameCasing::SCRIPT_NAME_CASING_SNAKE_CASE;
  345. }
  346. GDVIRTUAL3RC_REQUIRED(Dictionary, _complete_code, const String &, const String &, Object *)
  347. virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<CodeCompletionOption> *r_options, bool &r_force, String &r_call_hint) override {
  348. Dictionary ret;
  349. GDVIRTUAL_CALL(_complete_code, p_code, p_path, p_owner, ret);
  350. if (!ret.has("result")) {
  351. return ERR_UNAVAILABLE;
  352. }
  353. if (r_options != nullptr && ret.has("options")) {
  354. Array options = ret["options"];
  355. for (const Variant &var : options) {
  356. Dictionary op = var;
  357. CodeCompletionOption option;
  358. ERR_CONTINUE(!op.has("kind"));
  359. option.kind = CodeCompletionKind(int(op["kind"]));
  360. ERR_CONTINUE(!op.has("display"));
  361. option.display = op["display"];
  362. ERR_CONTINUE(!op.has("insert_text"));
  363. option.insert_text = op["insert_text"];
  364. ERR_CONTINUE(!op.has("font_color"));
  365. option.font_color = op["font_color"];
  366. ERR_CONTINUE(!op.has("icon"));
  367. option.icon = op["icon"];
  368. ERR_CONTINUE(!op.has("default_value"));
  369. option.default_value = op["default_value"];
  370. ERR_CONTINUE(!op.has("location"));
  371. option.location = op["location"];
  372. if (op.has("matches")) {
  373. PackedInt32Array matches = op["matches"];
  374. ERR_CONTINUE(matches.size() & 1);
  375. for (int j = 0; j < matches.size(); j += 2) {
  376. option.matches.push_back(Pair<int, int>(matches[j], matches[j + 1]));
  377. }
  378. }
  379. r_options->push_back(option);
  380. }
  381. }
  382. ERR_FAIL_COND_V(!ret.has("force"), ERR_UNAVAILABLE);
  383. r_force = ret["force"];
  384. ERR_FAIL_COND_V(!ret.has("call_hint"), ERR_UNAVAILABLE);
  385. r_call_hint = ret["call_hint"];
  386. ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE);
  387. Error result = Error(int(ret["result"]));
  388. return result;
  389. }
  390. GDVIRTUAL4RC_REQUIRED(Dictionary, _lookup_code, const String &, const String &, const String &, Object *)
  391. virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override {
  392. Dictionary ret;
  393. GDVIRTUAL_CALL(_lookup_code, p_code, p_symbol, p_path, p_owner, ret);
  394. ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE);
  395. const Error result = Error(int(ret["result"]));
  396. ERR_FAIL_COND_V(!ret.has("type"), ERR_UNAVAILABLE);
  397. r_result.type = LookupResultType(int(ret["type"]));
  398. r_result.class_name = ret.get("class_name", "");
  399. r_result.class_member = ret.get("class_member", "");
  400. r_result.description = ret.get("description", "");
  401. r_result.is_deprecated = ret.get("is_deprecated", false);
  402. r_result.deprecated_message = ret.get("deprecated_message", "");
  403. r_result.is_deprecated = ret.get("is_deprecated", false);
  404. r_result.experimental_message = ret.get("experimental_message", "");
  405. r_result.doc_type = ret.get("doc_type", "");
  406. r_result.enumeration = ret.get("enumeration", "");
  407. r_result.is_bitfield = ret.get("is_bitfield", false);
  408. r_result.value = ret.get("value", "");
  409. r_result.script = ret.get("script", Ref<Script>());
  410. r_result.script_path = ret.get("script_path", "");
  411. r_result.location = ret.get("location", -1);
  412. return result;
  413. }
  414. GDVIRTUAL3RC_REQUIRED(String, _auto_indent_code, const String &, int, int)
  415. virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {
  416. String ret;
  417. GDVIRTUAL_CALL(_auto_indent_code, p_code, p_from_line, p_to_line, ret);
  418. p_code = ret;
  419. }
  420. EXBIND2(add_global_constant, const StringName &, const Variant &)
  421. EXBIND2(add_named_global_constant, const StringName &, const Variant &)
  422. EXBIND1(remove_named_global_constant, const StringName &)
  423. /* MULTITHREAD FUNCTIONS */
  424. //some VMs need to be notified of thread creation/exiting to allocate a stack
  425. EXBIND0(thread_enter)
  426. EXBIND0(thread_exit)
  427. EXBIND0RC(String, debug_get_error)
  428. EXBIND0RC(int, debug_get_stack_level_count)
  429. EXBIND1RC(int, debug_get_stack_level_line, int)
  430. EXBIND1RC(String, debug_get_stack_level_function, int)
  431. EXBIND1RC(String, debug_get_stack_level_source, int)
  432. GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_locals, int, int, int)
  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) override {
  434. Dictionary ret;
  435. GDVIRTUAL_CALL(_debug_get_stack_level_locals, p_level, p_max_subitems, p_max_depth, ret);
  436. if (ret.size() == 0) {
  437. return;
  438. }
  439. if (p_locals != nullptr && ret.has("locals")) {
  440. PackedStringArray strings = ret["locals"];
  441. for (int i = 0; i < strings.size(); i++) {
  442. p_locals->push_back(strings[i]);
  443. }
  444. }
  445. if (p_values != nullptr && ret.has("values")) {
  446. Array values = ret["values"];
  447. for (const Variant &value : values) {
  448. p_values->push_back(value);
  449. }
  450. }
  451. }
  452. GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_members, int, int, int)
  453. 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 {
  454. Dictionary ret;
  455. GDVIRTUAL_CALL(_debug_get_stack_level_members, p_level, p_max_subitems, p_max_depth, ret);
  456. if (ret.size() == 0) {
  457. return;
  458. }
  459. if (p_members != nullptr && ret.has("members")) {
  460. PackedStringArray strings = ret["members"];
  461. for (int i = 0; i < strings.size(); i++) {
  462. p_members->push_back(strings[i]);
  463. }
  464. }
  465. if (p_values != nullptr && ret.has("values")) {
  466. Array values = ret["values"];
  467. for (const Variant &value : values) {
  468. p_values->push_back(value);
  469. }
  470. }
  471. }
  472. GDVIRTUAL1R_REQUIRED(GDExtensionPtr<void>, _debug_get_stack_level_instance, int)
  473. virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override {
  474. GDExtensionPtr<void> ret = nullptr;
  475. GDVIRTUAL_CALL(_debug_get_stack_level_instance, p_level, ret);
  476. return reinterpret_cast<ScriptInstance *>(ret.operator void *());
  477. }
  478. GDVIRTUAL2R_REQUIRED(Dictionary, _debug_get_globals, int, int)
  479. virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override {
  480. Dictionary ret;
  481. GDVIRTUAL_CALL(_debug_get_globals, p_max_subitems, p_max_depth, ret);
  482. if (ret.size() == 0) {
  483. return;
  484. }
  485. if (p_globals != nullptr && ret.has("globals")) {
  486. PackedStringArray strings = ret["globals"];
  487. for (int i = 0; i < strings.size(); i++) {
  488. p_globals->push_back(strings[i]);
  489. }
  490. }
  491. if (p_values != nullptr && ret.has("values")) {
  492. Array values = ret["values"];
  493. for (const Variant &value : values) {
  494. p_values->push_back(value);
  495. }
  496. }
  497. }
  498. EXBIND4R(String, debug_parse_stack_level_expression, int, const String &, int, int)
  499. GDVIRTUAL0R_REQUIRED(TypedArray<Dictionary>, _debug_get_current_stack_info)
  500. virtual Vector<StackInfo> debug_get_current_stack_info() override {
  501. TypedArray<Dictionary> ret;
  502. GDVIRTUAL_CALL(_debug_get_current_stack_info, ret);
  503. Vector<StackInfo> sret;
  504. for (const Variant &var : ret) {
  505. StackInfo si;
  506. Dictionary d = var;
  507. ERR_CONTINUE(!d.has("file"));
  508. ERR_CONTINUE(!d.has("func"));
  509. ERR_CONTINUE(!d.has("line"));
  510. si.file = d["file"];
  511. si.func = d["func"];
  512. si.line = d["line"];
  513. sret.push_back(si);
  514. }
  515. return sret;
  516. }
  517. EXBIND0(reload_all_scripts)
  518. EXBIND2(reload_scripts, const Array &, bool)
  519. EXBIND2(reload_tool_script, const Ref<Script> &, bool)
  520. /* LOADER FUNCTIONS */
  521. GDVIRTUAL0RC_REQUIRED(PackedStringArray, _get_recognized_extensions)
  522. virtual void get_recognized_extensions(List<String> *p_extensions) const override {
  523. PackedStringArray ret;
  524. GDVIRTUAL_CALL(_get_recognized_extensions, ret);
  525. for (int i = 0; i < ret.size(); i++) {
  526. p_extensions->push_back(ret[i]);
  527. }
  528. }
  529. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_functions)
  530. virtual void get_public_functions(List<MethodInfo> *p_functions) const override {
  531. TypedArray<Dictionary> ret;
  532. GDVIRTUAL_CALL(_get_public_functions, ret);
  533. for (const Variant &var : ret) {
  534. MethodInfo mi = MethodInfo::from_dict(var);
  535. p_functions->push_back(mi);
  536. }
  537. }
  538. GDVIRTUAL0RC_REQUIRED(Dictionary, _get_public_constants)
  539. virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override {
  540. Dictionary ret;
  541. GDVIRTUAL_CALL(_get_public_constants, ret);
  542. for (int i = 0; i < ret.size(); i++) {
  543. Dictionary d = ret[i];
  544. ERR_CONTINUE(!d.has("name"));
  545. ERR_CONTINUE(!d.has("value"));
  546. p_constants->push_back(Pair<String, Variant>(d["name"], d["value"]));
  547. }
  548. }
  549. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_annotations)
  550. virtual void get_public_annotations(List<MethodInfo> *p_annotations) const override {
  551. TypedArray<Dictionary> ret;
  552. GDVIRTUAL_CALL(_get_public_annotations, ret);
  553. for (const Variant &var : ret) {
  554. MethodInfo mi = MethodInfo::from_dict(var);
  555. p_annotations->push_back(mi);
  556. }
  557. }
  558. EXBIND0(profiling_start)
  559. EXBIND0(profiling_stop)
  560. EXBIND1(profiling_set_save_native_calls, bool)
  561. GDVIRTUAL2R_REQUIRED(int, _profiling_get_accumulated_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)
  562. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override {
  563. int ret = 0;
  564. GDVIRTUAL_CALL(_profiling_get_accumulated_data, p_info_arr, p_info_max, ret);
  565. return ret;
  566. }
  567. GDVIRTUAL2R_REQUIRED(int, _profiling_get_frame_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)
  568. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override {
  569. int ret = 0;
  570. GDVIRTUAL_CALL(_profiling_get_frame_data, p_info_arr, p_info_max, ret);
  571. return ret;
  572. }
  573. EXBIND0(frame)
  574. EXBIND1RC(bool, handles_global_class_type, const String &)
  575. GDVIRTUAL1RC_REQUIRED(Dictionary, _get_global_class_name, const String &)
  576. 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 {
  577. Dictionary ret;
  578. GDVIRTUAL_CALL(_get_global_class_name, p_path, ret);
  579. if (!ret.has("name")) {
  580. return String();
  581. }
  582. if (r_base_type != nullptr && ret.has("base_type")) {
  583. *r_base_type = ret["base_type"];
  584. }
  585. if (r_icon_path != nullptr && ret.has("icon_path")) {
  586. *r_icon_path = ret["icon_path"];
  587. }
  588. if (r_is_abstract != nullptr && ret.has("is_abstract")) {
  589. *r_is_abstract = ret["is_abstract"];
  590. }
  591. if (r_is_tool != nullptr && ret.has("is_tool")) {
  592. *r_is_tool = ret["is_tool"];
  593. }
  594. return ret["name"];
  595. }
  596. };
  597. VARIANT_ENUM_CAST(ScriptLanguageExtension::LookupResultType)
  598. VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionKind)
  599. VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionLocation)
  600. class ScriptInstanceExtension : public ScriptInstance {
  601. public:
  602. const GDExtensionScriptInstanceInfo3 *native_info;
  603. #ifndef DISABLE_DEPRECATED
  604. bool free_native_info = false;
  605. struct DeprecatedNativeInfo {
  606. GDExtensionScriptInstanceNotification notification_func = nullptr;
  607. GDExtensionScriptInstanceFreePropertyList free_property_list_func = nullptr;
  608. GDExtensionScriptInstanceFreeMethodList free_method_list_func = nullptr;
  609. };
  610. DeprecatedNativeInfo *deprecated_native_info = nullptr;
  611. #endif // DISABLE_DEPRECATED
  612. GDExtensionScriptInstanceDataPtr instance = nullptr;
  613. // There should not be warnings on explicit casts.
  614. #if defined(__GNUC__) && !defined(__clang__)
  615. #pragma GCC diagnostic push
  616. #pragma GCC diagnostic ignored "-Wignored-qualifiers"
  617. #endif
  618. virtual bool set(const StringName &p_name, const Variant &p_value) override {
  619. if (native_info->set_func) {
  620. return native_info->set_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);
  621. }
  622. return false;
  623. }
  624. virtual bool get(const StringName &p_name, Variant &r_ret) const override {
  625. if (native_info->get_func) {
  626. return native_info->get_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);
  627. }
  628. return false;
  629. }
  630. virtual void get_property_list(List<PropertyInfo> *p_list) const override {
  631. if (native_info->get_property_list_func) {
  632. uint32_t pcount;
  633. const GDExtensionPropertyInfo *pinfo = native_info->get_property_list_func(instance, &pcount);
  634. #ifdef TOOLS_ENABLED
  635. if (pcount > 0) {
  636. if (native_info->get_class_category_func) {
  637. GDExtensionPropertyInfo gdext_class_category;
  638. if (native_info->get_class_category_func(instance, &gdext_class_category)) {
  639. p_list->push_back(PropertyInfo(gdext_class_category));
  640. }
  641. } else {
  642. Ref<Script> script = get_script();
  643. if (script.is_valid()) {
  644. p_list->push_back(script->get_class_category());
  645. }
  646. }
  647. }
  648. #endif // TOOLS_ENABLED
  649. for (uint32_t i = 0; i < pcount; i++) {
  650. p_list->push_back(PropertyInfo(pinfo[i]));
  651. }
  652. if (native_info->free_property_list_func) {
  653. native_info->free_property_list_func(instance, pinfo, pcount);
  654. #ifndef DISABLE_DEPRECATED
  655. } else if (deprecated_native_info && deprecated_native_info->free_property_list_func) {
  656. deprecated_native_info->free_property_list_func(instance, pinfo);
  657. #endif // DISABLE_DEPRECATED
  658. }
  659. }
  660. }
  661. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const override {
  662. if (native_info->get_property_type_func) {
  663. GDExtensionBool is_valid = 0;
  664. GDExtensionVariantType type = native_info->get_property_type_func(instance, (GDExtensionConstStringNamePtr)&p_name, &is_valid);
  665. if (r_is_valid) {
  666. *r_is_valid = is_valid != 0;
  667. }
  668. return Variant::Type(type);
  669. }
  670. return Variant::NIL;
  671. }
  672. virtual void validate_property(PropertyInfo &p_property) const override {
  673. if (native_info->validate_property_func) {
  674. // GDExtension uses a StringName rather than a String for property name.
  675. StringName prop_name = p_property.name;
  676. GDExtensionPropertyInfo gdext_prop = {
  677. (GDExtensionVariantType)p_property.type,
  678. &prop_name,
  679. &p_property.class_name,
  680. (uint32_t)p_property.hint,
  681. &p_property.hint_string,
  682. p_property.usage,
  683. };
  684. if (native_info->validate_property_func(instance, &gdext_prop)) {
  685. p_property.type = (Variant::Type)gdext_prop.type;
  686. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  687. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  688. p_property.hint = (PropertyHint)gdext_prop.hint;
  689. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  690. p_property.usage = gdext_prop.usage;
  691. }
  692. }
  693. }
  694. virtual bool property_can_revert(const StringName &p_name) const override {
  695. if (native_info->property_can_revert_func) {
  696. return native_info->property_can_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name);
  697. }
  698. return false;
  699. }
  700. virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const override {
  701. if (native_info->property_get_revert_func) {
  702. return native_info->property_get_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);
  703. }
  704. return false;
  705. }
  706. virtual Object *get_owner() override {
  707. if (native_info->get_owner_func) {
  708. return (Object *)native_info->get_owner_func(instance);
  709. }
  710. return nullptr;
  711. }
  712. static void _add_property_with_state(GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value, void *p_userdata) {
  713. List<Pair<StringName, Variant>> *state = (List<Pair<StringName, Variant>> *)p_userdata;
  714. state->push_back(Pair<StringName, Variant>(*(const StringName *)p_name, *(const Variant *)p_value));
  715. }
  716. virtual void get_property_state(List<Pair<StringName, Variant>> &state) override {
  717. if (native_info->get_property_state_func) {
  718. native_info->get_property_state_func(instance, _add_property_with_state, &state);
  719. }
  720. }
  721. virtual void get_method_list(List<MethodInfo> *p_list) const override {
  722. if (native_info->get_method_list_func) {
  723. uint32_t mcount;
  724. const GDExtensionMethodInfo *minfo = native_info->get_method_list_func(instance, &mcount);
  725. for (uint32_t i = 0; i < mcount; i++) {
  726. p_list->push_back(MethodInfo(minfo[i]));
  727. }
  728. if (native_info->free_method_list_func) {
  729. native_info->free_method_list_func(instance, minfo, mcount);
  730. #ifndef DISABLE_DEPRECATED
  731. } else if (deprecated_native_info && deprecated_native_info->free_method_list_func) {
  732. deprecated_native_info->free_method_list_func(instance, minfo);
  733. #endif // DISABLE_DEPRECATED
  734. }
  735. }
  736. }
  737. virtual bool has_method(const StringName &p_method) const override {
  738. if (native_info->has_method_func) {
  739. return native_info->has_method_func(instance, (GDExtensionStringNamePtr)&p_method);
  740. }
  741. return false;
  742. }
  743. virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override {
  744. if (native_info->get_method_argument_count_func) {
  745. GDExtensionBool is_valid = 0;
  746. GDExtensionInt ret = native_info->get_method_argument_count_func(instance, (GDExtensionStringNamePtr)&p_method, &is_valid);
  747. if (r_is_valid) {
  748. *r_is_valid = is_valid != 0;
  749. }
  750. return ret;
  751. }
  752. // Fallback to default.
  753. return ScriptInstance::get_method_argument_count(p_method, r_is_valid);
  754. }
  755. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
  756. Variant ret;
  757. if (native_info->call_func) {
  758. GDExtensionCallError ce;
  759. native_info->call_func(instance, (GDExtensionConstStringNamePtr)&p_method, (GDExtensionConstVariantPtr *)p_args, p_argcount, (GDExtensionVariantPtr)&ret, &ce);
  760. r_error.error = Callable::CallError::Error(ce.error);
  761. r_error.argument = ce.argument;
  762. r_error.expected = ce.expected;
  763. }
  764. return ret;
  765. }
  766. virtual void notification(int p_notification, bool p_reversed = false) override {
  767. if (native_info->notification_func) {
  768. native_info->notification_func(instance, p_notification, p_reversed);
  769. #ifndef DISABLE_DEPRECATED
  770. } else if (deprecated_native_info && deprecated_native_info->notification_func) {
  771. deprecated_native_info->notification_func(instance, p_notification);
  772. #endif // DISABLE_DEPRECATED
  773. }
  774. }
  775. virtual String to_string(bool *r_valid) override {
  776. if (native_info->to_string_func) {
  777. GDExtensionBool valid;
  778. String ret;
  779. native_info->to_string_func(instance, &valid, reinterpret_cast<GDExtensionStringPtr>(&ret));
  780. if (r_valid) {
  781. *r_valid = valid != 0;
  782. }
  783. return ret;
  784. }
  785. return String();
  786. }
  787. virtual void refcount_incremented() override {
  788. if (native_info->refcount_incremented_func) {
  789. native_info->refcount_incremented_func(instance);
  790. }
  791. }
  792. virtual bool refcount_decremented() override {
  793. if (native_info->refcount_decremented_func) {
  794. return native_info->refcount_decremented_func(instance);
  795. }
  796. return false;
  797. }
  798. virtual Ref<Script> get_script() const override {
  799. if (native_info->get_script_func) {
  800. GDExtensionObjectPtr script = native_info->get_script_func(instance);
  801. return Ref<Script>(reinterpret_cast<Script *>(script));
  802. }
  803. return Ref<Script>();
  804. }
  805. virtual bool is_placeholder() const override {
  806. if (native_info->is_placeholder_func) {
  807. return native_info->is_placeholder_func(instance);
  808. }
  809. return false;
  810. }
  811. virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) override {
  812. if (native_info->set_fallback_func) {
  813. bool ret = native_info->set_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);
  814. if (r_valid) {
  815. *r_valid = ret;
  816. }
  817. }
  818. }
  819. virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid) override {
  820. Variant ret;
  821. if (native_info->get_fallback_func) {
  822. bool valid = native_info->get_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret);
  823. if (r_valid) {
  824. *r_valid = valid;
  825. }
  826. }
  827. return ret;
  828. }
  829. virtual ScriptLanguage *get_language() override {
  830. if (native_info->get_language_func) {
  831. GDExtensionScriptLanguagePtr lang = native_info->get_language_func(instance);
  832. return reinterpret_cast<ScriptLanguage *>(lang);
  833. }
  834. return nullptr;
  835. }
  836. virtual ~ScriptInstanceExtension() {
  837. if (native_info->free_func) {
  838. native_info->free_func(instance);
  839. }
  840. #ifndef DISABLE_DEPRECATED
  841. if (free_native_info) {
  842. memfree(const_cast<GDExtensionScriptInstanceInfo3 *>(native_info));
  843. }
  844. if (deprecated_native_info) {
  845. memfree(deprecated_native_info);
  846. }
  847. #endif // DISABLE_DEPRECATED
  848. }
  849. #if defined(__GNUC__) && !defined(__clang__)
  850. #pragma GCC diagnostic pop
  851. #endif
  852. };
  853. #endif // SCRIPT_LANGUAGE_EXTENSION_H