Skip to content

Commit

Permalink
fix: Resolve issue with resolution of algo-ts types from namespace ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
tristanmenzel committed Nov 16, 2024
1 parent ad871e8 commit 005551a
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 45 deletions.
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-struct.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/approvals/arc4-types.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
5 changes: 5 additions & 0 deletions src/awst_build/ast-visitors/base-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { BigIntLiteralExpressionBuilder } from '../eb/literal/big-int-literal-ex
import { ConditionalExpressionBuilder } from '../eb/literal/conditional-expression-builder'
import type { ObjectLiteralParts } from '../eb/literal/object-literal-expression-builder'
import { ObjectLiteralExpressionBuilder } from '../eb/literal/object-literal-expression-builder'
import { NamespaceBuilder } from '../eb/namespace-builder'
import { OmittedExpressionBuilder } from '../eb/omitted-expression-builder'
import { StringExpressionBuilder, StringFunctionBuilder } from '../eb/string-expression-builder'
import { requireExpressionOfType, requireInstanceBuilder } from '../eb/util'
Expand Down Expand Up @@ -192,6 +193,10 @@ export abstract class BaseVisitor implements Visitor<Expressions, NodeBuilder> {
visitPropertyAccessExpression(node: ts.PropertyAccessExpression): NodeBuilder {
this.logNotSupported(node.questionDotToken, 'The optional chaining (?.) operator is not supported')
const target = this.baseAccept(node.expression)
if (target instanceof NamespaceBuilder) {
codeInvariant(!ts.isPrivateIdentifier(node.name), 'Private identifiers are not supported here', this.sourceLocation(node.name))
return this.context.getBuilderForNode(node.name)
}
const property = this.textVisitor.accept(node.name)
return target.memberAccess(property, this.sourceLocation(node))
}
Expand Down
13 changes: 1 addition & 12 deletions src/awst_build/eb/namespace-builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { SourceLocation } from '../../awst/source-location'
import { codeInvariant, invariant } from '../../util'
import { invariant } from '../../util'
import type { PType } from '../ptypes'
import { NamespacePType } from '../ptypes'
import { SymbolName } from '../symbol-name'
import { typeRegistry } from '../type-registry'
import { NodeBuilder } from './index'

export class NamespaceBuilder extends NodeBuilder {
Expand All @@ -17,13 +15,4 @@ export class NamespaceBuilder extends NodeBuilder {
get ptype(): PType {
return this._ptype
}

memberAccess(name: string, sourceLocation: SourceLocation): NodeBuilder {
const symbolName = new SymbolName({ module: this._ptype.module, name })

const type = typeRegistry.tryResolveSingletonName(symbolName)
codeInvariant(type, `${name} does not exist in namespace ${this._ptype}`)

return typeRegistry.getSingletonEb(type, sourceLocation)
}
}
5 changes: 4 additions & 1 deletion tests/approvals/arc4-types.algo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { biguint, uint64 } from '@algorandfoundation/algorand-typescript'
import { assert, BaseContract, Bytes, Txn } from '@algorandfoundation/algorand-typescript'
import { arc4, assert, BaseContract, Bytes, Txn } from '@algorandfoundation/algorand-typescript'
import { Address, Byte, DynamicArray, StaticArray, Str, Tuple, UFixedNxM, UintN } from '@algorandfoundation/algorand-typescript/arc4'

function testUFixed() {
Expand Down Expand Up @@ -88,6 +88,9 @@ export class Arc4TypesTestContract extends BaseContract {
testArrays(new UintN<64>(65))
testAddress()
testTuple()

const result = new arc4.DynamicArray<arc4.UintN<64>>()
assert(result.length === 0)
return true
}
}
64 changes: 38 additions & 26 deletions tests/approvals/out/arc4-types/Arc4TypesTestContract.approval.teal
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma version 10

tests/approvals/arc4-types.algo.ts::Arc4TypesTestContract.approvalProgram:
intcblock 1 8 0 2
bytecblock 0x base32(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) 0x0000 0x00
intcblock 0 1 8 2
bytecblock 0x 0x0000 base32(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) 0x00
txn ApplicationID
bnz main_after_if_else@2
callsub constructor
Expand All @@ -13,7 +13,7 @@ main_after_if_else@2:
callsub testStr
// tests/approvals/arc4-types.algo.ts:86
// test(1, 2n, new UintN<256>(4))
intc_0 // 1
intc_1 // 1
pushbytes 0x02
pushbytes 0x0000000000000000000000000000000000000000000000000000000000000004
callsub test
Expand All @@ -30,9 +30,21 @@ main_after_if_else@2:
// tests/approvals/arc4-types.algo.ts:90
// testTuple()
callsub testTuple
// tests/approvals/arc4-types.algo.ts:91
// tests/approvals/arc4-types.algo.ts:92
// const result = new arc4.DynamicArray<arc4.UintN<64>>()
bytec_1 // 0x0000
bytec_0 // 0x
concat
// tests/approvals/arc4-types.algo.ts:93
// assert(result.length === 0)
intc_0 // 0
extract_uint16
intc_0 // 0
==
assert
// tests/approvals/arc4-types.algo.ts:94
// return true
intc_0 // 1
intc_1 // 1
return


Expand All @@ -51,7 +63,7 @@ testStr:
proto 0 0
// tests/approvals/arc4-types.algo.ts:31
// const s1 = new Str()
bytec_2 // 0x0000
bytec_1 // 0x0000
// tests/approvals/arc4-types.algo.ts:32
// assert(s1.bytes === new UintN<16>(0).bytes, 'Empty string should equal the uint16 length prefix')
dup
Expand Down Expand Up @@ -80,7 +92,7 @@ test:
// tests/approvals/arc4-types.algo.ts:14
// assert(x.bytes.length === 1)
len
intc_0 // 1
intc_1 // 1
==
assert
// tests/approvals/arc4-types.algo.ts:15
Expand All @@ -93,7 +105,7 @@ test:
assert
// tests/approvals/arc4-types.algo.ts:18
// const y = new UintN<16>()
bytec_2 // 0x0000
bytec_1 // 0x0000
// tests/approvals/arc4-types.algo.ts:19
// assert(y.bytes.length === 2)
len
Expand Down Expand Up @@ -183,7 +195,7 @@ testArrays:
concat
dup
len
intc_1 // 8
intc_2 // 8
/
itob
extract 6 2
Expand All @@ -201,13 +213,13 @@ testArrays:
concat
// tests/approvals/arc4-types.algo.ts:47
// assert(myStatic[0].equals(myArray.pop()))
intc_2 // 0
intc_1 // 8
intc_0 // 0
intc_2 // 8
*
intc_1 // 8
intc_2 // 8
extract3 // on error: Index access is out of bounds
swap
intc_1 // 8
intc_2 // 8
callsub dynamic_array_pop_fixed_size
pop
==
Expand All @@ -225,32 +237,32 @@ testAddress:
txn Sender
// tests/approvals/arc4-types.algo.ts:61
// const a = new Address()
bytec_1 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
bytec_2 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
// tests/approvals/arc4-types.algo.ts:64
// assert(!a.equals(b), 'Zero address should not match sender')
==
!
assert // Zero address should not match sender
// tests/approvals/arc4-types.algo.ts:61
// const a = new Address()
bytec_1 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
bytec_2 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
// tests/approvals/arc4-types.algo.ts:65
// assert(a.equals(new Address()), 'Two zero addresses should match')
dup
==
assert // Two zero addresses should match
// tests/approvals/arc4-types.algo.ts:66
// assert(a[0].equals(new Byte()), 'Zero address should start with zero byte')
intc_2 // 0
intc_0 // 1
intc_0 // 0
intc_1 // 1
*
// tests/approvals/arc4-types.algo.ts:61
// const a = new Address()
bytec_1 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
bytec_2 // addr AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ
// tests/approvals/arc4-types.algo.ts:66
// assert(a[0].equals(new Byte()), 'Zero address should start with zero byte')
swap
intc_0 // 1
intc_1 // 1
extract3 // on error: Index access is out of bounds
bytec_3 // 0x00
==
Expand All @@ -271,14 +283,14 @@ testTuple:
// tests/approvals/arc4-types.algo.ts:71
// const firstItem = t.at(0)
dup
intc_2 // 0
intc_1 // 8
intc_0 // 0
intc_2 // 8
extract3 // on error: Index access is out of bounds
swap
// tests/approvals/arc4-types.algo.ts:72
// const firstItemIndexer = t.native[0]
intc_2 // 0
intc_1 // 8
intc_0 // 0
intc_2 // 8
extract3 // on error: Index access is out of bounds
// tests/approvals/arc4-types.algo.ts:73
// assert(firstItem.equals(firstItemIndexer))
Expand All @@ -297,9 +309,9 @@ testTuple:
dynamic_array_pop_fixed_size:
proto 2 2
frame_dig -2
intc_2 // 0
intc_0 // 0
extract_uint16
intc_0 // 1
intc_1 // 1
-
itob
extract 6 0
Expand All @@ -315,7 +327,7 @@ dynamic_array_pop_fixed_size:
extract3
cover 2
swap
intc_2 // 0
intc_0 // 0
uncover 2
substring3
retsub
5 changes: 5 additions & 0 deletions tests/approvals/out/arc4-types/Arc4TypesTestContract.ssa.ir
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ contract tests/approvals/arc4-types.algo.ts::Arc4TypesTestContract:
tests/approvals/arc4-types.algo.ts::testArrays(0x0000000000000041)
tests/approvals/arc4-types.algo.ts::testAddress()
tests/approvals/arc4-types.algo.ts::testTuple()
let array_data%0#0: bytes = (concat 0x0000 0x)
let result#0: bytes = array_data%0#0
let tmp%0#0: uint64 = (extract_uint16 result#0 0u)
let tmp%1#0: bool = (== tmp%0#0 0u)
(assert tmp%1#0)
return 1u

subroutine tests/approvals/arc4-types.algo.ts::Arc4TypesTestContract.constructor() -> void:
Expand Down
2 changes: 2 additions & 0 deletions tests/approvals/out/arc4-types/arc4-types.awst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ contract Arc4TypesTestContract
tests/approvals/arc4-types.algo.ts::testArrays(65)
tests/approvals/arc4-types.algo.ts::testAddress()
tests/approvals/arc4-types.algo.ts::testTuple()
result: arc4.dynamic_array<arc4.uint64> = new arc4.dynamic_array<arc4.uint64>()
assert(extract_uint16(result, 0) == 0)
return True
}

Expand Down
Loading

0 comments on commit 005551a

Please sign in to comment.