script_language_extension.h 30 KB

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