Skip to content

Commit

Permalink
add traversing of init AST when filling dependsOn
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Jul 2, 2024
1 parent 7a7047a commit 80af730
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Do not throw error when overriding abstract and virtual getters: PR [#503](https://github.com/tact-lang/tact/pull/503)
- Error message for non-existent storage variables: PR [#519](https://github.com/tact-lang/tact/issues/519)
- Error message for duplicate receiver definitions inherited from traits: PR [#519](https://github.com/tact-lang/tact/issues/519)
- Usage of `initOf` inside of `init()` does not cause error `135` anymore: PR [#521](https://github.com/tact-lang/tact/issues/521)

## [1.4.0] - 2024-06-21

Expand Down
32 changes: 32 additions & 0 deletions src/test/e2e-emulated/__snapshots__/initof.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`initof should implement initof correctly - 2 1`] = `
[
{
"$seq": 0,
"events": [
{
"$type": "deploy",
},
{
"$type": "received",
"message": {
"body": {
"text": "aa",
"type": "text",
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "kQBMKzwvmyn0kLoIPFPZ3N1raKFO3S-_K7-i-wqoJff5C3FN",
"type": "internal",
"value": "10",
},
},
{
"$type": "processed",
"gasUsed": 10085n,
},
],
},
]
`;
19 changes: 19 additions & 0 deletions src/test/e2e-emulated/contracts/initof-2.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contract B {
owner: Address;
init(addr: Address){
self.owner = addr;
}
}

fun get_init(addr: Address): StateInit {
return initOf B(addr);
}

contract A {
receive("aa") {
let c1: StateInit = get_init(myAddress());
dump(c1.data);
let c2: StateInit = initOf B(myAddress());
dump(c2.data);
}
}
30 changes: 30 additions & 0 deletions src/test/e2e-emulated/contracts/initof.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "@stdlib/deploy";

message(42) Foo { }

contract TestNested with Deployable {
init() { dump("Nested----init@SUCCESS") }
}

contract TestInit with Deployable {
init() {
try {
let init: StateInit = initOf TestNested();
dump("init@TestInit-1");
send(SendParameters {
to: contractAddress(init),
value: 0,
mode: SendIgnoreErrors | SendRemainingValue,
code: init.code,
data: init.data,
body: Deploy { queryId: 0 }.toCell(),
});
dump("init@TestInit-SUCCESS");
} catch (e) {
dump("ERROR@TestInit");
dump(e);
}
}

receive(_: Foo) {}
}
43 changes: 43 additions & 0 deletions src/test/e2e-emulated/initof.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { toNano } from "@ton/core";
import { ContractSystem } from "@tact-lang/emulator";
import { __DANGER_resetNodeId } from "../../grammar/ast";
import { TestInit } from "./contracts/output/initof_TestInit";
import { A } from "./contracts/output/initof-2_A";

describe("initof", () => {
beforeEach(() => {
__DANGER_resetNodeId();
});
it("should implement initof correctly - 1", async () => {
// Init
const system = await ContractSystem.create();
const treasure = system.treasure("treasure");
const contract = system.open(await TestInit.fromInit());
const logger = system.log(contract.address);
await contract.send(
treasure,
{ value: toNano("10") },
{
$$type: "Deploy",
queryId: 0n,
},
);
await system.run();

const res = logger.collect();

expect(res.includes("init@TestInit-SUCCESS")).toBe(true);
expect(res.includes("ERROR@TestInit")).toBe(false);
});
it("should implement initof correctly - 2", async () => {
// Init
const system = await ContractSystem.create();
const treasure = system.treasure("treasure");
const contract = system.open(await A.fromInit());
const tracker = system.track(contract.address);
await contract.send(treasure, { value: toNano("10") }, "aa");
await system.run();

expect(tracker.collect()).toMatchSnapshot();
});
});
1 change: 1 addition & 0 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,7 @@ export function resolveDescriptors(ctx: CompilerContext) {
for (const f of t.receivers) {
traverse(f.ast, handler);
}
if (t.init) traverse(t.init.ast, handler);

// Add dependencies
for (const s of dependsOn) {
Expand Down
16 changes: 16 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@
"name": "structs",
"path": "./src/test/e2e-emulated/contracts/structs.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "initof",
"path": "./src/test/e2e-emulated/contracts/initof.tact",
"output": "./src/test/e2e-emulated/contracts/output",
"options": {
"debug": true
}
},
{
"name": "initof-2",
"path": "./src/test/e2e-emulated/contracts/initof-2.tact",
"output": "./src/test/e2e-emulated/contracts/output",
"options": {
"debug": true
}
}
]
}

0 comments on commit 80af730

Please sign in to comment.