Skip to content

Commit

Permalink
fix: Expand supported args for uint64 constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Nov 19, 2024
1 parent 005551a commit df9a59b
Show file tree
Hide file tree
Showing 26 changed files with 1,629 additions and 126 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dev:examples": "tsx src/cli.ts build examples --output-awst --output-awst-json",
"dev:approvals": "rimraf tests/approvals/out && tsx src/cli.ts build tests/approvals --output-awst --output-awst-json --output-ssa-ir --out-dir out/[name] --optimization-level 0",
"dev:expected-output": "tsx src/cli.ts build tests/expected-output --dry-run",
"dev:testing": "tsx src/cli.ts build tests/approvals/arc4-types.algo.ts --output-awst --output-awst-json --output-ssa-ir --log-level=info --log-level info --out-dir out/[name]",
"dev:testing": "tsx src/cli.ts build tests/expected-output/uint64-expressions.algo.ts --output-awst --output-awst-json --output-ssa-ir --log-level=info --log-level info --out-dir out/[name]",
"audit": "better-npm-audit audit",
"format": "prettier --write .",
"lint": "eslint \"src/**/*.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/algo-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@algorandfoundation/algorand-typescript",
"version": "0.0.1-alpha.16",
"version": "0.0.1-alpha.17",
"description": "This package contains definitions for the types which comprise Algorand TypeScript which can be compiled to run on the Algorand Virtual Machine using the Puya compiler.",
"private": false,
"main": "index.js",
Expand Down
15 changes: 13 additions & 2 deletions packages/algo-ts/src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export type uint64 = {
__type?: 'uint64'
} & number

/**
* Create a uint64 with the default value of 0
*/
export function Uint64(): uint64
/**
* Create a uint64 from a string literal
*/
export function Uint64(v: string): uint64
/**
* Create a uint64 from a bigint literal
*/
Expand All @@ -25,8 +33,11 @@ export function Uint64(v: number): uint64
* Create a uint64 from a boolean value. True is 1, False is 0
*/
export function Uint64(v: boolean): uint64
export function Uint64(v: Uint64Compat): uint64 {
return Uint64Cls.fromCompat(v).asAlgoTs()
export function Uint64(v?: Uint64Compat | string): uint64 {
if (typeof v === 'string') {
v = BigInt(v)
}
return Uint64Cls.fromCompat(v ?? 0).asAlgoTs()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ export class BigIntLiteralExpressionBuilder extends LiteralExpressionBuilder {
}

resolvableToPType(ptype: PTypeOrClass): boolean {
if (!isValidLiteralForPType(this.value, ptype)) return false
if (this.ptype instanceof NumericLiteralPType || this.ptype.equals(numberPType)) {
return ptype.equals(uint64PType) || ptype.equals(numberPType) || ptype.equals(this.ptype)
return ptype.equals(biguintPType) || ptype.equals(uint64PType) || ptype.equals(numberPType) || ptype.equals(this.ptype)
} else if (this.ptype instanceof BigIntLiteralPType || this.ptype.equals(bigIntPType)) {
return ptype.equals(biguintPType) || ptype.equals(bigIntPType) || ptype.equals(this.ptype)
return ptype.equals(biguintPType) || ptype.equals(uint64PType) || ptype.equals(bigIntPType) || ptype.equals(this.ptype)
}
return false
}
Expand Down
16 changes: 15 additions & 1 deletion src/awst_build/eb/tuple-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Expression } from '../../awst/nodes'
import type { SourceLocation } from '../../awst/source-location'
import { codeInvariant, invariant } from '../../util'
import type { PType } from '../ptypes'
import { TuplePType } from '../ptypes'
import { TuplePType, uint64PType } from '../ptypes'
import { instanceEb } from '../type-registry'
import type { InstanceBuilder, NodeBuilder } from './index'
import { InstanceExpressionBuilder } from './index'
Expand All @@ -19,6 +19,20 @@ export class TupleExpressionBuilder extends InstanceExpressionBuilder<TuplePType
return this.resolve()
}

memberAccess(name: string, sourceLocation: SourceLocation): NodeBuilder {
switch (name) {
case 'length':
return instanceEb(
nodeFactory.uInt64Constant({
value: BigInt(this.ptype.items.length),
sourceLocation,
}),
uint64PType,
)
}
return super.memberAccess(name, sourceLocation)
}

