rename decode -> decodeFromMemory

pull/7/head
yann300 8 years ago
parent 4c09db3707
commit abc5a47925
  1. 4
      src/solidity/types/ArrayType.js
  2. 2
      src/solidity/types/DynamicByteArray.js
  3. 42
      src/solidity/types/RefType.js
  4. 4
      src/solidity/types/StringType.js
  5. 4
      src/solidity/types/Struct.js
  6. 11
      src/solidity/types/ValueType.js

@ -56,7 +56,7 @@ class ArrayType extends RefType {
} }
} }
decodeFromMemory (offset, memory) { decodeFromMemoryInternal (offset, memory) {
var ret = [] var ret = []
var length = this.arraySize var length = this.arraySize
if (this.arraySize === 'dynamic') { if (this.arraySize === 'dynamic') {
@ -66,7 +66,7 @@ class ArrayType extends RefType {
} }
for (var k = 0; k < length; k++) { for (var k = 0; k < length; k++) {
var contentOffset = offset var contentOffset = offset
ret.push(this.underlyingType.decode(contentOffset, memory)) ret.push(this.underlyingType.decodeFromMemory(contentOffset, memory))
offset += 32 offset += 32
} }
return ret return ret

@ -35,7 +35,7 @@ class DynamicByteArray extends RefType {
} }
} }
decodeFromMemory (offset, memory) { decodeFromMemoryInternal (offset, memory) {
offset = 2 * offset offset = 2 * offset
var length = memory.substr(offset, 64) var length = memory.substr(offset, 64)
length = 2 * parseInt(length, 16) length = 2 * parseInt(length, 16)

@ -14,29 +14,39 @@ class RefType {
* *
* @param {Int} stackDepth - position of the type in the stack * @param {Int} stackDepth - position of the type in the stack
* @param {Array} stack - stack * @param {Array} stack - stack
* @return {String} - memory * @param {String} - memory
* @return {Object} - storage * @param {Object} - storage
* @return {Object} decoded value
*/ */
decodeFromStack (stackDepth, stack, memory, storage) { decodeFromStack (stackDepth, stack, memory, storage) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return { error: '<decoding failed - stack underflow ' + stackDepth + '>' } return { error: '<decoding failed - stack underflow ' + stackDepth + '>' }
} }
if (!storage) {
storage = {} // TODO this is a fallback, should manage properly locals store in storage
}
var offset = stack[stack.length - 1 - stackDepth] var offset = stack[stack.length - 1 - stackDepth]
offset = parseInt(offset, 16) offset = parseInt(offset, 16)
return decodeInternal(this, offset, memory, storage) if (this.storageStore()) {
return this.decodeFromStorage({ offset: 0, slot: offset }, storage)
} else if (this.memoryStore()) {
return this.decodeFromMemoryInternal(offset, memory)
} else {
return { error: '<decoding failed - no decoder for ' + this.location + '>' }
}
} }
/** /**
* decode the type with the @arg offset location from the memory or storage * decode the type from the memory
* *
* @param {Int} offset - position of the type in the memory * @param {Int} offset - position of the ref of the type in memory
* @return {String} - memory * @param {String} memory - memory
* @return {Object} - storage * @return {Object} decoded value
*/ */
decode (offset, memory, storage) { decodeFromMemory (offset, memory) {
offset = memory.substr(2 * offset, 64) offset = memory.substr(2 * offset, 64)
offset = parseInt(offset, 16) offset = parseInt(offset, 16)
return decodeInternal(this, offset, memory, storage) return this.decodeFromMemoryInternal(offset, memory)
} }
/** /**
@ -56,20 +66,6 @@ class RefType {
memoryStore () { memoryStore () {
return this.location.indexOf('memory') === 0 return this.location.indexOf('memory') === 0
} }
}
function decodeInternal (self, offset, memory, storage) {
if (!storage) {
storage = {} // TODO this is a fallback, should manage properly locals store in storage
}
if (self.storageStore()) {
return self.decodeFromStorage({ offset: 0, slot: offset }, storage)
} else if (self.memoryStore()) {
return self.decodeFromMemory(offset, memory)
} else {
return { error: '<decoding failed - no decoder for ' + self.location + '>' }
}
} }
module.exports = RefType module.exports = RefType

@ -16,8 +16,8 @@ class StringType extends DynamicBytes {
return super.decodeFromStack(stackDepth, stack, memory) return super.decodeFromStack(stackDepth, stack, memory)
} }
decodeFromMemory (offset, memory) { decodeFromMemoryInternal (offset, memory) {
var decoded = super.decodeFromMemory(offset, memory) var decoded = super.decodeFromMemoryInternal(offset, memory)
return format(decoded) return format(decoded)
} }
} }

@ -20,11 +20,11 @@ class Struct extends RefType {
return ret return ret
} }
decodeFromMemory (offset, memory) { decodeFromMemoryInternal (offset, memory) {
var ret = {} var ret = {}
this.members.map((item, i) => { this.members.map((item, i) => {
var contentOffset = offset var contentOffset = offset
var member = item.type.decode(contentOffset, memory) var member = item.type.decodeFromMemory(contentOffset, memory)
ret[item.name] = member ret[item.name] = member
offset += 32 offset += 32
}) })

@ -48,17 +48,6 @@ class ValueType {
var value = memory.substr(2 * offset, 64) var value = memory.substr(2 * offset, 64)
return this.decodeValue(util.extractHexByteSlice(value, this.storageBytes, 0)) return this.decodeValue(util.extractHexByteSlice(value, this.storageBytes, 0))
} }
/**
* decode the type with the @arg offset location from the memory
*
* @param {Int} offset - position of the type in the memory
* @return {String} - memory
* @return {Object} - storage
*/
decode (offset, memory) {
return this.decodeFromMemory(offset, memory)
}
} }
module.exports = ValueType module.exports = ValueType

Loading…
Cancel
Save