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 Apr 22, 2024
1 parent b9dd1b8 commit 6c91c48
Show file tree
Hide file tree
Showing 4 changed files with 743 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/abi/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,73 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
},
},
],
[
"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",
{
Expand Down
16 changes: 16 additions & 0 deletions src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ 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)`,
Expand Down
Loading

0 comments on commit 6c91c48

Please sign in to comment.