script_language_extension.h 33 KB

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