diff options
author | reduz <reduzio@gmail.com> | 2022-03-26 16:48:43 +0100 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2022-03-27 16:13:00 +0200 |
commit | 360dea5348652d8806d33598e111651afb3d193a (patch) | |
tree | 3726e871844722d9561943a85a447037342df7dc /core | |
parent | a5eed70fa2edcf755d6abea2077232e38381449b (diff) |
Add GDExtension support to Script
* Ability to create script languages from GDExtension
* Some additions to gdnative_extension.h to make this happen
* Moved the GDExtension binder to core
This now allows creating scripting languages from GDExtension, with the same ease as if it was a module. It replaces the old PluginScript from Godot 3.x.
Warning: GodotCPP will need to be updated to support this (it may be a bit of work as ScriptInstance needs to be created over there again).
Diffstat (limited to 'core')
-rw-r--r-- | core/core_bind.cpp | 16 | ||||
-rw-r--r-- | core/core_bind.h | 5 | ||||
-rw-r--r-- | core/extension/SCsub | 6 | ||||
-rw-r--r-- | core/extension/gdnative_interface.cpp | 12 | ||||
-rw-r--r-- | core/extension/gdnative_interface.h | 88 | ||||
-rw-r--r-- | core/extension/make_wrappers.py | 93 | ||||
-rw-r--r-- | core/object/class_db.cpp | 9 | ||||
-rw-r--r-- | core/object/script_language.h | 97 | ||||
-rw-r--r-- | core/object/script_language_extension.cpp | 175 | ||||
-rw-r--r-- | core/object/script_language_extension.h | 832 | ||||
-rw-r--r-- | core/register_core_types.cpp | 6 |
11 files changed, 1291 insertions, 48 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 7150459d84..8b060b09f9 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -2296,6 +2296,18 @@ Vector<String> Engine::get_singleton_list() const { return ret; } +void Engine::register_script_language(ScriptLanguage *p_language) { + ScriptServer::register_language(p_language); +} + +int Engine::get_script_language_count() { + return ScriptServer::get_language_count(); +} + +ScriptLanguage *Engine::get_script_language(int p_index) const { + return ScriptServer::get_language(p_index); +} + void Engine::set_editor_hint(bool p_enabled) { ::Engine::get_singleton()->set_editor_hint(p_enabled); } @@ -2347,6 +2359,10 @@ void Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton); ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list); + ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language); + ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count); + ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language); + ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint); ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages); diff --git a/core/core_bind.h b/core/core_bind.h index a0fdf26dff..2942f54a15 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -38,6 +38,7 @@ #include "core/io/image.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" +#include "core/object/script_language.h" #include "core/os/os.h" #include "core/os/semaphore.h" #include "core/os/thread.h" @@ -664,6 +665,10 @@ public: void unregister_singleton(const StringName &p_name); Vector<String> get_singleton_list() const; + void register_script_language(ScriptLanguage *p_language); + int get_script_language_count(); + ScriptLanguage *get_script_language(int p_index) const; + void set_editor_hint(bool p_enabled); bool is_editor_hint() const; diff --git a/core/extension/SCsub b/core/extension/SCsub index a3a54250c1..23727c1b76 100644 --- a/core/extension/SCsub +++ b/core/extension/SCsub @@ -2,6 +2,12 @@ Import("env") +import make_wrappers +from platform_methods import run_in_subprocess + +env.CommandNoCache(["ext_wrappers.gen.inc"], "make_wrappers.py", run_in_subprocess(make_wrappers.run)) + + env_extension = env.Clone() env_extension.add_source_files(env.core_sources, "*.cpp") diff --git a/core/extension/gdnative_interface.cpp b/core/extension/gdnative_interface.cpp index d0461611ec..b5b340731d 100644 --- a/core/extension/gdnative_interface.cpp +++ b/core/extension/gdnative_interface.cpp @@ -32,6 +32,7 @@ #include "core/config/engine.h" #include "core/object/class_db.h" +#include "core/object/script_language_extension.h" #include "core/os/memory.h" #include "core/variant/variant.h" #include "core/version.h" @@ -864,6 +865,13 @@ static GDObjectInstanceID gdnative_object_get_instance_id(const GDNativeObjectPt return (GDObjectInstanceID)o->get_instance_id(); } +static GDNativeScriptInstancePtr gdnative_script_instance_create(const GDNativeExtensionScriptInstanceInfo *p_info, GDNativeExtensionScriptInstanceDataPtr p_instance_data) { + ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension); + script_instance_extension->instance = p_instance_data; + script_instance_extension->native_info = p_info; + return reinterpret_cast<GDNativeScriptInstancePtr>(script_instance_extension); +} + static GDNativeMethodBindPtr gdnative_classdb_get_method_bind(const char *p_classname, const char *p_methodname, GDNativeInt p_hash) { MethodBind *mb = ClassDB::get_method(StringName(p_classname), StringName(p_methodname)); ERR_FAIL_COND_V(!mb, nullptr); @@ -1032,6 +1040,10 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) { gdni.object_get_instance_from_id = gdnative_object_get_instance_from_id; gdni.object_get_instance_id = gdnative_object_get_instance_id; + /* SCRIPT INSTANCE */ + + gdni.script_instance_create = gdnative_script_instance_create; + /* CLASSDB */ gdni.classdb_construct_object = gdnative_classdb_construct_object; diff --git a/core/extension/gdnative_interface.h b/core/extension/gdnative_interface.h index cc2957ec56..50c747a7b4 100644 --- a/core/extension/gdnative_interface.h +++ b/core/extension/gdnative_interface.h @@ -205,6 +205,17 @@ typedef struct { uint32_t usage; } GDNativePropertyInfo; +typedef struct { + const char *name; + GDNativePropertyInfo return_value; + uint32_t flags; // From GDNativeExtensionClassMethodFlags + int32_t id; + GDNativePropertyInfo *arguments; + uint32_t argument_count; + GDNativeVariantPtr default_arguments; + uint32_t default_argument_count; +} GDNativeMethodInfo; + typedef const GDNativePropertyInfo *(*GDNativeExtensionClassGetPropertyList)(GDExtensionClassInstancePtr p_instance, uint32_t *r_count); typedef void (*GDNativeExtensionClassFreePropertyList)(GDExtensionClassInstancePtr p_instance, const GDNativePropertyInfo *p_list); typedef void (*GDNativeExtensionClassNotification)(GDExtensionClassInstancePtr p_instance, int32_t p_what); @@ -289,6 +300,79 @@ typedef struct { GDNativeVariantPtr *default_arguments; } GDNativeExtensionClassMethodInfo; +/* SCRIPT INSTANCE EXTENSION */ + +typedef void *GDNativeExtensionScriptInstanceDataPtr; // Pointer to custom ScriptInstance native implementation + +typedef GDNativeBool (*GDNativeExtensionScriptInstanceSet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceGet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); +typedef const GDNativePropertyInfo *(*GDNativeExtensionScriptInstanceGetPropertyList)(GDNativeExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); +typedef void (*GDNativeExtensionScriptInstanceFreePropertyList)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativePropertyInfo *p_list); +typedef GDNativeVariantType (*GDNativeExtensionScriptInstanceGetPropertyType)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeBool *r_is_valid); + +typedef GDNativeObjectPtr (*GDNativeExtensionScriptInstanceGetOwner)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef void (*GDNativeExtensionScriptInstancePropertyStateAdd)(const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value, void *p_userdata); +typedef void (*GDNativeExtensionScriptInstanceGetPropertyState)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeExtensionScriptInstancePropertyStateAdd p_add_func, void *p_userdata); + +typedef const GDNativeMethodInfo *(*GDNativeExtensionScriptInstanceGetMethodList)(GDNativeExtensionScriptInstanceDataPtr p_instance, uint32_t *r_count); +typedef void (*GDNativeExtensionScriptInstanceFreeMethodList)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeMethodInfo *p_list); + +typedef GDNativeBool (*GDNativeExtensionScriptInstanceHasMethod)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name); + +typedef void (*GDNativeExtensionScriptInstanceCall)(GDNativeExtensionScriptInstanceDataPtr p_self, const GDNativeStringNamePtr p_method, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error); +typedef void (*GDNativeExtensionScriptInstanceNotification)(GDNativeExtensionScriptInstanceDataPtr p_instance, int32_t p_what); +typedef const char *(*GDNativeExtensionScriptInstanceToString)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeBool *r_is_valid); + +typedef void (*GDNativeExtensionScriptInstanceRefCountIncremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceRefCountDecremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef GDNativeObjectPtr (*GDNativeExtensionScriptInstanceGetScript)(GDNativeExtensionScriptInstanceDataPtr p_instance); +typedef GDNativeBool (*GDNativeExtensionScriptInstanceIsPlaceholder)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void *GDNativeExtensionScriptLanguagePtr; + +typedef GDNativeExtensionScriptLanguagePtr (*GDNativeExtensionScriptInstanceGetLanguage)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void (*GDNativeExtensionScriptInstanceFree)(GDNativeExtensionScriptInstanceDataPtr p_instance); + +typedef void *GDNativeScriptInstancePtr; // Pointer to ScriptInstance. + +typedef struct { + GDNativeExtensionScriptInstanceSet set_func; + GDNativeExtensionScriptInstanceGet get_func; + GDNativeExtensionScriptInstanceGetPropertyList get_property_list_func; + GDNativeExtensionScriptInstanceFreePropertyList free_property_list_func; + GDNativeExtensionScriptInstanceGetPropertyType get_property_type_func; + + GDNativeExtensionScriptInstanceGetOwner get_owner_func; + GDNativeExtensionScriptInstanceGetPropertyState get_property_state_func; + + GDNativeExtensionScriptInstanceGetMethodList get_method_list_func; + GDNativeExtensionScriptInstanceFreeMethodList free_method_list_func; + + GDNativeExtensionScriptInstanceHasMethod has_method_func; + + GDNativeExtensionScriptInstanceCall call_func; + GDNativeExtensionScriptInstanceNotification notification_func; + + GDNativeExtensionScriptInstanceToString to_string_func; + + GDNativeExtensionScriptInstanceRefCountIncremented refcount_incremented_func; + GDNativeExtensionScriptInstanceRefCountDecremented refcount_decremented_func; + + GDNativeExtensionScriptInstanceGetScript get_script_func; + + GDNativeExtensionScriptInstanceIsPlaceholder is_placeholder_func; + + GDNativeExtensionScriptInstanceSet set_fallback_func; + GDNativeExtensionScriptInstanceGet get_fallback_func; + + GDNativeExtensionScriptInstanceGetLanguage get_language_func; + + GDNativeExtensionScriptInstanceFree free_func; + +} GDNativeExtensionScriptInstanceInfo; + /* INTERFACE */ typedef struct { @@ -441,6 +525,10 @@ typedef struct { GDNativeObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id); GDObjectInstanceID (*object_get_instance_id)(const GDNativeObjectPtr p_object); + /* SCRIPT INSTANCE */ + + GDNativeScriptInstancePtr (*script_instance_create)(const GDNativeExtensionScriptInstanceInfo *p_info, GDNativeExtensionScriptInstanceDataPtr p_instance_data); + /* CLASSDB */ GDNativeObjectPtr (*classdb_construct_object)(const char *p_classname); /* The passed class must be a built-in godot class, or an already-registered extension class. In both case, object_set_instance should be called to fully initialize the object. */ GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); diff --git a/core/extension/make_wrappers.py b/core/extension/make_wrappers.py new file mode 100644 index 0000000000..862d313fba --- /dev/null +++ b/core/extension/make_wrappers.py @@ -0,0 +1,93 @@ +proto = """ +#define EXBIND$VER($RETTYPE m_name$ARG) \\ +GDVIRTUAL$VER($RETTYPE_##m_name$ARG)\\ +virtual $RETVAL m_name($FUNCARGS) $CONST override { \\ + $RETPRE\\ + GDVIRTUAL_REQUIRED_CALL(_##m_name$CALLARGS$RETREF);\\ + $RETPOST\\ +} +""" + + +def generate_version(argcount, const=False, returns=False): + s = proto + sproto = str(argcount) + method_info = "" + if returns: + sproto += "R" + s = s.replace("$RETTYPE", "m_ret, ") + s = s.replace("$RETVAL", "m_ret") + s = s.replace("$RETPRE", "m_ret ret; ZeroInitializer<m_ret>::initialize(ret);\\\n") + s = s.replace("$RETPOST", "return ret;\\\n") + + else: + s = s.replace("$RETTYPE", "") + s = s.replace("$RETVAL", "void") + s = s.replace("$RETPRE", "") + s = s.replace("$RETPOST", "return;") + + if const: + sproto += "C" + s = s.replace("$CONST", "const") + else: + s = s.replace("$CONST", "") + + s = s.replace("$VER", sproto) + argtext = "" + funcargs = "" + callargs = "" + + for i in range(argcount): + if i > 0: + funcargs += ", " + + argtext += ", m_type" + str(i + 1) + funcargs += "m_type" + str(i + 1) + " arg" + str(i + 1) + callargs += ", arg" + str(i + 1) + + if argcount: + s = s.replace("$ARG", argtext) + s = s.replace("$FUNCARGS", funcargs) + s = s.replace("$CALLARGS", callargs) + else: + s = s.replace("$ARG", "") + s = s.replace("$FUNCARGS", funcargs) + s = s.replace("$CALLARGS", callargs) + + if returns: + s = s.replace("$RETREF", ", ret") + else: + s = s.replace("$RETREF", "") + + return s + + +def run(target, source, env): + + max_versions = 12 + + txt = """ +#ifndef GDEXTENSION_WRAPPERS_GEN_H +#define GDEXTENSION_WRAPPERS_GEN_H + + +""" + + for i in range(max_versions + 1): + + txt += "/* " + str(i) + " Arguments */\n\n" + txt += generate_version(i, False, False) + txt += generate_version(i, False, True) + txt += generate_version(i, True, False) + txt += generate_version(i, True, True) + + txt += "#endif" + + with open(target[0], "w") as f: + f.write(txt) + + +if __name__ == "__main__": + from platform_methods import subprocess_main + + subprocess_main(globals()) diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index e09c6cb97c..51ca06d9d2 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1485,9 +1485,10 @@ void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_ if (p_object_core) { mi.flags |= METHOD_FLAG_OBJECT_CORE; } - if (p_arg_names.size()) { + + if (!p_object_core) { if (p_arg_names.size() != mi.arguments.size()) { - WARN_PRINT("Mismatch argument name count for virtual function: " + String(p_class) + "::" + p_method.name); + WARN_PRINT("Mismatch argument name count for virtual method: " + String(p_class) + "::" + p_method.name); } else { for (int i = 0; i < p_arg_names.size(); i++) { mi.arguments[i].name = p_arg_names[i]; @@ -1495,6 +1496,10 @@ void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_ } } + if (classes[p_class].virtual_methods_map.has(p_method.name)) { + // overloading not supported + ERR_FAIL_MSG("Virtual method already bound '" + String(p_class) + "::" + p_method.name + "'."); + } classes[p_class].virtual_methods.push_back(mi); classes[p_class].virtual_methods_map[p_method.name] = mi; diff --git a/core/object/script_language.h b/core/object/script_language.h index 2122f785b6..6161a0fc0f 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -132,7 +132,7 @@ public: virtual Error reload(bool p_keep_state = false) = 0; #ifdef TOOLS_ENABLED - virtual const Vector<DocData::ClassDoc> &get_documentation() const = 0; + virtual Vector<DocData::ClassDoc> get_documentation() const = 0; #endif // TOOLS_ENABLED virtual bool has_method(const StringName &p_method) const = 0; @@ -212,44 +212,12 @@ public: virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid); virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid); - virtual const Vector<Multiplayer::RPCConfig> get_rpc_methods() const = 0; + virtual const Vector<Multiplayer::RPCConfig> get_rpc_methods() const { return get_script()->get_rpc_methods(); } virtual ScriptLanguage *get_language() = 0; virtual ~ScriptInstance(); }; -struct ScriptCodeCompletionOption { - /* Keep enum in Sync with: */ - /* /scene/gui/code_edit.h - CodeEdit::CodeCompletionKind */ - enum Kind { - KIND_CLASS, - KIND_FUNCTION, - KIND_SIGNAL, - KIND_VARIABLE, - KIND_MEMBER, - KIND_ENUM, - KIND_CONSTANT, - KIND_NODE_PATH, - KIND_FILE_PATH, - KIND_PLAIN_TEXT, - }; - Kind kind = KIND_PLAIN_TEXT; - String display; - String insert_text; - Color font_color; - RES icon; - Variant default_value; - Vector<Pair<int, int>> matches; - - ScriptCodeCompletionOption() {} - - ScriptCodeCompletionOption(const String &p_text, Kind p_kind) { - display = p_text; - insert_text = p_text; - kind = p_kind; - } -}; - class ScriptCodeCompletionCache { static ScriptCodeCompletionCache *singleton; @@ -261,7 +229,8 @@ public: virtual ~ScriptCodeCompletionCache() {} }; -class ScriptLanguage { +class ScriptLanguage : public Object { + GDCLASS(ScriptLanguage, Object) public: virtual String get_name() const = 0; @@ -326,19 +295,55 @@ public: virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; } virtual bool overrides_external_editor() { return false; } - virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_force, String &r_call_hint) { return ERR_UNAVAILABLE; } + /* Keep enum in Sync with: */ + /* /scene/gui/code_edit.h - CodeEdit::CodeCompletionKind */ + enum CodeCompletionKind { + CODE_COMPLETION_KIND_CLASS, + CODE_COMPLETION_KIND_FUNCTION, + CODE_COMPLETION_KIND_SIGNAL, + CODE_COMPLETION_KIND_VARIABLE, + CODE_COMPLETION_KIND_MEMBER, + CODE_COMPLETION_KIND_ENUM, + CODE_COMPLETION_KIND_CONSTANT, + CODE_COMPLETION_KIND_NODE_PATH, + CODE_COMPLETION_KIND_FILE_PATH, + CODE_COMPLETION_KIND_PLAIN_TEXT, + CODE_COMPLETION_KIND_MAX + }; + + struct CodeCompletionOption { + CodeCompletionKind kind = CODE_COMPLETION_KIND_PLAIN_TEXT; + String display; + String insert_text; + Color font_color; + RES icon; + Variant default_value; + Vector<Pair<int, int>> matches; + + CodeCompletionOption() {} + + CodeCompletionOption(const String &p_text, CodeCompletionKind p_kind) { + display = p_text; + insert_text = p_text; + kind = p_kind; + } + }; + + 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) { return ERR_UNAVAILABLE; } + + enum LookupResultType { + LOOKUP_RESULT_SCRIPT_LOCATION, + LOOKUP_RESULT_CLASS, + LOOKUP_RESULT_CLASS_CONSTANT, + LOOKUP_RESULT_CLASS_PROPERTY, + LOOKUP_RESULT_CLASS_METHOD, + LOOKUP_RESULT_CLASS_ENUM, + LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE, + LOOKUP_RESULT_MAX + }; struct LookupResult { - enum Type { - RESULT_SCRIPT_LOCATION, - RESULT_CLASS, - RESULT_CLASS_CONSTANT, - RESULT_CLASS_PROPERTY, - RESULT_CLASS_METHOD, - RESULT_CLASS_ENUM, - RESULT_CLASS_TBD_GLOBALSCOPE - }; - Type type; + LookupResultType type; Ref<Script> script; String class_name; String class_member; diff --git a/core/object/script_language_extension.cpp b/core/object/script_language_extension.cpp new file mode 100644 index 0000000000..bf0966c803 --- /dev/null +++ b/core/object/script_language_extension.cpp @@ -0,0 +1,175 @@ +/*************************************************************************/ +/* script_language_extension.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "script_language_extension.h" + +void ScriptExtension::_bind_methods() { + GDVIRTUAL_BIND(_editor_can_reload_from_file); + GDVIRTUAL_BIND(_placeholder_erased, "placeholder"); + + GDVIRTUAL_BIND(_can_instantiate); + GDVIRTUAL_BIND(_get_base_script); + GDVIRTUAL_BIND(_inherits_script, "script"); + + GDVIRTUAL_BIND(_get_instance_base_type); + GDVIRTUAL_BIND(_instance_create, "for_object"); + GDVIRTUAL_BIND(_placeholder_instance_create, "for_object"); + + GDVIRTUAL_BIND(_instance_has, "object"); + + GDVIRTUAL_BIND(_has_source_code); + GDVIRTUAL_BIND(_get_source_code); + + GDVIRTUAL_BIND(_set_source_code, "code"); + GDVIRTUAL_BIND(_reload, "keep_state"); + + GDVIRTUAL_BIND(_get_documentation); + + GDVIRTUAL_BIND(_has_method, "method"); + GDVIRTUAL_BIND(_get_method_info, "method"); + + GDVIRTUAL_BIND(_is_tool); + GDVIRTUAL_BIND(_is_valid); + GDVIRTUAL_BIND(_get_language); + + GDVIRTUAL_BIND(_has_script_signal, "signal"); + GDVIRTUAL_BIND(_get_script_signal_list); + + GDVIRTUAL_BIND(_get_property_default_value, "property"); + + GDVIRTUAL_BIND(_update_exports); + GDVIRTUAL_BIND(_get_script_method_list); + GDVIRTUAL_BIND(_get_script_property_list); + + GDVIRTUAL_BIND(_get_member_line, "member"); + + GDVIRTUAL_BIND(_get_constants); + GDVIRTUAL_BIND(_get_members); + GDVIRTUAL_BIND(_is_placeholder_fallback_enabled); + + GDVIRTUAL_BIND(_get_rpc_methods); +} + +void ScriptLanguageExtension::_bind_methods() { + GDVIRTUAL_BIND(_get_name); + GDVIRTUAL_BIND(_init); + GDVIRTUAL_BIND(_get_type); + GDVIRTUAL_BIND(_get_extension); + GDVIRTUAL_BIND(_execute_file, "path"); + GDVIRTUAL_BIND(_finish); + + GDVIRTUAL_BIND(_get_reserved_words); + GDVIRTUAL_BIND(_is_control_flow_keyword, "keyword"); + GDVIRTUAL_BIND(_get_comment_delimiters); + GDVIRTUAL_BIND(_get_string_delimiters); + GDVIRTUAL_BIND(_make_template, "template", "class_name", "base_class_name"); + GDVIRTUAL_BIND(_get_built_in_templates, "object"); + GDVIRTUAL_BIND(_is_using_templates); + GDVIRTUAL_BIND(_validate, "script", "path", "validate_functions", "validate_errors", "validate_warnings", "validate_safe_lines"); + + GDVIRTUAL_BIND(_validate_path, "path"); + GDVIRTUAL_BIND(_create_script); + GDVIRTUAL_BIND(_has_named_classes); + GDVIRTUAL_BIND(_supports_builtin_mode); + GDVIRTUAL_BIND(_supports_documentation); + GDVIRTUAL_BIND(_can_inherit_from_file); + GDVIRTUAL_BIND(_find_function, "class_name", "function_name"); + GDVIRTUAL_BIND(_make_function, "class_name", "function_name", "function_args"); + GDVIRTUAL_BIND(_open_in_external_editor, "script", "line", "column"); + GDVIRTUAL_BIND(_overrides_external_editor); + + GDVIRTUAL_BIND(_complete_code, "code", "path", "owner"); + GDVIRTUAL_BIND(_lookup_code, "code", "symbol", "path", "owner"); + GDVIRTUAL_BIND(_auto_indent_code, "code", "from_line", "to_line"); + + GDVIRTUAL_BIND(_add_global_constant, "name", "value"); + GDVIRTUAL_BIND(_add_named_global_constant, "name", "value"); + GDVIRTUAL_BIND(_remove_named_global_constant, "name"); + + GDVIRTUAL_BIND(_thread_enter); + GDVIRTUAL_BIND(_thread_exit); + GDVIRTUAL_BIND(_debug_get_error); + GDVIRTUAL_BIND(_debug_get_stack_level_count); + + GDVIRTUAL_BIND(_debug_get_stack_level_line, "level"); + GDVIRTUAL_BIND(_debug_get_stack_level_function, "level"); + GDVIRTUAL_BIND(_debug_get_stack_level_locals, "level", "max_subitems", "max_depth"); + GDVIRTUAL_BIND(_debug_get_stack_level_members, "level", "max_subitems", "max_depth"); + GDVIRTUAL_BIND(_debug_get_stack_level_instance, "level"); + GDVIRTUAL_BIND(_debug_get_globals, "max_subitems", "max_depth"); + GDVIRTUAL_BIND(_debug_parse_stack_level_expression, "level", "expression", "max_subitems", "max_depth"); + + GDVIRTUAL_BIND(_debug_get_current_stack_info); + + GDVIRTUAL_BIND(_reload_all_scripts); + GDVIRTUAL_BIND(_reload_tool_script, "script", "soft_reload"); + + GDVIRTUAL_BIND(_get_recognized_extensions); + GDVIRTUAL_BIND(_get_public_functions); + GDVIRTUAL_BIND(_get_public_constants); + + GDVIRTUAL_BIND(_profiling_start); + GDVIRTUAL_BIND(_profiling_stop); + + GDVIRTUAL_BIND(_profiling_get_accumulated_data, "info_array", "info_max"); + GDVIRTUAL_BIND(_profiling_get_frame_data, "info_array", "info_max"); + + GDVIRTUAL_BIND(_alloc_instance_binding_data, "object"); + GDVIRTUAL_BIND(_free_instance_binding_data, "data"); + + GDVIRTUAL_BIND(_refcount_incremented_instance_binding, "object"); + GDVIRTUAL_BIND(_refcount_decremented_instance_binding, "object"); + + GDVIRTUAL_BIND(_frame); + + GDVIRTUAL_BIND(_handles_global_class_type, "type"); + GDVIRTUAL_BIND(_get_global_class_name, "path"); + + BIND_ENUM_CONSTANT(LOOKUP_RESULT_SCRIPT_LOCATION); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS_CONSTANT); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS_PROPERTY); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS_METHOD); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS_ENUM); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE); + BIND_ENUM_CONSTANT(LOOKUP_RESULT_MAX); + + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_CLASS); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_FUNCTION); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_SIGNAL); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_VARIABLE); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_MEMBER); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_ENUM); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_CONSTANT); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_NODE_PATH); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_FILE_PATH); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_PLAIN_TEXT); + BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_MAX); +} diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h new file mode 100644 index 0000000000..b9ec79da26 --- /dev/null +++ b/core/object/script_language_extension.h @@ -0,0 +1,832 @@ +/*************************************************************************/ +/* script_language_extension.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef SCRIPT_LANGUAGE_EXTENSION_H +#define SCRIPT_LANGUAGE_EXTENSION_H + +#include "core/extension/ext_wrappers.gen.inc" +#include "core/object/gdvirtual.gen.inc" +#include "core/object/script_language.h" +#include "core/variant/native_ptr.h" +#include "core/variant/typed_array.h" + +class ScriptExtension : public Script { + GDCLASS(ScriptExtension, Script) + +protected: + EXBIND0R(bool, editor_can_reload_from_file) + + GDVIRTUAL1(_placeholder_erased, GDNativePtr<void>) + virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) override { + GDVIRTUAL_CALL(_placeholder_erased, p_placeholder); + } + + static void _bind_methods(); + +public: + EXBIND0RC(bool, can_instantiate) + EXBIND0RC(Ref<Script>, get_base_script) + EXBIND1RC(bool, inherits_script, const Ref<Script> &) + EXBIND0RC(StringName, get_instance_base_type) + + GDVIRTUAL1RC(GDNativePtr<void>, _instance_create, Object *) + virtual ScriptInstance *instance_create(Object *p_this) override { + GDNativePtr<void> ret = nullptr; + GDVIRTUAL_REQUIRED_CALL(_instance_create, p_this, ret); + return reinterpret_cast<ScriptInstance *>(ret.operator void *()); + } + GDVIRTUAL1RC(GDNativePtr<void>, _placeholder_instance_create, Object *) + PlaceHolderScriptInstance *placeholder_instance_create(Object *p_this) override { + GDNativePtr<void> ret = nullptr; + GDVIRTUAL_REQUIRED_CALL(_placeholder_instance_create, p_this, ret); + return reinterpret_cast<PlaceHolderScriptInstance *>(ret.operator void *()); + } + + EXBIND1RC(bool, instance_has, const Object *) + EXBIND0RC(bool, has_source_code) + EXBIND0RC(String, get_source_code) + EXBIND1(set_source_code, const String &) + EXBIND1R(Error, reload, bool) + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_documentation) +#ifdef TOOLS_ENABLED + virtual Vector<DocData::ClassDoc> get_documentation() const override { + TypedArray<Dictionary> doc; + GDVIRTUAL_REQUIRED_CALL(_get_documentation, doc); + + Vector<DocData::ClassDoc> class_doc; +#ifndef _MSC_VER +#warning missing conversion from documentation to ClassDoc +#endif + return class_doc; + } +#endif // TOOLS_ENABLED + + EXBIND1RC(bool, has_method, const StringName &) + + GDVIRTUAL1RC(Dictionary, _get_method_info, const StringName &) + virtual MethodInfo get_method_info(const StringName &p_method) const override { + Dictionary mi; + GDVIRTUAL_REQUIRED_CALL(_get_method_info, p_method, mi); + return MethodInfo::from_dict(mi); + } + + EXBIND0RC(bool, is_tool) + EXBIND0RC(bool, is_valid) + + EXBIND0RC(ScriptLanguage *, get_language) + EXBIND1RC(bool, has_script_signal, const StringName &) + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_script_signal_list) + + virtual void get_script_signal_list(List<MethodInfo> *r_signals) const override { + TypedArray<Dictionary> sl; + GDVIRTUAL_REQUIRED_CALL(_get_script_signal_list, sl); + for (int i = 0; i < sl.size(); i++) { + r_signals->push_back(MethodInfo::from_dict(sl[i])); + } + } + + GDVIRTUAL1RC(bool, _has_property_default_value, const StringName &) + GDVIRTUAL1RC(Variant, _get_property_default_value, const StringName &) + + virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const override { + bool has_dv = false; + if (!GDVIRTUAL_REQUIRED_CALL(_has_property_default_value, p_property, has_dv) || !has_dv) { + return false; + } + Variant ret; + GDVIRTUAL_REQUIRED_CALL(_get_property_default_value, p_property, ret); + return ret; + } + + EXBIND0(update_exports) + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_script_method_list) + + virtual void get_script_method_list(List<MethodInfo> *r_methods) const override { + TypedArray<Dictionary> sl; + GDVIRTUAL_REQUIRED_CALL(_get_script_method_list, sl); + for (int i = 0; i < sl.size(); i++) { + r_methods->push_back(MethodInfo::from_dict(sl[i])); + } + } + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_script_property_list) + + virtual void get_script_property_list(List<PropertyInfo> *r_propertys) const override { + TypedArray<Dictionary> sl; + GDVIRTUAL_REQUIRED_CALL(_get_script_property_list, sl); + for (int i = 0; i < sl.size(); i++) { + r_propertys->push_back(PropertyInfo::from_dict(sl[i])); + } + } + + EXBIND1RC(int, get_member_line, const StringName &) + + GDVIRTUAL0RC(Dictionary, _get_constants) + + virtual void get_constants(Map<StringName, Variant> *p_constants) override { + Dictionary constants; + GDVIRTUAL_REQUIRED_CALL(_get_constants, constants); + List<Variant> keys; + constants.get_key_list(&keys); + for (const Variant &K : keys) { + p_constants->insert(K, constants[K]); + } + } + GDVIRTUAL0RC(TypedArray<StringName>, _get_members) + virtual void get_members(Set<StringName> *p_members) override { + TypedArray<StringName> members; + GDVIRTUAL_REQUIRED_CALL(_get_members, members); + for (int i = 0; i < members.size(); i++) { + p_members->insert(members[i]); + } + } + + EXBIND0RC(bool, is_placeholder_fallback_enabled) + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_rpc_methods) + + virtual const Vector<Multiplayer::RPCConfig> get_rpc_methods() const override { + TypedArray<Dictionary> ret; + GDVIRTUAL_REQUIRED_CALL(_get_rpc_methods, ret); + Vector<Multiplayer::RPCConfig> rpcret; + for (int i = 0; i < ret.size(); i++) { + Dictionary d = ret[i]; + Multiplayer::RPCConfig rpc; + ERR_CONTINUE(!d.has("name")); + rpc.name = d["name"]; + ERR_CONTINUE(!d.has("rpc_mode")); + rpc.rpc_mode = Multiplayer::RPCMode(int(d["rpc_mode"])); + ERR_CONTINUE(!d.has("call_local")); + rpc.call_local = d["call_local"]; + ERR_CONTINUE(!d.has("transfer_mode")); + rpc.transfer_mode = Multiplayer::TransferMode(int(d["transfer_mode"])); + ERR_CONTINUE(!d.has("channel")); + rpc.channel = d["channel"]; + rpcret.push_back(rpc); + } + return rpcret; + } + + ScriptExtension() {} +}; + +typedef ScriptLanguage::ProfilingInfo ScriptLanguageExtensionProfilingInfo; + +GDVIRTUAL_NATIVE_PTR(ScriptLanguageExtensionProfilingInfo) + +class ScriptLanguageExtension : public ScriptLanguage { + GDCLASS(ScriptLanguageExtension, ScriptLanguage) +protected: + static void _bind_methods(); + +public: + EXBIND0RC(String, get_name) + + EXBIND0(init) + EXBIND0RC(String, get_type) + EXBIND0RC(String, get_extension) + EXBIND1R(Error, execute_file, const String &) + EXBIND0(finish) + + /* EDITOR FUNCTIONS */ + + GDVIRTUAL0RC(Vector<String>, _get_reserved_words) + + virtual void get_reserved_words(List<String> *p_words) const override { + Vector<String> ret; + GDVIRTUAL_REQUIRED_CALL(_get_reserved_words, ret); + for (int i = 0; i < ret.size(); i++) { + p_words->push_back(ret[i]); + } + } + EXBIND1RC(bool, is_control_flow_keyword, String) + + GDVIRTUAL0RC(Vector<String>, _get_comment_delimiters) + + virtual void get_comment_delimiters(List<String> *p_words) const override { + Vector<String> ret; + GDVIRTUAL_REQUIRED_CALL(_get_comment_delimiters, ret); + for (int i = 0; i < ret.size(); i++) { + p_words->push_back(ret[i]); + } + } + + GDVIRTUAL0RC(Vector<String>, _get_string_delimiters) + + virtual void get_string_delimiters(List<String> *p_words) const override { + Vector<String> ret; + GDVIRTUAL_REQUIRED_CALL(_get_string_delimiters, ret); + for (int i = 0; i < ret.size(); i++) { + p_words->push_back(ret[i]); + } + } + + EXBIND3RC(Ref<Script>, make_template, const String &, const String &, const String &) + + GDVIRTUAL1RC(TypedArray<Dictionary>, _get_built_in_templates, StringName) + + virtual Vector<ScriptTemplate> get_built_in_templates(StringName p_object) override { + TypedArray<Dictionary> ret; + GDVIRTUAL_REQUIRED_CALL(_get_built_in_templates, p_object, ret); + Vector<ScriptTemplate> stret; + for (int i = 0; i < ret.size(); i++) { + Dictionary d = ret[i]; + ScriptTemplate st; + ERR_CONTINUE(!d.has("inherit")); + st.inherit = d["inherit"]; + ERR_CONTINUE(!d.has("name")); + st.name = d["name"]; + ERR_CONTINUE(!d.has("description")); + st.description = d["description"]; + ERR_CONTINUE(!d.has("content")); + st.content = d["content"]; + ERR_CONTINUE(!d.has("id")); + st.id = d["id"]; + ERR_CONTINUE(!d.has("origin")); + st.origin = TemplateLocation(int(d["origin"])); + stret.push_back(st); + } + return stret; + } + + EXBIND0R(bool, is_using_templates) + + GDVIRTUAL6RC(Dictionary, _validate, const String &, const String &, bool, bool, bool, bool) + 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, Set<int> *r_safe_lines = nullptr) const override { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_validate, p_script, p_path, r_functions != nullptr, r_errors != nullptr, r_warnings != nullptr, r_safe_lines != nullptr, ret); + if (!ret.has("valid")) { + return false; + } + if (r_functions != nullptr && ret.has("functions")) { + Vector<String> functions = ret["functions"]; + for (int i = 0; i < functions.size(); i++) { + r_functions->push_back(functions[i]); + } + } + if (r_errors != nullptr && ret.has("errors")) { + Array errors = ret["errors"]; + for (int i = 0; i < errors.size(); i++) { + Dictionary err = errors[i]; + ERR_CONTINUE(!err.has("line")); + ERR_CONTINUE(!err.has("column")); + ERR_CONTINUE(!err.has("message")); + + ScriptError serr; + serr.line = err["line"]; + serr.column = err["column"]; + serr.message = err["message"]; + + r_errors->push_back(serr); + } + } + if (r_warnings != nullptr && ret.has("warnings")) { + ERR_FAIL_COND_V(!ret.has("warnings"), false); + Array warnings = ret["warnings"]; + for (int i = 0; i < warnings.size(); i++) { + Dictionary warn = warnings[i]; + ERR_CONTINUE(!warn.has("start_line")); + ERR_CONTINUE(!warn.has("end_line")); + ERR_CONTINUE(!warn.has("leftmost_column")); + ERR_CONTINUE(!warn.has("rightmost_column")); + ERR_CONTINUE(!warn.has("code")); + ERR_CONTINUE(!warn.has("string_code")); + ERR_CONTINUE(!warn.has("message")); + + Warning swarn; + swarn.start_line = warn["start_line"]; + swarn.end_line = warn["end_line"]; + swarn.leftmost_column = warn["leftmost_column"]; + swarn.rightmost_column = warn["rightmost_column"]; + swarn.code = warn["code"]; + swarn.string_code = warn["string_code"]; + swarn.message = warn["message"]; + + r_warnings->push_back(swarn); + } + } + if (r_safe_lines != nullptr && ret.has("safe_lines")) { + PackedInt32Array safe_lines = ret["safe_lines"]; + for (int i = 0; i < safe_lines.size(); i++) { + r_safe_lines->insert(safe_lines[i]); + } + } + return ret["valid"]; + } + + EXBIND1RC(String, validate_path, const String &) + GDVIRTUAL0RC(Object *, _create_script) + Script *create_script() const override { + Object *ret = nullptr; + GDVIRTUAL_REQUIRED_CALL(_create_script, ret); + return Object::cast_to<Script>(ret); + } + EXBIND0RC(bool, has_named_classes) + EXBIND0RC(bool, supports_builtin_mode) + EXBIND0RC(bool, supports_documentation) + EXBIND0RC(bool, can_inherit_from_file) + + EXBIND2RC(int, find_function, const String &, const String &) + EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &) + EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int) + EXBIND0R(bool, overrides_external_editor) + + GDVIRTUAL3RC(Dictionary, _complete_code, const String &, const String &, Object *) + + 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 { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_complete_code, p_code, p_path, p_owner, ret); + if (!ret.has("result")) { + return ERR_UNAVAILABLE; + } + + if (r_options != nullptr && ret.has("options")) { + Array options = ret["options"]; + for (int i = 0; i < options.size(); i++) { + Dictionary op = options[i]; + CodeCompletionOption option; + ERR_CONTINUE(!op.has("kind")); + option.kind = CodeCompletionKind(int(op["kind"])); + ERR_CONTINUE(!op.has("display")); + option.display = op["display"]; + ERR_CONTINUE(!op.has("insert_text")); + option.insert_text = op["insert_text"]; + ERR_CONTINUE(!op.has("font_color")); + option.font_color = op["font_color"]; + ERR_CONTINUE(!op.has("icon")); + option.icon = op["icon"]; + ERR_CONTINUE(!op.has("default_value")); + option.default_value = op["default_value"]; + if (op.has("matches")) { + PackedInt32Array matches = op["matches"]; + ERR_CONTINUE(matches.size() & 1); + for (int j = 0; j < matches.size(); j += 2) { + option.matches.push_back(Pair<int, int>(matches[j], matches[j + 1])); + } + } + r_options->push_back(option); + } + } + + ERR_FAIL_COND_V(!ret.has("force"), ERR_UNAVAILABLE); + r_force = ret["force"]; + ERR_FAIL_COND_V(!ret.has("call_hint"), ERR_UNAVAILABLE); + r_call_hint = ret["call_hint"]; + ERR_FAIL_COND_V(!ret.has("result"), ERR_UNAVAILABLE); + Error result = Error(int(ret["result"])); + + return result; + } + + GDVIRTUAL4RC(Dictionary, _lookup_code, const String &, const String &, const String &, Object *) + + virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_lookup_code, p_code, p_symbol, p_path, p_owner, ret); + if (!ret.has("result")) { + return ERR_UNAVAILABLE; + } + + ERR_FAIL_COND_V(!ret.has("type"), ERR_UNAVAILABLE); + r_result.type = LookupResultType(int(ret["type"])); + ERR_FAIL_COND_V(!ret.has("script"), ERR_UNAVAILABLE); + r_result.script = ret["script"]; + ERR_FAIL_COND_V(!ret.has("class_name"), ERR_UNAVAILABLE); + r_result.class_name = ret["class_name"]; + ERR_FAIL_COND_V(!ret.has("class_path"), ERR_UNAVAILABLE); + r_result.class_path = ret["class_path"]; + ERR_FAIL_COND_V(!ret.has("location"), ERR_UNAVAILABLE); + r_result.location = ret["location"]; + + Error result = Error(int(ret["result"])); + + return result; + } + + GDVIRTUAL3RC(String, _auto_indent_code, const String &, int, int) + virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override { + String ret; + GDVIRTUAL_REQUIRED_CALL(_auto_indent_code, p_code, p_from_line, p_to_line, ret); + p_code = ret; + } + EXBIND2(add_global_constant, const StringName &, const Variant &) + EXBIND2(add_named_global_constant, const StringName &, const Variant &) + EXBIND1(remove_named_global_constant, const StringName &) + + /* MULTITHREAD FUNCTIONS */ + + //some VMs need to be notified of thread creation/exiting to allocate a stack + EXBIND0(thread_enter) + EXBIND0(thread_exit) + + EXBIND0RC(String, debug_get_error) + EXBIND0RC(int, debug_get_stack_level_count) + EXBIND1RC(int, debug_get_stack_level_line, int) + EXBIND1RC(String, debug_get_stack_level_function, int) + EXBIND1RC(String, debug_get_stack_level_source, int) + + GDVIRTUAL3R(Dictionary, _debug_get_stack_level_locals, int, int, int) + 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 { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_debug_get_stack_level_locals, p_level, p_max_subitems, p_max_depth, ret); + if (ret.size() == 0) { + return; + } + if (p_locals != nullptr && ret.has("locals")) { + PackedStringArray strings = ret["locals"]; + for (int i = 0; i < strings.size(); i++) { + p_locals->push_back(strings[i]); + } + } + if (p_values != nullptr && ret.has("values")) { + Array values = ret["values"]; + for (int i = 0; i < values.size(); i++) { + p_values->push_back(values[i]); + } + } + } + GDVIRTUAL3R(Dictionary, _debug_get_stack_level_members, int, int, int) + 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 { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_debug_get_stack_level_members, p_level, p_max_subitems, p_max_depth, ret); + if (ret.size() == 0) { + return; + } + if (p_members != nullptr && ret.has("members")) { + PackedStringArray strings = ret["members"]; + for (int i = 0; i < strings.size(); i++) { + p_members->push_back(strings[i]); + } + } + if (p_values != nullptr && ret.has("values")) { + Array values = ret["values"]; + for (int i = 0; i < values.size(); i++) { + p_values->push_back(values[i]); + } + } + } + GDVIRTUAL1R(GDNativePtr<void>, _debug_get_stack_level_instance, int) + + virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override { + GDNativePtr<void> ret = nullptr; + GDVIRTUAL_REQUIRED_CALL(_debug_get_stack_level_instance, p_level, ret); + return reinterpret_cast<ScriptInstance *>(ret.operator void *()); + } + GDVIRTUAL2R(Dictionary, _debug_get_globals, int, int) + virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_debug_get_globals, p_max_subitems, p_max_depth, ret); + if (ret.size() == 0) { + return; + } + if (p_globals != nullptr && ret.has("globals")) { + PackedStringArray strings = ret["globals"]; + for (int i = 0; i < strings.size(); i++) { + p_globals->push_back(strings[i]); + } + } + if (p_values != nullptr && ret.has("values")) { + Array values = ret["values"]; + for (int i = 0; i < values.size(); i++) { + p_values->push_back(values[i]); + } + } + } + + EXBIND4R(String, debug_parse_stack_level_expression, int, const String &, int, int) + + GDVIRTUAL0R(TypedArray<Dictionary>, _debug_get_current_stack_info) + virtual Vector<StackInfo> debug_get_current_stack_info() override { + TypedArray<Dictionary> ret; + GDVIRTUAL_REQUIRED_CALL(_debug_get_current_stack_info, ret); + Vector<StackInfo> sret; + for (int i = 0; i < ret.size(); i++) { + StackInfo si; + Dictionary d = ret[i]; + ERR_CONTINUE(!d.has("file")); + ERR_CONTINUE(!d.has("func")); + ERR_CONTINUE(!d.has("line")); + si.file = d["file"]; + si.func = d["func"]; + si.line = d["line"]; + sret.push_back(si); + } + return sret; + } + + EXBIND0(reload_all_scripts) + EXBIND2(reload_tool_script, const Ref<Script> &, bool) + /* LOADER FUNCTIONS */ + + GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions) + + virtual void get_recognized_extensions(List<String> *p_extensions) const override { + PackedStringArray ret; + GDVIRTUAL_REQUIRED_CALL(_get_recognized_extensions, ret); + for (int i = 0; i < ret.size(); i++) { + p_extensions->push_back(ret[i]); + } + } + + GDVIRTUAL0RC(TypedArray<Dictionary>, _get_public_functions) + virtual void get_public_functions(List<MethodInfo> *p_functions) const override { + TypedArray<Dictionary> ret; + GDVIRTUAL_REQUIRED_CALL(_get_public_functions, ret); + for (int i = 0; i < ret.size(); i++) { + MethodInfo mi = MethodInfo::from_dict(ret[i]); + p_functions->push_back(mi); + } + } + GDVIRTUAL0RC(Dictionary, _get_public_constants) + virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_get_public_constants, ret); + for (int i = 0; i < ret.size(); i++) { + Dictionary d = ret[i]; + ERR_CONTINUE(!d.has("name")); + ERR_CONTINUE(!d.has("value")); + p_constants->push_back(Pair<String, Variant>(d["name"], d["value"])); + } + } + + EXBIND0(profiling_start) + EXBIND0(profiling_stop) + + GDVIRTUAL2R(int, _profiling_get_accumulated_data, GDNativePtr<ScriptLanguageExtensionProfilingInfo>, int) + + virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override { + int ret = 0; + GDVIRTUAL_REQUIRED_CALL(_profiling_get_accumulated_data, p_info_arr, p_info_max, ret); + return ret; + } + + GDVIRTUAL2R(int, _profiling_get_frame_data, GDNativePtr<ScriptLanguageExtensionProfilingInfo>, int) + + virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override { + int ret = 0; + GDVIRTUAL_REQUIRED_CALL(_profiling_get_accumulated_data, p_info_arr, p_info_max, ret); + return ret; + } + + GDVIRTUAL1R(GDNativePtr<void>, _alloc_instance_binding_data, Object *) + + virtual void *alloc_instance_binding_data(Object *p_object) override { + GDNativePtr<void> ret = nullptr; + GDVIRTUAL_REQUIRED_CALL(_alloc_instance_binding_data, p_object, ret); + return ret.operator void *(); + } + + GDVIRTUAL1(_free_instance_binding_data, GDNativePtr<void>) + + virtual void free_instance_binding_data(void *p_data) override { + GDVIRTUAL_REQUIRED_CALL(_free_instance_binding_data, p_data); + } + + EXBIND1(refcount_incremented_instance_binding, Object *) + EXBIND1R(bool, refcount_decremented_instance_binding, Object *) + + EXBIND0(frame) + + EXBIND1RC(bool, handles_global_class_type, const String &) + + GDVIRTUAL1RC(Dictionary, _get_global_class_name, const String &) + + virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr) const override { + Dictionary ret; + GDVIRTUAL_REQUIRED_CALL(_get_global_class_name, p_path, ret); + if (!ret.has("name")) { + return String(); + } + if (r_base_type != nullptr && ret.has("base_type")) { + *r_base_type = ret["base_type"]; + } + if (r_icon_path != nullptr && ret.has("icon_path")) { + *r_icon_path = ret["icon_path"]; + } + return ret["name"]; + } +}; + +VARIANT_ENUM_CAST(ScriptLanguageExtension::LookupResultType) +VARIANT_ENUM_CAST(ScriptLanguageExtension::CodeCompletionKind) + +class ScriptInstanceExtension : public ScriptInstance { +public: + const GDNativeExtensionScriptInstanceInfo *native_info; + GDNativeExtensionScriptInstanceDataPtr instance = nullptr; + +// There should not be warnings on explicit casts. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wignored-qualifiers" +#endif + + virtual bool set(const StringName &p_name, const Variant &p_value) override { + if (native_info->set_func) { + return native_info->set_func(instance, (const GDNativeStringNamePtr)&p_name, (const GDNativeVariantPtr)&p_value); + } + return false; + } + virtual bool get(const StringName &p_name, Variant &r_ret) const override { + if (native_info->get_func) { + return native_info->get_func(instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&r_ret); + } + return false; + } + virtual void get_property_list(List<PropertyInfo> *p_list) const override { + if (native_info->get_property_list_func) { + uint32_t pcount; + const GDNativePropertyInfo *pinfo = native_info->get_property_list_func(instance, &pcount); + for (uint32_t i = 0; i < pcount; i++) { + 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)); + } + if (native_info->free_property_list_func) { + native_info->free_property_list_func(instance, pinfo); + } + } + } + virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const override { + if (native_info->get_property_type_func) { + GDNativeBool is_valid = 0; + GDNativeVariantType type = native_info->get_property_type_func(instance, (const GDNativeStringNamePtr)&p_name, &is_valid); + if (r_is_valid) { + *r_is_valid = is_valid != 0; + } + + return Variant::Type(type); + } + return Variant::NIL; + } + + virtual Object *get_owner() override { + if (native_info->get_owner_func) { + return (Object *)native_info->get_owner_func(instance); + } + return nullptr; + } + static void _add_property_with_state(const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value, void *p_userdata) { + List<Pair<StringName, Variant>> *state = (List<Pair<StringName, Variant>> *)p_userdata; + state->push_back(Pair<StringName, Variant>(*(const StringName *)p_name, *(const Variant *)p_value)); + } + virtual void get_property_state(List<Pair<StringName, Variant>> &state) override { + if (native_info->get_property_state_func) { + native_info->get_property_state_func(instance, _add_property_with_state, &state); + } + } + + virtual void get_method_list(List<MethodInfo> *p_list) const override { + if (native_info->get_method_list_func) { + uint32_t mcount; + const GDNativeMethodInfo *minfo = native_info->get_method_list_func(instance, &mcount); + for (uint32_t i = 0; i < mcount; i++) { + MethodInfo m; + m.name = minfo[i].name; + m.flags = minfo[i].flags; + m.id = minfo[i].id; + 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); + for (uint32_t j = 0; j < minfo[i].argument_count; j++) { + 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)); + } + const Variant *def_values = (const Variant *)minfo[i].default_arguments; + for (uint32_t j = 0; j < minfo[i].default_argument_count; j++) { + m.default_arguments.push_back(def_values[j]); + } + p_list->push_back(m); + } + if (native_info->free_method_list_func) { + native_info->free_method_list_func(instance, minfo); + } + } + } + virtual bool has_method(const StringName &p_method) const override { + if (native_info->has_method_func) { + return native_info->has_method_func(instance, (GDNativeStringNamePtr)&p_method); + } + return false; + } + + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override { + Variant ret; + if (native_info->call_func) { + GDNativeCallError ce; + native_info->call_func(instance, (const GDNativeStringNamePtr)&p_method, (const GDNativeVariantPtr *)p_args, p_argcount, (GDNativeVariantPtr)&ret, &ce); + r_error.error = Callable::CallError::Error(ce.error); + r_error.argument = ce.argument; + r_error.expected = ce.expected; + } + return ret; + } + + virtual void notification(int p_notification) override { + if (native_info->notification_func) { + native_info->notification_func(instance, p_notification); + } + } + virtual String to_string(bool *r_valid) override { + if (native_info->to_string_func) { + GDNativeBool valid; + String ret = native_info->to_string_func(instance, &valid); + if (r_valid) { + *r_valid = valid != 0; + } + return ret; + } + return String(); + } + + virtual void refcount_incremented() override { + if (native_info->refcount_incremented_func) { + native_info->refcount_incremented_func(instance); + } + } + virtual bool refcount_decremented() override { + if (native_info->refcount_decremented_func) { + return native_info->refcount_decremented_func(instance); + } + return false; + } + + virtual Ref<Script> get_script() const override { + if (native_info->get_script_func) { + GDNativeObjectPtr script = native_info->get_script_func(instance); + return Ref<Script>(reinterpret_cast<Script *>(script)); + } + return Ref<Script>(); + } + + virtual bool is_placeholder() const override { + if (native_info->is_placeholder_func) { + return native_info->is_placeholder_func(instance); + } + return false; + } + + virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) override { + if (native_info->set_fallback_func) { + bool ret = native_info->set_fallback_func(instance, (const GDNativeStringNamePtr)&p_name, (const GDNativeVariantPtr)&p_value); + if (r_valid) { + *r_valid = ret; + } + } + } + virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid) override { + Variant ret; + if (native_info->get_fallback_func) { + bool valid = native_info->get_fallback_func(instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&ret); + if (r_valid) { + *r_valid = valid; + } + } + return ret; + } + + virtual ScriptLanguage *get_language() override { + if (native_info->get_language_func) { + GDNativeExtensionScriptLanguagePtr lang = native_info->get_language_func(instance); + return reinterpret_cast<ScriptLanguage *>(lang); + } + return nullptr; + ; + } + virtual ~ScriptInstanceExtension() { + if (native_info->free_func) { + native_info->free_func(instance); + } + } + +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif +}; + +#endif // SCRIPT_LANGUAGE_EXTENSION_H diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 1a306e88fc..415b56cd83 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -71,6 +71,7 @@ #include "core/multiplayer/multiplayer_api.h" #include "core/multiplayer/multiplayer_peer.h" #include "core/object/class_db.h" +#include "core/object/script_language_extension.h" #include "core/object/undo_redo.h" #include "core/os/main_loop.h" #include "core/os/time.h" @@ -142,6 +143,10 @@ void register_core_types() { GDREGISTER_CLASS(Object); GDREGISTER_ABSTRACT_CLASS(Script); + GDREGISTER_ABSTRACT_CLASS(ScriptLanguage); + + GDREGISTER_VIRTUAL_CLASS(ScriptExtension); + GDREGISTER_VIRTUAL_CLASS(ScriptLanguageExtension); GDREGISTER_CLASS(RefCounted); GDREGISTER_CLASS(WeakRef); @@ -263,6 +268,7 @@ void register_core_types() { _engine_debugger = memnew(core_bind::EngineDebugger); GDREGISTER_NATIVE_STRUCT(AudioFrame, "float left;float right"); + GDREGISTER_NATIVE_STRUCT(ScriptLanguageExtensionProfilingInfo, "StringName signature;uint64_t call_count;uint64_t total_time;uint64_t self_time"); } void register_core_settings() { |