See More

/**************************************************************************/ /* javascript.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 "javascript.h" #include "core/config/engine.h" #include "core/io/file_access_encrypted.h" #include "javascript_instance.h" #include "javascript_language.h" #include "scene/resources/resource_format_text.h" ScriptLanguage *JavaScript::get_language() const { return JavaScriptLanguage::get_singleton(); } JavaScript::JavaScript() { } JavaScript::~JavaScript() { } bool JavaScript::can_instantiate() const { #ifdef TOOLS_ENABLED return is_valid() && (is_tool() || ScriptServer::is_scripting_enabled()); #else return is_valid(); #endif } StringName JavaScript::get_global_name() const { return StringName(); } StringName JavaScript::get_instance_base_type() const { static StringName empty; ERR_FAIL_NULL_V(javascript_class, empty); ERR_FAIL_NULL_V(javascript_class->native_class, empty); return javascript_class->native_class->name; } ScriptInstance *JavaScript::instance_create(Object *p_this) { JavaScriptBinder *binder = JavaScriptLanguage::get_thread_binder(Thread::get_caller_id()); ERR_FAIL_NULL_V_MSG(binder, NULL, "Cannot create instance from this thread"); const JavaScriptClassInfo *cls = NULL; JavaScriptError js_err; if (!bytecode.is_empty()) { cls = binder->parse_javascript_class(bytecode, script_path, false, &js_err); } else { cls = binder->parse_javascript_class(code, script_path, false, &js_err); } ERR_FAIL_NULL_V_MSG(cls, NULL, vformat("Cannot parse class from %s", get_script_path())); if (!ClassDB::is_parent_class(p_this->get_class_name(), cls->native_class->name)) { ERR_FAIL_V_MSG(NULL, vformat("Script inherits from native type '%s', so it can't be instanced in object of type: '%s'", cls->native_class->name, p_this->get_class())); } JavaScriptGCHandler js_instance = binder->create_js_instance_for_godot_object(cls, p_this); ERR_FAIL_NULL_V(js_instance.javascript_object, NULL); JavaScriptInstance *instance = memnew(JavaScriptInstance); instance->script = Ref(this); instance->owner = p_this; instance->binder = binder; instance->javascript_object = js_instance; instance->javascript_class = cls; instance->owner->set_script_instance(instance); instances.insert(p_this); return instance; } PlaceHolderScriptInstance *JavaScript::placeholder_instance_create(Object *p_this) { #ifdef TOOLS_ENABLED PlaceHolderScriptInstance *si = memnew(PlaceHolderScriptInstance(JavaScriptLanguage::get_singleton(), Ref