Wrong generated setter for arrays
Story: Building a bridge to use C code from C#.
CppSharp generates this:
public __pltd_rssi_test[] rssi_test
{
get
{
__pltd_rssi_test[] __value = null;
if (((__Internal*)__Instance)->rssi_test != null)
{
__value = new __pltd_rssi_test[10];
for (int i = 0; i < 10; i++)
__value[i] = __pltd_rssi_test.__CreateInstance(*((__pltd_rssi_test.__Internal*)&(((__Internal*)__Instance)->rssi_test[i * sizeof(__pltd_rssi_test.__Internal)])));
}
return __value;
}
set
{
if (value != null)
{
if (value.Length != 10)
throw new ArgumentOutOfRangeException("value", "The dimensions of the provided array don't match the required size.");
for (int i = 0; i < 10; i++)
((__Internal*)__Instance)->rssi_test[i * sizeof(__pltd_rssi_test.__Internal)] = *((byte*)(__pltd_rssi_test.__Internal*)value[i].__Instance);
}
}
}
Setter doesn't look correct. To be honest, I'm lost in the pointers and type conversions there - but when I do this:
rssiTest.en = true.ToSbyte();
rssiTest.freq = 2450;
rssiTest.limit = -40;
arr[0] = rssiTest;
tests.rssi_test = arr;
Resulting tests.rssi_test[0].limit is unchanged, although tests.rssi_test[0].en is set correctly. I think it sets only the first byte of the structure.
The header:
#define MAX_SUPPORTED_RF_TESTS 10
typedef struct __pltd_580_test_params
{
_pltd_XTAL_trim xtal_trim; /*!< XTAL trim calibration parameters. */
_pltd_rssi_test rssi_test[MAX_SUPPORTED_RF_TESTS];
//....
}
typedef struct __pltd_rssi_test {
bool en; /*!< Enable or disable the RF RSSI GU test. */
uint32_t freq; /*!< Channel frequency for the RF RSSI tests. */
float limit; /*!< RSSI limit for the RF RSSI test. */
}_pltd_rssi_test;
Also, I would be really thankful if somebody could explain to me how to manually fix this line full of pointers.
Ah, I managed to do this:
for (int i = 0; i < 10; i++)
for (int j = 0; j < sizeof(__pltd_rssi_test.__Internal); j++)
{
byte b = ((byte*)(value[i].__Instance))[j];
((__Internal*) __Instance)->rssi_test[i* sizeof(__pltd_rssi_test.__Internal) + j] = b;
}
Does it look correct?
I think your analysis matches the behavior, the setter does look in fact to be only copying the first byte.
Not sure about the right fix here, I think it depends if __pltd_rssi_test is being considered a value type or not by the binder. I'd think if it's a value type then we can just copy the struct's contents directly, instead of the byte approach we seem to generate right now.
Can you paste the generated struct/class for __pltd_rssi_test?
Sure. https://gist.github.com/archeg/7e1dbbc3f60613704cd5ab069d067c84
Can try removing the (byte*) bit from the setter line and see if that compiles and fixes the issue?
@tritao I think left part of assignment expects byte, so if I remove (byte*) from the right part it doesn't make it better. The VS reports error Cannot cast __Internal to byte
But the code I've posted seem to work, so unless it has some serious flaws I am ok with it.
I see, with the removal of the cast I was trying to make the compiler generate code to do the struct copy itself. With your version it will do the copy byte-by-byte, which should be just a little bit slower, but not really noticeable unless you're doing thousands of them.
Thanks for the bug report, we'll see about fixing this one when possible.
Great! Thanks for your help. If you need anything else for the bug report, please let me know