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

Upgrade and fix debugging feature #271

Merged
merged 14 commits into from
Apr 21, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `@stdlib/stoppable` now imports `@stdlib/ownable` so the programmer does not have to do it separately: PR [#193](https://github.com/tact-lang/tact/pull/193)
- Support escape sequences for strings (`\\`, `\"`, `\n`, `\r`, `\t`, `\v`, `\b`, `\f`, `\u{0}` through `\u{FFFFFF}`, `\u0000` through `\uFFFF`, `\x00` through `\xFF`): PR [#192](https://github.com/tact-lang/tact/pull/192)
- `newAddress` function now evaluates to a constant value if possible: PR [#237](https://github.com/tact-lang/tact/pull/237)
- The `dump()` and `dumpStack()` functions now print the file path, line number, and column number in addition to the data: PR [#271](https://github.com/tact-lang/tact/pull/271).
- `pow` function is now in the standard library, allowing its use at runtime. If constant arguments are used, the result is evaluated at compile-time: PR [#267](https://github.com/tact-lang/tact/pull/267)

### Fixed
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow chaining method calls with `!!`, for instance, `map.asCell()!!.hash()` is grammatically correct now: PR [#257](ttps://github.com/tact-lang/tact/pull/257)
- Operation precendence for bitwise operators, equality and comparisons now matches common languages, like JavaScript: PR [#265](https://github.com/tact-lang/tact/pull/265)
- Incorrect variable scoping in `repeat`, `while` and `until` loops: PR [#269](https://github.com/tact-lang/tact/pull/269)
- FunC compilation errors when trying to `dump()` such types as `Cell`, `Slice`, `Builder` and `StringBuilder`: PR [#271](https://github.com/tact-lang/tact/pull/271)

## [1.2.0] - 2024-02-29

Expand Down
61 changes: 47 additions & 14 deletions src/abi/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { resolveConstantValue } from "../types/resolveConstantValue";
import { getErrorId } from "../types/resolveErrors";
import { AbiFunction } from "./AbiFunction";
import { sha256_sync } from "@ton/crypto";
import path from "path";
import { cwd } from "process";

export const GlobalFunctions: Map<string, AbiFunction> = new Map([
[
Expand Down Expand Up @@ -174,32 +176,40 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
return `${ctx.used("__tact_nop")}()`;
}
const arg = args[0];

const filePath = ref.file
? path.relative(cwd(), ref.file!)
: "unknown";
const lineCol = ref.interval.getLineAndColumn();
const debugPrint = `[DEBUG] File ${filePath}:${lineCol.lineNum}:${lineCol.colNum}`;

if (arg.kind === "map") {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug`)}(${exp})`;
return `${ctx.used(`__tact_debug`)}(${exp}, "${debugPrint}")`;
} else if (arg.kind === "null") {
return `${ctx.used(`__tact_debug_str`)}("null")`;
return `${ctx.used(`__tact_debug_str`)}("null", "${debugPrint}")`;
} else if (arg.kind === "void") {
return `${ctx.used(`__tact_debug_str`)}("void")`;
return `${ctx.used(`__tact_debug_str`)}("void", "${debugPrint}")`;
} else if (arg.kind === "ref") {
if (
arg.name === "Int" ||
arg.name === "Builder" ||
arg.name === "Slice" ||
arg.name === "Cell" ||
arg.name === "StringBuilder"
) {
if (arg.name === "Int") {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug_str`)}(${ctx.used(`__tact_int_to_string`)}(${exp}))`;
return `${ctx.used(`__tact_debug_str`)}(${ctx.used(`__tact_int_to_string`)}(${exp}), "${debugPrint}")`;
} else if (arg.name === "Bool") {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug_bool`)}(${exp})`;
return `${ctx.used(`__tact_debug_bool`)}(${exp}, "${debugPrint}")`;
} else if (arg.name === "String") {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug_str`)}(${exp})`;
return `${ctx.used(`__tact_debug_str`)}(${exp}, "${debugPrint}")`;
} else if (arg.name === "Address") {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug_address`)}(${exp})`;
return `${ctx.used(`__tact_debug_address`)}(${exp}, "${debugPrint}")`;
} else if (
arg.name === "Builder" ||
arg.name === "Slice" ||
arg.name === "Cell"
) {
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug`)}(${exp}, "${debugPrint}")`;
}
throwError(
"dump() not supported for type: " + arg.name,
Expand All @@ -211,6 +221,29 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
},
},
],
[
"dumpStack",
{
name: "dumpStack",
resolve: (_ctx, args, ref) => {
if (args.length !== 0) {
throwError("dumpStack expects no arguments", ref);
}
return { kind: "void" };
},
generate: (ctx, _args, _resolved, ref) => {
if (!enabledDebug(ctx.ctx)) {
return `${ctx.used("__tact_nop")}()`;
}
const filePath = ref.file
? path.relative(cwd(), ref.file!)
: "unknown";
const lineCol = ref.interval.getLineAndColumn();
const debugPrint = `[DEBUG] File ${filePath}:${lineCol.lineNum}:${lineCol.colNum}`;
return `${ctx.used(`__tact_debug_stack`)}("${debugPrint}")`;
},
},
],
[
"emptyMap",
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ return __tact_create_address(chain, hash);",
},
{
"code": {
"code": "asm "s0 DUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "s0 DUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -283,11 +283,11 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug",
"signature": "forall X -> () __tact_debug(X value)",
"signature": "forall X -> () __tact_debug(X value, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "STRDUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -297,14 +297,14 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_str",
"signature": "() __tact_debug_str(slice value)",
"signature": "() __tact_debug_str(slice value, slice debug_print)",
},
{
"code": {
"code": "if (value) {
__tact_debug_str("true");
__tact_debug_str("true", debug_print);
} else {
__tact_debug_str("false");
__tact_debug_str("false", debug_print);
}",
"kind": "generic",
},
Expand All @@ -317,7 +317,7 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_bool",
"signature": "() __tact_debug_bool(int value)",
"signature": "() __tact_debug_bool(int value, slice debug_print)",
},
{
"code": {
Expand Down Expand Up @@ -431,7 +431,7 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
},
{
"code": {
"code": "__tact_debug_str(__tact_address_to_userfriendly(address));",
"code": "__tact_debug_str(__tact_address_to_userfriendly(address), debug_print);",
"kind": "generic",
},
"comment": null,
Expand All @@ -444,7 +444,21 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
"impure",
},
"name": "__tact_debug_address",
"signature": "() __tact_debug_address(slice address)",
"signature": "() __tact_debug_address(slice address, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP" "DUMPSTK"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {
"impure",
},
"name": "__tact_debug_stack",
"signature": "() __tact_debug_stack(slice debug_print)",
},
{
"code": {
Expand Down Expand Up @@ -4299,7 +4313,7 @@ return __tact_create_address(chain, hash);",
},
{
"code": {
"code": "asm "s0 DUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "s0 DUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -4309,11 +4323,11 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug",
"signature": "forall X -> () __tact_debug(X value)",
"signature": "forall X -> () __tact_debug(X value, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "STRDUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -4323,14 +4337,14 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_str",
"signature": "() __tact_debug_str(slice value)",
"signature": "() __tact_debug_str(slice value, slice debug_print)",
},
{
"code": {
"code": "if (value) {
__tact_debug_str("true");
__tact_debug_str("true", debug_print);
} else {
__tact_debug_str("false");
__tact_debug_str("false", debug_print);
}",
"kind": "generic",
},
Expand All @@ -4343,7 +4357,7 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_bool",
"signature": "() __tact_debug_bool(int value)",
"signature": "() __tact_debug_bool(int value, slice debug_print)",
},
{
"code": {
Expand Down Expand Up @@ -4457,7 +4471,7 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
},
{
"code": {
"code": "__tact_debug_str(__tact_address_to_userfriendly(address));",
"code": "__tact_debug_str(__tact_address_to_userfriendly(address), debug_print);",
"kind": "generic",
},
"comment": null,
Expand All @@ -4470,7 +4484,21 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
"impure",
},
"name": "__tact_debug_address",
"signature": "() __tact_debug_address(slice address)",
"signature": "() __tact_debug_address(slice address, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP" "DUMPSTK"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {
"impure",
},
"name": "__tact_debug_stack",
"signature": "() __tact_debug_stack(slice debug_print)",
},
{
"code": {
Expand Down Expand Up @@ -8325,7 +8353,7 @@ return __tact_create_address(chain, hash);",
},
{
"code": {
"code": "asm "s0 DUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "s0 DUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -8335,11 +8363,11 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug",
"signature": "forall X -> () __tact_debug(X value)",
"signature": "forall X -> () __tact_debug(X value, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP"",
"code": "asm "STRDUMP" "DROP" "STRDUMP" "DROP"",
"kind": "asm",
},
"comment": null,
Expand All @@ -8349,14 +8377,14 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_str",
"signature": "() __tact_debug_str(slice value)",
"signature": "() __tact_debug_str(slice value, slice debug_print)",
},
{
"code": {
"code": "if (value) {
__tact_debug_str("true");
__tact_debug_str("true", debug_print);
} else {
__tact_debug_str("false");
__tact_debug_str("false", debug_print);
}",
"kind": "generic",
},
Expand All @@ -8369,7 +8397,7 @@ return __tact_create_address(chain, hash);",
"impure",
},
"name": "__tact_debug_bool",
"signature": "() __tact_debug_bool(int value)",
"signature": "() __tact_debug_bool(int value, slice debug_print)",
},
{
"code": {
Expand Down Expand Up @@ -8483,7 +8511,7 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
},
{
"code": {
"code": "__tact_debug_str(__tact_address_to_userfriendly(address));",
"code": "__tact_debug_str(__tact_address_to_userfriendly(address), debug_print);",
"kind": "generic",
},
"comment": null,
Expand All @@ -8496,7 +8524,21 @@ return __tact_base64_encode(user_friendly_address_with_checksum);",
"impure",
},
"name": "__tact_debug_address",
"signature": "() __tact_debug_address(slice address)",
"signature": "() __tact_debug_address(slice address, slice debug_print)",
},
{
"code": {
"code": "asm "STRDUMP" "DROP" "DUMPSTK"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {
"impure",
},
"name": "__tact_debug_stack",
"signature": "() __tact_debug_stack(slice debug_print)",
},
{
"code": {
Expand Down
Loading
Loading