Skip to content

Commit

Permalink
feat: BoxRef.resize
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Oct 11, 2024
1 parent 35d9216 commit 4757566
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 277 deletions.
26 changes: 26 additions & 0 deletions src/awst_build/eb/storage/box/box-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class BoxRefExpressionBuilder extends BoxProxyExpressionBuilder<BoxRefPTy
return new BoxRefSpliceFunctionBuilder(boxValueExpr)
case 'create':
return new BoxRefCreateFunctionBuilder(boxValueExpr)
case 'resize':
return new BoxRefResizeFunctionBuilder(boxValueExpr)
case 'extract':
return new BoxRefExtractFunctionBuilder(boxValueExpr)
case 'replace':
Expand Down Expand Up @@ -116,6 +118,30 @@ export class BoxRefCreateFunctionBuilder extends BoxRefBaseFunctionBuilder {
)
}
}
export class BoxRefResizeFunctionBuilder extends BoxRefBaseFunctionBuilder {
call(args: ReadonlyArray<InstanceBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
const {
args: [size],
} = parseFunctionArgs({
args,
typeArgs,
genericTypeArgs: 0,
funcName: 'BoxRef.resize',
callLocation: sourceLocation,
argSpec: (a) => [a.required(uint64PType)],
})
return instanceEb(
nodeFactory.intrinsicCall({
opCode: 'box_resize',
stackArgs: [this.boxValue, size.resolve()],
wtype: boolWType,
immediates: [],
sourceLocation,
}),
boolPType,
)
}
}
export class BoxRefExtractFunctionBuilder extends BoxRefBaseFunctionBuilder {
call(args: ReadonlyArray<InstanceBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
const {
Expand Down
4 changes: 3 additions & 1 deletion tests/approvals/box-proxies.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ function testBoxMap(box: BoxMap<string, bytes>, key: string, value: bytes) {
const boxRef = BoxRef({ key: 'abc' })

function testBoxRef(box: BoxRef, length: uint64) {
if (!boxRef.exists && boxRef.length !== length) {
if (!boxRef.exists) {
boxRef.create({ size: 1000 })
} else if (boxRef.length !== length) {
boxRef.resize(length)
}
const someBytes = Bytes.fromHex('FFFFFFFF')
box.put(someBytes)
Expand Down
8 changes: 6 additions & 2 deletions tests/approvals/out/box-proxies.awst
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ subroutine testBoxMap(box: box_key, key: string, value: bytes): void
{
concat(box, reinterpret_cast<bytes>(key)).value: bytes = value
concat("", reinterpret_cast<bytes>(key)).value: bytes = value
assert(Boolean(checked_maybe(box_length(concat(box, reinterpret_cast<bytes>(key)).value), comment=Box must exist)))
assert(Boolean(checked_maybe(box_len(concat(box, reinterpret_cast<bytes>(key)).value), comment=Box must exist)))
assert(STATE_GET_EX(concat(box, reinterpret_cast<bytes>(key)).value).1)
assert(concat(box, reinterpret_cast<bytes>(key)).value == concat("", reinterpret_cast<bytes>(key)).value)
STATE_DEL(concat(box, reinterpret_cast<bytes>(key)).value)
assert(STATE_GET(concat(box, reinterpret_cast<bytes>("" + key + "x")).value, default="b") == STATE_GET(concat("", reinterpret_cast<bytes>("" + key + "x")).value, default="b"))
}
subroutine testBoxRef(box: box_key, length: uint64): void
{
if (!box_length(Box["abc"].value).1 and checked_maybe(box_length(Box["abc"].value), comment=Box must exist) != length) {
if (!box_length(Box["abc"].value).1) {
box_create(Box["abc"].value, 1000)
} else {
if (checked_maybe(box_length(Box["abc"].value), comment=Box must exist) != length) {
box_resize(Box["abc"].value, length)
}
}
someBytes: bytes = 0xffffffff
box_put(box.value, someBytes)
Expand Down
Loading

0 comments on commit 4757566

Please sign in to comment.