Skip to content

Commit dc328ac

Browse files
committed
[Tolk] Deprecate and comment some functions in stdlib
1 parent bce0e5e commit dc328ac

File tree

7 files changed

+50
-30
lines changed

7 files changed

+50
-30
lines changed

crypto/smartcont/tolk-stdlib/common.tolk

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ fun Cell<T>.beginParse(self): slice
388388

389389
/// Returns hash of a typed cell, same as [cell.hash].
390390
@pure
391-
fun Cell<T>.hash(self): slice
391+
fun Cell<T>.hash(self): uint256
392392
asm "HASHCU";
393393

394394
/// RemainingBitsAndRefs is a special built-in type to get "all the rest" slice tail on reading.
@@ -458,20 +458,20 @@ fun stringToBase256(constString: slice): int
458458
/// Computes the representation hash of a `cell` and returns it as a 256-bit unsigned integer `x`.
459459
/// Useful for signing and checking signatures of arbitrary entities represented by a tree of cells.
460460
@pure
461-
fun cell.hash(self): int
461+
fun cell.hash(self): uint256
462462
asm "HASHCU";
463463

464464
/// Computes the hash of a `slice` and returns it as a 256-bit unsigned integer `x`.
465465
/// The result is the same as if an ordinary cell containing only data and references from `s` had been created
466466
/// and its hash computed by [cell.hash].
467467
@pure
468-
fun slice.hash(self): int
468+
fun slice.hash(self): uint256
469469
asm "HASHSU";
470470

471471
/// Computes sha256 of the data bits of a `slice`. If the bit length of `s` is not divisible by eight,
472472
/// throws a cell underflow exception. The hash value is returned as a 256-bit unsigned integer `x`.
473473
@pure
474-
fun slice.bitsHash(self): int
474+
fun slice.bitsHash(self): uint256
475475
asm "SHA256U";
476476

