Skip to content

Commit

Permalink
feat: add .del function to maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Nov 28, 2023
1 parent 17f1afa commit 1fac6c5
Show file tree
Hide file tree
Showing 5 changed files with 512 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/abi/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,66 @@ export const MapFunctions: { [key: string]: AbiFunction } = {
throwError(`set expects a map with Int keys`, ref);
}
},
del: {
name: 'del',
resolve(ctx, args, ref) {

// Check arguments
if (args.length !== 2) {
throwError('del expects one argument', ref); // Ignore self argument
}
let self = args[0];
if (!self || self.kind !== 'map') {
throwError('del expects a map as self argument', ref); // Should not happen
}

// Check key type
if (args[1].kind !== 'ref' || args[1].optional) {
throwError('del expects a direct type as first argument', ref);
}
if (args[1].name !== self.key) {
throwError(`del expects a ${self.key} as first argument`, ref);
}

// Returns nothing
return { kind: 'void' };
},
generate: (ctx, args, exprs, ref) => {

if (args.length !== 2) {
throwError('del expects one argument', ref); // Ignore self argument
}
let self = args[0];
if (!self || self.kind !== 'map') {
throwError('del expects a map as self argument', ref); // Should not happen
}

// Render expressions
let resolved = exprs.map((v) => writeExpression(v, ctx));

// Handle Int key
if (self.key === 'Int') {
let bits = 257;
let kind = 'int';
if (self.keyAs && self.keyAs.startsWith('int')) {
bits = parseInt(self.keyAs.slice(3), 10);
} else if (self.keyAs && self.keyAs.startsWith('uint')) {
bits = parseInt(self.keyAs.slice(4), 10);
kind = 'uint';
}
ctx.used(`__tact_dict_delete_${kind}`);
return `${resolved[0]}~__tact_dict_delete_${kind}(${bits}, ${resolved[1]})`;
}

// Handle Address key
if (self.key === 'Address') {
ctx.used(`__tact_dict_delete`);
return `${resolved[0]}~__tact_dict_delete(267, ${resolved[1]})`;
}

throwError(`del expects a map with Int keys`, ref);
}
},
asCell: {
name: 'asCell',
resolve(ctx, args, ref) {
Expand Down
12 changes: 12 additions & 0 deletions src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ export function writeStdlib(ctx: WriterContext) {
ctx.asm(`asm(index dict key_len) "DICTDEL"`);
});

ctx.fun('__tact_dict_delete_int', () => {
ctx.signature(`(cell, int) __tact_dict_delete_int(cell dict, int key_len, int index)`);
ctx.context('stdlib');
ctx.asm(`asm(index dict key_len) "DICTIDEL"`);
});

ctx.fun('__tact_dict_delete_uint', () => {
ctx.signature(`(cell, int) __tact_dict_delete_uint(cell dict, int key_len, int index)`);
ctx.context('stdlib');
ctx.asm(`asm(index dict key_len) "DICTUDEL"`);
});

ctx.fun('__tact_dict_set_ref', () => {
ctx.signature(`((cell), ()) __tact_dict_set_ref(cell dict, int key_len, slice index, cell value)`);
ctx.context('stdlib');
Expand Down
4 changes: 2 additions & 2 deletions src/imports/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ files['std/contract.tact'] =
files['std/crypto.tact'] =
'QG5hbWUoY2VsbF9oYXNoKQpleHRlbmRzIG5hdGl2ZSBoYXNoKHNlbGY6IENlbGwpOiBJbnQ7CgpAbmFtZShzbGljZV9oYXNoKQpleHRlbmRzIG5hdGl2ZSBoYXNoKHNl' +
'bGY6IFNsaWNlKTogSW50OwoKQG5hbWUoY2hlY2tfc2lnbmF0dXJlKQpuYXRpdmUgY2hlY2tTaWduYXR1cmUoaGFzaDogSW50LCBzaWduYXR1cmU6IFNsaWNlLCBwdWJs' +
'aWNfa2V5OiBJbnQpOiBCb29sOwoKQG5hbWUoY2hlY2tfZGF0YV9zaWduYXR1cmUpCm5hdGl2ZSBjaGVja0RhdGFTaWduYXR1cmUoaGFzaDogU2xpY2UsIHNpZ25hdHVy' +
'ZTogU2xpY2UsIHB1YmxpY19rZXk6IFNsaWNlKTogQm9vbDs=';
'aWNfa2V5OiBJbnQpOiBCb29sOwoKQG5hbWUoY2hlY2tfZGF0YV9zaWduYXR1cmUpCm5hdGl2ZSBjaGVja0RhdGFTaWduYXR1cmUoZGF0YTogU2xpY2UsIHNpZ25hdHVy' +
'ZTogU2xpY2UsIHB1YmxpY19rZXk6IEludCk6IEJvb2w7Cg==';
files['std/debug.tact'] =
'QG5hbWUodGhyb3cpCm5hdGl2ZSBuYXRpdmVUaHJvdyhjb2RlOiBJbnQpOwoKQG5hbWUodGhyb3dfd2hlbikKbmF0aXZlIG5hdGl2ZVRocm93V2hlbihjb2RlOiBJbnQs' +
'IGNvbmRpdGlvbjogQm9vbCk7CgpAbmFtZSh0aHJvdykKbmF0aXZlIHRocm93KGNvZGU6IEludCk7CgpAbmFtZSh0aHJvd191bmxlc3MpCm5hdGl2ZSBuYXRpdmVUaHJv' +
Expand Down
Loading

0 comments on commit 1fac6c5

Please sign in to comment.