script_language_extension.h 29 KB

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