Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

GDScript VM: guard err_text with debug

aurpineb80e350896b7
diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h
index 86b8b7da6d7..48820e7d3e2 100644
--- a/modules/gdscript/gdscript_function.h
+++ b/modules/gdscript/gdscript_function.h
@@ -449,10 +449,11 @@ private:
HashMap<String, NativeProfile> native_calls;
HashMap<String, NativeProfile> last_native_calls;
} profile;
-#endif
String _get_call_error(const String &p_where, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const;
String _get_callable_call_error(const String &p_where, const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const;
+#endif
+
Variant _get_default_variant_for_data_type(const GDScriptDataType &p_data_type);
public:
diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp
index 87b75bfb727..65cbb550070 100644
--- a/modules/gdscript/gdscript_vm.cpp
+++ b/modules/gdscript/gdscript_vm.cpp
@@ -152,6 +152,7 @@ Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataT
return Variant();
}
+#ifdef DEBUG_ENABLED
String GDScriptFunction::_get_call_error(const String &p_where, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const {
switch (p_err.error) {
case Callable::CallError::CALL_OK:
@@ -165,7 +166,6 @@ String GDScriptFunction::_get_call_error(const String &p_where, const Variant **
ERR_FAIL_INDEX_V_MSG(p_err.argument, p_argcount, "Bug: Invalid call error argument index.", "Bug: Invalid call error argument index.");
ERR_FAIL_NULL_V_MSG(p_argptrs[p_err.argument], "Bug: Argument is null pointer.", "Bug: Argument is null pointer.");
// Handle the Object to Object case separately as we don't have further class details.
-#ifdef DEBUG_ENABLED
if (p_err.expected == Variant::OBJECT && p_argptrs[p_err.argument]->get_type() == p_err.expected) {
return "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(p_err.argument + 1) + " (" + _get_var_type(p_argptrs[p_err.argument]) + ") is not a subclass of the expected argument class.";
}
@@ -175,7 +175,6 @@ String GDScriptFunction::_get_call_error(const String &p_where, const Variant **
if (p_err.expected == Variant::DICTIONARY && p_argptrs[p_err.argument]->get_type() == p_err.expected) {
return "Invalid type in " + p_where + ". The dictionary of argument " + itos(p_err.argument + 1) + " (" + _get_var_type(p_argptrs[p_err.argument]) + ") does not have the same element type as the expected typed dictionary argument.";
}
-#endif // DEBUG_ENABLED
return "Invalid type in " + p_where + ". Cannot convert argument " + itos(p_err.argument + 1) + " from " + Variant::get_type_name(p_argptrs[p_err.argument]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
@@ -208,6 +207,7 @@ String GDScriptFunction::_get_callable_call_error(const String &p_where, const C
return _get_call_error(p_where, (const Variant **)argptrs.ptr(), argptrs.size(), p_ret, p_err);
}
}
+#endif // DEBUG_ENABLED
void (*type_init_function_table[])(Variant *) = {
nullptr, // NIL (shouldn't be called).
@@ -662,7 +662,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
memnew_placement(&stack[ADDR_STACK_NIL], Variant);
+#ifdef DEBUG_ENABLED
String err_text;
+#endif
GDScriptLanguage::CallLevel call_level;
GDScriptLanguage::get_singleton()->enter_function(&call_level, p_instance, this, stack, &ip, &line);
@@ -937,7 +939,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
bool was_freed = false;
Object *object = value->get_validated_object_with_check(was_freed);
if (was_freed) {
+#ifdef DEBUG_ENABLED
err_text = "Left operand of 'is' is a previously freed instance.";
+#endif
OPCODE_BREAK;
}
@@ -959,7 +963,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
bool was_freed = false;
Object *object = value->get_validated_object_with_check(was_freed);
if (was_freed) {
+#ifdef DEBUG_ENABLED
err_text = "Left operand of 'is' is a previously freed instance.";
+#endif
OPCODE_BREAK;
}
@@ -2186,7 +2192,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
#endif
if (err.error != Callable::CallError::CALL_OK) {
+#ifdef DEBUG_ENABLED
err_text = _get_call_error("static function '" + method->get_name().string() + "' in type '" + method->get_instance_class().string() + "'", argptrs, argc, *ret, err);
+#endif
OPCODE_BREAK;
}
@@ -2550,8 +2558,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
}
if (err.error != Callable::CallError::CALL_OK) {
+#ifdef DEBUG_ENABLED
String methodstr = *methodname;
err_text = _get_call_error("function '" + methodstr + "'", (const Variant **)argptrs, argc, *dst, err);
+#endif
OPCODE_BREAK;
}
@@ -2577,7 +2587,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
Object *obj = argobj->get_validated_object_with_check(was_freed);
if (was_freed) {
+#ifdef DEBUG_ENABLED
err_text = "Trying to await on a freed object.";
+#endif
OPCODE_BREAK;
}
@@ -2642,7 +2654,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback").bind(retvalue), Object::CONNECT_ONE_SHOT);
if (err != OK) {
+#ifdef DEBUG_ENABLED
err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
+#endif
OPCODE_BREAK;
}
@@ -3804,7 +3818,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
GD_ERR_BREAK(globalname_idx < 0 || globalname_idx >= _global_names_count);
const StringName *globalname = &_global_names_ptr[globalname_idx];
if (unlikely(!GDScriptLanguage::get_singleton()->get_named_globals_map().has(*globalname))) {
+#ifdef DEBUG_ENABLED
err_text = vformat(R"(Trying to access non-existent autoload singleton "%s".)", *globalname);
+#endif
OPCODE_BREAK;
}