477477
/// Checks the Ed25519-`signature` of a `hash` (a 256-bit unsigned integer, usually computed as the hash of some data)
@@ -1119,7 +1119,7 @@ struct (0x00) ExtOutLogBucket {
11191119

11201120
/// Options for creating an external outgoing message.
11211121
/// Consider [createExternalLogMessage] for examples.
1122-
struct createExternalLogMessageOptions<TBody = never> {
1122+
struct CreateExternalLogMessageOptions<TBody = never> {
11231123
/// destination is either an external address or a pattern to calculate it
11241124
dest: address | // either some valid external/none address (not internal!)
11251125
builder | // ... or a manually constructed builder with a valid external address
@@ -1143,7 +1143,7 @@ struct createExternalLogMessageOptions<TBody = never> {
11431143
/// (if body is small, it will be inlined without an expensive cell creation)
11441144
/// (if body is large, the compiler will automatically wrap it into a cell)
11451145
@pure
1146-
fun createExternalLogMessage<TBody>(options: createExternalLogMessageOptions<TBody>): OutMessage
1146+
fun createExternalLogMessage<TBody>(options: CreateExternalLogMessageOptions<TBody>): OutMessage
11471147
builtin;
11481148

11491149
/// UnsafeBodyNoRef is used to prevent default behavior: when message body is potentially large,
@@ -1157,13 +1157,13 @@ fun createExternalLogMessage<TBody>(options: createExternalLogMessageOptions<TBo
11571157
/// createMessage({
11581158
/// // body: contents, // by default, it will be stored a ref (it's large)
11591159
/// body: UnsafeBodyNoRef {
1160-
/// bodyForceNoRef: contents, // but wrapping forces the compiler to inline it
1160+
/// forceInline: contents, // but wrapping forces the compiler to inline it
11611161
/// }
11621162
/// ```
11631163
/// Another example: your body contains `builder` or `RemainingBitsAndRefs`: unpredictable size.
11641164
/// If you guarantee it's small and refs won't clash with code/data, avoid creating a cell.
11651165
struct UnsafeBodyNoRef<T> {
1166-
bodyForceNoRef: T;
1166+
forceInline: T;
11671167
}
11681168

11691169
/// OutMessage is a result of [createMessage].
@@ -1202,6 +1202,13 @@ struct StateInit {
12021202
}
12031203

12041204
/// Calculates a hash of StateInit if only code+data are set.
1205+
/// Example:
1206+
/// ```
1207+
/// val addrHash = StateInit.calcHashCodeData(codeCell, dataCell);
1208+
/// createMessage({
1209+
/// dest: (workchain, addrHash),
1210+
/// ...
1211+
/// ```
12051212
@pure
12061213
fun StateInit.calcHashCodeData(code: cell, data: cell): uint256 asm
12071214
""" // code data
@@ -1304,23 +1311,41 @@ fun sendRawMessage(msg: cell, mode: int): void
13041311
Receiving and handling messages.
13051312
*/
13061313

1314+
/// Skip 0xFFFFFFFF prefix (when a message is bounced).
1315+
@pure
1316+
fun slice.skipBouncedPrefix(mutate self): self
1317+
asm "32 LDU" "NIP";
1318+
13071319
/// Load msgFlags from incoming message body (4 bits).
13081320
@pure
1321+
@deprecated("use `onInternalMessage(in: InMessage)` and `in.isBounced` instead of parsing msgCell manually")
13091322
fun slice.loadMessageFlags(mutate self): int
13101323
asm( -> 1 0) "4 LDU";
13111324

1312-
/// Skip 0xFFFFFFFF prefix (when a message is bounced).
1313-
@pure
1314-
fun slice.skipBouncedPrefix(mutate self): self
1315-
asm "32 PUSHINT" "SDSKIPFIRST";
1316-
1317-
/// The guideline recommends to start the body of an internal message with uint32 `op` and uint64 `queryId`.
1325+
/// Loads a message opcode (32-bit integer) when parsing message body manually.
13181326
@pure
13191327
fun slice.loadMessageOp(mutate self): int
13201328
asm( -> 1 0) "32 LDU";
13211329

1322-
/// The guideline recommends that uint64 `queryId` should follow uint32 `op`.
1330+
/// Stores a message opcode (32-bit integer) when composing an output message manually.
1331+
@pure
1332+
@deprecated("use `createMessage` instead of composing messages manually")
1333+
fun builder.storeMessageOp(mutate self, op: int): self
1334+
asm(op self) "32 STU";
1335+
1336+
/// Loads a message queryId (64-bit integer, immediately following the opcode) when parsing message body manually.
13231337
@pure
1338+
@deprecated("use structures and lazy match instead of parsing messages manually")
13241339
fun slice.loadMessageQueryId(mutate self): int
13251340
asm( -> 1 0) "64 LDU";
13261341

1342+
@pure
1343+
@deprecated("use structures and lazy loading instead of parsing messages manually")
1344+
fun slice.skipMessageQueryId(mutate self): self
1345+
asm "64 LDU" "NIP";
1346+
1347+
@pure
1348+
@deprecated("use `createMessage` instead of composing messages manually")
1349+
fun builder.storeMessageQueryId(mutate self, queryId: int): self
1350+
asm(queryId self) "64 STU";
1351+

tolk-tester/tests/cells-slices.tolk

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
fun builder.store_u32(mutate self, value: int): self {
22
return self.storeUint(value, 32);
33
}
4-
fun builder.storeMessageOp(mutate self, op: int): self
5-
asm(op self) "32 STU";
6-
fun builder.storeMessageQueryId(mutate self, queryId: int): self
7-
asm(queryId self) "64 STU";
8-
94
fun slice.load_u32(mutate self): int {
105
return self.loadUint(32);
116
}

tolk-tester/tests/send-msg-1.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ fun test22(op: uint32, queryId: uint64) {
643643
dest: address("0:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFfffffffffffffffffffffffffffff"),
644644
value: ton("0.08"),
645645
body: UnsafeBodyNoRef {
646-
bodyForceNoRef: bodyB
646+
forceInline: bodyB
647647
},
648648
});
649649
assert(b.hash() == test22_manual(bodyB).hash(), 122);
@@ -690,7 +690,7 @@ fun test23(state32: uint32) {
690690
var b = createMessage({
691691
bounce: true,
692692
body: UnsafeBodyNoRef {
693-
bodyForceNoRef: bd,
693+
forceInline: bd,
694694
},
695695
dest: { workchain: 9, stateInit: init },
696696
value: ton("0.10009"),

tolk-tester/tests/send-msg-2.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ fun test5() {
294294
closeTo: pivot,
295295
}
296296
},
297-
body: UnsafeBodyNoRef { bodyForceNoRef: body },
297+
body: UnsafeBodyNoRef { forceInline: body },
298298
value: ton("0.1"),
299299
});
300300
return b.hash()

tolk-tester/tests/send-msg-3.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fun test9(topic: int) {
171171
};
172172
var b = createExternalLogMessage({
173173
body: UnsafeBodyNoRef {
174-
bodyForceNoRef: bd,
174+
forceInline: bd,
175175
},
176176
dest: ExtOutLogBucket { topic: topic },
177177
});

tolk/builtins.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ void define_builtins() {
12481248
TypePtr PackOptions = TypeDataUnknown::create();
12491249
TypePtr UnpackOptions = TypeDataUnknown::create();
12501250
TypePtr CreateMessageOptions = TypeDataUnknown::create();
1251-
TypePtr createExternalLogMessageOptions = TypeDataUnknown::create();
1251+
TypePtr CreateExternalLogMessageOptions = TypeDataUnknown::create();
12521252
TypePtr OutMessage = TypeDataUnknown::create();
12531253
TypePtr AddressShardingOptions = TypeDataUnknown::create();
12541254
const GenericsDeclaration* declTBody = new GenericsDeclaration(std::vector<GenericsDeclaration::ItemT>{{"TBody", nullptr}}, 0);
@@ -1510,7 +1510,7 @@ void define_builtins() {
15101510
define_builtin_func("createMessage", {CreateMessageOptions}, OutMessage, declTBody,
15111511
compile_time_only_function,
15121512
FunctionData::flagCompileTimeGen | FunctionData::flagAllowAnyWidthT);
1513-
define_builtin_func("createExternalLogMessage", {createExternalLogMessageOptions}, OutMessage, declTBody,
1513+
define_builtin_func("createExternalLogMessage", {CreateExternalLogMessageOptions}, OutMessage, declTBody,
15141514
compile_time_only_function,
15151515
FunctionData::flagCompileTimeGen | FunctionData::flagAllowAnyWidthT);
15161516

@@ -1584,15 +1584,15 @@ void patch_builtins_after_stdlib_loaded() {
15841584
lookup_function("builder.storeAny")->mutate()->parameters[2].default_value = v_empty_PackOptions;
15851585

15861586
StructPtr struct_ref_CreateMessageOptions = lookup_global_symbol("CreateMessageOptions")->try_as<StructPtr>();
1587-
StructPtr struct_ref_createExternalLogMessageOptions = lookup_global_symbol("createExternalLogMessageOptions")->try_as<StructPtr>();
1587+
StructPtr struct_ref_CreateExternalLogMessageOptions = lookup_global_symbol("CreateExternalLogMessageOptions")->try_as<StructPtr>();
15881588
StructPtr struct_ref_OutMessage = lookup_global_symbol("OutMessage")->try_as<StructPtr>();
15891589
TypePtr CreateMessageOptions = TypeDataGenericTypeWithTs::create(struct_ref_CreateMessageOptions, nullptr, {TypeDataGenericT::create("TBody")});
1590-
TypePtr createExternalLogMessageOptions = TypeDataGenericTypeWithTs::create(struct_ref_createExternalLogMessageOptions, nullptr, {TypeDataGenericT::create("TBody")});
1590+
TypePtr CreateExternalLogMessageOptions = TypeDataGenericTypeWithTs::create(struct_ref_CreateExternalLogMessageOptions, nullptr, {TypeDataGenericT::create("TBody")});
15911591
TypePtr OutMessage = TypeDataStruct::create(struct_ref_OutMessage);
15921592

15931593
lookup_function("createMessage")->mutate()->parameters[0].declared_type = CreateMessageOptions;
15941594
lookup_function("createMessage")->mutate()->declared_return_type = OutMessage;
1595-
lookup_function("createExternalLogMessage")->mutate()->parameters[0].declared_type = createExternalLogMessageOptions;
1595+
lookup_function("createExternalLogMessage")->mutate()->parameters[0].declared_type = CreateExternalLogMessageOptions;
15961596
lookup_function("createExternalLogMessage")->mutate()->declared_return_type = OutMessage;
15971597
}
15981598

tolk/send-message-api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ std::vector<var_idx_t> generate_createMessage(CodeBlob& code, SrcLocation loc, T
329329
}
330330

331331
std::vector<var_idx_t> generate_createExternalLogMessage(CodeBlob& code, SrcLocation loc, TypePtr bodyT, std::vector<var_idx_t>&& rvect) {
332-
StructPtr s_Options = lookup_global_symbol("createExternalLogMessageOptions")->try_as<StructPtr>();
332+
StructPtr s_Options = lookup_global_symbol("CreateExternalLogMessageOptions")->try_as<StructPtr>();
333333
StructPtr s_ExtOutLogBucket = lookup_global_symbol("ExtOutLogBucket")->try_as<StructPtr>();
334334

335335
const TypeDataUnion* t_dest = s_Options->find_field("dest")->declared_type->try_as<TypeDataUnion>();

0 commit comments

Comments
 (0)