Skip to content

Commit

Permalink
Merge pull request #35 from algorandfoundation/feat/extract-op-nuances
Browse files Browse the repository at this point in the history
feat: allow extracting to the end by omitting length parameter for `extract` op code
  • Loading branch information
boblat authored Jan 20, 2025
2 parents d610b71 + 903d595 commit 5e6478b
Show file tree
Hide file tree
Showing 9 changed files with 1,280 additions and 1,286 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"tslib": "^2.6.2"
},
"dependencies": {
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.6",
"@algorandfoundation/puya-ts": "^1.0.0-beta.10",
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.7",
"@algorandfoundation/puya-ts": "^1.0.0-beta.11",
"elliptic": "^6.5.7",
"js-sha256": "^0.11.0",
"js-sha3": "^0.9.3",
Expand Down
17 changes: 8 additions & 9 deletions src/impl/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,29 @@ export const expw = (a: internal.primitives.StubUint64Compat, b: internal.primit
return toUint128(base ** exponent)
}

export const extract = (
type ExtractType = ((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat) => bytes) &
((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubUint64Compat) => bytes)
export const extract = ((
a: internal.primitives.StubBytesCompat,
b: internal.primitives.StubUint64Compat,
c: internal.primitives.StubUint64Compat,
c?: internal.primitives.StubUint64Compat,
): bytes => {
const bytesValue = internal.primitives.BytesCls.fromCompat(a)
const bytesLength = bytesValue.length.asBigInt()

const start = internal.primitives.Uint64Cls.fromCompat(b).asBigInt()
const length = internal.primitives.Uint64Cls.fromCompat(c).asBigInt()
let end = start + length
if ((typeof b === 'number' || typeof b === 'bigint') && (typeof c === 'number' || typeof c === 'bigint') && length === 0n) {
end = bytesLength
}
const length = c !== undefined ? internal.primitives.Uint64Cls.fromCompat(c).asBigInt() : undefined
const end = length !== undefined ? start + length : undefined

if (start > bytesLength) {
internal.errors.codeError(`extraction start ${start} is beyond length`)
}
if (end > bytesLength) {
if (end !== undefined && end > bytesLength) {
internal.errors.codeError(`extraction end ${end} is beyond length`)
}

return bytesValue.slice(start, end).asAlgoTs()
}
}) as ExtractType

export const extractUint16 = (a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): uint64 => {
const result = extract(a, b, 2)
Expand Down
3 changes: 1 addition & 2 deletions tests/artifacts/miscellaneous-ops/contract.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ export class MiscellaneousOpsContract extends arc4.Contract {
return result
}

// TODO: recompile to check if this results in correct TEAL code
@arc4.abimethod()
public verify_extract_from_2(a: bytes): bytes {
const result = op.extract(a, 2, 0)
const result = op.extract(a, 2)
return result
}

Expand Down
Loading

0 comments on commit 5e6478b

Please sign in to comment.