Commit a8d89aa4 authored by yann300's avatar yann300

Add RefType + provide location

parent 60ce2bbb
...@@ -118,7 +118,8 @@ function Array (type, stateDefinitions, contractName) { ...@@ -118,7 +118,8 @@ function Array (type, stateDefinitions, contractName) {
console.log('unable to parse type ' + type) console.log('unable to parse type ' + type)
return null return null
} }
return new ArrayType(underlyingType, arraySize) var location = match[3].trim()
return new ArrayType(underlyingType, arraySize, location)
} }
/** /**
...@@ -149,12 +150,13 @@ function Enum (type, stateDefinitions, contractName) { ...@@ -149,12 +150,13 @@ function Enum (type, stateDefinitions, contractName) {
*/ */
function Struct (type, stateDefinitions, contractName) { function Struct (type, stateDefinitions, contractName) {
var match = type.match(/struct (.*?)( storage ref| storage pointer| memory| calldata)?$/) var match = type.match(/struct (.*?)( storage ref| storage pointer| memory| calldata)?$/)
if (!match) { if (match && match.length > 2) {
var memberDetails = getStructMembers(match[1], stateDefinitions, contractName) // type is used to extract the ast struct definition
if (!memberDetails) return null
return new StructType(memberDetails, match[2].trim())
} else {
return null return null
} }
var memberDetails = getStructMembers(match[1], stateDefinitions, contractName) // type is used to extract the ast struct definition
if (!memberDetails) return null
return new StructType(memberDetails)
} }
/** /**
......
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var BN = require('ethereumjs-util').BN var BN = require('ethereumjs-util').BN
var RefType = require('./RefType')
class ArrayType { class ArrayType extends RefType {
constructor (underlyingType, arraySize) { constructor (underlyingType, arraySize, location) {
this.typeName = 'array' var storageSlots = null
this.storageBytes = 32
this.underlyingType = underlyingType
this.arraySize = arraySize
this.storageSlots = null
if (arraySize === 'dynamic') { if (arraySize === 'dynamic') {
this.storageSlots = 1 storageSlots = 1
} else { } else {
if (underlyingType.storageBytes < 32) { if (underlyingType.storageBytes < 32) {
var itemPerSlot = Math.floor(32 / underlyingType.storageBytes) var itemPerSlot = Math.floor(32 / underlyingType.storageBytes)
this.storageSlots = Math.ceil(arraySize / itemPerSlot) storageSlots = Math.ceil(arraySize / itemPerSlot)
} else { } else {
this.storageSlots = arraySize * underlyingType.storageSlots storageSlots = arraySize * underlyingType.storageSlots
} }
} }
super(storageSlots, 32, 'array', location)
this.underlyingType = underlyingType
this.arraySize = arraySize
} }
decodeFromStorage (location, storageContent) { decodeFromStorage (location, storageContent) {
...@@ -56,16 +56,6 @@ class ArrayType { ...@@ -56,16 +56,6 @@ class ArrayType {
} }
} }
decodeFromStack (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) {
return []
} else { // TODO manage decoding locals from storage
var offset = stack[stack.length - 1 - stackDepth]
offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory)
}
}
decodeFromMemory (offset, memory) { decodeFromMemory (offset, memory) {
var ret = [] var ret = []
var length = extractLength(this, offset, memory) var length = extractLength(this, offset, memory)
......
'use strict'
var util = require('./util')
class RefType {
constructor (storageSlots, storageBytes, typeName, location) {
this.location = location
this.storageSlots = storageSlots
this.storageBytes = storageBytes
this.typeName = typeName
}
decodeFromStack (stackDepth, stack, memory, storage) {
if (stack.length - 1 < stackDepth) {
return []
}
var offset = stack[stack.length - 1 - stackDepth]
offset = 2 * parseInt(offset, 16)
if (util.storageStore(this)) {
return this.decodeFromStorage(offset, storage)
} else if (util.memoryStore(this)) {
return this.decodeFromMemory(offset, memory)
} else {
return '<decoding failed - no decoder for ' + this.location + '>'
}
}
}
module.exports = RefType
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var RefType = require('./RefType')
class Struct { class Struct extends RefType {
constructor (memberDetails) { constructor (memberDetails, location) {
this.storageSlots = memberDetails.storageSlots super(memberDetails.storageSlots, 32, 'struct', location)
this.storageBytes = 32
this.members = memberDetails.members this.members = memberDetails.members
this.typeName = 'struct'
} }
decodeFromStorage (location, storageContent) { decodeFromStorage (location, storageContent) {
...@@ -21,16 +20,6 @@ class Struct { ...@@ -21,16 +20,6 @@ class Struct {
return ret return ret
} }
decodeFromStack (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) {
return {}
} else { // TODO manage decoding locals from storage
var offset = stack[stack.length - 1 - stackDepth]
offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory)
}
}
decodeFromMemory (offset, memory) { decodeFromMemory (offset, memory) {
var ret = {} var ret = {}
this.members.map(function (item, i) { this.members.map(function (item, i) {
......
...@@ -10,7 +10,9 @@ module.exports = { ...@@ -10,7 +10,9 @@ module.exports = {
extractHexByteSlice: extractHexByteSlice, extractHexByteSlice: extractHexByteSlice,
sha3: sha3, sha3: sha3,
toBN: toBN, toBN: toBN,
add: add add: add,
storageStore: storageStore,
memoryStore: memoryStore
} }
function decodeInt (location, storageContent, byteLength, signed) { function decodeInt (location, storageContent, byteLength, signed) {
...@@ -91,3 +93,11 @@ function toBN (value) { ...@@ -91,3 +93,11 @@ function toBN (value) {
function add (value1, value2) { function add (value1, value2) {
return toBN(value1).add(toBN(value2)) return toBN(value1).add(toBN(value2))
} }
function storageStore (type) {
return type.location.indexOf('storage') === 0
}
function memoryStore (type) {
return type.location.indexOf('memory') === 0
}
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