Browse Source

GDScript: Properly return value with await on non-coroutine

If the keyword `await` is used without a coroutine, it should still
return the value synchronally.
George Marques 3 years ago
parent
commit
056a54db7b

+ 3 - 1
modules/gdscript/gdscript_vm.cpp

@@ -2098,8 +2098,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
 					}
 					}
 
 
 					if (result.get_type() != Variant::SIGNAL) {
 					if (result.get_type() != Variant::SIGNAL) {
+						// Not async, return immediately using the target from OPCODE_AWAIT_RESUME.
+						GET_VARIANT_PTR(target, 3);
+						*target = result;
 						ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
 						ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
-						// The stack pointer should be the same, so we don't need to set a return value.
 						is_signal = false;
 						is_signal = false;
 					} else {
 					} else {
 						sig = result;
 						sig = result;

+ 8 - 0
modules/gdscript/tests/scripts/runtime/features/await_without_coroutine.gd

@@ -0,0 +1,8 @@
+# https://github.com/godotengine/godot/issues/50894
+
+func test():
+	print(await not_coroutine())
+
+
+func not_coroutine():
+	return "awaited"

+ 6 - 0
modules/gdscript/tests/scripts/runtime/features/await_without_coroutine.out

@@ -0,0 +1,6 @@
+GDTEST_OK
+>> WARNING
+>> Line: 4
+>> REDUNDANT_AWAIT
+>> "await" keyword not needed in this case, because the expression isn't a coroutine nor a signal.
+awaited