diff options
author | Juan Linietsky <juan@godotengine.org> | 2020-02-13 16:03:10 -0300 |
---|---|---|
committer | Juan Linietsky <juan@godotengine.org> | 2020-02-15 08:36:04 -0300 |
commit | 867d073b98344b848c96012418912a7e72841a31 (patch) | |
tree | 3a0fa22ce848b4ee20df2057ebb5eb5d7ea8e89b /modules/gdscript | |
parent | 53cf289f309ef44821e5bb1fe0c81de29a82d9d3 (diff) |
Changed logic and optimized ObjectID in ObjectDB and Variant, removed RefPtr.
Diffstat (limited to 'modules/gdscript')
-rw-r--r-- | modules/gdscript/gdscript.cpp | 9 | ||||
-rw-r--r-- | modules/gdscript/gdscript.h | 1 | ||||
-rw-r--r-- | modules/gdscript/gdscript_function.cpp | 62 | ||||
-rw-r--r-- | modules/gdscript/gdscript_function.h | 8 | ||||
-rw-r--r-- | modules/gdscript/gdscript_functions.cpp | 4 |
5 files changed, 51 insertions, 33 deletions
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index c71ec6ec76..07c74a2e26 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -93,6 +93,7 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco instance->members.resize(member_indices.size()); instance->script = Ref<GDScript>(this); instance->owner = p_owner; + instance->owner_id = p_owner->get_instance_id(); #ifdef DEBUG_ENABLED //needed for hot reloading for (Map<StringName, MemberInfo>::Element *E = member_indices.front(); E; E = E->next()) { @@ -1792,7 +1793,7 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so obj->get_script_instance()->get_property_state(state); map[obj->get_instance_id()] = state; - obj->set_script(RefPtr()); + obj->set_script(Variant()); } } @@ -1808,7 +1809,7 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so map.insert(obj->get_instance_id(), List<Pair<StringName, Variant> >()); List<Pair<StringName, Variant> > &state = map[obj->get_instance_id()]; obj->get_script_instance()->get_property_state(state); - obj->set_script(RefPtr()); + obj->set_script(Variant()); } else { // no instance found. Let's remove it so we don't loop forever E->get()->placeholders.erase(E->get()->placeholders.front()->get()); @@ -1839,9 +1840,9 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so if (!p_soft_reload) { //clear it just in case (may be a pending reload state) - obj->set_script(RefPtr()); + obj->set_script(Variant()); } - obj->set_script(scr.get_ref_ptr()); + obj->set_script(scr); ScriptInstance *script_instance = obj->get_script_instance(); diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index 4af574cd9d..3d24f9b3f5 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -241,6 +241,7 @@ class GDScriptInstance : public ScriptInstance { friend class GDScriptFunctions; friend class GDScriptCompiler; + ObjectID owner_id; Object *owner; Ref<GDScript> script; #ifdef DEBUG_ENABLED diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp index 7392bbc10a..cbf7d81a61 100644 --- a/modules/gdscript/gdscript_function.cpp +++ b/modules/gdscript/gdscript_function.cpp @@ -137,18 +137,19 @@ static String _get_var_type(const Variant *p_var) { String basestr; if (p_var->get_type() == Variant::OBJECT) { - Object *bobj = *p_var; + bool was_freed; + Object *bobj = p_var->get_validated_object_with_check(was_freed); if (!bobj) { - basestr = "null instance"; - } else { - if (ObjectDB::instance_validate(bobj)) { - if (bobj->get_script_instance()) - basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")"; - else - basestr = bobj->get_class(); + if (was_freed) { + basestr = "null instance"; } else { - basestr = "previously freed instance"; + basestr = "previously freed"; } + } else { + if (bobj->get_script_instance()) + basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")"; + else + basestr = bobj->get_class(); } } else { @@ -497,14 +498,26 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool extends_ok = false; if (a->get_type() == Variant::OBJECT && a->operator Object *() != NULL) { - Object *obj_A = *a; - Object *obj_B = *b; #ifdef DEBUG_ENABLED - if (!ObjectDB::instance_validate(obj_A)) { - err_text = "Left operand of 'is' was already freed."; + bool was_freed; + Object *obj_A = a->get_validated_object_with_check(was_freed); + + if (was_freed) { + err_text = "Left operand of 'is' is a previously freed instance."; OPCODE_BREAK; } + + Object *obj_B = b->get_validated_object_with_check(was_freed); + + if (was_freed) { + err_text = "Right operand of 'is' is a previously freed instance."; + OPCODE_BREAK; + } +#else + + Object *obj_A = *a; + Object *obj_B = *b; #endif // DEBUG_ENABLED GDScript *scr_B = Object::cast_to<GDScript>(obj_B); @@ -1298,20 +1311,20 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #endif - Object *obj = argobj->operator Object *(); +#ifdef DEBUG_ENABLED + bool was_freed; + Object *obj = argobj->get_validated_object_with_check(was_freed); String signal = argname->operator String(); -#ifdef DEBUG_ENABLED + if (was_freed) { + err_text = "First argument of yield() is a previously freed instance."; + OPCODE_BREAK; + } + if (!obj) { err_text = "First argument of yield() is null."; OPCODE_BREAK; } - if (ScriptDebugger::get_singleton()) { - if (!ObjectDB::instance_validate(obj)) { - err_text = "First argument of yield() is a previously freed instance."; - OPCODE_BREAK; - } - } if (signal.length() == 0) { err_text = "Second argument of yield() is an empty string (for signal name)."; @@ -1324,6 +1337,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_BREAK; } #else + Object *obj = argobj->operator Object *(); + String signal = argname->operator String(); + obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT); #endif } @@ -1565,14 +1581,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a //error // function, file, line, error, explanation String err_file; - if (p_instance && ObjectDB::instance_validate(p_instance->owner) && p_instance->script->is_valid() && p_instance->script->path != "") + if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") err_file = p_instance->script->path; else if (script) err_file = script->path; if (err_file == "") err_file = "<built-in>"; String err_func = name; - if (p_instance && ObjectDB::instance_validate(p_instance->owner) && p_instance->script->is_valid() && p_instance->script->name != "") + if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") err_func = p_instance->script->name + "." + err_func; int err_line = line; if (err_text == "") { diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h index ad95ebc543..7b7bcbaac9 100644 --- a/modules/gdscript/gdscript_function.h +++ b/modules/gdscript/gdscript_function.h @@ -77,8 +77,8 @@ struct GDScriptDataType { return false; } - Object *obj = p_variant.operator Object *(); - if (!obj || !ObjectDB::instance_validate(obj)) { + Object *obj = p_variant.get_validated_object(); + if (!obj) { return false; } @@ -100,8 +100,8 @@ struct GDScriptDataType { return false; } - Object *obj = p_variant.operator Object *(); - if (!obj || !ObjectDB::instance_validate(obj)) { + Object *obj = p_variant.get_validated_object(); + if (!obj) { return false; } diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index 1a5087eb4d..a46337d7dd 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -1441,8 +1441,8 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ if (p_args[0]->get_type() != Variant::OBJECT) { r_ret = false; } else { - Object *obj = *p_args[0]; - r_ret = ObjectDB::instance_validate(obj); + Object *obj = p_args[0]->get_validated_object(); + r_ret = obj != nullptr; } } break; |