'use strict'

function solidityLocals (vmtraceIndex, internalTreeCall, stack, memory, storage) {
  var scope = internalTreeCall.findScope(vmtraceIndex)
  if (!scope) {
    var error = { 'message': 'Can\'t display locals. reason: compilation result might not have been provided' }
    throw error
  }
  var locals = {}
  memory = formatMemory(memory)
  for (var local in scope.locals) {
    let variable = scope.locals[local]
    if (variable.stackDepth < stack.length) {
      locals[variable.name] = variable.type.decodeFromStack(variable.stackDepth, stack, memory, storage)
    }
  }
  return locals
}

function formatMemory (memory) {
  if (memory instanceof Array) {
    memory = memory.join('').replace(/0x/g, '')
  }
  return memory
}

module.exports = {
  solidityLocals: solidityLocals
}

'use strict'

function solidityLocals (vmtraceIndex, internalTreeCall, stack, memory, storage) {
  var scope = internalTreeCall.findScope(vmtraceIndex)
  if (!scope) {
    var error = { 'message': 'Can\'t display locals. reason: compilation result might not have been provided' }
    throw error
  }
  var locals = {}
  memory = formatMemory(memory)
  var anonymousIncr = 1
  for (var local in scope.locals) {
    let variable = scope.locals[local]
    if (variable.stackDepth < stack.length) {
      var name = variable.name
      if (name === '') {
        name = '<' + anonymousIncr + '>'
        anonymousIncr++
      }
      locals[name] = variable.type.decodeFromStack(variable.stackDepth, stack, memory, storage)
    }
  }
  return locals
}

function formatMemory (memory) {
  if (memory instanceof Array) {
    memory = memory.join('').replace(/0x/g, '')
  }
  return memory
}

module.exports = {
  solidityLocals: solidityLocals
}

set <n> for the name if anonymous