Skip to content

Commit 555af04

Browse files
committed
[[ Bug 22047 ]] Allow nothing as variadic argument to foreign handler
This patch ensures that the value nothing can be passed as a variadic argument to a foreign handler - this enables the calling of variadic foreign handlers which might take a nullptr as one of its variadic arguments.
1 parent b0a89ec commit 555af04

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

docs/lcb/notes/22047.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [22047] Allow nothing to be passed as a variadic argument to a foreign handler

libscript/src/script-execute.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,24 +503,27 @@ MCScriptExecuteContext::InvokeForeignVarArgument(MCScriptForeignInvocation& p_in
503503
MCTypeInfoRef t_actual_type = t_resolved_type.type;
504504

505505
// Fetch the promoted type of arg type (sub ints promote to int, and float
506-
// promotes to double).
506+
// promotes to double) - but only if the actual type is foreign.
507507
const MCForeignTypeDescriptor *t_desc = nullptr;
508508
if (MCTypeInfoIsForeign(t_actual_type))
509509
{
510510
t_desc = MCForeignTypeInfoGetDescriptor(t_actual_type);
511511
}
512512

513513
MCTypeInfoRef t_arg_type = t_actual_type;
514-
if (t_desc->promote != nullptr)
514+
if (t_desc != nullptr)
515515
{
516-
MCResolvedTypeInfo t_resolved_arg_type;
517-
if (!ResolveTypeInfo(t_desc->promotedtype,
518-
t_resolved_arg_type))
516+
if (t_desc->promote != nullptr)
519517
{
520-
return false;
518+
MCResolvedTypeInfo t_resolved_arg_type;
519+
if (!ResolveTypeInfo(t_desc->promotedtype,
520+
t_resolved_arg_type))
521+
{
522+
return false;
523+
}
524+
525+
t_arg_type = t_resolved_arg_type.type;
521526
}
522-
523-
t_arg_type = t_resolved_arg_type.type;
524527
}
525528

526529
// Compute the slot attributes - using the (potentially) promoted type.

tests/lcb/vm/foreign-invoke.lcb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ public handler TestForeignInvoke_Varargs()
4040
variable tLongLong as SInt64
4141
variable tFloat as CFloat
4242
variable tDouble as CDouble
43+
variable tNothing as optional Pointer
4344
put 1000 into tInt
4445
put 1000000000 into tLong
4546
put tLong * 1000000 into tLongLong
4647
put 3.5 into tFloat
4748
put 7.5 into tDouble
48-
sprintf(tOutputBuffer, "%d %ld %lld %.1f %.1lf", tInt, tLong, tLongLong, tFloat, tDouble)
49+
sprintf(tOutputBuffer, "%d %ld %lld %.1f %.1lf %p", tInt, tLong, tLongLong, tFloat, tDouble, tNothing)
4950
MCStringCreateWithCString(tOutputBuffer, tString2)
5051

5152
free(tOutputBuffer)
5253
end unsafe
5354
test "sprintf works with no variadic arguments" when tString1 is "no formats"
54-
test "sprintf works with variadic arguments" when tString2 is "1000 1000000000 1000000000000000 3.5 7.5"
55+
test "sprintf works with variadic arguments" when tString2 is "1000 1000000000 1000000000000000 3.5 7.5 0x0"
5556
end handler
5657

5758
--------

0 commit comments

Comments
 (0)