Skip to content

Commit 5e6478b

Browse files
authored
Merge pull request #35 from algorandfoundation/feat/extract-op-nuances
feat: allow extracting to the end by omitting length parameter for `extract` op code
2 parents d610b71 + 903d595 commit 5e6478b

File tree

9 files changed

+1280
-1286
lines changed

9 files changed

+1280
-1286
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
"tslib": "^2.6.2"
6464
},
6565
"dependencies": {
66-
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.6",
67-
"@algorandfoundation/puya-ts": "^1.0.0-beta.10",
66+
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.7",
67+
"@algorandfoundation/puya-ts": "^1.0.0-beta.11",
6868
"elliptic": "^6.5.7",
6969
"js-sha256": "^0.11.0",
7070
"js-sha3": "^0.9.3",

src/impl/pure.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,29 @@ export const expw = (a: internal.primitives.StubUint64Compat, b: internal.primit
103103
return toUint128(base ** exponent)
104104
}
105105

106-
export const extract = (
106+
type ExtractType = ((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat) => bytes) &
107+
((a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubUint64Compat) => bytes)
108+
export const extract = ((
107109
a: internal.primitives.StubBytesCompat,
108110
b: internal.primitives.StubUint64Compat,
109-
c: internal.primitives.StubUint64Compat,
111+
c?: internal.primitives.StubUint64Compat,
110112
): bytes => {
111113
const bytesValue = internal.primitives.BytesCls.fromCompat(a)
112114
const bytesLength = bytesValue.length.asBigInt()
113115

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

121120
if (start > bytesLength) {
122121
internal.errors.codeError(`extraction start ${start} is beyond length`)
123122
}
124-
if (end > bytesLength) {
123+
if (end !== undefined && end > bytesLength) {
125124
internal.errors.codeError(`extraction end ${end} is beyond length`)
126125
}
127126

128127
return bytesValue.slice(start, end).asAlgoTs()
129-
}
128+
}) as ExtractType
130129

131130
export const extractUint16 = (a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): uint64 => {
132131
const result = extract(a, b, 2)

tests/artifacts/miscellaneous-ops/contract.algo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ export class MiscellaneousOpsContract extends arc4.Contract {
9595
return result
9696
}
9797

98-
// TODO: recompile to check if this results in correct TEAL code
9998
@arc4.abimethod()
10099
public verify_extract_from_2(a: bytes): bytes {
101-
const result = op.extract(a, 2, 0)
100+
const result = op.extract(a, 2)
102101
return result
103102
}
104103

0 commit comments

Comments
 (0)