Path: blob/master/core/object/script_language_extension.h
9903 views
/**************************************************************************/1/* script_language_extension.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#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"3738class ScriptExtension : public Script {39GDCLASS(ScriptExtension, Script)4041protected:42EXBIND0R(bool, editor_can_reload_from_file)4344GDVIRTUAL1(_placeholder_erased, GDExtensionPtr<void>)45virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override {46GDVIRTUAL_CALL(_placeholder_erased, p_placeholder);47}4849static void _bind_methods();5051public:52EXBIND0RC(bool, can_instantiate)53EXBIND0RC(Ref<Script>, get_base_script)54EXBIND0RC(StringName, get_global_name)55EXBIND1RC(bool, inherits_script, const Ref<Script> &)56EXBIND0RC(StringName, get_instance_base_type)5758GDVIRTUAL1RC_REQUIRED(GDExtensionPtr<void>, _instance_create, Object *)59virtual ScriptInstance *instance_create(Object *p_this) override {60GDExtensionPtr<void> ret = nullptr;61GDVIRTUAL_CALL(_instance_create, p_this, ret);62return reinterpret_cast<ScriptInstance *>(ret.operator void *());63}64GDVIRTUAL1RC_REQUIRED(GDExtensionPtr<void>, _placeholder_instance_create, Object *)65PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override {66GDExtensionPtr<void> ret = nullptr;67GDVIRTUAL_CALL(_placeholder_instance_create, p_this, ret);68return reinterpret_cast<PlaceHolderScriptInstance *>(ret.operator void *());69}7071EXBIND1RC(bool, instance_has, const Object *)72EXBIND0RC(bool, has_source_code)73EXBIND0RC(String, get_source_code)74EXBIND1(set_source_code, const String &)75EXBIND1R(Error, reload, bool)7677GDVIRTUAL0RC_REQUIRED(StringName, _get_doc_class_name)78GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_documentation)79GDVIRTUAL0RC(String, _get_class_icon_path)80#ifdef TOOLS_ENABLED81virtual StringName get_doc_class_name() const override {82StringName ret;83GDVIRTUAL_CALL(_get_doc_class_name, ret);84return ret;85}8687virtual Vector<DocData::ClassDoc> get_documentation() const override {88TypedArray<Dictionary> doc;89GDVIRTUAL_CALL(_get_documentation, doc);9091Vector<DocData::ClassDoc> class_doc;92for (int i = 0; i < doc.size(); i++) {93class_doc.append(DocData::ClassDoc::from_dict(doc[i]));94}9596return class_doc;97}9899virtual String get_class_icon_path() const override {100String ret;101GDVIRTUAL_CALL(_get_class_icon_path, ret);102return ret;103}104#endif // TOOLS_ENABLED105106EXBIND1RC(bool, has_method, const StringName &)107EXBIND1RC(bool, has_static_method, const StringName &)108109GDVIRTUAL1RC(Variant, _get_script_method_argument_count, const StringName &)110virtual int get_script_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override {111Variant ret;112if (GDVIRTUAL_CALL(_get_script_method_argument_count, p_method, ret) && ret.get_type() == Variant::INT) {113if (r_is_valid) {114*r_is_valid = true;115}116return ret.operator int();117}118// Fallback to default.119return Script::get_script_method_argument_count(p_method, r_is_valid);120}121122GDVIRTUAL1RC_REQUIRED(Dictionary, _get_method_info, const StringName &)123virtual MethodInfo get_method_info(const StringName &p_method) const override {124Dictionary mi;125GDVIRTUAL_CALL(_get_method_info, p_method, mi);126return MethodInfo::from_dict(mi);127}128129EXBIND0RC(bool, is_tool)130EXBIND0RC(bool, is_valid)131132virtual bool is_abstract() const override {133bool abst;134return GDVIRTUAL_CALL(_is_abstract, abst) && abst;135}136GDVIRTUAL0RC(bool, _is_abstract)137138EXBIND0RC(ScriptLanguage *, get_language)139EXBIND1RC(bool, has_script_signal, const StringName &)140141GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_signal_list)142143virtual void get_script_signal_list(List<MethodInfo> *r_signals) const override {144TypedArray<Dictionary> sl;145GDVIRTUAL_CALL(_get_script_signal_list, sl);146for (int i = 0; i < sl.size(); i++) {147r_signals->push_back(MethodInfo::from_dict(sl[i]));148}149}150151GDVIRTUAL1RC_REQUIRED(bool, _has_property_default_value, const StringName &)152GDVIRTUAL1RC_REQUIRED(Variant, _get_property_default_value, const StringName &)153154virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const override {155bool has_dv = false;156if (!GDVIRTUAL_CALL(_has_property_default_value, p_property, has_dv) || !has_dv) {157return false;158}159Variant ret;160GDVIRTUAL_CALL(_get_property_default_value, p_property, ret);161r_value = ret;162return true;163}164165EXBIND0(update_exports)166167GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_method_list)168169virtual void get_script_method_list(List<MethodInfo> *r_methods) const override {170TypedArray<Dictionary> sl;171GDVIRTUAL_CALL(_get_script_method_list, sl);172for (int i = 0; i < sl.size(); i++) {173r_methods->push_back(MethodInfo::from_dict(sl[i]));174}175}176177GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_script_property_list)178179virtual void get_script_property_list(List<PropertyInfo> *r_propertys) const override {180TypedArray<Dictionary> sl;181GDVIRTUAL_CALL(_get_script_property_list, sl);182for (int i = 0; i < sl.size(); i++) {183r_propertys->push_back(PropertyInfo::from_dict(sl[i]));184}185}186187EXBIND1RC(int, get_member_line, const StringName &)188189GDVIRTUAL0RC_REQUIRED(Dictionary, _get_constants)190191virtual void get_constants(HashMap<StringName, Variant> *p_constants) override {192Dictionary constants;193GDVIRTUAL_CALL(_get_constants, constants);194for (const KeyValue<Variant, Variant> &kv : constants) {195p_constants->insert(kv.key, kv.value);196}197}198GDVIRTUAL0RC_REQUIRED(TypedArray<StringName>, _get_members)199virtual void get_members(HashSet<StringName> *p_members) override {200TypedArray<StringName> members;201GDVIRTUAL_CALL(_get_members, members);202for (int i = 0; i < members.size(); i++) {203p_members->insert(members[i]);204}205}206207EXBIND0RC(bool, is_placeholder_fallback_enabled)208209GDVIRTUAL0RC_REQUIRED(Variant, _get_rpc_config)210211virtual const Variant get_rpc_config() const override {212Variant ret;213GDVIRTUAL_CALL(_get_rpc_config, ret);214return ret;215}216217ScriptExtension() {}218};219220typedef ScriptLanguage::ProfilingInfo ScriptLanguageExtensionProfilingInfo;221222GDVIRTUAL_NATIVE_PTR(ScriptLanguageExtensionProfilingInfo)223224class ScriptLanguageExtension : public ScriptLanguage {225GDCLASS(ScriptLanguageExtension, ScriptLanguage)226protected:227static void _bind_methods();228229public:230EXBIND0RC(String, get_name)231232EXBIND0(init)233EXBIND0RC(String, get_type)234EXBIND0RC(String, get_extension)235EXBIND0(finish)236237/* EDITOR FUNCTIONS */238239GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_reserved_words)240241virtual Vector<String> get_reserved_words() const override {242Vector<String> ret;243GDVIRTUAL_CALL(_get_reserved_words, ret);244return ret;245}246EXBIND1RC(bool, is_control_flow_keyword, const String &)247248GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_comment_delimiters)249250virtual Vector<String> get_comment_delimiters() const override {251Vector<String> ret;252GDVIRTUAL_CALL(_get_comment_delimiters, ret);253return ret;254}255256GDVIRTUAL0RC(Vector<String>, _get_doc_comment_delimiters)257258virtual Vector<String> get_doc_comment_delimiters() const override {259Vector<String> ret;260GDVIRTUAL_CALL(_get_doc_comment_delimiters, ret);261return ret;262}263264GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_string_delimiters)265266virtual Vector<String> get_string_delimiters() const override {267Vector<String> ret;268GDVIRTUAL_CALL(_get_string_delimiters, ret);269return ret;270}271272EXBIND3RC(Ref<Script>, make_template, const String &, const String &, const String &)273274GDVIRTUAL1RC_REQUIRED(TypedArray<Dictionary>, _get_built_in_templates, StringName)275276virtual Vector<ScriptTemplate> get_built_in_templates(const StringName &p_object) override {277TypedArray<Dictionary> ret;278GDVIRTUAL_CALL(_get_built_in_templates, p_object, ret);279Vector<ScriptTemplate> stret;280for (int i = 0; i < ret.size(); i++) {281Dictionary d = ret[i];282ScriptTemplate st;283ERR_CONTINUE(!d.has("inherit"));284st.inherit = d["inherit"];285ERR_CONTINUE(!d.has("name"));286st.name = d["name"];287ERR_CONTINUE(!d.has("description"));288st.description = d["description"];289ERR_CONTINUE(!d.has("content"));290st.content = d["content"];291ERR_CONTINUE(!d.has("id"));292st.id = d["id"];293ERR_CONTINUE(!d.has("origin"));294st.origin = TemplateLocation(int(d["origin"]));295stret.push_back(st);296}297return stret;298}299300EXBIND0R(bool, is_using_templates)301302GDVIRTUAL6RC_REQUIRED(Dictionary, _validate, const String &, const String &, bool, bool, bool, bool)303virtual 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 {304Dictionary ret;305GDVIRTUAL_CALL(_validate, p_script, p_path, r_functions != nullptr, r_errors != nullptr, r_warnings != nullptr, r_safe_lines != nullptr, ret);306if (!ret.has("valid")) {307return false;308}309if (r_functions != nullptr && ret.has("functions")) {310Vector<String> functions = ret["functions"];311for (int i = 0; i < functions.size(); i++) {312r_functions->push_back(functions[i]);313}314}315if (r_errors != nullptr && ret.has("errors")) {316Array errors = ret["errors"];317for (const Variant &error : errors) {318Dictionary err = error;319ERR_CONTINUE(!err.has("line"));320ERR_CONTINUE(!err.has("column"));321ERR_CONTINUE(!err.has("message"));322323ScriptError serr;324if (err.has("path")) {325serr.path = err["path"];326}327serr.line = err["line"];328serr.column = err["column"];329serr.message = err["message"];330331r_errors->push_back(serr);332}333}334if (r_warnings != nullptr && ret.has("warnings")) {335ERR_FAIL_COND_V(!ret.has("warnings"), false);336Array warnings = ret["warnings"];337for (const Variant &warning : warnings) {338Dictionary warn = warning;339ERR_CONTINUE(!warn.has("start_line"));340ERR_CONTINUE(!warn.has("end_line"));341ERR_CONTINUE(!warn.has("code"));342ERR_CONTINUE(!warn.has("string_code"));343ERR_CONTINUE(!warn.has("message"));344345Warning swarn;346swarn.start_line = warn["start_line"];347swarn.end_line = warn["end_line"];348swarn.code = warn["code"];349swarn.string_code = warn["string_code"];350swarn.message = warn["message"];351352r_warnings->push_back(swarn);353}354}355if (r_safe_lines != nullptr && ret.has("safe_lines")) {356PackedInt32Array safe_lines = ret["safe_lines"];357for (int i = 0; i < safe_lines.size(); i++) {358r_safe_lines->insert(safe_lines[i]);359}360}361return ret["valid"];362}363364EXBIND1RC(String, validate_path, const String &)365GDVIRTUAL0RC_REQUIRED(Object *, _create_script)366Script *create_script() const override {367Object *ret = nullptr;368GDVIRTUAL_CALL(_create_script, ret);369return Object::cast_to<Script>(ret);370}371#ifndef DISABLE_DEPRECATED372EXBIND0RC(bool, has_named_classes)373#endif374EXBIND0RC(bool, supports_builtin_mode)375EXBIND0RC(bool, supports_documentation)376EXBIND0RC(bool, can_inherit_from_file)377378EXBIND2RC(int, find_function, const String &, const String &)379EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &)380EXBIND0RC(bool, can_make_function)381EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int)382EXBIND0R(bool, overrides_external_editor)383384GDVIRTUAL0RC(ScriptNameCasing, _preferred_file_name_casing);385386virtual ScriptNameCasing preferred_file_name_casing() const override {387ScriptNameCasing ret;388if (GDVIRTUAL_CALL(_preferred_file_name_casing, ret)) {389return ret;390}391return ScriptNameCasing::SCRIPT_NAME_CASING_SNAKE_CASE;392}393394GDVIRTUAL3RC_REQUIRED(Dictionary, _complete_code, const String &, const String &, Object *)395396virtual 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 {397Dictionary ret;398GDVIRTUAL_CALL(_complete_code, p_code, p_path, p_owner, ret);399if (!ret.has("result")) {400return ERR_UNAVAILABLE;401}402403if (r_options != nullptr && ret.has("options")) {404Array options = ret["options"];405for (const Variant &var : options) {406Dictionary op = var;407CodeCompletionOption option;408ERR_CONTINUE(!op.has("kind"));409option.kind = CodeCompletionKind(int(op["kind"]));410ERR_CONTINUE(!op.has("display"));411option.display = op["display"];412ERR_CONTINUE(!op.has("insert_text"));413option.insert_text = op["insert_text"];414ERR_CONTINUE(!op.has("font_color"));415option.font_color = op["font_color"];416ERR_CONTINUE(!op.has("icon"));417option.icon = op["icon"];418ERR_CONTINUE(!op.has("default_value"));419option.default_value = op["default_value"];420ERR_CONTINUE(!op.has("location"));421option.location = op["location"];422if (op.has("matches")) {423PackedInt32Array matches = op["matches"];424ERR_CONTINUE(matches.size() & 1);425for (int j = 0; j < matches.size(); j += 2) {426option.matches.push_back(Pair<int, int>(matches[j], matches[j + 1]));427}428}429r_options->push_back(option);430}431}432433ERR_FAIL_COND_V(!ret.has("force"), ERR_UNAVAILABLE);434r_force = ret["force"];435ERR_FAIL_COND_V(!ret.has("call_hint"), ERR_UNAVAILABLE);436r_call_hint = ret["call_hint"];437ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE);438Error result = Error(int(ret["result"]));439440return result;441}442443GDVIRTUAL4RC_REQUIRED(Dictionary, _lookup_code, const String &, const String &, const String &, Object *)444445virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override {446Dictionary ret;447GDVIRTUAL_CALL(_lookup_code, p_code, p_symbol, p_path, p_owner, ret);448449ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE);450const Error result = Error(int(ret["result"]));451452ERR_FAIL_COND_V(!ret.has("type"), ERR_UNAVAILABLE);453r_result.type = LookupResultType(int(ret["type"]));454455r_result.class_name = ret.get("class_name", "");456r_result.class_member = ret.get("class_member", "");457458r_result.description = ret.get("description", "");459r_result.is_deprecated = ret.get("is_deprecated", false);460r_result.deprecated_message = ret.get("deprecated_message", "");461r_result.is_experimental = ret.get("is_experimental", false);462r_result.experimental_message = ret.get("experimental_message", "");463464r_result.doc_type = ret.get("doc_type", "");465r_result.enumeration = ret.get("enumeration", "");466r_result.is_bitfield = ret.get("is_bitfield", false);467468r_result.value = ret.get("value", "");469470r_result.script = ret.get("script", Ref<Script>());471r_result.script_path = ret.get("script_path", "");472r_result.location = ret.get("location", -1);473474return result;475}476477GDVIRTUAL3RC_REQUIRED(String, _auto_indent_code, const String &, int, int)478virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {479String ret;480GDVIRTUAL_CALL(_auto_indent_code, p_code, p_from_line, p_to_line, ret);481p_code = ret;482}483EXBIND2(add_global_constant, const StringName &, const Variant &)484EXBIND2(add_named_global_constant, const StringName &, const Variant &)485EXBIND1(remove_named_global_constant, const StringName &)486487/* MULTITHREAD FUNCTIONS */488489//some VMs need to be notified of thread creation/exiting to allocate a stack490EXBIND0(thread_enter)491EXBIND0(thread_exit)492493EXBIND0RC(String, debug_get_error)494EXBIND0RC(int, debug_get_stack_level_count)495EXBIND1RC(int, debug_get_stack_level_line, int)496EXBIND1RC(String, debug_get_stack_level_function, int)497EXBIND1RC(String, debug_get_stack_level_source, int)498499GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_locals, int, int, int)500virtual 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 {501Dictionary ret;502GDVIRTUAL_CALL(_debug_get_stack_level_locals, p_level, p_max_subitems, p_max_depth, ret);503if (ret.is_empty()) {504return;505}506if (p_locals != nullptr && ret.has("locals")) {507PackedStringArray strings = ret["locals"];508for (int i = 0; i < strings.size(); i++) {509p_locals->push_back(strings[i]);510}511}512if (p_values != nullptr && ret.has("values")) {513Array values = ret["values"];514for (const Variant &value : values) {515p_values->push_back(value);516}517}518}519GDVIRTUAL3R_REQUIRED(Dictionary, _debug_get_stack_level_members, int, int, int)520virtual 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 {521Dictionary ret;522GDVIRTUAL_CALL(_debug_get_stack_level_members, p_level, p_max_subitems, p_max_depth, ret);523if (ret.is_empty()) {524return;525}526if (p_members != nullptr && ret.has("members")) {527PackedStringArray strings = ret["members"];528for (int i = 0; i < strings.size(); i++) {529p_members->push_back(strings[i]);530}531}532if (p_values != nullptr && ret.has("values")) {533Array values = ret["values"];534for (const Variant &value : values) {535p_values->push_back(value);536}537}538}539GDVIRTUAL1R_REQUIRED(GDExtensionPtr<void>, _debug_get_stack_level_instance, int)540541virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override {542GDExtensionPtr<void> ret = nullptr;543GDVIRTUAL_CALL(_debug_get_stack_level_instance, p_level, ret);544return reinterpret_cast<ScriptInstance *>(ret.operator void *());545}546GDVIRTUAL2R_REQUIRED(Dictionary, _debug_get_globals, int, int)547virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override {548Dictionary ret;549GDVIRTUAL_CALL(_debug_get_globals, p_max_subitems, p_max_depth, ret);550if (ret.is_empty()) {551return;552}553if (p_globals != nullptr && ret.has("globals")) {554PackedStringArray strings = ret["globals"];555for (int i = 0; i < strings.size(); i++) {556p_globals->push_back(strings[i]);557}558}559if (p_values != nullptr && ret.has("values")) {560Array values = ret["values"];561for (const Variant &value : values) {562p_values->push_back(value);563}564}565}566567EXBIND4R(String, debug_parse_stack_level_expression, int, const String &, int, int)568569GDVIRTUAL0R_REQUIRED(TypedArray<Dictionary>, _debug_get_current_stack_info)570virtual Vector<StackInfo> debug_get_current_stack_info() override {571TypedArray<Dictionary> ret;572GDVIRTUAL_CALL(_debug_get_current_stack_info, ret);573Vector<StackInfo> sret;574for (const Variant &var : ret) {575StackInfo si;576Dictionary d = var;577ERR_CONTINUE(!d.has("file"));578ERR_CONTINUE(!d.has("func"));579ERR_CONTINUE(!d.has("line"));580si.file = d["file"];581si.func = d["func"];582si.line = d["line"];583sret.push_back(si);584}585return sret;586}587588EXBIND0(reload_all_scripts)589EXBIND2(reload_scripts, const Array &, bool)590EXBIND2(reload_tool_script, const Ref<Script> &, bool)591/* LOADER FUNCTIONS */592593GDVIRTUAL0RC_REQUIRED(PackedStringArray, _get_recognized_extensions)594595virtual void get_recognized_extensions(List<String> *p_extensions) const override {596PackedStringArray ret;597GDVIRTUAL_CALL(_get_recognized_extensions, ret);598for (int i = 0; i < ret.size(); i++) {599p_extensions->push_back(ret[i]);600}601}602603GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_functions)604virtual void get_public_functions(List<MethodInfo> *p_functions) const override {605TypedArray<Dictionary> ret;606GDVIRTUAL_CALL(_get_public_functions, ret);607for (const Variant &var : ret) {608MethodInfo mi = MethodInfo::from_dict(var);609p_functions->push_back(mi);610}611}612GDVIRTUAL0RC_REQUIRED(Dictionary, _get_public_constants)613virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override {614Dictionary ret;615GDVIRTUAL_CALL(_get_public_constants, ret);616for (int i = 0; i < ret.size(); i++) {617Dictionary d = ret[i];618ERR_CONTINUE(!d.has("name"));619ERR_CONTINUE(!d.has("value"));620p_constants->push_back(Pair<String, Variant>(d["name"], d["value"]));621}622}623GDVIRTUAL0RC_REQUIRED(TypedArray<Dictionary>, _get_public_annotations)624virtual void get_public_annotations(List<MethodInfo> *p_annotations) const override {625TypedArray<Dictionary> ret;626GDVIRTUAL_CALL(_get_public_annotations, ret);627for (const Variant &var : ret) {628MethodInfo mi = MethodInfo::from_dict(var);629p_annotations->push_back(mi);630}631}632633EXBIND0(profiling_start)634EXBIND0(profiling_stop)635EXBIND1(profiling_set_save_native_calls, bool)636637GDVIRTUAL2R_REQUIRED(int, _profiling_get_accumulated_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)638639virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override {640int ret = 0;641GDVIRTUAL_CALL(_profiling_get_accumulated_data, p_info_arr, p_info_max, ret);642return ret;643}644645GDVIRTUAL2R_REQUIRED(int, _profiling_get_frame_data, GDExtensionPtr<ScriptLanguageExtensionProfilingInfo>, int)646647virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override {648int ret = 0;649GDVIRTUAL_CALL(_profiling_get_frame_data, p_info_arr, p_info_max, ret);650return ret;651}652653EXBIND0(frame)654655EXBIND1RC(bool, handles_global_class_type, const String &)656657GDVIRTUAL1RC_REQUIRED(Dictionary, _get_global_class_name, const String &)658659virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr, bool *r_is_abstract = nullptr, bool *r_is_tool = nullptr) const override {660Dictionary ret;661GDVIRTUAL_CALL(_get_global_class_name, p_path, ret);662if (!ret.has("name")) {663return String();664}665if (r_base_type != nullptr && ret.has("base_type")) {666*r_base_type = ret["base_type"];667}668if (r_icon_path != nullptr && ret.has("icon_path")) {669*r_icon_path = ret["icon_path"];670}671if (r_is_abstract != nullptr && ret.has("is_abstract")) {672*r_is_abstract = ret["is_abstract"];673}674if (r_is_tool != nullptr && ret.has("is_tool")) {675*r_is_tool = ret["is_tool"];676}677return ret["name"];678}679};680681VARIANT_ENUM_CAST(ScriptLanguageExtension::LookupResultType)682VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionKind)683VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionLocation)684685class ScriptInstanceExtension : public ScriptInstance {686public:687const GDExtensionScriptInstanceInfo3 *native_info;688689#ifndef DISABLE_DEPRECATED690bool free_native_info = false;691struct DeprecatedNativeInfo {692GDExtensionScriptInstanceNotification notification_func = nullptr;693GDExtensionScriptInstanceFreePropertyList free_property_list_func = nullptr;694GDExtensionScriptInstanceFreeMethodList free_method_list_func = nullptr;695};696DeprecatedNativeInfo *deprecated_native_info = nullptr;697#endif // DISABLE_DEPRECATED698699GDExtensionScriptInstanceDataPtr instance = nullptr;700701GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wignored-qualifiers") // There should not be warnings on explicit casts.702703virtual bool set(const StringName &p_name, const Variant &p_value) override {704if (native_info->set_func) {705return native_info->set_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);706}707return false;708}709virtual bool get(const StringName &p_name, Variant &r_ret) const override {710if (native_info->get_func) {711return native_info->get_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);712}713return false;714}715virtual void get_property_list(List<PropertyInfo> *p_list) const override {716if (native_info->get_property_list_func) {717uint32_t pcount;718const GDExtensionPropertyInfo *pinfo = native_info->get_property_list_func(instance, &pcount);719720#ifdef TOOLS_ENABLED721if (pcount > 0) {722if (native_info->get_class_category_func) {723GDExtensionPropertyInfo gdext_class_category;724if (native_info->get_class_category_func(instance, &gdext_class_category)) {725p_list->push_back(PropertyInfo(gdext_class_category));726}727} else {728Ref<Script> script = get_script();729if (script.is_valid()) {730p_list->push_back(script->get_class_category());731}732}733}734#endif // TOOLS_ENABLED735736for (uint32_t i = 0; i < pcount; i++) {737p_list->push_back(PropertyInfo(pinfo[i]));738}739if (native_info->free_property_list_func) {740native_info->free_property_list_func(instance, pinfo, pcount);741#ifndef DISABLE_DEPRECATED742} else if (deprecated_native_info && deprecated_native_info->free_property_list_func) {743deprecated_native_info->free_property_list_func(instance, pinfo);744#endif // DISABLE_DEPRECATED745}746}747}748virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const override {749if (native_info->get_property_type_func) {750GDExtensionBool is_valid = 0;751GDExtensionVariantType type = native_info->get_property_type_func(instance, (GDExtensionConstStringNamePtr)&p_name, &is_valid);752if (r_is_valid) {753*r_is_valid = is_valid != 0;754}755return Variant::Type(type);756}757return Variant::NIL;758}759virtual void validate_property(PropertyInfo &p_property) const override {760if (native_info->validate_property_func) {761// GDExtension uses a StringName rather than a String for property name.762StringName prop_name = p_property.name;763GDExtensionPropertyInfo gdext_prop = {764(GDExtensionVariantType)p_property.type,765&prop_name,766&p_property.class_name,767(uint32_t)p_property.hint,768&p_property.hint_string,769p_property.usage,770};771if (native_info->validate_property_func(instance, &gdext_prop)) {772p_property.type = (Variant::Type)gdext_prop.type;773p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);774p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);775p_property.hint = (PropertyHint)gdext_prop.hint;776p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);777p_property.usage = gdext_prop.usage;778}779}780}781782virtual bool property_can_revert(const StringName &p_name) const override {783if (native_info->property_can_revert_func) {784return native_info->property_can_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name);785}786return false;787}788virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const override {789if (native_info->property_get_revert_func) {790return native_info->property_get_revert_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&r_ret);791}792return false;793}794795virtual Object *get_owner() override {796if (native_info->get_owner_func) {797return (Object *)native_info->get_owner_func(instance);798}799return nullptr;800}801static void _add_property_with_state(GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value, void *p_userdata) {802List<Pair<StringName, Variant>> *state = (List<Pair<StringName, Variant>> *)p_userdata;803state->push_back(Pair<StringName, Variant>(*(const StringName *)p_name, *(const Variant *)p_value));804}805virtual void get_property_state(List<Pair<StringName, Variant>> &state) override {806if (native_info->get_property_state_func) {807native_info->get_property_state_func(instance, _add_property_with_state, &state);808return;809}810ScriptInstance::get_property_state(state);811}812813virtual void get_method_list(List<MethodInfo> *p_list) const override {814if (native_info->get_method_list_func) {815uint32_t mcount;816const GDExtensionMethodInfo *minfo = native_info->get_method_list_func(instance, &mcount);817for (uint32_t i = 0; i < mcount; i++) {818p_list->push_back(MethodInfo(minfo[i]));819}820if (native_info->free_method_list_func) {821native_info->free_method_list_func(instance, minfo, mcount);822#ifndef DISABLE_DEPRECATED823} else if (deprecated_native_info && deprecated_native_info->free_method_list_func) {824deprecated_native_info->free_method_list_func(instance, minfo);825#endif // DISABLE_DEPRECATED826}827}828}829virtual bool has_method(const StringName &p_method) const override {830if (native_info->has_method_func) {831return native_info->has_method_func(instance, (GDExtensionStringNamePtr)&p_method);832}833return false;834}835836virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const override {837if (native_info->get_method_argument_count_func) {838GDExtensionBool is_valid = 0;839GDExtensionInt ret = native_info->get_method_argument_count_func(instance, (GDExtensionStringNamePtr)&p_method, &is_valid);840if (r_is_valid) {841*r_is_valid = is_valid != 0;842}843return ret;844}845// Fallback to default.846return ScriptInstance::get_method_argument_count(p_method, r_is_valid);847}848849virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {850Variant ret;851if (native_info->call_func) {852GDExtensionCallError ce;853native_info->call_func(instance, (GDExtensionConstStringNamePtr)&p_method, (GDExtensionConstVariantPtr *)p_args, p_argcount, (GDExtensionVariantPtr)&ret, &ce);854r_error.error = Callable::CallError::Error(ce.error);855r_error.argument = ce.argument;856r_error.expected = ce.expected;857}858return ret;859}860861virtual void notification(int p_notification, bool p_reversed = false) override {862if (native_info->notification_func) {863native_info->notification_func(instance, p_notification, p_reversed);864#ifndef DISABLE_DEPRECATED865} else if (deprecated_native_info && deprecated_native_info->notification_func) {866deprecated_native_info->notification_func(instance, p_notification);867#endif // DISABLE_DEPRECATED868}869}870871virtual String to_string(bool *r_valid) override {872if (native_info->to_string_func) {873GDExtensionBool valid;874String ret;875native_info->to_string_func(instance, &valid, reinterpret_cast<GDExtensionStringPtr>(&ret));876if (r_valid) {877*r_valid = valid != 0;878}879return ret;880}881return String();882}883884virtual void refcount_incremented() override {885if (native_info->refcount_incremented_func) {886native_info->refcount_incremented_func(instance);887}888}889virtual bool refcount_decremented() override {890if (native_info->refcount_decremented_func) {891return native_info->refcount_decremented_func(instance);892}893return false;894}895896virtual Ref<Script> get_script() const override {897if (native_info->get_script_func) {898GDExtensionObjectPtr script = native_info->get_script_func(instance);899return Ref<Script>(reinterpret_cast<Script *>(script));900}901return Ref<Script>();902}903904virtual bool is_placeholder() const override {905if (native_info->is_placeholder_func) {906return native_info->is_placeholder_func(instance);907}908return false;909}910911virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) override {912if (native_info->set_fallback_func) {913bool ret = native_info->set_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value);914if (r_valid) {915*r_valid = ret;916}917}918}919virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid) override {920Variant ret;921if (native_info->get_fallback_func) {922bool valid = native_info->get_fallback_func(instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret);923if (r_valid) {924*r_valid = valid;925}926}927return ret;928}929930virtual ScriptLanguage *get_language() override {931if (native_info->get_language_func) {932GDExtensionScriptLanguagePtr lang = native_info->get_language_func(instance);933return reinterpret_cast<ScriptLanguage *>(lang);934}935return nullptr;936}937virtual ~ScriptInstanceExtension() {938if (native_info->free_func) {939native_info->free_func(instance);940}941#ifndef DISABLE_DEPRECATED942if (free_native_info) {943memfree(const_cast<GDExtensionScriptInstanceInfo3 *>(native_info));944}945if (deprecated_native_info) {946memfree(deprecated_native_info);947}948#endif // DISABLE_DEPRECATED949}950951GODOT_GCC_WARNING_POP952};953954955