script_language_extension.h 33 KB

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