'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
}

@@ -343,16 +343,40 @@
 memory)%0A
+  var anonymousIncr = 1%0A
   for (v
@@ -399,24 +399,24 @@
 e.locals) %7B%0A
-
     let vari
@@ -484,24 +484,153 @@
 k.length) %7B%0A
+      var name = variable.name%0A      if (name === '') %7B%0A        name = '%3C' + anonymousIncr + '%3E'%0A        anonymousIncr++%0A      %7D%0A
       locals
@@ -630,25 +630,16 @@
  locals%5B
-variable.
 name%5D = 

set <n> for the name if anonymous