script_language_extension.h 30 KB

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