Commit e7c45d29 authored by yann300's avatar yann300

use es6 class for type

parent d2da5fd6
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function Address () { class Address extends ValueType {
this.storageSlots = 1 constructor () {
this.storageBytes = 20 super(1, 20, 'address')
this.typeName = 'address' }
}
Address.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes) var value = util.extractHexValue(location, storageContent, this.storageBytes)
return '0x' + value.toUpperCase() return '0x' + value.toUpperCase()
} }
Address.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeLocals (stackDepth, stack, memory) {
if (stackDepth >= stack.length) { if (stackDepth >= stack.length) {
return '0x0000000000000000000000000000000000000000' return '0x0000000000000000000000000000000000000000'
} else { } else {
return '0x' + util.extractHexByteSlice(stack[stack.length - 1 - stackDepth], this.storageBytes, 0) return '0x' + util.extractHexByteSlice(stack[stack.length - 1 - stackDepth], this.storageBytes, 0)
} }
} }
Address.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64) var value = memory.substr(offset, 64)
value = util.extractHexByteSlice(value, this.storageBytes, 0) value = util.extractHexByteSlice(value, this.storageBytes, 0)
return value return value
}
} }
module.exports = Address module.exports = Address
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
var util = require('./util') var util = require('./util')
var BN = require('ethereumjs-util').BN var BN = require('ethereumjs-util').BN
function ArrayType (underlyingType, arraySize) { class ArrayType {
constructor (underlyingType, arraySize) {
this.typeName = 'array' this.typeName = 'array'
this.storageBytes = 32 this.storageBytes = 32
this.underlyingType = underlyingType this.underlyingType = underlyingType
...@@ -18,9 +20,9 @@ function ArrayType (underlyingType, arraySize) { ...@@ -18,9 +20,9 @@ function ArrayType (underlyingType, arraySize) {
this.storageSlots = arraySize * underlyingType.storageSlots this.storageSlots = arraySize * underlyingType.storageSlots
} }
} }
} }
ArrayType.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
var ret = [] var ret = []
var size = null var size = null
var slotValue = util.extractHexValue(location, storageContent, this.storageBytes) var slotValue = util.extractHexValue(location, storageContent, this.storageBytes)
...@@ -52,9 +54,9 @@ ArrayType.prototype.decodeFromStorage = function (location, storageContent) { ...@@ -52,9 +54,9 @@ ArrayType.prototype.decodeFromStorage = function (location, storageContent) {
value: ret, value: ret,
length: '0x' + size.toString(16) length: '0x' + size.toString(16)
} }
} }
ArrayType.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeLocals (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return [] return []
} else { // TODO manage decoding locals from storage } else { // TODO manage decoding locals from storage
...@@ -62,9 +64,9 @@ ArrayType.prototype.decodeLocals = function (stackDepth, stack, memory) { ...@@ -62,9 +64,9 @@ ArrayType.prototype.decodeLocals = function (stackDepth, stack, memory) {
offset = 2 * parseInt(offset, 16) offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory) return this.decodeFromMemory(offset, memory)
} }
} }
ArrayType.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var ret = [] var ret = []
var length = extractLength(this, offset, memory) var length = extractLength(this, offset, memory)
if (this.arraySize === 'dynamic') { if (this.arraySize === 'dynamic') {
...@@ -80,6 +82,8 @@ ArrayType.prototype.decodeFromMemory = function (offset, memory) { ...@@ -80,6 +82,8 @@ ArrayType.prototype.decodeFromMemory = function (offset, memory) {
offset += 64 offset += 64
} }
return ret return ret
}
} }
function extractLength (type, offset, memory) { function extractLength (type, offset, memory) {
......
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function Bool () { class Bool extends ValueType {
this.storageSlots = 1 constructor () {
this.storageBytes = 1 super(1, 1, 'bool')
this.typeName = 'bool' }
} }
Bool.prototype.decodeFromStorage = function (location, storageContent) { Bool.prototype.decodeFromStorage = function (location, storageContent) {
......
'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 ValueType = require('./ValueType')
function DynamicByteArray () { class DynamicByteArray extends ValueType {
this.storageSlots = 1 constructor () {
this.storageBytes = 32 super(1, 32, 'bytes')
this.typeName = 'bytes' }
}
DynamicByteArray.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes) var value = util.extractHexValue(location, storageContent, this.storageBytes)
var bn = new BN(value, 16) var bn = new BN(value, 16)
if (bn.testn(0)) { if (bn.testn(0)) {
...@@ -33,31 +33,9 @@ DynamicByteArray.prototype.decodeFromStorage = function (location, storageConten ...@@ -33,31 +33,9 @@ DynamicByteArray.prototype.decodeFromStorage = function (location, storageConten
length: '0x' + size.toString(16) length: '0x' + size.toString(16)
} }
} }
}
DynamicByteArray.prototype.decodeLocals = function (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) {
return {
value: '0x',
length: '0x0'
}
} else {
var offset = stack[stack.length - 1 - stackDepth]
offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory)
} }
}
DynamicByteArray.prototype.decodeFromMemory = function (offset, memory) {
var length = memory.substr(offset, 64)
length = 2 * parseInt(length, 16)
return {
length: '0x' + length.toString(16),
value: '0x' + memory.substr(offset + 64, length)
}
}
DynamicByteArray.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeLocals (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return { return {
value: '0x', value: '0x',
...@@ -68,15 +46,16 @@ DynamicByteArray.prototype.decodeLocals = function (stackDepth, stack, memory) { ...@@ -68,15 +46,16 @@ DynamicByteArray.prototype.decodeLocals = function (stackDepth, stack, memory) {
offset = 2 * parseInt(offset, 16) offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory) return this.decodeFromMemory(offset, memory)
} }
} }
DynamicByteArray.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var length = memory.substr(offset, 64) var length = memory.substr(offset, 64)
length = 2 * parseInt(length, 16) length = 2 * parseInt(length, 16)
return { return {
length: '0x' + length.toString(16), length: '0x' + length.toString(16),
value: '0x' + memory.substr(offset + 64, length) value: '0x' + memory.substr(offset + 64, length)
} }
}
} }
module.exports = DynamicByteArray module.exports = DynamicByteArray
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function Enum (enumDef) { class Enum extends ValueType {
this.enumDef = enumDef constructor (enumDef) {
this.typeName = 'enum' var storageBytes = 0
this.storageSlots = 1
var length = enumDef.children.length var length = enumDef.children.length
this.storageBytes = 0
while (length > 1) { while (length > 1) {
length = length / 256 length = length / 256
this.storageBytes++ storageBytes++
}
super(1, storageBytes, 'enum')
this.enumDef = enumDef
} }
} }
......
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function FixedByteArray (storageBytes) { class FixedByteArray extends ValueType {
this.storageSlots = 1 constructor (storageBytes) {
this.storageBytes = storageBytes super(1, storageBytes, 'bytesX')
this.typeName = 'bytesX' }
}
FixedByteArray.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
var value = util.extractHexValue(location, storageContent, this.storageBytes) var value = util.extractHexValue(location, storageContent, this.storageBytes)
return '0x' + value.toUpperCase() return '0x' + value.toUpperCase()
} }
FixedByteArray.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeLocals (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return '0x' return '0x'
} else { } else {
var value = stack[stack.length - 1 - stackDepth] var value = stack[stack.length - 1 - stackDepth]
return '0x' + value.substr(2, 2 * this.storageBytes).toUpperCase() return '0x' + value.substr(2, 2 * this.storageBytes).toUpperCase()
} }
} }
FixedByteArray.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var value = memory.substr(offset, 64) var value = memory.substr(offset, 64)
return util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase() return util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
}
} }
module.exports = FixedByteArray module.exports = FixedByteArray
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function Int (storageBytes) { class Int extends ValueType {
this.storageSlots = 1 constructor (storageBytes) {
this.storageBytes = storageBytes super(1, storageBytes, 'int')
this.typeName = 'int' }
} }
Int.prototype.decodeFromStorage = function (location, storageContent) { Int.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
function Mapping () { class Mapping {
constructor () {
this.storageSlots = 1 this.storageSlots = 1
this.storageBytes = 32 this.storageBytes = 32
this.typeName = 'mapping' this.typeName = 'mapping'
} }
Mapping.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
return '<not implemented>' return '<not implemented>'
}
} }
module.exports = Mapping module.exports = Mapping
'use strict' 'use strict'
var DynamicBytes = require('./DynamicByteArray') var DynamicBytes = require('./DynamicByteArray')
function StringType () { class StringType extends DynamicBytes {
this.storageSlots = 1 constructor () {
this.storageBytes = 32 super()
this.typeName = 'string' this.typeName = 'string'
this.dynamicBytes = new DynamicBytes() }
}
StringType.prototype.decodeFromStorage = function (location, storageContent) {
var decoded = this.dynamicBytes.decodeFromStorage(location, storageContent)
return format(decoded)
}
StringType.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeFromStorage (location, storageContent) {
var decoded = this.dynamicBytes.decodeLocals(stackDepth, stack, memory) var decoded = super.decodeFromStorage(location, storageContent)
return format(decoded) return format(decoded)
} }
StringType.prototype.decodeFromMemory = function (offset, memory) { decodeLocals (stackDepth, stack, memory) {
var decoded = this.dynamicBytes.decodeFromMemory(offset, memory) var decoded = super.decodeLocals(stackDepth, stack, memory)
return format(decoded) return format(decoded)
} }
StringType.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var decoded = this.dynamicBytes.decodeFromMemory(offset, memory) var decoded = super.decodeFromMemory(offset, memory)
return format(decoded) return format(decoded)
}
} }
function format (decoded) { function format (decoded) {
......
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
function Struct (memberDetails) { class Struct {
constructor (memberDetails) {
this.storageSlots = memberDetails.storageSlots this.storageSlots = memberDetails.storageSlots
this.storageBytes = 32 this.storageBytes = 32
this.members = memberDetails.members this.members = memberDetails.members
this.typeName = 'struct' this.typeName = 'struct'
} }
Struct.prototype.decodeFromStorage = function (location, storageContent) { decodeFromStorage (location, storageContent) {
var ret = {} var ret = {}
this.members.map(function (item, i) { this.members.map(function (item, i) {
var globalLocation = { var globalLocation = {
...@@ -18,34 +19,9 @@ Struct.prototype.decodeFromStorage = function (location, storageContent) { ...@@ -18,34 +19,9 @@ Struct.prototype.decodeFromStorage = function (location, storageContent) {
ret[item.name] = item.type.decodeFromStorage(globalLocation, storageContent) ret[item.name] = item.type.decodeFromStorage(globalLocation, storageContent)
}) })
return ret return ret
}
Struct.prototype.decodeLocals = function (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)
}
}
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
}
Struct.prototype.decodeLocals = function (stackDepth, stack, memory) { decodeLocals (stackDepth, stack, memory) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return {} return {}
} else { // TODO manage decoding locals from storage } else { // TODO manage decoding locals from storage
...@@ -53,9 +29,9 @@ Struct.prototype.decodeLocals = function (stackDepth, stack, memory) { ...@@ -53,9 +29,9 @@ Struct.prototype.decodeLocals = function (stackDepth, stack, memory) {
offset = 2 * parseInt(offset, 16) offset = 2 * parseInt(offset, 16)
return this.decodeFromMemory(offset, memory) return this.decodeFromMemory(offset, memory)
} }
} }
Struct.prototype.decodeFromMemory = function (offset, memory) { decodeFromMemory (offset, memory) {
var ret = {} var ret = {}
this.members.map(function (item, i) { this.members.map(function (item, i) {
var contentOffset = offset var contentOffset = offset
...@@ -68,6 +44,7 @@ Struct.prototype.decodeFromMemory = function (offset, memory) { ...@@ -68,6 +44,7 @@ Struct.prototype.decodeFromMemory = function (offset, memory) {
offset += 64 offset += 64
}) })
return ret return ret
}
} }
module.exports = Struct module.exports = Struct
'use strict' 'use strict'
var util = require('./util') var util = require('./util')
var ValueType = require('./ValueType')
function Uint (storageBytes) { class Uint extends ValueType {
this.storageSlots = 1 constructor (storageBytes) {
this.storageBytes = storageBytes super(1, storageBytes, 'uint')
this.typeName = 'uint' }
} }
Uint.prototype.decodeFromStorage = function (location, storageContent) { Uint.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict'
class ValueType {
constructor (storageSlots, storageBytes, typeName) {
this.storageSlots = storageSlots
this.storageBytes = storageBytes
this.typeName = typeName
}
}
module.exports = ValueType
...@@ -132,7 +132,7 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId) { ...@@ -132,7 +132,7 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId) {
tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = { tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = {
name: variableDeclaration.attributes.name, name: variableDeclaration.attributes.name,
type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName), type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName),
stackDepth: stack.stackDepth stackDepth: stack.length
} }
} }
}) })
......
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