Skip to content

Commit

Permalink
feat(stdlib): loadBool for slices (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Jun 13, 2024
1 parent 56fa063 commit 7afc87e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The bitwise NOT operation (`~`): PR [#337](https://github.com/tact-lang/tact/pull/337)
- Augmented assignment bitwise operators `|=`, `&=`, `^=`: PR [#350](https://github.com/tact-lang/tact/pull/350)
- Traversing maps from contract storage and structs is now allowed: PR [#389](https://github.com/tact-lang/tact/pull/389)
- The `loadBool` method for `Slice` type: PR [#412](https://github.com/tact-lang/tact/pull/412)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ return address;",
"name": "__tact_verify_address",
"signature": "slice __tact_verify_address(slice address)",
},
{
"code": {
"code": "asm( -> 1 0) "1 LDI"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_load_bool",
"signature": "(slice, int) __tact_load_bool(slice s)",
},
{
"code": {
"code": "slice raw = cs~load_msg_addr();
Expand Down Expand Up @@ -4664,6 +4676,18 @@ return address;",
"name": "__tact_verify_address",
"signature": "slice __tact_verify_address(slice address)",
},
{
"code": {
"code": "asm( -> 1 0) "1 LDI"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_load_bool",
"signature": "(slice, int) __tact_load_bool(slice s)",
},
{
"code": {
"code": "slice raw = cs~load_msg_addr();
Expand Down Expand Up @@ -9253,6 +9277,18 @@ return address;",
"name": "__tact_verify_address",
"signature": "slice __tact_verify_address(slice address)",
},
{
"code": {
"code": "asm( -> 1 0) "1 LDI"",
"kind": "asm",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_load_bool",
"signature": "(slice, int) __tact_load_bool(slice s)",
},
{
"code": {
"code": "slice raw = cs~load_msg_addr();
Expand Down
6 changes: 6 additions & 0 deletions src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export function writeStdlib(ctx: WriterContext) {
});
});

ctx.fun("__tact_load_bool", () => {
ctx.signature(`(slice, int) __tact_load_bool(slice s)`);
ctx.context("stdlib");
ctx.asm(`asm( -> 1 0) "1 LDI"`);
});

ctx.fun("__tact_load_address", () => {
ctx.signature(`(slice, slice) __tact_load_address(slice cs)`);
ctx.flag("inline");
Expand Down
26 changes: 13 additions & 13 deletions src/imports/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ files['std/cells.tact'] =
'b2FkX2ludCkKZXh0ZW5kcyBtdXRhdGVzIG5hdGl2ZSBsb2FkSW50KHNlbGY6IFNsaWNlLCBsOiBJbnQpOiBJbnQ7CgpAbmFtZShwcmVsb2FkX2ludCkKZXh0ZW5kcyBu' +
'YXRpdmUgcHJlbG9hZEludChzZWxmOiBTbGljZSwgbDogSW50KTogSW50OwoKQG5hbWUobG9hZF91aW50KQpleHRlbmRzIG11dGF0ZXMgbmF0aXZlIGxvYWRVaW50KHNl' +
'bGY6IFNsaWNlLCBsOiBJbnQpOiBJbnQ7CgpAbmFtZShwcmVsb2FkX3VpbnQpCmV4dGVuZHMgbmF0aXZlIHByZWxvYWRVaW50KHNlbGY6IFNsaWNlLCBsOiBJbnQpOiBJ' +
'bnQ7CgpAbmFtZShsb2FkX2NvaW5zKQpleHRlbmRzIG11dGF0ZXMgbmF0aXZlIGxvYWRDb2lucyhzZWxmOiBTbGljZSk6IEludDsKCkBuYW1lKF9fdGFjdF9sb2FkX2Fk' +
'ZHJlc3MpCmV4dGVuZHMgbXV0YXRlcyBuYXRpdmUgbG9hZEFkZHJlc3Moc2VsZjogU2xpY2UpOiBBZGRyZXNzOwoKQG5hbWUoc2tpcF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: 4 additions & 0 deletions src/test/e2e-emulated/contracts/stdlib.tact
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ contract StdlibTest {
get fun sliceRefs(sc: Slice): Int {
return sc.refs();
}

get fun loadBool(sc: Slice): Bool {
return sc.loadBool();
}
}
10 changes: 4 additions & 6 deletions src/test/e2e-emulated/stdlib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ describe("stdlib", () => {
.storeBit(1)
.storeRef(beginCell().storeBit(1).endCell())
.endCell();
const bits = await contract.getSliceBits(slice);
const refs = await contract.getSliceRefs(slice);
const empty = await contract.getSliceEmpty(slice);
expect(bits).toBe(2n);
expect(refs).toBe(1n);
expect(empty).toBe(false);
expect(await contract.getSliceBits(slice)).toBe(2n);
expect(await contract.getSliceRefs(slice)).toBe(1n);
expect(await contract.getSliceEmpty(slice)).toBe(false);
expect(await contract.getLoadBool(slice)).toBe(true);
});
});
3 changes: 3 additions & 0 deletions stdlib/std/cells.tact
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extends mutates native loadUint(self: Slice, l: Int): Int;
@name(preload_uint)
extends native preloadUint(self: Slice, l: Int): Int;

@name(__tact_load_bool)
extends mutates native loadBool(self: Slice): Bool;

@name(load_coins)
extends mutates native loadCoins(self: Slice): Int;

Expand Down

0 comments on commit 7afc87e

Please sign in to comment.