script_language_extension.h 32 KB

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