Clarify how data should be passed into and out of a contract depending on its type
A contract call from an external context requires passing some data through the $rA register (see CALL).
Register $rA is a memory address from which the following fields are set (word-aligned):
| bytes | type | value | description |
|---|---|---|---|
| 32 | byte[32] |
to |
Contract ID to call. |
| 8 | byte[8] |
param1 |
First parameter. |
| 8 | byte[8] |
param2 |
Second parameter. |
param2 is used to pass the arguments to the contract method.
In the special case where the contract method has a single argument that fits in a word, it is possible to store the value of that argument in param2 directly. In all other cases (single large argument or multiple arguments), param2 has to store a pointer to the pile of data containing the arguments.
The specs should be made clear as to how the above should be done. I think having param2 always containing a pointer to the data regardless of how the data looks like is more sensible than the above as it simplifies external callers.
Relevant case: https://github.com/FuelLabs/fuels-rs/issues/201 for more context.
In https://github.com/FuelLabs/sway/issues/1230 it's suggested that single element structs which fit in a register and so that should also be specified explicitly (if it isn't already). 'Fits in a word' is slightly open to interpretation.
It is not just structs though right?.. so we have to be clear about other types as well such as strings.
This issue: https://github.com/FuelLabs/sway/issues/1368 now has more information about the problem.
The spec needs to clarify two things:
- How data is passed into the contract using
param2 - How data is passed out of the contract using
ret/retd
As per the recent discussion around this, integers and booleans should be passed by value into the contract and should be returned using ret. All other types should be passed as a pointer (i.e. param2 contains a pointer to the data and not the data itself) and returned using retd.
Closed in favor of https://github.com/FuelLabs/sway/issues/5512