Skip to content

Commit c8abc46

Browse files
committed
refactor: according to review feedback
- use weak map to store generic type info - rename GenericTypeInfo interface to TypeInfo - stop explicitly filling Uint8Array with 0 as it is the default behaviour
1 parent 17ce036 commit c8abc46

File tree

12 files changed

+35
-39
lines changed

12 files changed

+35
-39
lines changed

src/encoders.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { AccountCls } from './impl/account'
44
import { ApplicationCls } from './impl/application'
55
import { AssetCls } from './impl/asset'
66

7-
export interface GenericTypeInfo {
7+
export interface TypeInfo {
88
name: string
9-
genericArgs?: GenericTypeInfo[] | Record<string, GenericTypeInfo>
9+
genericArgs?: TypeInfo[] | Record<string, TypeInfo>
1010
}
1111

12-
type fromBytes<T> = (val: Uint8Array, typeInfo: GenericTypeInfo) => T
12+
type fromBytes<T> = (val: Uint8Array, typeInfo: TypeInfo) => T
1313

1414
const booleanFromBytes: fromBytes<boolean> = (val) => {
1515
return internal.encodingUtil.uint8ArrayToBigInt(val) > 0n
@@ -53,7 +53,7 @@ export const encoders = {
5353
// 'Tuple<*>': tupleFromBytes,
5454
}
5555

56-
export const getEncoder = <T>(typeInfo: GenericTypeInfo): fromBytes<T> => {
56+
export const getEncoder = <T>(typeInfo: TypeInfo): fromBytes<T> => {
5757
const encoder = Object.entries(encoders).find(([k, _]) => new RegExp(`^${k}$`, 'i').test(typeInfo.name))?.[1]
5858
if (!encoder) {
5959
throw new Error(`No encoder found for type ${typeInfo.name}`)

src/impl/base.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Bytes, bytes, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { encodingUtil } from '@algorandfoundation/puya-ts'
3-
import type { GenericTypeInfo } from '../encoders'
3+
import type { TypeInfo } from '../encoders'
44

55
export abstract class BytesBackedCls {
66
#value: bytes
@@ -9,16 +9,12 @@ export abstract class BytesBackedCls {
99
get bytes() {
1010
return this.#value
1111
}
12-
constructor(value: bytes, _typeInfo?: GenericTypeInfo) {
12+
constructor(value: bytes, _typeInfo?: TypeInfo) {
1313
this.#value = value
1414
// this.#typeInfo = typeInfo
1515
}
1616

17-
static fromBytes<T extends BytesBackedCls>(
18-
this: { new (v: bytes, typeInfo?: GenericTypeInfo): T },
19-
value: Uint8Array,
20-
typeInfo?: GenericTypeInfo,
21-
) {
17+
static fromBytes<T extends BytesBackedCls>(this: { new (v: bytes, typeInfo?: TypeInfo): T }, value: Uint8Array, typeInfo?: TypeInfo) {
2218
return new this(Bytes(value), typeInfo)
2319
}
2420
}

src/impl/box.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const Box: internal.opTypes.BoxType = {
1414
if (lazyContext.ledger.boxExists(app, name)) {
1515
return false
1616
}
17-
lazyContext.ledger.setBox(app, name, new Uint8Array(Array(size).fill(0)))
17+
lazyContext.ledger.setBox(app, name, new Uint8Array(size))
1818
return true
1919
},
2020
delete(a: internal.primitives.StubBytesCompat): boolean {
@@ -89,7 +89,7 @@ export const Box: internal.opTypes.BoxType = {
8989
const size = boxContent.length
9090
let updatedContent
9191
if (newSize > size) {
92-
updatedContent = conactUint8Arrays(boxContent, new Uint8Array(Array(newSize - size).fill(0)))
92+
updatedContent = conactUint8Arrays(boxContent, new Uint8Array(newSize - size))
9393
} else {
9494
updatedContent = boxContent.slice(0, newSize)
9595
}
@@ -120,7 +120,7 @@ export const Box: internal.opTypes.BoxType = {
120120
if (updatedContent.length > size) {
121121
updatedContent = updatedContent.slice(0, size)
122122
} else if (updatedContent.length < size) {
123-
updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(Array(size - asNumber(updatedContent.length)).fill(0)))
123+
updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(size - asNumber(updatedContent.length)))
124124
}
125125
lazyContext.ledger.setBox(app, name, updatedContent)
126126
},

src/impl/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const bzero = (a: internal.primitives.StubUint64Compat): bytes => {
5252
if (size > MAX_BYTES_SIZE) {
5353
internal.errors.avmError('bzero attempted to create a too large string')
5454
}
55-
return Bytes(new Uint8Array(Array(Number(size)).fill(0x00)))
55+
return Bytes(new Uint8Array(Number(size)))
5656
}
5757

5858
export const concat = (a: internal.primitives.StubBytesCompat, b: internal.primitives.StubBytesCompat): bytes => {

src/impl/state.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { AccountMap } from '../collections/custom-key-map'
1818
import { MAX_BOX_SIZE } from '../constants'
1919
import { lazyContext } from '../context-helpers/internal-context'
20-
import { GenericTypeInfo, getEncoder } from '../encoders'
20+
import { getEncoder, TypeInfo } from '../encoders'
2121
import { getGenericTypeInfo } from '../runtime-helpers'
2222
import { asBytes, asBytesCls, asNumber, asUint8Array, conactUint8Arrays, toBytes } from '../util'
2323

@@ -131,7 +131,7 @@ export class BoxCls<TValue> {
131131

132132
private get fromBytes() {
133133
const typeInfo = getGenericTypeInfo(this)
134-
const valueType = (typeInfo!.genericArgs! as GenericTypeInfo[])[0]
134+
const valueType = (typeInfo!.genericArgs! as TypeInfo[])[0]
135135
return (val: Uint8Array) => getEncoder<TValue>(valueType)(val, valueType)
136136
}
137137

@@ -199,7 +199,7 @@ export class BoxMapCls<TKey, TValue> {
199199

200200
private get fromBytes() {
201201
const typeInfo = getGenericTypeInfo(this)
202-
const valueType = (typeInfo!.genericArgs! as GenericTypeInfo[])[1]
202+
const valueType = (typeInfo!.genericArgs! as TypeInfo[])[1]
203203
return (val: Uint8Array) => getEncoder<TValue>(valueType)(val, valueType)
204204
}
205205

@@ -320,7 +320,7 @@ export class BoxRefCls {
320320
if (this.exists) {
321321
return false
322322
}
323-
this.backingValue = new Uint8Array(Array(size).fill(0))
323+
this.backingValue = new Uint8Array(size)
324324
return true
325325
}
326326

@@ -354,7 +354,7 @@ export class BoxRefCls {
354354
if (updatedContent.length > content.length) {
355355
updatedContent = updatedContent.slice(0, content.length)
356356
} else if (updatedContent.length < content.length) {
357-
updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(Array(content.length - updatedContent.length).fill(0)))
357+
updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(content.length - updatedContent.length))
358358
}
359359
this.backingValue = updatedContent
360360
}
@@ -404,7 +404,7 @@ export class BoxRefCls {
404404
}
405405
let updatedContent
406406
if (newSizeNumber > content.length) {
407-
updatedContent = conactUint8Arrays(content, new Uint8Array(Array(newSizeNumber - content.length).fill(0)))
407+
updatedContent = conactUint8Arrays(content, new Uint8Array(newSizeNumber - content.length))
408408
} else {
409409
updatedContent = content.slice(0, newSize)
410410
}

src/runtime-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { internal } from '@algorandfoundation/algorand-typescript'
22
import { MAX_UINT64 } from './constants'
3-
import type { GenericTypeInfo } from './encoders'
3+
import type { TypeInfo } from './encoders'
44
import { DeliberateAny } from './typescript-helpers'
55
import { nameOfType } from './util'
66

@@ -274,12 +274,12 @@ function defaultUnaryOp(_operand: DeliberateAny, op: UnaryOps): DeliberateAny {
274274
internal.errors.internalError(`Unsupported operator ${op}`)
275275
}
276276

277-
const genericTypeMap = new Map<DeliberateAny, GenericTypeInfo>()
277+
const genericTypeMap = new WeakMap<DeliberateAny, TypeInfo>()
278278
export function captureGenericTypeInfo(target: DeliberateAny, t: string) {
279279
genericTypeMap.set(target, JSON.parse(t))
280280
return target
281281
}
282282

283-
export function getGenericTypeInfo(target: DeliberateAny): GenericTypeInfo | undefined {
283+
export function getGenericTypeInfo(target: DeliberateAny): TypeInfo | undefined {
284284
return genericTypeMap.get(target)
285285
}

src/subcontexts/contract-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Account, Application, Asset, BaseContract, Bytes, bytes, Contract, Loca
22
import { getAbiMetadata } from '../abi-metadata'
33
import { BytesMap } from '../collections/custom-key-map'
44
import { lazyContext } from '../context-helpers/internal-context'
5-
import type { GenericTypeInfo } from '../encoders'
5+
import type { TypeInfo } from '../encoders'
66
import { AccountCls } from '../impl/account'
77
import { ApplicationCls } from '../impl/application'
88
import { AssetCls } from '../impl/asset'
@@ -32,7 +32,7 @@ interface States {
3232
totals: StateTotals
3333
}
3434

35-
const isUint64GenericType = (typeInfo: GenericTypeInfo | undefined) => {
35+
const isUint64GenericType = (typeInfo: TypeInfo | undefined) => {
3636
if (!Array.isArray(typeInfo?.genericArgs) || !typeInfo?.genericArgs?.length) return false
3737
return typeInfo.genericArgs.some((t) => t.name.toLocaleLowerCase() === 'uint64')
3838
}

src/test-transformer/visitors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { anyPType, ContractClassPType, FunctionPType, PType, SourceLocation, typeRegistry, TypeResolver } from '@algorandfoundation/puya-ts'
22
import ts from 'typescript'
3-
import type { GenericTypeInfo } from '../encoders'
3+
import type { TypeInfo } from '../encoders'
44
import { DeliberateAny } from '../typescript-helpers'
55
import { TransformerConfig } from './index'
66
import { nodeFactory } from './node-factory'
@@ -187,8 +187,8 @@ class ClassVisitor {
187187
}
188188
}
189189

190-
const getGenericTypeInfo = (type: PType): GenericTypeInfo => {
191-
let genericArgs: GenericTypeInfo[] | Record<string, GenericTypeInfo> | undefined = typeRegistry.isGeneric(type)
190+
const getGenericTypeInfo = (type: PType): TypeInfo => {
191+
let genericArgs: TypeInfo[] | Record<string, TypeInfo> | undefined = typeRegistry.isGeneric(type)
192192
? type.getGenericArgs().map(getGenericTypeInfo)
193193
: undefined
194194

@@ -204,7 +204,7 @@ const getGenericTypeInfo = (type: PType): GenericTypeInfo => {
204204
}
205205
}
206206

207-
const result: GenericTypeInfo = { name: type?.name ?? 'unknown' }
207+
const result: TypeInfo = { name: type?.name ?? 'unknown' }
208208
if (genericArgs && (genericArgs.length || Object.keys(genericArgs).length)) {
209209
result.genericArgs = genericArgs
210210
}

tests/primitives/bytes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('Bytes', async () => {
170170

171171
describe.each([MAX_BYTES_SIZE + 1, MAX_BYTES_SIZE * 2])('value overflows', (size) => {
172172
it(`${size} bytes`, () => {
173-
const a = new Uint8Array(size).fill(0)
173+
const a = new Uint8Array(size)
174174
expect(() => Bytes(a)).toThrow(/Bytes length \d+ exceeds maximum length/)
175175
})
176176
})

tests/references/asset.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Asset', () => {
5656
unitName: asBytes('TEST'),
5757
name: asBytes('Test Asset'),
5858
url: asBytes('https://test.com'),
59-
metadataHash: Bytes(new Uint8Array(Array(32).fill(0x00))),
59+
metadataHash: Bytes(new Uint8Array(32)),
6060
manager: Account(),
6161
freeze: Account(),
6262
clawback: Account(),

0 commit comments

Comments
 (0)