Skip to content

Commit

Permalink
fix: Include new properties required by puya in awst nodes and wtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Nov 12, 2024
1 parent 1e06a3a commit ccec890
Show file tree
Hide file tree
Showing 9 changed files with 4,132 additions and 1,850 deletions.
4 changes: 2 additions & 2 deletions packages/algo-ts/package-lock.json

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

4 changes: 4 additions & 0 deletions src/awst/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,11 +1190,13 @@ export class LogicSignature extends RootNode {
this.shortName = props.shortName
this.program = props.program
this.docstring = props.docstring
this.avmVersion = props.avmVersion
}
id: LogicSigReference
shortName: string
program: Subroutine
docstring: string | null
avmVersion: bigint | null
accept<T>(visitor: RootNodeVisitor<T>): T {
return visitor.visitLogicSignature(this)
}
Expand Down Expand Up @@ -1263,6 +1265,7 @@ export class Contract extends RootNode {
this.appState = props.appState
this.stateTotals = props.stateTotals
this.reservedScratchSpace = props.reservedScratchSpace
this.avmVersion = props.avmVersion
}
id: ContractReference
name: string
Expand All @@ -1274,6 +1277,7 @@ export class Contract extends RootNode {
appState: Array<AppStorageDefinition>
stateTotals: StateTotals | null
reservedScratchSpace: Set<bigint>
avmVersion: bigint | null
accept<T>(visitor: RootNodeVisitor<T>): T {
return visitor.visitContract(this)
}
Expand Down
15 changes: 14 additions & 1 deletion src/awst/wtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export namespace wtypes {

export class WStructType extends WType {
fields: Record<string, WType>
readonly frozen = true

constructor({ fields, name }: { fields: Record<string, WType>; name: string }) {
super({
Expand Down Expand Up @@ -248,8 +249,19 @@ export namespace wtypes {
export class ARC4Struct extends ARC4Type {
fields: Record<string, ARC4Type>
sourceLocation: SourceLocation | null
frozen: boolean

constructor({ fields, sourceLocation, name }: { name: string; fields: Record<string, ARC4Type>; sourceLocation?: SourceLocation }) {
constructor({
fields,
sourceLocation,
name,
frozen,
}: {
frozen: boolean
name: string
fields: Record<string, ARC4Type>
sourceLocation?: SourceLocation
}) {
super({
arc4Name: Object.values(fields)
.map((f) => f.arc4Name)
Expand All @@ -259,6 +271,7 @@ export namespace wtypes {
})
this.sourceLocation = sourceLocation ?? null
this.fields = fields
this.frozen = frozen
}
}
export class ARC4Tuple extends ARC4Type {
Expand Down
9 changes: 5 additions & 4 deletions src/awst_build/context/awst-build-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { invariant } from '../../util'
import { ConstantStore } from '../constant-store'
import type { NodeBuilder } from '../eb'
import type { AppStorageDeclaration } from '../models/app-storage-declaration'
import type { ContractClassModel, LogicSig } from '../models/contract-class-model'
import type { ContractClassModel } from '../models/contract-class-model'
import { CompilationSet } from '../models/contract-class-model'
import type { ContractClassPType, PType } from '../ptypes'
import { typeRegistry } from '../type-registry'
import { TypeResolver } from '../type-resolver'
import { EvaluationContext } from './evaluation-context'
import { SwitchLoopContext } from './switch-loop-context'
import { UniqueNameResolver } from './unique-name-resolver'
import type { LogicSigClassModel } from '../models/logic-sig-class-model'

export interface AwstBuildContext {
/**
Expand Down Expand Up @@ -91,7 +92,7 @@ export interface AwstBuildContext {
getStorageDefinitionsForContract(contractType: ContractClassPType): AppStorageDefinition[]

addToCompilationSet(compilationTarget: ContractReference, contract: ContractClassModel): void
addToCompilationSet(compilationTarget: LogicSigReference, logicSig: LogicSig): void
addToCompilationSet(compilationTarget: LogicSigReference, logicSig: LogicSigClassModel): void

get compilationSet(): CompilationSet
}
Expand Down Expand Up @@ -238,8 +239,8 @@ class AwstBuildContextImpl implements AwstBuildContext {
}

addToCompilationSet(compilationTarget: ContractReference, contract: ContractClassModel): void
addToCompilationSet(compilationTarget: LogicSigReference, logicSig: LogicSig): void
addToCompilationSet(compilationTarget: ContractReference | LogicSigReference, contractOrSig: ContractClassModel | LogicSig) {
addToCompilationSet(compilationTarget: LogicSigReference, logicSig: LogicSigClassModel): void
addToCompilationSet(compilationTarget: ContractReference | LogicSigReference, contractOrSig: ContractClassModel | LogicSigClassModel) {
if (this.#compilationSet.has(compilationTarget)) {
logger.debug(undefined, `${compilationTarget.id} already exists in compilation set`)
return
Expand Down
1 change: 1 addition & 0 deletions src/awst_build/models/contract-class-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class ContractClassModel {
stateTotals: this.stateTotals, // TODO: Tally
reservedScratchSpace: this.reservedScratchSpace,
sourceLocation: this.sourceLocation,
avmVersion: null, // TODO: Allow this to be set with class decorator
})
}

Expand Down
1 change: 1 addition & 0 deletions src/awst_build/models/logic-sig-class-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class LogicSigClassModel {
program: this.program,
sourceLocation: this.sourceLocation,
docstring: this.description,
avmVersion: null, // TODO: Allow this to be set from class decorator
})
}
}
1 change: 1 addition & 0 deletions src/awst_build/ptypes/arc4-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class ARC4StructType extends ARC4EncodedType {
name: this.name,
fields: Object.fromEntries(Object.entries(this.fields).map(([f, t]) => [f, t.wtype])),
sourceLocation: this.sourceLocation,
frozen: false,
})
}

Expand Down
52 changes: 46 additions & 6 deletions tests/approvals/out/arc4-struct/arc4-struct.awst
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
subroutine test(): void
contract StructDemo
{
v1: Vector = new Vector(x=(#14 = { x: 0, y: 0 }).x, y=#14.y)
log(reinterpret_cast<bytes>(v1.x))
log(reinterpret_cast<bytes>(v1.y))
v2: Vector = new Vector(x=(#16 = { x: (#15 = { y: 0, x: 0 }).x, y: #15.y }).x, y=#16.y)
assert(reinterpret_cast<bytes>(v1) == reinterpret_cast<bytes>(v2))
approvalProgram(): bool
{
if (!Boolean(txn<ApplicationID>())) {
this.constructor()
}
return arc4Router()
}

clearProgram(): bool
{
return True
}

testVectorCreationAndEquality(): void
{
v1: Vector = new Vector(x=(#0 = { x: 0, y: 0 }).x, y=#0.y)
log(reinterpret_cast<bytes>(v1.x))
log(reinterpret_cast<bytes>(v1.y))
v2: Vector = new Vector(x=(#2 = { x: (#1 = { y: 0, x: 0 }).x, y: #1.y }).x, y=#2.y)
assert(reinterpret_cast<bytes>(v1) == reinterpret_cast<bytes>(v2))
}

addVectors(): Vector
{
return new Vector(x=(#3 = { x: ARC4_ENCODE(ARC4_DECODE(v1.x) + ARC4_DECODE(v2.x), wtype=arc4.uint64), y: ARC4_ENCODE(ARC4_DECODE(v1.y) + ARC4_DECODE(v2.y), wtype=arc4.uint64) }).x, y=#3.y)
}

constructor(): void
{
void
}

Contract::constructor(): void
{
}

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

__algots__.defaultCreate(): void
{
}

}
Loading

0 comments on commit ccec890

Please sign in to comment.