|
| 1 | +// encoded as TL/B `len: (## 8) data: (bits (len*8))` |
| 2 | +type TelegramString = slice; |
| 3 | + |
| 4 | +fun TelegramString.packToBuilder(self, mutate b: builder) { |
| 5 | + val bytes = self.remainingBitsCount() / 8; |
| 6 | + b.storeUint(bytes, 8); |
| 7 | + b.storeSlice(self); |
| 8 | +} |
| 9 | + |
| 10 | +fun TelegramString.unpackFromSlice(mutate s: slice) { |
| 11 | + val bytes = s.loadUint(8); |
| 12 | + return s.loadBits(bytes * 8); |
| 13 | +} |
| 14 | + |
| 15 | +type Custom8 = int; |
| 16 | + |
| 17 | +fun Custom8.packToBuilder(self, mutate b: builder) { |
| 18 | + b.storeUint(self, 8) |
| 19 | +} |
| 20 | + |
| 21 | +fun Custom8.unpackFromSlice(mutate s: slice) { |
| 22 | + return s.loadUint(8) |
| 23 | +} |
| 24 | + |
| 25 | +struct StorWithStr { |
| 26 | + a: int32; |
| 27 | + str: TelegramString; |
| 28 | + b: int32; |
| 29 | +} |
| 30 | + |
| 31 | +struct PointWithCustomInt { |
| 32 | + a: Custom8; |
| 33 | + b: int8; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +type MyBorderedInt = int; |
| 38 | + |
| 39 | +fun MyBorderedInt.packToBuilder(self, mutate b: builder) { |
| 40 | + if (self > 10) { b.storeUint(1, 4) } |
| 41 | + else if (self > 0) { b.storeUint(2, 4) } |
| 42 | + else { b.storeUint(3, 4) } |
| 43 | +} |
| 44 | + |
| 45 | +fun MyBorderedInt.unpackFromSlice(mutate s: slice) { |
| 46 | + return match (s.loadUint(4)) { |
| 47 | + 1 => 10, |
| 48 | + 2 => 0, |
| 49 | + 3 => -1, |
| 50 | + else => throw 123 |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +struct WithMyBorder { |
| 55 | + a: int8; |
| 56 | + b: MyBorderedInt; |
| 57 | +} |
| 58 | + |
| 59 | +type MyCustomNothing = (); |
| 60 | + |
| 61 | +struct WithFakeWriter { |
| 62 | + a: int8; |
| 63 | + fake: MyCustomNothing = (); |
| 64 | + b: int8; |
| 65 | +} |
| 66 | + |
| 67 | +fun MyCustomNothing.packToBuilder(self, mutate b: builder) { |
| 68 | + b.storeUint(123, 32); |
| 69 | + b.storeRef(createEmptyCell()); |
| 70 | +} |
| 71 | + |
| 72 | +global gModByCustom: int; |
| 73 | + |
| 74 | +type MagicGlobalModifier = (); |
| 75 | + |
| 76 | +fun MagicGlobalModifier.packToBuilder(self, mutate b: builder) { |
| 77 | + b.storeUint(gModByCustom, 8); |
| 78 | +} |
| 79 | + |
| 80 | +fun MagicGlobalModifier.unpackFromSlice(mutate s: slice) { |
| 81 | + gModByCustom = s.loadUint(8); |
| 82 | + return (); |
| 83 | +} |
| 84 | + |
| 85 | +struct WithGlobalModifier { |
| 86 | + a: int8; |
| 87 | + g: MagicGlobalModifier = (); |
| 88 | + n: int8; |
| 89 | +} |
| 90 | + |
| 91 | +type Tensor3Skipping1 = (int, int, int); |
| 92 | + |
| 93 | +fun Tensor3Skipping1.unpackFromSlice(mutate s: slice): Tensor3Skipping1 { |
| 94 | + val e0 = s.loadUint(8); |
| 95 | + val e2 = s.loadUint(8); |
| 96 | + return (e0, 0, e2); |
| 97 | +} |
| 98 | + |
| 99 | +fun Tensor3Skipping1.packToBuilder(self, mutate b: builder) { |
| 100 | + b.storeUint(self.0, 8).storeUint(self.2, 8); |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +@method_id(101) |
| 105 | +fun test1() { |
| 106 | + var t: StorWithStr = { a: 10, str: "abc", b: 20 }; |
| 107 | + var c = t.toCell(); |
| 108 | + var back = c.load(); |
| 109 | + return ((t.b == back.b) & (t.str.bitsEqual(back.str)), back.str.remainingBitsCount(), c.hash() & 0xFFFF); |
| 110 | +} |
| 111 | + |
| 112 | +@method_id(102) |
| 113 | +fun test2() { |
| 114 | + var s = ("" as TelegramString, "" as TelegramString); |
| 115 | + return beginCell().storeAny(s).endCell().beginParse().remainingBitsCount(); |
| 116 | +} |
| 117 | + |
| 118 | +@method_id(103) |
| 119 | +fun test3() { |
| 120 | + return PointWithCustomInt.fromSlice(stringHexToSlice("0102")); |
| 121 | +} |
| 122 | + |
| 123 | +@method_id(104) |
| 124 | +fun test4(initialInt: int) { |
| 125 | + var c = WithMyBorder { a: 0, b: initialInt }.toCell(); |
| 126 | + return c.load().b; |
| 127 | +} |
| 128 | + |
| 129 | +@method_id(105) |
| 130 | +fun test5() { |
| 131 | + var f: WithFakeWriter = { a: 10, b: 20 }; |
| 132 | + var s = beginCell().storeAny(f).endCell().beginParse(); |
| 133 | + val size = s.remainingBitsAndRefsCount(); |
| 134 | + return (s.skipBits(8).loadUint(32), size.1); |
| 135 | +} |
| 136 | + |
| 137 | +@method_id(106) |
| 138 | +fun test6() { |
| 139 | + gModByCustom = 6; |
| 140 | + val m1: WithGlobalModifier = { a: 8, n: 16 }; |
| 141 | + val c1 = m1.toCell(); |
| 142 | + val r2 = WithGlobalModifier.fromSlice(stringHexToSlice("01FF02")); |
| 143 | + val gAfter2 = gModByCustom; |
| 144 | + WithGlobalModifier.fromSlice(stringHexToSlice("010002")); // not deleted, sets 0 |
| 145 | + val gAfter3 = gModByCustom; |
| 146 | + var s = stringHexToSlice("010902FF"); |
| 147 | + s.skipAny<WithGlobalModifier>(); // custom loader also called |
| 148 | + return (c1.beginParse().skipBits(8).loadUint(8), r2.n, gAfter2, gAfter3, s.remainingBitsCount(), gModByCustom); |
| 149 | +} |
| 150 | + |
| 151 | +@method_id(107) |
| 152 | +fun test7() { |
| 153 | + val t = (1, 2, 3) as Tensor3Skipping1; |
| 154 | + __expect_type(t.toCell(), "Cell<Tensor3Skipping1>"); |
| 155 | + return t.toCell().load(); |
| 156 | +} |
| 157 | + |
| 158 | +fun main() { |
| 159 | + __expect_type("" as TelegramString, "TelegramString"); |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | +@testcase | 101 | | -1 24 5203 |
| 164 | +@testcase | 102 | | 16 |
| 165 | +@testcase | 103 | | 1 2 |
| 166 | +@testcase | 104 | 55 | 10 |
| 167 | +@testcase | 104 | 8 | 0 |
| 168 | +@testcase | 104 | -5 | -1 |
| 169 | +@testcase | 105 | | 123 1 |
| 170 | +@testcase | 106 | | 6 2 255 0 8 9 |
| 171 | +@testcase | 107 | | 1 0 3 |
| 172 | + |
| 173 | +@fif_codegen |
| 174 | +""" |
| 175 | + test3() PROC:<{ |
| 176 | + x{0102} PUSHSLICE |
| 177 | + 8 LDU |
| 178 | + 8 LDI |
| 179 | + DROP |
| 180 | + }> |
| 181 | +""" |
| 182 | + */ |
0 commit comments