Skip to content

Commit

Permalink
Fix wrong arguments in callv with const Array
Browse files Browse the repository at this point in the history
Add unit test
  • Loading branch information
Nolkaloid committed Jun 27, 2024
1 parent ba3bb44 commit 606ec2e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/variant/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_ret
Variant Callable::callv(const Array &p_arguments) const {
int argcount = p_arguments.size();
const Variant **argptrs = nullptr;

if (argcount) {
argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);

// Use a duplicate if the array is readonly, otherwise the address of p_arguments->read_only will be copied
const Array &arguments_source = p_arguments.is_read_only() ? p_arguments.duplicate() : p_arguments;

for (int i = 0; i < argcount; i++) {
argptrs[i] = &p_arguments[i];
argptrs[i] = &arguments_source[i];
}
}

CallError ce;
Variant ret;
callp(argptrs, argcount, ret, ce);
Expand Down
20 changes: 20 additions & 0 deletions modules/gdscript/tests/scripts/runtime/features/callable_callv.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var array_var: Array = ["one", "two", "three", "four"]
const array_const: Array = ["one", "two", "three", "four"]

var array_nested_var: Array = [["one"], ["two"], ["three"], ["four"]]
const array_nested_const: Array = [["one"], ["two"], ["three"], ["four"]]


func test():
assert(array_const.is_read_only() == true)
assert(array_nested_const.is_read_only() == true)

print_four_variants.callv(array_var)
print_four_variants.callv(array_const)

print_four_variants.callv(array_nested_var)
print_four_variants.callv(array_nested_const)


func print_four_variants(v1, v2, v3, v4):
print("%s %s %s %s" % [v1, v2, v3, v4])
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GDTEST_OK
one two three four
one two three four
["one"] ["two"] ["three"] ["four"]
["one"] ["two"] ["three"] ["four"]

0 comments on commit 606ec2e

Please sign in to comment.