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: fromCell method for maps #1271

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `VarInt16`, `VarInt32`, `VarUint16`, `VarUint32` integer serialization types: PR [#1186](https://github.com/tact-lang/tact/pull/1186)
- `unboc`: a standalone CLI utility to expose Tact's TVM disassembler: PR [#1259](https://github.com/tact-lang/tact/pull/1259)
- Added alternative parser: PR [#1258](https://github.com/tact-lang/tact/pull/1258)
- The `fromCell` method for the `Map` type: PR [#1271](https://github.com/tact-lang/tact/pull/1271)

### Changed

Expand Down
15 changes: 15 additions & 0 deletions docs/src/content/docs/book/maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ contract Example {
}
```

### Convert from a `Cell`, `.fromCell()` {#fromcell}

<Badge text="Available since Tact 1.6 (not released yet)" variant="tip" size="medium"/><p/>

To convert a [`Cell{:tact}`][cell] type back to a map, use the `.fromCell(){:tact}` [method](/book/functions#extension-function).

```tact
// Suppose we have a Cell
let cell: Cell = ...;

// And we want to initialize a map variable from it
let fizz: map<Int, Int> = emptyMap();
fizz.fromCell(cell);
Comment on lines +374 to +375
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, this should be consistent with what we have for structs:

struct Fizz { foo: Int }
let fizzCell = ...
let parsedFizz: Fizz = Fizz.fromCell(fizzCell);

So I would expect something like:

let fizz = map<Int, Int>.fromCell(cell);

This makes parsing harder but it seems it's too late to do it differently

```

### Traverse over entries {#traverse}

To iterate over map entries there is a [`foreach{:tact}`](/book/statements#foreach-loop) loop statement:
Expand Down
64 changes: 64 additions & 0 deletions src/abi/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,70 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
},
},
],
[
"fromCell",
{
name: "fromCell",
resolve(
ctx: CompilerContext,
args: (TypeRef | undefined)[],
ref: SrcInfo,
) {
checkArgumentsLength(
args,
2,
"fromCell expects one argument",
ref,
);

const [self, newCell] = args;
checkMapType(self, ref);

if (
!newCell ||
newCell.kind !== "ref" ||
newCell.name !== "Cell"
) {
throwCompilationError(
"fromCell expects a Cell as second argument",
ref,
);
}

return { kind: "void" };
},
generate(
ctx: WriterContext,
args: (TypeRef | undefined)[],
exprs: AstExpression[],
ref: SrcInfo,
) {
checkArgumentsLength(
args,
2,
"fromCell expects one argument",
ref,
);

const [self, newCell] = args;
checkMapType(self, ref);

if (
!newCell ||
newCell.kind !== "ref" ||
newCell.name !== "Cell"
) {
throwCompilationError(
"fromCell expects a Cell as second argument",
ref,
);
}

const resolved = exprs.map((v) => writeExpression(v, ctx));
return `${resolved[0]} = ${resolved[1]}`;
},
},
],
[
"isEmpty",
{
Expand Down
156 changes: 156 additions & 0 deletions src/test/e2e-emulated/contracts/maps1.tact
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,88 @@ message ReplaceGetAllMaps {
valueStruct: SomeStruct?;
}

message FromCellAllMaps {
// Integer (`Int`) Key Maps
int_varint16: Cell;
int_varint32: Cell;
int_varuint16: Cell;
int_varuint32: Cell;
int_bool: Cell;
int_cell: Cell;
int_address: Cell;
int_struct: Cell;

// Integer (`Int as int8`) Key Maps
int8_varint16: Cell;
int8_varint32: Cell;
int8_varuint16: Cell;
int8_varuint32: Cell;
int8_bool: Cell;
int8_cell: Cell;
int8_address: Cell;
int8_struct: Cell;

// Integer (`Int as int42`) Key Maps
int42_varint16: Cell;
int42_varint32: Cell;
int42_varuint16: Cell;
int42_varuint32: Cell;
int42_bool: Cell;
int42_cell: Cell;
int42_address: Cell;
int42_struct: Cell;

// Integer (`Int as int256`) Key Maps
int256_varint16: Cell;
int256_varint32: Cell;
int256_varuint16: Cell;
int256_varuint32: Cell;
int256_bool: Cell;
int256_cell: Cell;
int256_address: Cell;
int256_struct: Cell;

// Integer (`Int as uint8`) Key Maps
uint8_varint16: Cell;
uint8_varint32: Cell;
uint8_varuint16: Cell;
uint8_varuint32: Cell;
uint8_bool: Cell;
uint8_cell: Cell;
uint8_address: Cell;
uint8_struct: Cell;

// Integer (`Int as uint42`) Key Maps
uint42_varint16: Cell;
uint42_varint32: Cell;
uint42_varuint16: Cell;
uint42_varuint32: Cell;
uint42_bool: Cell;
uint42_cell: Cell;
uint42_address: Cell;
uint42_struct: Cell;

// Integer (`Int as uint256`) Key Maps
uint256_varint16: Cell;
uint256_varint32: Cell;
uint256_varuint16: Cell;
uint256_varuint32: Cell;
uint256_bool: Cell;
uint256_cell: Cell;
uint256_address: Cell;
uint256_struct: Cell;

// Address Key Maps
address_varint16: Cell;
address_varint32: Cell;
address_varuint16: Cell;
address_varuint32: Cell;
address_bool: Cell;
address_cell: Cell;
address_address: Cell;
address_struct: Cell;
}

message CheckNullReference {

}
Expand Down Expand Up @@ -972,6 +1054,80 @@ contract MapTestContract {
self.address_struct.replaceGet(msg.keyAddress, msg.valueStruct);
}

receive(msg: FromCellAllMaps) {
self.int_varint16.fromCell(msg.int_varint16);
self.int_varint32.fromCell(msg.int_varint32);
self.int_varuint16.fromCell(msg.int_varuint16);
self.int_varuint32.fromCell(msg.int_varuint32);
self.int_bool.fromCell(msg.int_bool);
self.int_cell.fromCell(msg.int_cell);
self.int_address.fromCell(msg.int_address);
self.int_struct.fromCell(msg.int_struct);

self.int8_varint16.fromCell(msg.int8_varint16);
self.int8_varint32.fromCell(msg.int8_varint32);
self.int8_varuint16.fromCell(msg.int8_varuint16);
self.int8_varuint32.fromCell(msg.int8_varuint32);
self.int8_bool.fromCell(msg.int8_bool);
self.int8_cell.fromCell(msg.int8_cell);
self.int8_address.fromCell(msg.int8_address);
self.int8_struct.fromCell(msg.int8_struct);

self.int42_varint16.fromCell(msg.int42_varint16);
self.int42_varint32.fromCell(msg.int42_varint32);
self.int42_varuint16.fromCell(msg.int42_varuint16);
self.int42_varuint32.fromCell(msg.int42_varuint32);
self.int42_bool.fromCell(msg.int42_bool);
self.int42_cell.fromCell(msg.int42_cell);
self.int42_address.fromCell(msg.int42_address);
self.int42_struct.fromCell(msg.int42_struct);

self.int256_varint16.fromCell(msg.int256_varint16);
self.int256_varint32.fromCell(msg.int256_varint32);
self.int256_varuint16.fromCell(msg.int256_varuint16);
self.int256_varuint32.fromCell(msg.int256_varuint32);
self.int256_bool.fromCell(msg.int256_bool);
self.int256_cell.fromCell(msg.int256_cell);
self.int256_address.fromCell(msg.int256_address);
self.int256_struct.fromCell(msg.int256_struct);

self.uint8_varint16.fromCell(msg.uint8_varint16);
self.uint8_varint32.fromCell(msg.uint8_varint32);
self.uint8_varuint16.fromCell(msg.uint8_varuint16);
self.uint8_varuint32.fromCell(msg.uint8_varuint32);
self.uint8_bool.fromCell(msg.uint8_bool);
self.uint8_cell.fromCell(msg.uint8_cell);
self.uint8_address.fromCell(msg.uint8_address);
self.uint8_struct.fromCell(msg.uint8_struct);

self.uint42_varint16.fromCell(msg.uint42_varint16);
self.uint42_varint32.fromCell(msg.uint42_varint32);
self.uint42_varuint16.fromCell(msg.uint42_varuint16);
self.uint42_varuint32.fromCell(msg.uint42_varuint32);
self.uint42_bool.fromCell(msg.uint42_bool);
self.uint42_cell.fromCell(msg.uint42_cell);
self.uint42_address.fromCell(msg.uint42_address);
self.uint42_struct.fromCell(msg.uint42_struct);

self.uint256_varint16.fromCell(msg.uint256_varint16);
self.uint256_varint32.fromCell(msg.uint256_varint32);
self.uint256_varuint16.fromCell(msg.uint256_varuint16);
self.uint256_varuint32.fromCell(msg.uint256_varuint32);
self.uint256_bool.fromCell(msg.uint256_bool);
self.uint256_cell.fromCell(msg.uint256_cell);
self.uint256_address.fromCell(msg.uint256_address);
self.uint256_struct.fromCell(msg.uint256_struct);

self.address_varint16.fromCell(msg.address_varint16);
self.address_varint32.fromCell(msg.address_varint32);
self.address_varuint16.fromCell(msg.address_varuint16);
self.address_varuint32.fromCell(msg.address_varuint32);
self.address_bool.fromCell(msg.address_bool);
self.address_cell.fromCell(msg.address_cell);
self.address_address.fromCell(msg.address_address);
self.address_struct.fromCell(msg.address_struct);
}

// ===============================
// Getters
// ===============================
Expand Down
Loading
Loading