Commit e8faaf52 authored by yann300's avatar yann300

change regex

parent f75fb461
...@@ -109,27 +109,19 @@ function ArrayType (type, stateDefinitions) { ...@@ -109,27 +109,19 @@ function ArrayType (type, stateDefinitions) {
var arraySize var arraySize
var storageBytes var storageBytes
var underlyingType = extractUnderlyingType(type) var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/)
if (!match) {
arraySize = extractArraySize(type) return null
var dimensions = extractArrayInfo(type)
type = underlyingType + dimensions.join('')
var subArrayType = type.substring(0, type.lastIndexOf('['))
var subArray = null
if (subArrayType.indexOf('[') !== -1) {
subArray = decode(subArrayType, stateDefinitions)
} }
underlyingType = decode(underlyingType, stateDefinitions) arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2])
storageBytes = underlyingType.storageBytes
var underlyingType = decode(match[1], stateDefinitions)
if (arraySize === 'dynamic') { if (arraySize === 'dynamic') {
storageBytes = 32 storageBytes = 32
} else { } else {
if (subArray) { storageBytes = underlyingType.storageBytes
storageBytes = subArray.storageBytes
}
if (storageBytes > 32) { if (storageBytes > 32) {
storageBytes = 32 * arraySize * Math.ceil(storageBytes / 32) storageBytes = 32 * arraySize * Math.ceil(storageBytes / 32)
} else { } else {
...@@ -142,7 +134,7 @@ function ArrayType (type, stateDefinitions) { ...@@ -142,7 +134,7 @@ function ArrayType (type, stateDefinitions) {
storageBytes: storageBytes, storageBytes: storageBytes,
typeName: type, typeName: type,
arraySize: arraySize, arraySize: arraySize,
subArray: subArray underlyingType: underlyingType
} }
} }
...@@ -175,9 +167,11 @@ function Enum (type, stateDefinitions) { ...@@ -175,9 +167,11 @@ function Enum (type, stateDefinitions) {
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, members} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, members}
*/ */
function Struct (type, stateDefinitions) { function Struct (type, stateDefinitions) {
var extracted = type.split(' ') var match = type.match(/struct (.*?)( storage ref| storage pointer| memory| calldata)?$/)
type = 'struct ' + extracted[1] // the second item is the name of the struct definition (newly declared type) if (!match) {
var memberDetails = getStructMembers(type, stateDefinitions) // type is used to extract the ast struct definition return null
}
var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition
return { return {
needsFreeStorageSlot: true, needsFreeStorageSlot: true,
storageBytes: memberDetails.storageBytes, storageBytes: memberDetails.storageBytes,
...@@ -215,7 +209,7 @@ function getStructMembers (typeName, stateDefinitions) { ...@@ -215,7 +209,7 @@ function getStructMembers (typeName, stateDefinitions) {
var storageBytes = 0 var storageBytes = 0
for (var k in stateDefinitions) { for (var k in stateDefinitions) {
var dec = stateDefinitions[k] var dec = stateDefinitions[k]
if (dec.name === 'StructDefinition' && typeName.indexOf('struct ' + dec.attributes.name) === 0) { if (dec.name === 'StructDefinition' && typeName === dec.attributes.name) {
for (var i in dec.children) { for (var i in dec.children) {
var member = dec.children[i] var member = dec.children[i]
var decoded = decode(member.attributes.type, stateDefinitions) var decoded = decode(member.attributes.type, stateDefinitions)
...@@ -235,68 +229,13 @@ function getStructMembers (typeName, stateDefinitions) { ...@@ -235,68 +229,13 @@ function getStructMembers (typeName, stateDefinitions) {
} }
/** /**
* return the size of the current array
*
* @param {String} typeName - short type ( e.g uint[][4] )
* @return {String|Int} return 'dynamic' if dynamic array | return size of the array
*/
function extractArraySize (typeName) {
if (typeName.indexOf('[') !== -1) {
var squareBracket = /\[[^\]]*\]/g // /\[([0-9]+|\s*)\]/g
var dim = typeName.match(squareBracket)
var size = dim[dim.length - 1]
if (size === '[]') {
return 'dynamic'
} else {
return parseInt(dim[dim.length - 1].replace('[', '').replace(']'))
}
}
}
/**
* extract the underlying type
*
* @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2])
* @return {String} return the first part of the full type. do not keep the array declaration ( uint[2] storage ref[2] will return uint)
*/
function extractUnderlyingType (fullType) {
var split = fullType.split(' ')
if (fullType.indexOf('enum') === 0 || fullType.indexOf('struct') === 0) {
return split[0] + ' ' + split[1]
}
if (split.length > 0) {
fullType = split[0]
}
if (fullType[fullType.length - 1] === ']') {
return fullType.substring(0, fullType.indexOf('['))
}
return fullType
}
/**
* get the array dimensions
*
* @param {String} fullType - type given by the AST
* @return {Array} containing all the dimensions and size of the array (e.g ['[3]', '[]'] )
*/
function extractArrayInfo (fullType) {
var ret = []
if (fullType.indexOf('[') !== -1) {
var squareBracket = /\[([0-9]+|\s*)\]/g
var dim = fullType.match(squareBracket)
return dim
}
return ret
}
/**
* parse the full type * parse the full type
* *
* @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2]) * @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2])
* @return {String} returns the token type (used to instanciate the right decoder) (uint[2] storage ref[2] will return 'array', uint256 will return uintX) * @return {String} returns the token type (used to instanciate the right decoder) (uint[2] storage ref[2] will return 'array', uint256 will return uintX)
*/ */
function typeClass (fullType) { function typeClass (fullType) {
if (fullType.indexOf('[') !== -1) { if (fullType.indexOf(']') !== -1) {
return 'array' return 'array'
} }
if (fullType.indexOf(' ') !== -1) { if (fullType.indexOf(' ') !== -1) {
......
...@@ -19,19 +19,19 @@ tape('solidity', function (t) { ...@@ -19,19 +19,19 @@ tape('solidity', function (t) {
stateDec = index.solidity.astHelper.extractStateVariables('contractStructAndArray', output.sources) stateDec = index.solidity.astHelper.extractStateVariables('contractStructAndArray', output.sources)
decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 64, 'struct structDef') checkDecodeInfo(st, decodeInfo, true, 64, 'struct structDef storage ref')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 192, 'struct structDef[3]') checkDecodeInfo(st, decodeInfo, true, 192, 'struct structDef storage ref[3] storage ref')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[3].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[3].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 64, 'bytes12[4]') checkDecodeInfo(st, decodeInfo, true, 64, 'bytes12[4] storage ref')
stateDec = index.solidity.astHelper.extractStateVariables('contractArray', output.sources) stateDec = index.solidity.astHelper.extractStateVariables('contractArray', output.sources)
decodeInfo = index.solidity.decodeInfo.decode(stateDec[0].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[0].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 4 * 5, 'uint32[5]') checkDecodeInfo(st, decodeInfo, true, 4 * 5, 'uint32[5] storage ref')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 32, 'int8[]') checkDecodeInfo(st, decodeInfo, true, 32, 'int8[] storage ref')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 4 * 32, 'int16[][3][][4]') checkDecodeInfo(st, decodeInfo, true, 4 * 32, 'int16[] storage ref[3] storage ref[] storage ref[4] storage ref')
stateDec = index.solidity.astHelper.extractStateVariables('contractEnum', output.sources) stateDec = index.solidity.astHelper.extractStateVariables('contractEnum', output.sources)
decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment