跳到主要内容

Tolk vs FunC: standard library

FunC has a rich standard library, known as "stdlib.fc" file. It's quite low-level and contains lots of asm functions named very closely to TVM commands.

Tolk also has a standard library based on a FunC one. Three main differences:

  1. It's split in multiple files: common.tolk, tvm-dicts.tolk, and others. Functions from common.tolk are available always. Functions from other files are available after import:
import "@stdlib/tvm-dicts"

beginCell() // available always
createEmptyDict() // available due to import
  1. You don't need to download it from GitHub, it's a part of Tolk distribution.
  2. Almost all FunC functions were renamed to verbose clear names. So that when you write contracts or read example, you better understand what's going on.

A list of renamed functions

If "Required import" column is empty, a function is available without any imports.

Note, that some of the functions were deleted, because they either can be expressed syntactically, or they were very uncommon in practice.

FunC nameTolk nameRequired import
empty_tuplecreateEmptyTuple
tpushtuplePush
firsttupleFirst
attupleAt
touchstackMoveToToptvm-lowlevel
impure_touch(deleted)
single(deleted)
unsingle(deleted)
pair(deleted)
unpair(deleted)
triple(deleted)
untriple(deleted)
tuple4(deleted)
untuple4(deleted)
second(deleted)
third(deleted)
fourth(deleted)
pair_first(deleted)
pair_second(deleted)
triple_first(deleted)
triple_second(deleted)
triple_third(deleted)
minmaxminMax
my_addressgetMyAddress
get_balancegetMyOriginalBalanceWithExtraCurrencies
cur_ltgetLogicalTime
block_ltgetCurrentBlockLogicalTime
cell_hashcellHash
slice_hashsliceHash
string_hashstringHash
check_signatureisSignatureValid
check_data_signatureisSliceSignatureValid
compute_data_sizecalculateCellSizeStrict
slice_compute_data_sizecalculateSliceSizeStrict
compute_data_size?calculateCellSize
slice_compute_data_size?calculateSliceSize
~dumpdebugPrint
~strdumpdebugPrintString
dump_stackdebugDumpStack
get_datagetContractData
set_datasetContractData
get_c3getTvmRegisterC3tvm-lowlevel
set_c3setTvmRegisterC3tvm-lowlevel
blesstransformSliceToContinuationtvm-lowlevel
accept_messageacceptExternalMessage
set_gas_limitsetGasLimit
buy_gas(deleted)
commitcommitContractDataAndActions
divmoddivMod
moddivmodDiv
muldivmulDivFloor
muldivrmulDivRound
muldivcmulDivCeil
muldivmodmulDivMod
begin_parsebeginParse
end_parseassertEndOfSlice
load_refloadRef
preload_refpreloadRef
load_intloadInt
load_uintloadUint
preload_intpreloadInt
preload_uintpreloadUint
load_bitsloadBits
preload_bitspreloadBits
load_gramsloadCoins
load_coinsloadCoins
skip_bitsskipBits
first_bitsgetFirstBits
skip_last_bitsremoveLastBits
slice_lastgetLastBits
load_dictloadDict
preload_dictpreloadDict
skip_dictskipDict
load_maybe_refloadMaybeRef
preload_maybe_refpreloadMaybeRef
cell_depthgetCellDepth
slice_refsgetRemainingRefsCount
slice_bitsgetRemainingBitsCount
slice_bits_refsgetRemainingBitsAndRefsCount
slice_empty?isEndOfSlice
slice_data_empty?isEndOfSliceBits
slice_refs_empty?isEndOfSliceRefs
slice_depthgetSliceDepth
equal_slice_bitsisSliceBitsEqual
builder_refsgetBuilderRefsCount
builder_bitsgetBuilderBitsCount
builder_depthgetBuilderDepth
begin_cellbeginCell
end_cellendCell
store_refstoreRef
store_uintstoreUint
store_intstoreInt
store_slicestoreSlice
store_gramsstoreCoins
store_coinsstoreCoins
store_dictstoreDict
store_maybe_refstoreMaybeRef
store_builderstoreBuilder
load_msg_addrloadAddress
parse_addrparseAddress
parse_std_addrparseStandardAddress
parse_var_addr(deleted)
config_paramgetBlockchainConfigParam
raw_reservereserveToncoinsOnBalance
raw_reserve_extrareserveExtraCurrenciesOnBalance
send_raw_messagesendRawMessage
set_codesetContractCodePostponed
randrandomRange
get_seedrandomGetSeed
set_seedrandomSetSeed
randomizerandomizeBy
randomize_ltrandomizeByLogicalTime
dumpdebugPrint
strdumpdebugPrintString
dump_stkdebugDumpStack
empty_listcreateEmptyListlisp-lists
conslistPrependlisp-lists
unconslistSplitlisp-lists
list_nextlistNextlisp-lists
carlistGetHeadlisp-lists
cdrlistGetTaillisp-lists
new_dictcreateEmptyDicttvm-dicts
dict_empty?dictIsEmptytvm-dicts
idict_set_refiDictSetReftvm-dicts
udict_set_refuDictSetReftvm-dicts
idict_get_refiDictGetRefOrNulltvm-dicts
idict_get_ref?iDictGetReftvm-dicts
udict_get_ref?uDictGetReftvm-dicts
idict_set_get_refiDictSetAndGetRefOrNulltvm-dicts
udict_set_get_refiDictSetAndGetRefOrNulltvm-dicts
idict_delete?iDictDeletetvm-dicts
udict_delete?uDictDeletetvm-dicts
idict_get?iDictGettvm-dicts
udict_get?uDictGettvm-dicts
idict_delete_get?iDictDeleteAndGettvm-dicts
udict_delete_get?uDictDeleteAndGettvm-dicts
udict_setuDictSettvm-dicts
idict_setiDictSettvm-dicts
dict_setsDictSettvm-dicts
udict_add?uDictSetIfNotExiststvm-dicts
udict_replace?uDictSetIfExiststvm-dicts
idict_add?iDictSetIfNotExiststvm-dicts
idict_replace?iDictSetIfExiststvm-dicts
udict_set_builderuDictSetBuildertvm-dicts
idict_set_builderiDictSetBuildertvm-dicts
dict_set_buildersDictSetBuildertvm-dicts
udict_add_builder?uDictSetBuilderIfNotExiststvm-dicts
udict_replace_builder?uDictSetBuilderIfExiststvm-dicts
idict_add_builder?iDictSetBuilderIfNotExiststvm-dicts
idict_replace_builder?iDictSetBuilderIfExiststvm-dicts
udict_delete_get_minuDictDeleteFirstAndGettvm-dicts
idict_delete_get_miniDictDeleteFirstAndGettvm-dicts
dict_delete_get_minsDictDeleteFirstAndGettvm-dicts
udict_delete_get_maxuDictDeleteLastAndGettvm-dicts
idict_delete_get_maxiDictDeleteLastAndGettvm-dicts
dict_delete_get_maxsDictDeleteLastAndGettvm-dicts
udict_get_min?uDictGetFirsttvm-dicts
udict_get_max?uDictGetLasttvm-dicts
udict_get_min_ref?uDictGetFirstAsReftvm-dicts
udict_get_max_ref?uDictGetLastAsReftvm-dicts
idict_get_min?iDictGetFirsttvm-dicts
idict_get_max?iDictGetLasttvm-dicts
idict_get_min_ref?iDictGetFirstAsReftvm-dicts
idict_get_max_ref?iDictGetLastAsReftvm-dicts
udict_get_next?uDictGetNexttvm-dicts
udict_get_nexteq?uDictGetNextOrEqualtvm-dicts
udict_get_prev?uDictGetPrevtvm-dicts
udict_get_preveq?uDictGetPrevOrEqualtvm-dicts
idict_get_next?iDictGetNexttvm-dicts
idict_get_nexteq?iDictGetNextOrEqualtvm-dicts
idict_get_prev?iDictGetPrevtvm-dicts
idict_get_preveq?iDictGetPrevOrEqualtvm-dicts
udict::delete_get_minuDictDeleteFirstAndGettvm-dicts
idict::delete_get_miniDictDeleteFirstAndGettvm-dicts
dict::delete_get_minsDictDeleteFirstAndGettvm-dicts
udict::delete_get_maxuDictDeleteLastAndGettvm-dicts
idict::delete_get_maxiDictDeleteLastAndGettvm-dicts
dict::delete_get_maxsDictDeleteLastAndGettvm-dicts
pfxdict_get?prefixDictGettvm-dicts
pfxdict_set?prefixDictSettvm-dicts
pfxdict_delete?prefixDictDeletetvm-dicts

