Skip to content

Commit

Permalink
0.17.0 - box len, extract, replace, and create (#55)
Browse files Browse the repository at this point in the history
* box len, extract, replace, and create

* 0.17.0
  • Loading branch information
joe-p committed Jun 30, 2023
1 parent a601293 commit 4da3eba
Show file tree
Hide file tree
Showing 8 changed files with 721 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@algorandfoundation/tealscript",
"version": "0.16.0",
"version": "0.17.0",
"description": "Enables Algorand smart contract development with native TypeScript syntax, tooling, and IDE support",
"homepage": "https://github.com/algorand-devrel/TEALScript",
"bugs": {
Expand Down
92 changes: 92 additions & 0 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,98 @@ export default class Compiler {
},
},
box: {
create: (node: ts.CallExpression) => {
if (!ts.isPropertyAccessExpression(node.expression)) throw new Error();
if (!ts.isPropertyAccessExpression(node.expression.expression)) throw new Error();

const name = node.expression.expression.name.getText();

const {
keyType, key, prefix,
} = this.storageProps[name];

if (key) {
this.pushVoid(node.expression, `byte "${key}"`);
} else {
if (prefix) this.pushVoid(node.arguments[0], `byte "${prefix}"`);
this.processNode(node.arguments[0]);
if (isNumeric(keyType)) this.pushVoid(node.arguments[0], 'itob');
if (prefix) this.pushVoid(node.arguments[0], 'concat');
}

this.processNode(node.arguments[key ? 0 : 1]);

this.pushVoid(node.expression, 'box_create');
},
extract: (node: ts.CallExpression) => {
if (!ts.isPropertyAccessExpression(node.expression)) throw new Error();
if (!ts.isPropertyAccessExpression(node.expression.expression)) throw new Error();

const name = node.expression.expression.name.getText();

const {
keyType, key, prefix,
} = this.storageProps[name];

if (key) {
this.pushVoid(node.expression, `byte "${key}"`);
} else {
if (prefix) this.pushVoid(node.arguments[0], `byte "${prefix}"`);
this.processNode(node.arguments[0]);
if (isNumeric(keyType)) this.pushVoid(node.arguments[0], 'itob');
if (prefix) this.pushVoid(node.arguments[0], 'concat');
}

this.processNode(node.arguments[key ? 0 : 1]);
this.processNode(node.arguments[key ? 1 : 2]);

this.push(node.expression, 'box_extract', StackType.bytes);
},
replace: (node: ts.CallExpression) => {
if (!ts.isPropertyAccessExpression(node.expression)) throw new Error();
if (!ts.isPropertyAccessExpression(node.expression.expression)) throw new Error();

const name = node.expression.expression.name.getText();

const {
keyType, key, prefix,
} = this.storageProps[name];

if (key) {
this.pushVoid(node.expression, `byte "${key}"`);
} else {
if (prefix) this.pushVoid(node.arguments[0], `byte "${prefix}"`);
this.processNode(node.arguments[0]);
if (isNumeric(keyType)) this.pushVoid(node.arguments[0], 'itob');
if (prefix) this.pushVoid(node.arguments[0], 'concat');
}

this.processNode(node.arguments[key ? 0 : 1]);
this.processNode(node.arguments[key ? 1 : 2]);

this.pushVoid(node.expression, 'box_replace');
},
length: (node: ts.CallExpression) => {
if (!ts.isPropertyAccessExpression(node.expression)) throw new Error();
if (!ts.isPropertyAccessExpression(node.expression.expression)) throw new Error();

const name = node.expression.expression.name.getText();

const {
keyType, key, prefix,
} = this.storageProps[name];

if (key) {
this.pushVoid(node.expression, `byte "${key}"`);
} else {
if (prefix) this.pushVoid(node.arguments[0], `byte "${prefix}"`);
this.processNode(node.arguments[0]);
if (isNumeric(keyType)) this.pushVoid(node.arguments[0], 'itob');
if (prefix) this.pushVoid(node.arguments[0], 'concat');
}

this.maybeValue(node.expression, 'box_len', StackType.uint64);
},
get: (node: ts.CallExpression) => {
if (!ts.isPropertyAccessExpression(node.expression)) throw new Error();
if (!ts.isPropertyAccessExpression(node.expression.expression)) throw new Error();
Expand Down
72 changes: 72 additions & 0 deletions tests/contracts/artifacts/StorageTest.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,78 @@
"type": "void",
"desc": ""
}
},
{
"name": "boxKeyCreate",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
},
{
"name": "boxMapCreate",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
},
{
"name": "boxKeyLength",
"args": [],
"desc": "",
"returns": {
"type": "uint64",
"desc": ""
}
},
{
"name": "boxMapLength",
"args": [],
"desc": "",
"returns": {
"type": "uint64",
"desc": ""
}
},
{
"name": "boxKeyReplace",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
},
{
"name": "boxMapReplace",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
},
{
"name": "boxKeyExtract",
"args": [],
"desc": "",
"returns": {
"type": "string",
"desc": ""
}
},
{
"name": "boxMapExtract",
"args": [],
"desc": "",
"returns": {
"type": "string",
"desc": ""
}
}
]
}
Loading

0 comments on commit 4da3eba

Please sign in to comment.