Skip to main content

encode

Function encode 

Source
pub fn encode(opcode: Opcode, operands: &[usize]) -> Vec<u8> 
Expand description

Encodes a bytecode instruction from an opcode and operands.

All multi-byte operands are encoded in big-endian format.

§Parameters

  • opcode - The operation code for the instruction
  • operands - Slice of operand values (e.g., constant pool indices)

§Returns

A byte vector containing the opcode byte followed by encoded operand bytes.

§Examples

use maat_bytecode::{Opcode, encode};

// Encode OpConstant instruction referencing constant pool index 65534
let instruction = encode(Opcode::Constant, &[65534]);
assert_eq!(instruction, vec![0, 255, 254]);

// Encode OpAdd instruction with no operands
let instruction = encode(Opcode::Add, &[]);
assert_eq!(instruction, vec![1]);