Skip to content

armgit / godot

Public Imported from GitHub
0

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

GDScript: remove temporary when calling void function or uncaptured return value

aurpine9a557077354f
diff --git a/modules/gdscript/gdscript_byte_codegen.cpp b/modules/gdscript/gdscript_byte_codegen.cpp
index da82871b74f..fb0c98696fc 100644
--- a/modules/gdscript/gdscript_byte_codegen.cpp
+++ b/modules/gdscript/gdscript_byte_codegen.cpp
@@ -1084,16 +1084,27 @@ GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target
}
void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
+ if (p_target.mode == Address::NIL) {
+ append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL, 2 + p_arguments.size());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ append(p_arguments[i]);
+ }
+ append(p_base);
+ append(Address());
+ append(p_arguments.size());
+ append(p_function_name);
+ } else {
+ append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ append(p_arguments[i]);
+ }
+ append(p_base);
+ CallTarget ct = get_call_target(p_target);
+ append(ct.target);
+ append(p_arguments.size());
+ append(p_function_name);
+ ct.cleanup();
}
- append(p_base);
- CallTarget ct = get_call_target(p_target);
- append(ct.target);
- append(p_arguments.size());
- append(p_function_name);
- ct.cleanup();
}
void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
@@ -1343,16 +1354,27 @@ void GDScriptByteCodeGenerator::write_call_method_bind_validated(const Address &
}
void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
- append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
- for (int i = 0; i < p_arguments.size(); i++) {
- append(p_arguments[i]);
+ if (p_target.mode == Address::NIL) {
+ append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL, 2 + p_arguments.size());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ append(p_arguments[i]);
+ }
+ append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
+ append(Address());
+ append(p_arguments.size());
+ append(p_function_name);
+ } else {
+ append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ append(p_arguments[i]);
+ }
+ append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
+ CallTarget ct = get_call_target(p_target);
+ append(ct.target);
+ append(p_arguments.size());
+ append(p_function_name);
+ ct.cleanup();
}
- append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
- CallTarget ct = get_call_target(p_target);
- append(ct.target);
- append(p_arguments.size());
- append(p_function_name);
- ct.cleanup();
}
void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
diff --git a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd
index ba16acd1688..a64088361e8 100644
--- a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd
+++ b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.gd
@@ -7,6 +7,7 @@ class TestRefCounted:
func test() -> void:
test1()
test2()
+ test3()
func test1(_a: Array = []) -> void:
print("test1 (untyped Array default)")
@@ -17,3 +18,11 @@ func test2(_a: Array[String] = []) -> void:
print("test2 (typed Array[String] default)")
var _count = TestRefCounted.new().get_reference_count()
print(" end of statement")
+
+func returns_param(param):
+ return param
+
+func test3() -> void:
+ print("test3 (function call)")
+ returns_param(TestRefCounted.new())
+ print(" end of statement")
diff --git a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out
index a242eab92e5..956ff6cf808 100644
--- a/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out
+++ b/modules/gdscript/tests/scripts/runtime/features/release_at_end_of_statement.out
@@ -5,3 +5,6 @@ test1 (untyped Array default)
test2 (typed Array[String] default)
TestRefCounted released
end of statement
+test3 (function call)
+ TestRefCounted released
+ end of statement