From 2d28606ca4b51e2bd165aae2d530f7c897190c1e Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 14 Dec 2016 13:25:17 +0100 Subject: [PATCH] decodeLocals, decodeFromMemory Struct --- src/solidity/types/Struct.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/solidity/types/Struct.js b/src/solidity/types/Struct.js index 172d919d01..666c45a02e 100644 --- a/src/solidity/types/Struct.js +++ b/src/solidity/types/Struct.js @@ -20,4 +20,29 @@ Struct.prototype.decodeFromStorage = function (location, storageContent) { return ret } +Struct.prototype.decodeLocals = function (stackHeight, stack, memory) { + if (stack.length - 1 < stackHeight) { + return {} + } else { // TODO manage decoding locals from storage + var offset = stack[stack.length - 1 - stackHeight] + offset = 2 * parseInt(offset, 16) + return this.decodeFromMemory(offset, memory) + } +} + +Struct.prototype.decodeFromMemory = function (offset, memory) { + var ret = {} + this.members.map(function (item, i) { + var contentOffset = offset + if (item.type.typeName === 'bytes' || item.type.typeName === 'string' || item.type.typeName === 'array' || item.type.typeName === 'struct') { + contentOffset = memory.substr(offset, 64) + contentOffset = 2 * parseInt(contentOffset, 16) + } + var member = item.type.decodeFromMemory(contentOffset, memory) + ret[item.name] = member + offset += 64 + }) + return ret +} + module.exports = Struct