Commit e7c45d29 authored by yann300's avatar yann300

use es6 class for type

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