// RetValueLoaderBox - Unified ret value loading
// Single Responsibility: Load return value from register for ret instruction/terminator

using selfhost.vm.boxes.result_box as Result
using selfhost.vm.hakorune-vm.json_field_extractor as JsonFieldExtractor
using selfhost.vm.hakorune-vm.value_manager as ValueManagerBox
using selfhost.vm.hakorune-vm.core_bridge_ops as CoreBridgeOps

static box RetValueLoaderBox {
  // Load return value from ret instruction (JSON format)
  // inst_json: {"op":"ret","value":X}
  // regs: register MapBox
  // Returns: Result.Ok(ret_value) or Result.Err(message)
  load_from_instruction(inst_json, regs) {
    return CoreBridgeOps.load_ret_from_instruction(inst_json, regs)
  }

  // Load return value from terminator (MapBox format)
  // term_map: MapBox with "type":"ret", "value":X
  // regs: register MapBox
  // Returns: Result.Ok(ret_value) or Result.Err(message)
  load_from_terminator(term_map, regs) {
    // Extract return value register
    local ret_reg = term_map.get("value")
    if ret_reg == null {
      return Result.Err("ret: value field not found in terminator")
    }

    // Load and validate (legacy Map terminator path)
    return me._load_value(ret_reg, regs)
  }

  // Internal: Load value from register with validation
  // ret_reg: register ID (integer)
  // regs: register MapBox
  // Returns: Result.Ok(ret_value) or Result.Err(message)
  _load_value(ret_reg, regs) {
    // Load return value from register
    local ret_value = ValueManagerBox.get(regs, ret_reg)
    if ret_value == null {
      return Result.Err("ret: register v%" + ret_reg + " is unset")
    }

    // Return the actual value
    return Result.Ok(ret_value)
  }
}
