script_language_extension.h 29 KB

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