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

feat: new functions in stdlib from stdlib.fc and math.fc #986

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `replace` and `replaceGet` methods for the `Map` type: PR [#941](https://github.com/tact-lang/tact/pull/941)
- Utility for logging errors in code that was supposed to be unreachable: PR [#991](https://github.com/tact-lang/tact/pull/991)
- Docs: `preloadRef` method for the `Slice` type: PR [#1044](https://github.com/tact-lang/tact/pull/1044)
- New functions in stdlib from `stdlib.fc` and `math.fc`: `Builder.depth`, `Slice.skipLastBits`, `Slice.firstBits`, `Slice.lastBits`, `Slice.depth`, `Cell.computeDataSize`, `Slice.computeDataSize`, `Cell.depth`, `curLt`, `blockLt`, `setGasLimit`, `getSeed`, `setSeed`, `myCode`, `sign`, `mulrshift`, `mulrshift_round`, `mulrshift_ceil`, `sqrt`, `addressNone`: PR [#986](https://github.com/tact-lang/tact/pull/986)
Gusarich marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"minmax",
"mintable",
"mktemp",
"muldivc",
"multiformats",
"nanotons",
"Neovim",
Expand Down
112 changes: 71 additions & 41 deletions src/imports/stdlib.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,39 @@ exports[`local-type-inference should automatically set types for let statements
},
],
"types": [
{
"fields": [
{
"name": "cells",
"type": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"name": "bits",
"type": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"name": "refs",
"type": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
],
"header": null,
"name": "DataSize",
},
{
"fields": [
{
Expand Down
25 changes: 25 additions & 0 deletions stdlib/std/cells.tact
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ asm extends fun refs(self: Builder): Int { BREFS }

asm extends fun bits(self: Builder): Int { BBITS }

asm extends fun depth(self: Builder): Int { BDEPTH }

//
// Slice
//
Expand Down Expand Up @@ -148,6 +150,14 @@ asm extends mutates fun skipBits(self: Slice, l: Int) { SDSKIPFIRST }

asm extends fun endParse(self: Slice) { ENDS }

asm extends fun skipLastBits(self: Slice, len: Int): Slice { SDSKIPLAST }

asm extends fun firstBits(self: Slice, len: Int): Slice { SDCUTFIRST }

asm extends fun lastBits(self: Slice, len: Int): Slice { SDCUTLAST }

asm extends fun depth(self: Slice): Int { SDEPTH }

//
// Slice size
//
Expand Down Expand Up @@ -191,3 +201,18 @@ inline fun emptyCell(): Cell {
inline fun emptySlice(): Slice {
return emptyCell().asSlice();
}

asm fun addressNone(): Address { b{00} PUSHSLICE }

struct DataSize {
cells: Int;
bits: Int;
refs: Int;
}

asm extends fun computeDataSize(self: Cell, maxCells: Int): DataSize { CDATASIZE TRIPLE }

asm extends fun computeDataSize(self: Slice, maxCells: Int): DataSize { SDATASIZE TRIPLE }

asm extends fun depth(self: Cell): Int { CDEPTH }

12 changes: 12 additions & 0 deletions stdlib/std/contract.tact
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ asm fun parseStdAddress(slice: Slice): StdAddress { REWRITESTDADDR }
/// @see https://docs.tact-lang.org/ref/core-advanced#parsevaraddress
///
asm fun parseVarAddress(slice: Slice): VarAddress { REWRITEVARADDR }

asm fun curLt(): Int { LTIME }

asm fun blockLt(): Int { BLOCKLT }

asm fun setGasLimit(limit: Int) { SETGASLIMIT }

asm fun getSeed(): Int { RANDSEED }

asm fun setSeed(seed: Int) { SETRAND }

asm fun myCode(): Cell { MYCODE }
35 changes: 34 additions & 1 deletion stdlib/std/math.tact
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,37 @@ native log(num: Int, base: Int): Int;
@name(__tact_pow)
native pow(base: Int, exp: Int): Int;

asm fun pow2(exp: Int): Int { POW2 }
asm fun pow2(exp: Int): Int { POW2 }

asm fun sign(x: Int): Int { SGN }

asm fun divc(x: Int, y: Int): Int { DIVC }

asm fun muldivc(x: Int, y: Int, z: Int): Int { MULDIVC }

// floor(x*y/2^z)
asm fun mulShiftRight(x: Int, y: Int, z: Int): Int { MULRSHIFT }

// round(x*y/2^z)
asm fun mulShiftRightRound(x: Int, y: Int, z: Int): Int { MULRSHIFTR }

// ceil(x*y/2^z)
asm fun mulShiftRightCeil(x: Int, y: Int, z: Int): Int { MULRSHIFTC }

fun sqrt(num: Int): Int {
if (num == 0) {
return 0;
}

let s: Int = log2(num);
let x: Int = (s == 1 ? (num - 1) / 2 + 1 : 1 << ((s + 1) / 2));

let q: Int = 0;

do {
q = (divc(num, x) - x) / 2;
x += q;
} until (q == 0);

return x;
}
19 changes: 19 additions & 0 deletions stdlib/stdlib.fc
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,22 @@ builder store_builder(builder to, builder from) asm "STBR";

;;; Retrieves code of smart-contract from c7
cell my_code() asm "MYCODE";

novusnota marked this conversation as resolved.
Show resolved Hide resolved
;;; Creates an output action and returns a fee for creating a message. Mode has the same effect as in the case of SENDRAWMSG
int send_message(cell msg, int mode) impure asm "SENDMSG";

int gas_consumed() asm "GASCONSUMED";

;; TVM V6 https://github.com/ton-blockchain/ton/blob/testnet/doc/GlobalVersions.md#version-6

int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";
int get_storage_fee(int workchain, int seconds, int bits, int cells) asm(cells bits seconds workchain) "GETSTORAGEFEE";
int get_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEE";
int get_precompiled_gas_consumption() asm "GETPRECOMPILEDGAS";

int get_simple_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEESIMPLE";
int get_simple_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEESIMPLE";
int get_original_fwd_fee(int workchain, int fwd_fee) asm(fwd_fee workchain) "GETORIGINALFWDFEE";
int my_storage_due() asm "DUEPAYMENT";

tuple get_fee_configs() asm "UNPACKEDCONFIGTUPLE";