Skip to content

Class BSV::Script::Stack

Inherits: Object

Script execution stack providing push/pop/peek operations for bytes, integers ({ScriptNumber}), and booleans.

Implements Forth-like stack manipulation operations (dup, drop, swap, rot, over, pick, roll, tuck) parameterised by count for the multi-element opcodes (OP_2DUP, OP_2SWAP, etc.).

A 32 MB aggregate memory cap is enforced on every push, matching the ts-sdk behaviour. This provides a natural bound for O(n²) opcodes such as OP_MUL operating on large script numbers.

Constants

MAX_MEMORY_BYTES

Maximum total bytes held across all stack items (32 MB).

Public Class Methods

cast_bool(bytes)

Bitcoin consensus boolean: false if empty, all-zero, or negative zero (0x80 last byte).

Public Instance Methods

clear()

Not documented.

depth()

--- Info ---

drop_n(count)

Remove the top N items.

dup_n(count)

Duplicate the top N items.

empty?()

  • @return [Boolean]

initialize()

  • @return [Stack] a new instance of Stack

nip_n(idx)

Remove item at offset idx from top (0 = top).

over_n(count)

Copy N items from 2N depth to top. OP_OVER (n=1): [x1 x2] -> [x1 x2 x1] OP_2OVER (n=2): [x1 x2 x3 x4] -> [x1 x2 x3 x4 x1 x2]

peek_bool(idx = 0)

Not documented.

peek_bytes(idx = 0)

--- Peek ---

peek_int(idx = 0, max_length: = ScriptNumber::MAX_BYTE_LENGTH, require_minimal: = true)

Decode a stack item as a {ScriptNumber} without removing it. - @param idx [Integer] depth from the top (0 = top). - @param max_length [Integer] maximum allowed byte length. Defaults to {ScriptNumber::MAX_BYTE_LENGTH} (750,000 bytes), matching Bitcoin's +nMaxNumSize+ constant. - @param require_minimal [Boolean] whether to enforce minimal encoding. Defaults to +true+ — post-Genesis BSV requires minimal encoding.

pick_n(idx)

Copy item at index n to top (0 = top).

pop_bool()

Not documented.

pop_bytes()

--- Pop ---

pop_int(max_length: = ScriptNumber::MAX_BYTE_LENGTH, require_minimal: = true)

Decode the top stack item as a {ScriptNumber}. - @param max_length [Integer] maximum allowed byte length. Defaults to {ScriptNumber::MAX_BYTE_LENGTH} (750,000 bytes), matching Bitcoin's +nMaxNumSize+ constant. Post-Genesis BSV makes this configurable at the node level; the SDK uses the standard default. - @param require_minimal [Boolean] whether to enforce minimal encoding. Defaults to +true+ — post-Genesis BSV requires minimal encoding for script numbers.

push_bool(val)

Not documented.

push_bytes(data)

--- Push ---

push_int(script_number)

Not documented.

roll_n(idx)

Move item at index n to top (0 = top). Memory usage is unchanged.

rot_n(count)

Rotate: move the bottom N of the top 3N items to the top. OP_ROT (n=1): [x1 x2 x3] -> [x2 x3 x1] OP_2ROT (n=2): [x1 x2 x3 x4 x5 x6] -> [x3 x4 x5 x6 x1 x2]

swap_n(count)

Swap the top N items with the next N. OP_SWAP (n=1): [x1 x2] -> [x2 x1] OP_2SWAP (n=2): [x1 x2 x3 x4] -> [x3 x4 x1 x2]

to_a()

Not documented.

tuck()

Copy top and insert before second: [x1 x2] -> [x2 x1 x2]