Skip to content

Commit

Permalink
fix: Add missing expression builder for BoxRef.maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Jan 31, 2025
1 parent c59f287 commit 5901a46
Show file tree
Hide file tree
Showing 6 changed files with 503 additions and 315 deletions.
27 changes: 26 additions & 1 deletion src/awst_build/eb/storage/box/box-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { wtypes } from '../../../../awst/wtypes'

import { invariant } from '../../../../util'
import type { PType } from '../../../ptypes'
import { boolPType, BoxRefPType, boxRefType, bytesPType, stringPType, uint64PType, voidPType } from '../../../ptypes'
import { boolPType, BoxRefPType, boxRefType, bytesPType, stringPType, TuplePType, uint64PType, voidPType } from '../../../ptypes'
import { instanceEb } from '../../../type-registry'
import { FunctionBuilder, type NodeBuilder } from '../../index'
import { parseFunctionArgs } from '../../util/arg-parsing'
Expand Down Expand Up @@ -56,6 +56,8 @@ export class BoxRefExpressionBuilder extends BoxProxyExpressionBuilder<BoxRefPTy
return new BoxRefReplaceFunctionBuilder(boxValueExpr)
case 'exists':
return boxExists(boxValueExpr, sourceLocation)
case 'maybe':
return new BoxRefMaybeFunctionBuilder(boxValueExpr)
case 'length': {
return boxLength(boxValueExpr, sourceLocation)
}
Expand Down Expand Up @@ -217,3 +219,26 @@ export class BoxRefSpliceFunctionBuilder extends BoxRefBaseFunctionBuilder {
)
}
}

class BoxRefMaybeFunctionBuilder extends BoxRefBaseFunctionBuilder {
call(args: ReadonlyArray<NodeBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
parseFunctionArgs({
args,
typeArgs,
funcName: 'BoxRef.maybe',
callLocation: sourceLocation,
genericTypeArgs: 0,
argSpec: () => [],
})
const type = new TuplePType({ items: [bytesPType, boolPType] })

return instanceEb(
nodeFactory.stateGetEx({
sourceLocation,
wtype: type.wtype,
field: this.boxValue,
}),
type,
)
}
}
3 changes: 3 additions & 0 deletions tests/approvals/box-proxies.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function testBoxRef(box: BoxRef, length: uint64) {
const someBytes = Bytes.fromHex('FFFFFFFF')
box.put(someBytes)

const maybeBox = box.maybe()
assert(maybeBox[1])

assert(box.value === Bytes.fromHex('FFFFFFFF'))
box.splice(1, 1, Bytes.fromHex('00'))
assert(box.value === Bytes.fromHex('FF00FFFF'))
Expand Down
Loading

0 comments on commit 5901a46

Please sign in to comment.