Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ScObject with ScOption. Unify everything under ScVal. #64

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 43 additions & 60 deletions Stellar-contract.x
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ namespace stellar
* Up here in XDR we have variable-length tagged disjoint unions but no
* bit-level packing, so we can be more explicit in their structure, at the
* cost of spending more than 64 bits to encode many cases, and also having
* to convert. It's a little non-obvious at the XDR level why there's a
* split between SCVal and SCObject given that they are both immutable types
* with value semantics; but the split reflects the split that happens in
* the implementation, and marks a place where different implementations of
* immutability (CoW, structural sharing, etc.) will likely occur.
* to convert.
*/

// A symbol is up to 10 chars drawn from [a-zA-Z0-9_], which can be packed
Expand All @@ -48,17 +44,28 @@ typedef string SCSymbol<10>;

enum SCValType
{
SCV_U63 = 0,
SCV_U32 = 1,
SCV_I32 = 2,
SCV_STATIC = 3,
SCV_OBJECT = 4,
SCV_SYMBOL = 5,
SCV_BITSET = 6,
SCV_STATUS = 7
};
// Numbers
SCV_U32 = 0,
SCV_I32 = 1,
SCV_U64 = 2,
SCV_I64 = 3,
SCV_U128 = 4,
SCV_I128 = 5,

// Other Primitives
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing out numbers like this isn't ideal. We reuse these numbers as object code tags which means they get encoded into the instruction stream as literals. Bigger numbers => larger codesize.

SCV_STATIC = 100,
SCV_SYMBOL = 101,
SCV_BITSET = 102,
SCV_STATUS = 103,
SCV_BYTES = 104,
SCV_ACCOUNT_ID = 105,

% struct SCObject;
// Containers
SCV_VEC = 1000,
SCV_MAP = 1001,
SCV_OPTION = 1002,
SCV_CONTRACT_CODE = 1003
};

enum SCStatic
{
Expand Down Expand Up @@ -186,40 +193,38 @@ case SST_CONTRACT_ERROR:

union SCVal switch (SCValType type)
{
case SCV_U63:
int64 u63;
case SCV_U32:
uint32 u32;
case SCV_I32:
int32 i32;
case SCV_U64:
uint64 u64;
case SCV_I64:
int64 i64;
case SCV_U128:
Int128Parts u128;
case SCV_I128:
Int128Parts i128;
case SCV_STATIC:
SCStatic ic;
case SCV_OBJECT:
SCObject* obj;
case SCV_SYMBOL:
SCSymbol sym;
case SCV_BITSET:
uint64 bits;
case SCV_STATUS:
SCStatus status;
};

enum SCObjectType
{
// We have a few objects that represent non-stellar-specific concepts
// like general-purpose maps, vectors, numbers, blobs.

SCO_VEC = 0,
SCO_MAP = 1,
SCO_U64 = 2,
SCO_I64 = 3,
SCO_U128 = 4,
SCO_I128 = 5,
SCO_BYTES = 6,
SCO_CONTRACT_CODE = 7,
SCO_ACCOUNT_ID = 8

// TODO: add more
case SCV_BYTES:
opaque bin<SCVAL_LIMIT>;
case SCV_ACCOUNT_ID:
AccountID accountID;
case SCV_VEC:
SCVec vec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SCVec and SCMap being recursive types here have to be SCVec* amd SCMap*

case SCV_MAP:
SCMap map;
case SCV_OPTION:
SCOption obj;
case SCV_CONTRACT_CODE:
SCContractCode contractCode;
};

struct SCMapEntry
Expand All @@ -232,6 +237,7 @@ const SCVAL_LIMIT = 256000;

typedef SCVal SCVec<SCVAL_LIMIT>;
typedef SCMapEntry SCMap<SCVAL_LIMIT>;
typedef *SCVal SCOption;

enum SCContractCodeType
{
Expand All @@ -254,26 +260,3 @@ struct Int128Parts {
uint64 lo;
uint64 hi;
};

union SCObject switch (SCObjectType type)
{
case SCO_VEC:
SCVec vec;
case SCO_MAP:
SCMap map;
case SCO_U64:
uint64 u64;
case SCO_I64:
int64 i64;
case SCO_U128:
Int128Parts u128;
case SCO_I128:
Int128Parts i128;
case SCO_BYTES:
opaque bin<SCVAL_LIMIT>;
case SCO_CONTRACT_CODE:
SCContractCode contractCode;
case SCO_ACCOUNT_ID:
AccountID accountID;
};
}