/**************************************************************************/ /* gdscript.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "gdscript.h" #include "gdscript_analyzer.h" #include "gdscript_cache.h" #include "gdscript_compiler.h" #include "gdscript_parser.h" #include "gdscript_rpc_callable.h" #include "gdscript_tokenizer_buffer.h" #include "gdscript_warning.h" #ifdef TOOLS_ENABLED #include "editor/gdscript_docgen.h" #endif #ifdef TESTS_ENABLED #include "tests/gdscript_test_runner.h" #endif #include "core/config/engine.h" #include "core/config/project_settings.h" #include "core/core_constants.h" #include "core/io/file_access.h" #include "core/io/file_access_encrypted.h" #include "core/os/os.h" #include "scene/resources/packed_scene.h" #include "scene/scene_string_names.h" #ifdef TOOLS_ENABLED #include "editor/editor_paths.h" #endif #include /////////////////////////// GDScriptNativeClass::GDScriptNativeClass(const StringName &p_name) { name = p_name; } bool GDScriptNativeClass::_get(const StringName &p_name, Variant &r_ret) const { bool ok; int64_t v = ClassDB::get_integer_constant(name, p_name, &ok); if (ok) { r_ret = v; return true; } else { return false; } } void GDScriptNativeClass::_bind_methods() { ClassDB::bind_method(D_METHOD("new"), &GDScriptNativeClass::_new); } Variant GDScriptNativeClass::_new() { Object *o = instantiate(); ERR_FAIL_NULL_V_MSG(o, Variant(), "Class type: '" + String(name) + "' is not instantiable."); RefCounted *rc = Object::cast_to(o); if (rc) { return Ref(rc); } else { return o; } } Object *GDScriptNativeClass::instantiate() { return ClassDB::instantiate_no_placeholders(name); } Variant GDScriptNativeClass::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_method == SNAME("new")) { // Constructor. return Object::callp(p_method, p_args, p_argcount, r_error); } MethodBind *method = ClassDB::get_method(name, p_method); if (method && method->is_static()) { // Native static method. return method->call(nullptr, p_args, p_argcount, r_error); } r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return Variant(); } GDScriptFunction *GDScript::_super_constructor(GDScript *p_script) { if (likely(p_script->valid) && p_script->initializer) { return p_script->initializer; } else { GDScript *base_src = p_script->_base; if (base_src != nullptr) { return _super_constructor(base_src); } else { return nullptr; } } } void GDScript::_super_implicit_constructor(GDScript *p_script, GDScriptInstance *p_instance, Callable::CallError &r_error) { GDScript *base_src = p_script->_base; if (base_src != nullptr) { _super_implicit_constructor(base_src, p_instance, r_error); if (r_error.error != Callable::CallError::CALL_OK) { return; } } ERR_FAIL_NULL(p_script->implicit_initializer); if (likely(valid)) { p_script->implicit_initializer->call(p_instance, nullptr, 0, r_error); } else { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; } } GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_is_ref_counted, Callable::CallError &r_error) { /* STEP 1, CREATE */ GDScriptInstance *instance = memnew(GDScriptInstance); instance->base_ref_counted = p_is_ref_counted; instance->members.resize(member_indices.size()); instance->script = Ref(this); instance->owner = p_owner; instance->owner_id = p_owner->get_instance_id(); #ifdef DEBUG_ENABLED //needed for hot reloading for (const KeyValue &E : member_indices) { instance->member_indices_cache[E.key] = E.value.index; } #endif instance->owner->set_script_instance(instance); /* STEP 2, INITIALIZE AND CONSTRUCT */ { MutexLock lock(GDScriptLanguage::singleton->mutex); instances.insert(instance->owner); } _super_implicit_constructor(this, instance, r_error); if (r_error.error != Callable::CallError::CALL_OK) { String error_text = Variant::get_call_error_text(instance->owner, "@implicit_new", nullptr, 0, r_error); instance->script = Ref(); instance->owner->set_script_instance(nullptr); { MutexLock lock(GDScriptLanguage::singleton->mutex); instances.erase(p_owner); } ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance: " + error_text); } if (p_argcount < 0) { return instance; } initializer = _super_constructor(this); if (initializer != nullptr) { initializer->call(instance, p_args, p_argcount, r_error); if (r_error.error != Callable::CallError::CALL_OK) { String error_text = Variant::get_call_error_text(instance->owner, "_init", p_args, p_argcount, r_error); instance->script = Ref(); instance->owner->set_script_instance(nullptr); { MutexLock lock(GDScriptLanguage::singleton->mutex); instances.erase(p_owner); } ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance: " + error_text); } } //@TODO make thread safe return instance; } Variant GDScript::_new(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { /* STEP 1, CREATE */ if (!valid) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return Variant(); } r_error.error = Callable::CallError::CALL_OK; Ref ref; Object *owner = nullptr; GDScript *_baseptr = this; while (_baseptr->_base) { _baseptr = _baseptr->_base; } ERR_FAIL_COND_V(_baseptr->native.is_null(), Variant()); if (_baseptr->native.ptr()) { owner = _baseptr->native->instantiate(); } else { owner = memnew(RefCounted); //by default, no base means use reference } ERR_FAIL_NULL_V_MSG(owner, Variant(), "Can't inherit from a virtual class."); RefCounted *r = Object::cast_to(owner); if (r) { ref = Ref(r); } GDScriptInstance *instance = _create_instance(p_args, p_argcount, owner, r != nullptr, r_error); if (!instance) { if (ref.is_null()) { memdelete(owner); //no owner, sorry } return Variant(); } if (ref.is_valid()) { return ref; } else { return owner; } } bool GDScript::can_instantiate() const { #ifdef TOOLS_ENABLED return valid && (tool || ScriptServer::is_scripting_enabled()); #else return valid; #endif } Ref