Reduce RefCounted copies in GDScriptFunction::call

This commit is contained in:
Mikael Hermansson
2026-03-26 00:46:43 +01:00
parent babc272d44
commit 0636f7adff
2 changed files with 21 additions and 2 deletions
+11 -2
View File
@@ -660,7 +660,11 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
memnew_placement(&stack[ADDR_STACK_SELF], Variant);
script = _script;
}
memnew_placement(&stack[ADDR_STACK_CLASS], Variant(script));
// We must call a `Variant` constructor here, as accessing an object without doing so is undefined behavior.
memnew_placement(&stack[ADDR_STACK_CLASS], Variant);
VariantInternal::object_assign_without_ref_unsafe(&stack[ADDR_STACK_CLASS], script);
memnew_placement(&stack[ADDR_STACK_NIL], Variant);
String err_text;
@@ -4009,7 +4013,12 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
GDScriptLanguage::get_singleton()->exit_function();
}
for (int i = 0; i < _stack_size; i++) {
// We deliberately avoid calling the destructor for `ADDR_STACK_CLASS`, since we initialized it
// without incrementing any reference count that it might have.
stack[ADDR_STACK_SELF].~Variant();
stack[ADDR_STACK_NIL].~Variant();
for (int i = FIXED_ADDRESSES_MAX; i < _stack_size; i++) {
stack[i].~Variant();
}