indexAccess(index: InstanceBuilder, sourceLocation: SourceLocation): NodeBuilder {
const indexNum = requireIntegerConstant(index).value
const itemType = this.ptype.items[Number(indexNum)]
Expand Down
15 changes: 12 additions & 3 deletions src/awst_build/eb/uint64-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { wtypes } from '../../awst/wtypes'
import { NotSupported } from '../../errors'
import { tryConvertEnum } from '../../util'
import type { InstanceType, PType } from '../ptypes'
import { boolPType, Uint64Function, uint64PType } from '../ptypes'
import { boolPType, stringPType, Uint64Function, uint64PType } from '../ptypes'
import type { BuilderComparisonOp, InstanceBuilder, NodeBuilder } from './index'
import { BuilderBinaryOp, BuilderUnaryOp, FunctionBuilder, InstanceExpressionBuilder } from './index'
import { requireExpressionOfType } from './util'
import { requireExpressionOfType, requireStringConstant } from './util'
import { parseFunctionArgs } from './util/arg-parsing'
import { compareUint64 } from './util/compare-uint64'
import { stringToBigint } from './util/string-to-bigint'

export class UInt64FunctionBuilder extends FunctionBuilder {
readonly ptype = Uint64Function
Expand All @@ -28,7 +29,7 @@ export class UInt64FunctionBuilder extends FunctionBuilder {
genericTypeArgs: 0,
callLocation: sourceLocation,
funcName: 'Uint64',
argSpec: (a) => [a.optional(uint64PType, boolPType)],
argSpec: (a) => [a.optional(uint64PType, boolPType, stringPType)],
})

if (!value) {
Expand Down Expand Up @@ -57,6 +58,14 @@ export class UInt64FunctionBuilder extends FunctionBuilder {
}),
)
}
} else if (value.ptype.equals(stringPType)) {
const valueStr = requireStringConstant(value)
return new UInt64ExpressionBuilder(
nodeFactory.uInt64Constant({
value: stringToBigint(valueStr),
sourceLocation,
}),
)
}
return value
}
Expand Down
13 changes: 13 additions & 0 deletions src/awst_build/eb/util/string-to-bigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { SourceLocation } from '../../../awst/source-location'
import { CodeError } from '../../../errors'

export function stringToBigint({ value, sourceLocation }: { value: string; sourceLocation: SourceLocation }): bigint {
try {
return BigInt(value)
} catch (e) {
if (e instanceof SyntaxError) {
throw new CodeError(`Cannot convert ${value} to an integer`, { sourceLocation })
}
throw e
}
}
10 changes: 9 additions & 1 deletion tests/approvals/biguint-expressions.algo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import { BigUint } from '@algorandfoundation/algorand-typescript'
import { BaseContract, BigUint } from '@algorandfoundation/algorand-typescript'
import { itob } from '@algorandfoundation/algorand-typescript/op'

function test(a: uint64, b: boolean, c: bytes) {
BigUint()
Expand All @@ -16,3 +17,10 @@ function test(a: uint64, b: boolean, c: bytes) {
BigUint(b)
BigUint(c)
}

class DemoContract extends BaseContract {
public approvalProgram() {
test(1, false, itob(4))
return true
}
}
2 changes: 1 addition & 1 deletion tests/approvals/out/arc4-types/arc4-types.awst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ subroutine test(n: uint64, b: biguint, c: arc4.uint256): void
{
x: arc4.uint8 = 4
assert(len(reinterpret_cast<bytes>(x)) == 1)
x2: arc4.uint8 = 0xff
x2: arc4.uint8 = 255
assert(reinterpret_cast<bytes>(x2) == 0xff)
y: arc4.uint16 = 0
assert(len(reinterpret_cast<bytes>(y)) == 2)
Expand Down
6 changes: 3 additions & 3 deletions tests/approvals/out/arc4-types/arc4-types.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@
"name": "x2"
},
"value": {
"_type": "BytesConstant",
"_type": "IntegerConstant",
"source_location": {
"file": "tests/approvals/arc4-types.algo.ts",
"line": 15,
Expand All @@ -663,8 +663,8 @@
"arc4_name": "uint8",
"n": "8"
},
"value": "{{",
"encoding": "unknown"
"value": "255",
"teal_alias": null
}
},
{
Expand Down
36 changes: 36 additions & 0 deletions tests/approvals/out/biguint-expressions/DemoContract.approval.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma version 10

tests/approvals/biguint-expressions.algo.ts::DemoContract.approvalProgram:
txn ApplicationID
bnz main_after_if_else@2
callsub constructor

main_after_if_else@2:
// tests/approvals/biguint-expressions.algo.ts:23
// test(1, false, itob(4))
pushint 4 // 4
itob
pushint 1 // 1
pushint 0 // 0
uncover 2
callsub test
// tests/approvals/biguint-expressions.algo.ts:24
// return true
pushint 1 // 1
return


// tests/approvals/biguint-expressions.algo.ts::DemoContract.constructor() -> void:
constructor:
// tests/approvals/biguint-expressions.algo.ts:21
// class DemoContract extends BaseContract {
proto 0 0
retsub


// tests/approvals/biguint-expressions.algo.ts::test(a: uint64, b: uint64, c: bytes) -> void:
test:
// tests/approvals/biguint-expressions.algo.ts:5
// function test(a: uint64, b: boolean, c: bytes) {
proto 3 0
retsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma version 10

tests/approvals/biguint-expressions.algo.ts::DemoContract.clearStateProgram:
pushint 1 // 1
return
27 changes: 27 additions & 0 deletions tests/approvals/out/biguint-expressions/biguint-expressions.awst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,31 @@ subroutine test(a: uint64, b: bool, c: bytes): void
reinterpret_cast<biguint>(itob(a * a))
reinterpret_cast<biguint>(itob(b))
reinterpret_cast<biguint>(c)
}
contract DemoContract
{
approvalProgram(): bool
{
if (!Boolean(txn<ApplicationID>())) {
this.constructor()
}
tests/approvals/biguint-expressions.algo.ts::test(1, False, itob(4))
return True
}

clearProgram(): bool
{
return True
}

constructor(): void
{
void
}

Contract::constructor(): void
{
this.constructor()
}

}
Loading

0 comments on commit df9a59b

Please sign in to comment.