script_language_extension.h 31 KB

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