Skip to content

Commit

Permalink
Merge branch 'main' into 1558,1649
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Jan 31, 2025
2 parents 6274c68 + be96b46 commit f999d1c
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 63 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@tonstudio/parser-runtime": "^0.0.1",
"blockstore-core": "1.0.5",
"change-case": "^4.1.2",
"crc-32": "1.2.2",
"ipfs-unixfs-importer": "9.0.10",
"json-bigint": "^1.0.0",
"ohm-js": "^17.1.0",
Expand Down
3 changes: 0 additions & 3 deletions src/ast/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ export function eqExpressions(
return ast1.value
.asCell()
.equals((ast2 as A.AstSlice).value.asCell());
case "comment_value":
return ast1.value === (ast2 as A.AstCommentValue).value;
case "simplified_string":
return ast1.value === (ast2 as A.AstSimplifiedString).value;
case "struct_value":
Expand Down Expand Up @@ -381,7 +379,6 @@ function checkLiteral<T>(
case "address":
case "cell":
case "slice":
case "comment_value":
case "simplified_string":
case "struct_value":
return t(ast);
Expand Down
4 changes: 0 additions & 4 deletions src/ast/ast-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ export const ppAstBoolean = ({ value }: A.AstBoolean) => value.toString();
export const ppAstId = ({ text }: A.AstId) => text;
export const ppAstNull = (_expr: A.AstNull) => "null";
export const ppAstString = ({ value }: A.AstString) => `"${value}"`;
export const ppAstCommentValue = ({ value }: A.AstCommentValue) =>
JSON.stringify(value);
export const ppAstSimplifiedString = ({ value }: A.AstSimplifiedString) =>
JSON.stringify(value);
export const ppAstAddress = ({ value }: A.AstAddress) =>
Expand Down Expand Up @@ -262,7 +260,6 @@ export const ppAstExpressionNested = makeVisitor<A.AstExpression>()({
init_of: ppLeaf(ppAstInitOf),
string: ppLeaf(ppAstString),
static_call: ppLeaf(ppAstStaticCall),
comment_value: ppLeaf(ppAstCommentValue),
simplified_string: ppLeaf(ppAstSimplifiedString),
address: ppLeaf(ppAstAddress),
cell: ppLeaf(ppAstCell),
Expand Down Expand Up @@ -874,7 +871,6 @@ export const ppAstNode: Printer<A.AstNode> = makeVisitor<A.AstNode>()({
id: exprNode(ppAstExpression),
boolean: exprNode(ppAstExpression),
string: exprNode(ppAstExpression),
comment_value: exprNode(ppAstExpression),
simplified_string: exprNode(ppAstExpression),
null: exprNode(ppAstExpression),
address: exprNode(ppAstExpression),
Expand Down
8 changes: 0 additions & 8 deletions src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ export type AstLiteral =
| AstAddress
| AstCell
| AstSlice
| AstCommentValue
| AstStructValue;

export type AstBinaryOperation =
Expand Down Expand Up @@ -599,13 +598,6 @@ export type AstSlice = {
loc: SrcInfo;
};

export type AstCommentValue = {
kind: "comment_value";
value: string;
id: number;
loc: SrcInfo;
};

export type AstStructValue = {
kind: "struct_value";
type: AstId;
Expand Down
1 change: 0 additions & 1 deletion src/ast/iterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ export function traverseAndCheck(
case "string":
case "null":
case "simplified_string":
case "comment_value":
case "address":
case "cell":
case "slice":
Expand Down
10 changes: 0 additions & 10 deletions src/ast/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
return result as A.AstSimplifiedString;
}

function makeCommentLiteral(s: string, loc: SrcInfo): A.AstCommentValue {
const result = createNode({
kind: "comment_value",
value: s,
loc: loc,
});
return result as A.AstCommentValue;
}

function makeNullLiteral(loc: SrcInfo): A.AstNull {
const result = createNode({
kind: "null",
Expand Down Expand Up @@ -145,7 +136,6 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
makeNumberLiteral,
makeBooleanLiteral,
makeSimplifiedStringLiteral,
makeCommentLiteral,
makeNullLiteral,
makeCellLiteral,
makeSliceLiteral,
Expand Down
5 changes: 0 additions & 5 deletions src/generator/writers/writeConstant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ export function writeString(str: string, ctx: WriterContext) {
return writeRawSlice("string", `String "${str}"`, cell, ctx);
}

export function writeComment(str: string, ctx: WriterContext) {
const cell = beginCell().storeUint(0, 32).storeStringTail(str).endCell();
return writeRawCell("comment", `Comment "${str}"`, cell, ctx);
}

export function writeAddress(address: Address, ctx: WriterContext) {
return writeRawSlice(
"address",
Expand Down
6 changes: 0 additions & 6 deletions src/generator/writers/writeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { resolveFuncType } from "./resolveFuncType";
import {
writeAddress,
writeCell,
writeComment,
writeSlice,
writeString,
} from "./writeConstant";
Expand Down Expand Up @@ -126,11 +125,6 @@ export function writeValue(val: A.AstLiteral, wCtx: WriterContext): string {
}
case "null":
return "null()";
case "comment_value": {
const id = writeComment(val.value, wCtx);
wCtx.used(id);
return `${id}()`;
}
case "struct_value": {
// Transform the struct fields into a map for lookup
const valMap: Map<string, A.AstLiteral> = new Map();
Expand Down
2 changes: 0 additions & 2 deletions src/optimizer/constEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ export const getOptimizer = (util: AstUtil) => {
return interpreter.interpretNumber(ast);
case "string":
return interpreter.interpretString(ast);
case "comment_value":
return ast;
case "simplified_string":
return ast;
case "struct_value":
Expand Down
19 changes: 9 additions & 10 deletions src/optimizer/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, beginCell, BitString, Cell, toNano } from "@ton/core";
import { paddedBufferToBits } from "@ton/core/dist/boc/utils/paddedBits";
import * as crc32 from "crc-32";
import { crc32 } from "../utils/crc32";
import * as A from "../ast/ast";
import { evalConstantExpression } from "./constEval";
import { CompilerContext } from "../context/context";
Expand Down Expand Up @@ -96,7 +96,6 @@ function ensureArgumentForEquality(val: A.AstLiteral): A.AstLiteral {
case "address":
case "boolean":
case "cell":
case "comment_value":
case "null":
case "number":
case "simplified_string":
Expand Down Expand Up @@ -853,8 +852,6 @@ export class Interpreter {
return this.interpretNumber(ast);
case "string":
return this.interpretString(ast);
case "comment_value":
return this.interpretCommentValue(ast);
case "simplified_string":
return this.interpretSimplifiedString(ast);
case "address":
Expand Down Expand Up @@ -931,7 +928,13 @@ export class Interpreter {
const comment = ensureSimplifiedString(
this.interpretExpression(ast.self),
).value;
return this.util.makeCommentLiteral(comment, ast.loc);
return this.util.makeCellLiteral(
beginCell()
.storeUint(0, 32)
.storeStringTail(comment)
.endCell(),
ast.loc,
);
}
default:
throwNonFatalErrorConstEval(
Expand Down Expand Up @@ -967,10 +970,6 @@ export class Interpreter {
);
}

public interpretCommentValue(ast: A.AstCommentValue): A.AstCommentValue {
return ast;
}

public interpretSimplifiedString(
ast: A.AstSimplifiedString,
): A.AstSimplifiedString {
Expand Down Expand Up @@ -1378,7 +1377,7 @@ export class Interpreter {
this.interpretExpression(ast.args[0]!),
);
return this.util.makeNumberLiteral(
BigInt(crc32.str(str.value) >>> 0),
BigInt(crc32(str.value) >>> 0),
ast.loc,
); // >>> 0 converts to unsigned
}
Expand Down
2 changes: 0 additions & 2 deletions src/optimizer/test/partial-eval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ function dummyEval(
return ast;
case "cell":
return ast;
case "comment_value":
return ast;
case "simplified_string":
return ast;
case "slice":
Expand Down
38 changes: 38 additions & 0 deletions src/test/e2e-emulated/as-comment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { AsCommentTester } from "./contracts/output/as-comment_AsCommentTester";
import "@ton/test-utils";

describe("asComment", () => {
let blockchain: Blockchain;
let treasure: SandboxContract<TreasuryContract>;
let contract: SandboxContract<AsCommentTester>;

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.verbosity.print = false;
treasure = await blockchain.treasury("treasure");
contract = blockchain.openContract(await AsCommentTester.fromInit());

const result = await contract.send(
treasure.getSender(),
{
value: toNano("10"),
},
null,
);

expect(result.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
deploy: true,
});
});

it("should calculate asComment in compile-time as in runtime", async () => {
expect(await contract.getConstantCell()).toEqualCell(
await contract.getAsCommentRuntimeCell("hello world"),
);
});
});
13 changes: 13 additions & 0 deletions src/test/e2e-emulated/contracts/as-comment.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const ConstantCell: Cell = "hello world".asComment();

contract AsCommentTester {
receive() {}

get fun constantCell(): Cell {
return ConstantCell;
}

get fun asCommentRuntimeCell(val: String): Cell {
return val.asComment();
}
}
6 changes: 1 addition & 5 deletions src/types/resolveExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function resolveSliceLiteral(
}

function resolveStringLiteral(
exp: A.AstString | A.AstSimplifiedString | A.AstCommentValue,
exp: A.AstString | A.AstSimplifiedString,
sctx: StatementContext,
ctx: CompilerContext,
): CompilerContext {
Expand Down Expand Up @@ -868,10 +868,6 @@ export function resolveExpression(
// A simplified string is resolved as a string
return resolveStringLiteral(exp, sctx, ctx);
}
case "comment_value": {
// A comment value is resolved as a string
return resolveStringLiteral(exp, sctx, ctx);
}
case "struct_value": {
// A struct value is resolved as a struct instance
return resolveStructNew(exp, sctx, ctx);
Expand Down
1 change: 0 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export function showValue(val: A.AstLiteral): string {
case "number":
return val.value.toString(val.base);
case "simplified_string":
case "comment_value":
return val.value;
case "boolean":
return val.value ? "true" : "false";
Expand Down
12 changes: 12 additions & 0 deletions src/utils/crc32.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { crc32 } from "./crc32";

describe("crc32", () => {
it("crc32 is correctly calculated from the string", () => {
expect(crc32("")).toBe(0);
expect(crc32("Hello Tact")).toBe(-1612685692);
expect(crc32("Привет Tact")).toBe(-1470995533);
expect(crc32("👋 Tact")).toBe(1855222621);
expect(crc32("\u0000")).toBe(-771559539);
expect(crc32("⚡")).toBe(2136484914);
});
});
28 changes: 28 additions & 0 deletions src/utils/crc32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function makeCRC32Table(polynomial: number): Int32Array {
let c: number;
const table = new Int32Array(256);
for (let n = 0; n < table.length; n++) {
c = n;
for (let k = 0; k < 8; k++) {
c = c & 1 ? (c >>> 1) ^ polynomial : c >>> 1;
}
table[n] = c;
}
return table;
}

// Reversed polynomial of ISO3309 CRC32
const CRC32C_TABLE = makeCRC32Table(0xedb88320);

export function crc32(data: string | Uint8Array): number {
if (typeof data === "string") {
data = new TextEncoder().encode(data);
}

let crc = 0xffffffff;
for (const byte of data) {
crc = CRC32C_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8);
}

return crc ^ 0xffffffff;
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2028,11 +2028,6 @@ core-util-is@^1.0.3:
resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz"
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==

crc-32@1.2.2:
version "1.2.2"
resolved "https://registry.npmjs.org/crc-32/-/crc-32-1.2.2.tgz#3cad35a934b8bf71f25ca524b6da51fb7eace2ff"
integrity sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==

create-jest@^29.7.0:
version "29.7.0"
resolved "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz#a355c5b3cb1e1af02ba177fe7afd7feee49a5320"
Expand Down

0 comments on commit f999d1c

Please sign in to comment.