A list of added functions

Tolk standard library has some functions that were missing in FunC, but are quite common for everyday tasks.

Since Tolk is actively developed, and its standard library changes, better consider tolk-stdlib/ folder in sources here. Besides functions, there some constants were added: SEND_MODE_*, RESERVE_MODE_*, etc.

When FunC becomes deprecated, the documentation about Tolk stdlib will be completely rewritten, anyway.

And remember, that all the functions above are actually wrappers over TVM assembler. If something is missing, you can easily wrap any TVM instruction yourself.

Some functions became mutating, not returning a copy

FunCTolk
int flags = cs~load_uint(32);var flags = cs.loadUint(32);
dict~udict_set(...);dict.uDictSet(...);
......

Most FunC functions, that were used with ~ tilda in practice, now mutate the object, see examples above.

For example, if you used dict~udict_set(…), just use dict.uDictSet(…), and everything is fine. But if you used dict.udict_set(…) to obtain a copy, you'll need to express it some other way.

Read about mutability.

How does embedded stdlib work under the hood

As told above, all standard functions are available out of the box. Yeah, for you need import for non-common functions (it's intentionally), but still, no external downloads.

It works the following way.

The first thing Tolk compiler does on start is locating stdlib folder by searching in predefined paths relative to an executable binary. For example, if you launch Tolk compiler from a package installed (e.g. /usr/bin/tolk), locate stdlib in /usr/share/ton/smartcont. If you have non-standard installation, you may pass TOLK_STDLIB env variable. It's standard practice for compilers.

A WASM wrapper tolk-js also contains stdlib. So, when you take tolk-js or blueprint, all stdlib functions are still available out of the box.

IDE plugins (both JetBrains and VS Code) also auto-locate stdlib to provide auto-completion. If you use blueprint, it automatically installs tolk-js, and therefore, folder node_modules/@ton/tolk-js/ exists in your project file structure. Inside, there are common.tolk, tvm-dicts.tolk, and others.