Skip to content

Commit

Permalink
feat: uint1-256 and int1-257 serialization formats for Ints (#558)
Browse files Browse the repository at this point in the history
In accordance to built-in types of TL-B & TON:
https://docs.ton.org/develop/data-formats/tl-b-language#built-in-types
  • Loading branch information
novusnota authored Aug 25, 2024
1 parent b220e21 commit 59677d8
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 136 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `exists` method for the `Map` type: PR [#581](https://github.com/tact-lang/tact/pull/581)
- The `storeBit` method for `Builder` type and the `loadBit` method for `Slice` type: PR [#699](https://github.com/tact-lang/tact/pull/699)
- The `toSlice` method for structs and messages: PR [#630](https://github.com/tact-lang/tact/pull/630)
- Wider range of serialization options for integers — `uint1` through `uint256` and `int1` through `int257`: PR [#558](https://github.com/tact-lang/tact/pull/558)

### Changed

Expand Down
93 changes: 19 additions & 74 deletions src/storage/allocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,49 +76,17 @@ export function getAllocationOperationFromField(
switch (src.kind) {
case "simple": {
if (src.type === "int") {
if (src.format === 8) {
return {
kind: "int",
bits: 8,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 16) {
return {
kind: "int",
bits: 16,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 32) {
return {
kind: "int",
bits: 32,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 64) {
return {
kind: "int",
bits: 64,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 128) {
return {
kind: "int",
bits: 128,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 256) {
return {
kind: "int",
bits: 256,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 257) {
if (typeof src.format === "number") {
if (src.format < 1 || src.format > 257) {
throw Error("Unsupported int format " + src.format);
}
return {
kind: "int",
bits: 257,
bits: src.format,
optional: src.optional ? src.optional : false,
};
} else if (src.format !== null && src.format !== undefined) {
}
if (src.format !== null && src.format !== undefined) {
throwInternalCompilerError(
`Unsupported int format: ${src.format}`,
);
Expand All @@ -130,50 +98,27 @@ export function getAllocationOperationFromField(
};
}
if (src.type === "uint") {
if (src.format === 8) {
return {
kind: "uint",
bits: 8,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 16) {
return {
kind: "uint",
bits: 16,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 32) {
return {
kind: "uint",
bits: 32,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 64) {
return {
kind: "uint",
bits: 64,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 128) {
return {
kind: "uint",
bits: 128,
optional: src.optional ? src.optional : false,
};
} else if (src.format === 256) {
if (typeof src.format === "number") {
if (src.format < 1 || src.format > 256) {
throwInternalCompilerError(
`Unsupported uint format: ${src.format}`,
);
}
return {
kind: "uint",
bits: 256,
bits: src.format,
optional: src.optional ? src.optional : false,
};
} else if (src.format === "coins") {
}
if (src.format === "coins") {
return {
kind: "coins",
optional: src.optional ? src.optional : false,
};
} else if (src.format !== null && src.format !== undefined) {
}
if (src.format !== null && src.format !== undefined) {
throwInternalCompilerError(
`Unsupported int format: ${src.format}`,
`Unsupported uint format: ${src.format}`,
);
}
return {
Expand Down
10 changes: 5 additions & 5 deletions src/test/e2e-emulated/__snapshots__/map.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
exports[`map should implement maps correctly 1`] = `
[
{
"$seq": 163,
"$seq": 173,
"events": [
{
"$type": "storage-charged",
"amount": "0.000000108",
"amount": "0.000000123",
},
{
"$type": "received",
Expand All @@ -18,7 +18,7 @@ exports[`map should implement maps correctly 1`] = `
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQD2HX3w4uRmCoe34-siEhRl5sEC6x8BSBZexutmtSOfYWoh",
"to": "kQB3MEsMEVBqn5m5-U9gWK8kE3oZHyWyY65DHGZB_94zLiUy",
"type": "internal",
"value": "1",
},
Expand All @@ -36,10 +36,10 @@ exports[`map should implement maps correctly 1`] = `
"type": "cell",
},
"bounce": false,
"from": "kQD2HX3w4uRmCoe34-siEhRl5sEC6x8BSBZexutmtSOfYWoh",
"from": "kQB3MEsMEVBqn5m5-U9gWK8kE3oZHyWyY65DHGZB_94zLiUy",
"to": "@treasure(treasure)",
"type": "internal",
"value": "0.982029",
"value": "0.981251",
},
},
],
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e-emulated/__snapshots__/structs.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ exports[`structs should implement structs correctly 13`] = `
"events": [
{
"$type": "storage-charged",
"amount": "0.000000035",
"amount": "0.000000038",
},
{
"$type": "received",
Expand All @@ -300,7 +300,7 @@ exports[`structs should implement structs correctly 13`] = `
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQCHYvnW92OPDk0lG_YI0DX9anWqZygSM4vJtR-ctm9picG2",
"to": "kQAEcYCw2VVyGBNnzjxjFiYHNMAveBH1SXQ3gL7H00QQaLQS",
"type": "internal",
"value": "10",
},
Expand All @@ -318,7 +318,7 @@ exports[`structs should implement structs correctly 13`] = `
"type": "cell",
},
"bounce": false,
"from": "kQCHYvnW92OPDk0lG_YI0DX9anWqZygSM4vJtR-ctm9picG2",
"from": "kQAEcYCw2VVyGBNnzjxjFiYHNMAveBH1SXQ3gL7H00QQaLQS",
"to": "@treasure(treasure)",
"type": "internal",
"value": "9.988015",
Expand Down
87 changes: 83 additions & 4 deletions src/test/e2e-emulated/contracts/maps.tact
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ message SetUIntMap9 {
value: Int?;
}

message SetUIntMap10 {
key: Int;
value: Int?;
}

message SetAddrMap1 {
key: Address;
value: Int?;
Expand Down Expand Up @@ -118,6 +123,10 @@ message DelUIntMap9 {
key: Int;
}

message DelUIntMap10 {
key: Int;
}

message DelAddrMap1 {
key: Address;
}
Expand Down Expand Up @@ -197,6 +206,11 @@ contract MapTestContract {
intMap9_5: map<Int, Int as uint128>;
intMap9_6: map<Int, Int as uint256>;

intMap10_1: map<Int as int42, Int as int27>;
intMap10_2: map<Int as int29, Int as uint31>;
intMap10_3: map<Int as uint33, Int as int34>;
intMap10_4: map<Int as uint35, Int as uint36>;

receive(msg: SetIntMap1) {
self.intMap1.set(msg.key, msg.value);
}
Expand Down Expand Up @@ -255,6 +269,13 @@ contract MapTestContract {
self.intMap9_6.set(msg.key, msg.value);
}

receive(msg: SetUIntMap10) {
self.intMap10_1.set(msg.key, msg.value);
self.intMap10_2.set(msg.key, msg.value);
self.intMap10_3.set(msg.key, msg.value);
self.intMap10_4.set(msg.key, msg.value);
}

receive(msg: DelIntMap1) {
self.intMap1.del(msg.key);
}
Expand Down Expand Up @@ -314,6 +335,13 @@ contract MapTestContract {
self.intMap9_6.del(msg.key);
}

receive(msg: DelUIntMap10) {
self.intMap10_1.del(msg.key);
self.intMap10_2.del(msg.key);
self.intMap10_3.del(msg.key);
self.intMap10_4.del(msg.key);
}

get fun intMap1(): map<Int, Int> {
return self.intMap1;
}
Expand Down Expand Up @@ -562,6 +590,38 @@ contract MapTestContract {
return self.intMap9_6.get(key);
}

get fun intMap10_1(): map<Int as int42, Int as int27> {
return self.intMap10_1;
}

get fun intMap10_1Value(key: Int): Int? {
return self.intMap10_1.get(key);
}

get fun intMap10_2(): map<Int as int29, Int as uint31> {
return self.intMap10_2;
}

get fun intMap10_2Value(key: Int): Int? {
return self.intMap10_2.get(key);
}

get fun intMap10_3(): map<Int as uint33, Int as int34> {
return self.intMap10_3;
}

get fun intMap10_3Value(key: Int): Int? {
return self.intMap10_3.get(key);
}

get fun intMap10_4(): map<Int as uint35, Int as uint36> {
return self.intMap10_4;
}

get fun intMap10_4Value(key: Int): Int? {
return self.intMap10_4.get(key);
}

//
// Int as Key inside the code (not storage)
//
Expand Down Expand Up @@ -593,7 +653,7 @@ contract MapTestContract {

return value1 + value2 + value3 + value4 + value5 + value6 + value7;
}

get fun intMap11Value(key: Int, value: Int): Int {
let map1: map<Int as uint8, Int> = emptyMap();
let map2: map<Int as uint16, Int> = emptyMap();
Expand Down Expand Up @@ -671,10 +731,29 @@ contract MapTestContract {
let value5: Int = map5.get(key)!!;
let value6: Int = map6.get(key)!!;
let value7: Int = map7.get(key)!!;

return value1 + value2 + value3 + value4 + value5 + value6 + value7;
}

get fun intMap14Value(key: Int, value: Int): Int {
let map1: map<Int as int42, Int as int27> = emptyMap();
let map2: map<Int as int29, Int as uint31> = emptyMap();
let map3: map<Int as uint33, Int as int34> = emptyMap();
let map4: map<Int as uint35, Int as uint36> = emptyMap();

map1.set(key, value);
map2.set(key, value);
map3.set(key, value);
map4.set(key, value);

let value1: Int = map1.get(key)!!;
let value2: Int = map2.get(key)!!;
let value3: Int = map3.get(key)!!;
let value4: Int = map4.get(key)!!;

return value1 + value2 + value3 + value4;
}

//
// Address Keys
//
Expand Down Expand Up @@ -795,7 +874,7 @@ contract MapTestContract {
get fun addrMap1(): map<Address, Int> {
return self.addrMap1;
}

get fun addrMap1Value(key: Address): Int? {
return self.addrMap1.get(key);
}
Expand Down Expand Up @@ -997,4 +1076,4 @@ contract MapTestContract {
get fun addrMap5Exists(key: Address): Bool {
return self.addrMap5.exists(key);
}
}
}
Loading

0 comments on commit 59677d8

Please sign in to comment.