Skip to content

Commit

Permalink
less mutable state
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Feb 16, 2025
1 parent 9e09027 commit e928049
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
11 changes: 4 additions & 7 deletions src/decompiler/operand-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {Instruction, Operand} from "../spec/tvm-spec"
import {removeCompletionTag} from "../utils/binutils"
import {PrefixMatcher} from "../utils/prefix-matcher"
import {DisassemblerError} from "./errors"
import {repeat} from "../utils/tricks"

type ExtractType<T extends Operand["type"], A = Operand> = A extends {type: T} ? A : never

Expand Down Expand Up @@ -147,17 +148,13 @@ function parseInstruction(source: Cell, operand: Operand, slice: Slice): Operand
const offsetBits = slice.offsetBits
const offsetRefs = slice.offsetRefs

let bits = slice.loadBits(bitLength)
if (operand.completion_tag) {
bits = removeCompletionTag(bits)
}
const loadedBits = slice.loadBits(bitLength)
const bits = operand.completion_tag ? removeCompletionTag(loadedBits) : loadedBits

const builder = new Builder()
builder.storeBits(bits)

for (let i = 0; i < refLength; i++) {
builder.storeRef(slice.loadRef())
}
repeat(refLength, () => builder.storeRef(slice.loadRef()))

return {
type: "subslice",
Expand Down
16 changes: 7 additions & 9 deletions src/utils/binutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ export const prefixToBin = (prefix: string): BitString => {
}

export const removeCompletionTag = (bits: BitString): BitString => {
let newLength = -1
for (let i = bits.length; i > 0; i--) {
if (bits.at(i - 1)) {
newLength = i - 1
break
}
}
if (newLength === -1) {
const lastSetBitIndex = Array.from({length: bits.length}).findIndex((_, i): boolean =>
bits.at(bits.length - 1 - i),
)

if (lastSetBitIndex === -1) {
throw new Error("no completion tag")
}
return bits.substring(0, newLength)

return bits.substring(0, bits.length - lastSetBitIndex - 1)
}
9 changes: 3 additions & 6 deletions src/utils/subcell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {beginCell, Cell, Slice} from "@ton/core"
import {repeat} from "./tricks"

export function subslice(args: {
cell: Cell
Expand All @@ -12,17 +13,13 @@ export function subslice(args: {

// Copy bits and refs
b.storeBits(s.loadBits(args.bits + args.offsetBits))
for (let i = 0; i < args.refs + args.offsetRefs; i++) {
b.storeRef(s.loadRef())
}
repeat(args.refs + args.offsetRefs, () => b.storeRef(s.loadRef()))

const s2 = b.endCell().beginParse()

// Skip bits and refs
s2.skip(args.offsetBits)
for (let i = 0; i < args.offsetRefs; i++) {
s2.loadRef()
}
repeat(args.offsetRefs, () => s2.loadRef())

return s2
}
5 changes: 5 additions & 0 deletions src/utils/tricks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const repeat = (length: number, callback: () => void): void => {
for (let i = 0; i < length; i++) {
callback()
}
}

0 comments on commit e928049

Please sign in to comment.