script_language_extension.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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_REQUIRED(GDExtensionPtr<void>, _instance_create, Object *)
  53. virtual ScriptInstance *instance_create(Object *p_this) override {
  54. GDExtensionPtr<void> ret = nullptr;
  55. GDVIRTUAL_CALL(_instance_create, p_this, ret);
  56. return reinterpret_cast<ScriptInstance *>(ret.operator void *());
  57. }
  58. GDVIRTUAL1RC_REQUIRED(GDExtensionPtr<void>, _placeholder_instance_create, Object *)
  59. PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override {
  60. GDExtensionPtr<void> ret = nullptr;
  61. GDVIRTUAL_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_REQUIRED(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_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_REQUIRED(Dictionary, _get_method_info, const StringName &)
  102. virtual MethodInfo get_method_info(const StringName &p_method) const override {
  103. Dictionary mi;
  104. GDVIRTUAL_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_REQUIRED(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_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_REQUIRED(bool, _has_property_default_value, const StringName &)
  125. GDVIRTUAL1RC_REQUIRED(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_CALL(_has_property_default_value, p_property, has_dv) || !has_dv) {
  129. return false;
  130. }
  131. Variant ret;
  132. GDVIRTUAL_CALL(_get_property_default_value, p_property, ret);
  133. r_value = ret;
  134. return true;
  135. }
  136. EXBIND0(update_exports)
  137. GDVIRTUAL0RC_REQUIRED(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_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_REQUIRED(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_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_REQUIRED(Dictionary, _get_constants)
  155. virtual void get_constants(HashMap<StringName, Variant> *p_constants) override {
  156. Dictionary constants;
  157. GDVIRTUAL_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_REQUIRED(TypedArray<StringName>, _get_members)
  165. virtual void get_members(HashSet<StringName> *p_members) override {
  166. TypedArray<StringName> members;
  167. GDVIRTUAL_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_REQUIRED(Variant, _get_rpc_config)
  174. virtual Variant get_rpc_config() const override {
  175. Variant ret;
  176. GDVIRTUAL_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_REQUIRED(Vector<String>, _get_reserved_words)
  195. virtual void get_reserved_words(List<String> *p_words) const override {
  196. Vector<String> ret;
  197. GDVIRTUAL_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_REQUIRED(Vector<String>, _get_comment_delimiters)
  204. virtual void get_comment_delimiters(List<String> *p_words) const override {
  205. Vector<String> ret;
  206. GDVIRTUAL_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_REQUIRED(Vector<String>, _get_string_delimiters)
  220. virtual void get_string_delimiters(List<String> *p_words) const override {
  221. Vector<String> ret;
  222. GDVIRTUAL_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_REQUIRED(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_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_REQUIRED(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_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_REQUIRED(Object *, _create_script)
  316. Script *create_script() const override {
  317. Object *ret = nullptr;
  318. GDVIRTUAL_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. GDVIRTUAL0RC(ScriptNameCasing, _preferred_file_name_casing);
  333. virtual ScriptNameCasing preferred_file_name_casing() const override {
  334. ScriptNameCasing ret;
  335. if (GDVIRTUAL_CALL(_preferred_file_name_casing, ret)) {
  336. return ret;
  337. }
  338. return ScriptNameCasing::SCRIPT_NAME_CASING_SNAKE_CASE;
  339. }
  340. GDVIRTUAL3RC_REQUIRED(Dictionary, _complete_code, const String &, const String &, Object *)
  341. 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 {
  342. Dictionary ret;
  343. GDVIRTUAL_CALL(_complete_code, p_code, p_path, p_owner, ret);
  344. if (!ret.has("result")) {
  345. return ERR_UNAVAILABLE;
  346. }
  347. if (r_options != nullptr && ret.has("options")) {
  348. Array options = ret["options"];
  349. for (const Variant &var : options) {
  350. Dictionary op = var;
  351. CodeCompletionOption option;
  352. ERR_CONTINUE(!op.has("kind"));
  353. option.kind = CodeCompletionKind(int(op["kind"]));
  354. ERR_CONTINUE(!op.has("display"));
  355. option.display = op["display"];
  356. ERR_CONTINUE(!op.has("insert_text"));
  357. option.insert_text = op["insert_text"];
  358. ERR_CONTINUE(!op.has("font_color"));
  359. option.font_color = op["font_color"];
  360. ERR_CONTINUE(!op.has("icon"));
  361. option.icon = op["icon"];
  362. ERR_CONTINUE(!op.has("default_value"));
  363. option.default_value = op["default_value"];
  364. ERR_CONTINUE(!op.has("location"));
  365. option.location = op["location"];
  366. if (op.has("matches")) {
  367. PackedInt32Array matches = op["matches"];
  368. ERR_CONTINUE(matches.size() & 1);
  369. for (int j = 0; j < matches.size(); j += 2) {
  370. option.matches.push_back(Pair<int, int>(matches[j], matches[j + 1]));
  371. }
  372. }
  373. r_options->push_back(option);
  374. }
  375. }
  376. ERR_FAIL_COND_V(!ret.has("force"), ERR_UNAVAILABLE);
  377. r_force = ret["force"];
  378. ERR_FAIL_COND_V(!ret.has("call_hint"), ERR_UNAVAILABLE);
  379. r_call_hint = ret["call_hint"];
  380. ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE);
  381. Error result = Error(int(ret["result"]));
  382. return result;
  383. }
  384. GDVIRTUAL4RC_REQUIRED(Dictionary, _lookup_code, const String &, const String &, const String &, Object *)
  385. virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override {
  386. Dictionary ret;
  387. GDVIRTUAL_CALL(_lookup_code, p_code, p_symbol, p_path, p_owner, ret);
  388. if (!ret.has("result")) {
  389. return ERR_UNAVAILABLE;
  390. }
  391. ERR_FAIL_COND_V(!ret.has("type"), ERR_UNAVAILABLE);
  392. r_result.type = LookupResultType(int(ret["type"]));
  393. ERR_FAIL_COND_V(!ret.has("script"), ERR_UNAVAILABLE);
  394. r_result.script = ret["script"];
  395. ERR_FAIL_COND_V(!ret.has("class_name"), ERR_UNAVAILABLE);
  396. r_result.class_name = ret["class_name"];
  397. ERR_FAIL_COND_V(!ret.has("class_path"), ERR_UNAVAILABLE);
  398. r_result.class_path = ret["class_path"];
  399. ERR_FAIL_COND_V(!ret.has("location"), ERR_UNAVAILABLE);
  400. r_result.location = ret["location"];
  401. Error result = Error(int(ret["result"]));
  402. return result;
  403. }
  404. GDVIRTUAL3RC_REQUIRED(String, _auto_indent_code, const String &, int, int)
  405. virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {
  406. String ret;
  407. GDVIRTUAL_CALL(_auto_indent_code, p_code, p_from_line, p_to_line, ret);
  408. p_code = ret;
  409. }
  410. EXBIND2(add_global_constant, const StringName &, const Variant &)
  411. EXBIND2(add_named_global_constant, const StringName &, const Variant &)
  412. EXBIND1(remove_named_global_constant, const StringName &)
  413. /* MULTITHREAD FUNCTIONS */
  414. //some VMs need to be notified of thread creation/exiting to allocate a stack
  415. EXBIND0(thread_enter)
  416. EXBIND0(thread_exit)
  417. EXBIND0RC(String, debug_get_error)
  418. EXBIND0RC(int, debug_get_stack_level_count)
  419. EXBIND1RC(int, debug_get_stack_level_line, int)
  420. EXBIND1RC(String, debug_get_stack_level_function, int)
  421. EXBIND1RC(String, debug_get_stack_level_source, int)
  422. GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_locals, int, int, int)
  423. 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 {
  424. Dictionary ret;
  425. GDVIRTUAL_CALL(_debug_get_stack_level_locals, p_level, p_max_subitems, p_max_depth, ret);
  426. if (ret.size() == 0) {
  427. return;
  428. }
  429. if (p_locals != nullptr && ret.has("locals")) {
  430. PackedStringArray strings = ret["locals"];
  431. for (int i = 0; i < strings.size(); i++) {
  432. p_locals->push_back(strings[i]);
  433. }
  434. }
  435. if (p_values != nullptr && ret.has("values")) {
  436. Array values = ret["values"];
  437. for (const Variant &value : values) {
  438. p_values->push_back(value);
  439. }
  440. }
  441. }
  442. GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_members, int, int, int)
  443. 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 {
  444. Dictionary ret;
  445. GDVIRTUAL_CALL(_debug_get_stack_level_members, p_level, p_max_subitems, p_max_depth, ret);
  446. if (ret.size() == 0) {
  447. return;
  448. }
  449. if (p_members != nullptr && ret.has("members")) {
  450. PackedStringArray strings = ret["members"];
  451. for (int i = 0; i < strings.size(); i++) {
  452. p_members->push_back(strings[i]);
  453. }
  454. }
  455. if (p_values != nullptr && ret.has("values")) {
  456. Array values = ret["values"];
  457. for (const Variant &value : values) {
  458. p_values->push_back(value);
  459. }
  460. }
  461. }
  462. GDVIRTUAL1R_REQUIRED(GDExtensionPtr<void>, _debug_get_stack_level_instance, int)
  463. virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override {
  464. GDExtensionPtr<void> ret = nullptr;
  465. GDVIRTUAL_CALL(_debug_get_stack_level_instance, p_level, ret);
  466. return reinterpret_cast<ScriptInstance *>(ret.operator void *());
  467. }
  468. GDVIRTUAL2R_REQUIRED(Dictionary, _debug_get_globals, int, int)
  469. virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override {
  470. Dictionary ret;
  471. GDVIRTUAL_CALL(_debug_get_globals, p_max_subitems, p_max_depth, ret);
  472. if (ret.size() == 0) {
  473. return;
  474. }
  475. if (p_globals != nullptr && ret.has("globals")) {
  476. PackedStringArray strings = ret["globals"];
  477. for (int i = 0; i < strings.size(); i++) {
  478. p_globals->push_back(strings[i]);
  479. }
  480. }
  481. if (p_values != nullptr && ret.has("values")) {
  482. Array values = ret["values"];
  483. for (const Variant &value : values) {
  484. p_values->push_back(value);
  485. }
  486. }
  487. }
  488. EXBIND4R(String, debug_parse_stack_level_expression, int, const String &, int, int)
  489. GDVIRTUAL0R_REQUIRED(TypedArray<Dictionary>, _debug_get_current_stack_info)
  490. virtual Vector<StackInfo> debug_get_current_stack_info() override {
  491. TypedArray<Dictionary> ret;
  492. GDVIRTUAL_CALL(_debug_get_current_stack_info, ret);
  493. Vector<StackInfo> sret;
  494. for (const Variant &var : ret) {
  495. StackInfo si;
  496. Dictionary d = var;
  497. ERR_CONTINUE(!d.has("file"));
  498. ERR_CONTINUE(!d.has("func"));
  499. ERR_CONTINUE(!d.has("line"));
  500. si.file = d["file"];
  501. si.func = d["func"];
  502. si.line = d["line"];
  503. sret.push_back(si);
  504. }
  505. return sret;
  506. }
  507. EXBIND0(reload_all_scripts)
  508. EXBIND2(reload_scripts, const Array &, bool)
  509. EXBIND2(reload_tool_script, const Ref<Script> &, bool)
  510. /* LOADER FUNCTIONS */
  511. GDVIRTUAL0RC_REQUIRED(PackedStringArray, _get_recognized_extensions)
  512. virtual void get_recognized_extensions(List<String> *p_extensions) const override {
  513. PackedStringArray ret;
  514. GDVIRTUAL_CALL(_get_recognized_extensions, ret);
  515. for (int i = 0; i < ret.size(); i++) {
  516. p_extensions->push_back(ret[i]);
  517. }
  518. }
  519. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_functions)
  520. virtual void get_public_functions(List<MethodInfo> *p_functions) const override {
  521. TypedArray<Dictionary> ret;
  522. GDVIRTUAL_CALL(_get_public_functions, ret);
  523. for (const Variant &var : ret) {
  524. MethodInfo mi = MethodInfo::from_dict(var);
  525. p_functions->push_back(mi);
  526. }
  527. }
  528. GDVIRTUAL0RC_REQUIRED(Dictionary, _get_public_constants)
  529. virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override {
  530. Dictionary ret;
  531. GDVIRTUAL_CALL(_get_public_constants, ret);
  532. for (int i = 0; i < ret.size(); i++) {
  533. Dictionary d = ret[i];
  534. ERR_CONTINUE(!d.has("name"));
  535. ERR_CONTINUE(!d.has("value"));
  536. p_constants->push_back(Pair<String, Variant>(d["name"], d["value"]));
  537. }
  538. }
  539. GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_annotations)
  540. virtual void get_public_annotations(List<MethodInfo> *p_annotations) const override {
  541. TypedArray<Dictionary> ret;
  542. GDVIRTUAL_CALL(_get_public_annotations, ret);
  543. for (const Variant &var : ret) {
  544. MethodInfo mi = MethodInfo::from_dict(var);
  545. p_annotations->push_back(mi);
  546. }
  547. }
  548. EXBIND0(profiling_start)
  549. EXBIND0(profiling_stop)
  550. EXBIND1(profiling_set_save_native_calls, bool)
  551. GDVIRTUAL2R_REQUIRED(int, _profiling_get_accumulated_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)
  552. virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override {
  553. int ret = 0;
  554. GDVIRTUAL_CALL(_profiling_get_accumulated_data, p_info_arr, p_info_max, ret);
  555. return ret;
  556. }
  557. GDVIRTUAL2R_REQUIRED(int, _profiling_get_frame_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)
  558. virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override {
  559. int ret = 0;
  560. GDVIRTUAL_CALL(_profiling_get_frame_data, p_info_arr, p_info_max, ret);
  561. return ret;
  562. }
  563. EXBIND0(frame)
  564. EXBIND1RC(bool, handles_global_class_type, const String &)
  565. GDVIRTUAL1RC_REQUIRED(Dictionary, _get_global_class_name, const String &)
  566. virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr) const override {
  567. Dictionary ret;
  568. GDVIRTUAL_CALL(_get_global_class_name, p_path, ret);
  569. if (!ret.has("name")) {
  570. return String();
  571. }
  572. if (r_base_type != nullptr && ret.has("base_type")) {
  573. *r_base_type = ret["base_type"];
  574. }
  575. if (r_icon_path != nullptr && ret.has("icon_path")) {
  576. *r_icon_path = ret["icon_path"];
  577. }
  578. return ret["name"];
  579. }
  580. };
  581. VARIANT_ENUM_CAST(ScriptLanguageExtension::LookupResultType)
  582. VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionKind)
  583. VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionLocation)
  584. class ScriptInstanceExtension : public ScriptInstance {
  585. public:
  586. const GDExtensionScriptInstanceInfo3 *native_info;
  587. #ifndef DISABLE_DEPRECATED
  588. bool free_native_info = false;
  589. struct DeprecatedNativeInfo {
  590. GDExtensionScriptInstanceNotification notification_func = nullptr;
  591. GDExtensionScriptInstanceFreePropertyList free_property_list_func = nullptr;
  592. GDExtensionScriptInstanceFreeMethodList free_method_list_func = nullptr;
  593. };
  594. DeprecatedNativeInfo *deprecated_native_info = nullptr;
  595. #endif // DISABLE_DEPRECATED
  596. GDExtensionScriptInstanceDataPtr instance = nullptr;
  597. // There should not be warnings on explicit casts.
  598. #if defined(__GNUC__) && !defined(__clang__)
  599. #pragma GCC diagnostic push
  600. #pragma GCC diagnostic ignored "-Wignored-qualifiers"
  601. #endif
  602. virtual bool set(const StringName &p_name, const Variant &p_value) override {
  603. if (native_info->set_func) {
  604. return native_info->set_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);
  605. }
  606. return false;
  607. }
  608. virtual bool get(const StringName &p_name, Variant &r_ret) const override {
  609. if (native_info->get_func) {
  610. return native_info->get_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);
  611. }
  612. return false;
  613. }
  614. virtual void get_property_list(List<PropertyInfo> *p_list) const override {
  615. if (native_info->get_property_list_func) {
  616. uint32_t pcount;
  617. const GDExtensionPropertyInfo *pinfo = native_info->get_property_list_func(instance, &pcount);
  618. #ifdef TOOLS_ENABLED
  619. if (pcount > 0) {
  620. if (native_info->get_class_category_func) {
  621. GDExtensionPropertyInfo gdext_class_category;
  622. if (native_info->get_class_category_func(instance, &gdext_class_category)) {
  623. p_list->push_back(PropertyInfo(gdext_class_category));
  624. }
  625. } else {
  626. Ref<Script> script = get_script();
  627. if (script.is_valid()) {
  628. p_list->push_back(script->get_class_category());
  629. }
  630. }
  631. }
  632. #endif // TOOLS_ENABLED
  633. for (uint32_t i = 0; i < pcount; i++) {
  634. p_list->push_back(PropertyInfo(pinfo[i]));
  635. }
  636. if (native_info->free_property_list_func) {
  637. native_info->free_property_list_func(instance, pinfo, pcount);
  638. #ifndef DISABLE_DEPRECATED
  639. } else if (deprecated_native_info && deprecated_native_info->free_property_list_func) {
  640. deprecated_native_info->free_property_list_func(instance, pinfo);
  641. #endif // DISABLE_DEPRECATED
  642. }
  643. }
  644. }
  645. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const override {
  646. if (native_info->get_property_type_func) {
  647. GDExtensionBool is_valid = 0;
  648. GDExtensionVariantType type = native_info->get_property_type_func(instance, (GDExtensionConstStringNamePtr)&p_name, &is_valid);
  649. if (r_is_valid) {
  650. *r_is_valid = is_valid != 0;
  651. }
  652. return Variant::Type(type);
  653. }
  654. return Variant::NIL;
  655. }
  656. virtual void validate_property(PropertyInfo &p_property) const override {
  657. if (native_info->validate_property_func) {
  658. // GDExtension uses a StringName rather than a String for property name.
  659. StringName prop_name = p_property.name;
  660. GDExtensionPropertyInfo gdext_prop = {
  661. (GDExtensionVariantType)p_property.type,
  662. &prop_name,
  663. &p_property.class_name,
  664. (uint32_t)p_property.hint,
  665. &p_property.hint_string,
  666. p_property.usage,
  667. };
  668. if (native_info->validate_property_func(instance, &gdext_prop)) {
  669. p_property.type = (Variant::Type)gdext_prop.type;
  670. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  671. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  672. p_property.hint = (PropertyHint)gdext_prop.hint;
  673. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  674. p_property.usage = gdext_prop.usage;
  675. }
  676. }
  677. }
  678. virtual bool property_can_revert(const StringName &p_name) const override {
  679. if (native_info->property_can_revert_func) {
  680. return native_info->property_can_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name);
  681. }
  682. return false;
  683. }
  684. virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const override {
  685. if (native_info->property_get_revert_func) {
  686. return native_info->property_get_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);
  687. }
  688. return false;
  689. }
  690. virtual Object *get_owner() override {
  691. if (native_info->get_owner_func) {
  692. return (Object *)native_info->get_owner_func(instance);
  693. }
  694. return nullptr;
  695. }
  696. static void _add_property_with_state(GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value, void *p_userdata) {
  697. List<Pair<StringName, Variant>> *state = (List<Pair<StringName, Variant>> *)p_userdata;
  698. state->push_back(Pair<StringName, Variant>(*(const StringName *)p_name, *(const Variant *)p_value));
  699. }
  700. virtual void get_property_state(List<Pair<StringName, Variant>> &state) override {
  701. if (native_info->get_property_state_func) {
  702. native_info->get_property_state_func(instance, _add_property_with_state, &state);
  703. }
  704. }
  705. virtual void get_method_list(List<MethodInfo> *p_list) const override {
  706. if (native_info->get_method_list_func) {
  707. uint32_t mcount;
  708. const GDExtensionMethodInfo *minfo = native_info->get_method_list_func(instance, &mcount);
  709. for (uint32_t i = 0; i < mcount; i++) {
  710. p_list->push_back(MethodInfo(minfo[i]));
  711. }
  712. if (native_info->free_method_list_func) {
  713. native_info->free_method_list_func(instance, minfo, mcount);
  714. #ifndef DISABLE_DEPRECATED
  715. } else if (deprecated_native_info && deprecated_native_info->free_method_list_func) {
  716. deprecated_native_info->free_method_list_func(instance, minfo);
  717. #endif // DISABLE_DEPRECATED
  718. }
  719. }
  720. }
  721. virtual bool has_method(const StringName &p_method) const override {
  722. if (native_info->has_method_func) {
  723. return native_info->has_method_func(instance, (GDExtensionStringNamePtr)&p_method);
  724. }
  725. return false;
  726. }
  727. virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override {
  728. if (native_info->get_method_argument_count_func) {
  729. GDExtensionBool is_valid = 0;
  730. GDExtensionInt ret = native_info->get_method_argument_count_func(instance, (GDExtensionStringNamePtr)&p_method, &is_valid);
  731. if (r_is_valid) {
  732. *r_is_valid = is_valid != 0;
  733. }
  734. return ret;
  735. }
  736. // Fallback to default.
  737. return ScriptInstance::get_method_argument_count(p_method, r_is_valid);
  738. }
  739. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
  740. Variant ret;
  741. if (native_info->call_func) {
  742. GDExtensionCallError ce;
  743. native_info->call_func(instance, (GDExtensionConstStringNamePtr)&p_method, (GDExtensionConstVariantPtr *)p_args, p_argcount, (GDExtensionVariantPtr)&ret, &ce);
  744. r_error.error = Callable::CallError::Error(ce.error);
  745. r_error.argument = ce.argument;
  746. r_error.expected = ce.expected;
  747. }
  748. return ret;
  749. }
  750. virtual void notification(int p_notification, bool p_reversed = false) override {
  751. if (native_info->notification_func) {
  752. native_info->notification_func(instance, p_notification, p_reversed);
  753. #ifndef DISABLE_DEPRECATED
  754. } else if (deprecated_native_info && deprecated_native_info->notification_func) {
  755. deprecated_native_info->notification_func(instance, p_notification);
  756. #endif // DISABLE_DEPRECATED
  757. }
  758. }
  759. virtual String to_string(bool *r_valid) override {
  760. if (native_info->to_string_func) {
  761. GDExtensionBool valid;
  762. String ret;
  763. native_info->to_string_func(instance, &valid, reinterpret_cast<GDExtensionStringPtr>(&ret));
  764. if (r_valid) {
  765. *r_valid = valid != 0;
  766. }
  767. return ret;
  768. }
  769. return String();
  770. }
  771. virtual void refcount_incremented() override {
  772. if (native_info->refcount_incremented_func) {
  773. native_info->refcount_incremented_func(instance);
  774. }
  775. }
  776. virtual bool refcount_decremented() override {
  777. if (native_info->refcount_decremented_func) {
  778. return native_info->refcount_decremented_func(instance);
  779. }
  780. return false;
  781. }
  782. virtual Ref<Script> get_script() const override {
  783. if (native_info->get_script_func) {
  784. GDExtensionObjectPtr script = native_info->get_script_func(instance);
  785. return Ref<Script>(reinterpret_cast<Script *>(script));
  786. }
  787. return Ref<Script>();
  788. }
  789. virtual bool is_placeholder() const override {
  790. if (native_info->is_placeholder_func) {
  791. return native_info->is_placeholder_func(instance);
  792. }
  793. return false;
  794. }
  795. virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) override {
  796. if (native_info->set_fallback_func) {
  797. bool ret = native_info->set_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);
  798. if (r_valid) {
  799. *r_valid = ret;
  800. }
  801. }
  802. }
  803. virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid) override {
  804. Variant ret;
  805. if (native_info->get_fallback_func) {
  806. bool valid = native_info->get_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret);
  807. if (r_valid) {
  808. *r_valid = valid;
  809. }
  810. }
  811. return ret;
  812. }
  813. virtual ScriptLanguage *get_language() override {
  814. if (native_info->get_language_func) {
  815. GDExtensionScriptLanguagePtr lang = native_info->get_language_func(instance);
  816. return reinterpret_cast<ScriptLanguage *>(lang);
  817. }
  818. return nullptr;
  819. }
  820. virtual ~ScriptInstanceExtension() {
  821. if (native_info->free_func) {
  822. native_info->free_func(instance);
  823. }
  824. #ifndef DISABLE_DEPRECATED
  825. if (free_native_info) {
  826. memfree(const_cast<GDExtensionScriptInstanceInfo3 *>(native_info));
  827. }
  828. if (deprecated_native_info) {
  829. memfree(deprecated_native_info);
  830. }
  831. #endif // DISABLE_DEPRECATED
  832. }
  833. #if defined(__GNUC__) && !defined(__clang__)
  834. #pragma GCC diagnostic pop
  835. #endif
  836. };
  837. #endif // SCRIPT_LANGUAGE_EXTENSION_H