script_language_extension.h 31 KB

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