[[ LCB ]] Add varargs support to foreign handlers#5666
Conversation
This patch adds varargs support to foreign C handlers, allowing binding to a wider range of C functions. The syntax for vararg handlers is similar to that of C: ``` foreign handler myvarargs(in pFirst as Pointer, ...) returns CInt binds to ... ``` Using the token `...` to indicate the start of the variable parameter list. To implement vararg functions, a new parameter mode 'variadic' has been added which causes a handler to be treated as variadic if such a parameter with a mode is present. As C requires any int type with rank less than int to be promoted to int, and float to be promoted to double (when passed to a non-fixed parameter in a variadic function), a 'promotedtype' and 'promote' function have been added to foreign type descriptors to describe the appropriate relation.
| { | ||
| // Get the value in the register, this determines the type. | ||
| MCValueRef t_arg_value = nil; | ||
| t_arg_value = CheckedFetchRegister(p_arg_reg); |
There was a problem hiding this comment.
This should probably just be MCValueRef t_arg_value = CheckedFetchRegister(p_arg_reg);
|
|
||
| if (p_fields[i].mode == kMCHandlerTypeFieldModeVariadic) | ||
| { | ||
| p_field_count = i; |
There was a problem hiding this comment.
Perhaps it's worth asserting that p_field_count == i + 1 and i != 0 as these things are checked at (lc-)compile-time aren't they? Just to ensure we don't break the assumptions about the position of the variadic param internally anywhere.
| @@ -202,10 +209,32 @@ class MCScriptForeignInvocation | |||
| break; | |||
|
|
|||
| case kMCScriptForeignHandlerLanguageC: | |||
There was a problem hiding this comment.
We should probably throw an error if a variadic foreign handler binds to java since it's not yet supported
There was a problem hiding this comment.
I fixed this in the MCJavaCheckSignature - it throws a binding error if there is a variadic parameter present.
This patch makes it so that JavaCheckSignature will fail if an attempt is made to bind to a variadic signature.
This patch ensures that the position of the variadic parameter is valid when creating a handler typeinfo. If it is not, the creation fails, and an error is thrown.
This patch tidies up a case of variable definition and initialization being in separate commands.
|
@livecodeali Hopefully that's all the current feedback addressed appropriately. |
|
@livecode-vulcan review ok a8b2d34 |
|
💙 review by @livecodeali ok a8b2d34 |
|
😎 test success a8b2d34
|
This patch adds varargs support to foreign C handlers, allowing
binding to a wider range of C functions.
The syntax for vararg handlers is similar to that of C:
Using the token
...to indicate the start of the variableparameter list.
To implement vararg functions, a new parameter mode 'variadic'
has been added which causes a handler to be treated as variadic if
such a parameter with a mode is present.
As C requires any int type with rank less than int to be promoted
to int, and float to be promoted to double (when passed to a non-fixed
parameter in a variadic function), a 'promotedtype' and 'promote'
function have been added to foreign type descriptors to describe
the appropriate relation.