Commit 5a833953 authored by yann300's avatar yann300

change types ctor

parent cf10206c
...@@ -19,12 +19,8 @@ var UintType = require('./types/Uint') ...@@ -19,12 +19,8 @@ var UintType = require('./types/Uint')
*/ */
function Uint (type) { function Uint (type) {
type === 'uint' ? 'uint256' : type type === 'uint' ? 'uint256' : type
var decodeInfo = { var storageBytes = parseInt(type.replace('uint', '')) / 8
storageSlots: 1, return new UintType(storageBytes)
storageBytes: parseInt(type.replace('uint', '')) / 8,
typeName: 'uint'
}
return new UintType(decodeInfo)
} }
/** /**
...@@ -35,12 +31,8 @@ function Uint (type) { ...@@ -35,12 +31,8 @@ function Uint (type) {
*/ */
function Int (type) { function Int (type) {
type === 'int' ? 'int256' : type type === 'int' ? 'int256' : type
var decodeInfo = { var storageBytes = parseInt(type.replace('int', '')) / 8
storageSlots: 1, return new IntType(storageBytes)
storageBytes: parseInt(type.replace('int', '')) / 8,
typeName: 'int'
}
return new IntType(decodeInfo)
} }
/** /**
...@@ -50,12 +42,7 @@ function Int (type) { ...@@ -50,12 +42,7 @@ function Int (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function Address (type) { function Address (type) {
var decodeInfo = { return new AddressType()
storageSlots: 1,
storageBytes: 20,
typeName: 'address'
}
return new AddressType(decodeInfo)
} }
/** /**
...@@ -65,12 +52,7 @@ function Address (type) { ...@@ -65,12 +52,7 @@ function Address (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function Bool (type) { function Bool (type) {
var decodeInfo = { return new BoolType()
storageSlots: 1,
storageBytes: 1,
typeName: 'bool'
}
return new BoolType(decodeInfo)
} }
/** /**
...@@ -80,12 +62,7 @@ function Bool (type) { ...@@ -80,12 +62,7 @@ function Bool (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function DynamicByteArray (type) { function DynamicByteArray (type) {
var decodeInfo = { return new BytesType()
storageSlots: 1,
storageBytes: 32,
typeName: 'bytes'
}
return new BytesType(decodeInfo)
} }
/** /**
...@@ -95,12 +72,8 @@ function DynamicByteArray (type) { ...@@ -95,12 +72,8 @@ function DynamicByteArray (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function FixedByteArray (type) { function FixedByteArray (type) {
var decodeInfo = { var storageBytes = parseInt(type.replace('bytes', ''))
storageSlots: 1, return new BytesXType(storageBytes)
storageBytes: parseInt(type.replace('bytes', '')),
typeName: 'bytesX'
}
return new BytesXType(decodeInfo)
} }
/** /**
...@@ -110,12 +83,7 @@ function FixedByteArray (type) { ...@@ -110,12 +83,7 @@ function FixedByteArray (type) {
* @return {Object} returns decoded info about the current type: { storageBytes, typeName} * @return {Object} returns decoded info about the current type: { storageBytes, typeName}
*/ */
function String (type) { function String (type) {
var decodeInfo = { return new StringType()
storageSlots: 1,
storageBytes: 32,
typeName: 'string'
}
return new StringType(decodeInfo)
} }
/** /**
...@@ -126,41 +94,18 @@ function String (type) { ...@@ -126,41 +94,18 @@ function String (type) {
*/ */
function Array (type, stateDefinitions) { function Array (type, stateDefinitions) {
var arraySize var arraySize
var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/) var match = type.match(/(.*)\[(.*?)\]( storage ref| storage pointer| memory| calldata)?$/)
if (!match || match.length < 3) { if (!match || match.length < 3) {
console.log('unable to parse type ' + type) console.log('unable to parse type ' + type)
return null return null
} }
arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2]) arraySize = match[2] === '' ? 'dynamic' : parseInt(match[2])
var underlyingType = parseType(match[1], stateDefinitions) var underlyingType = parseType(match[1], stateDefinitions)
if (underlyingType === null) { if (underlyingType === null) {
console.log('unable to parse type ' + type) console.log('unable to parse type ' + type)
return null return null
} }
return new ArrayType(underlyingType, arraySize)
var storageSlots
if (arraySize === 'dynamic') {
storageSlots = 1
} else {
if (underlyingType.storageBytes < 32) {
var itemPerSlot = Math.floor(32 / underlyingType.storageBytes)
storageSlots = Math.ceil(arraySize / itemPerSlot)
} else {
storageSlots = arraySize * underlyingType.storageSlots
}
}
var decodeInfo = {
storageSlots: storageSlots,
storageBytes: 32,
typeName: 'array',
arraySize: arraySize,
underlyingType: underlyingType
}
return new ArrayType(decodeInfo)
} }
/** /**
...@@ -175,19 +120,7 @@ function Enum (type, stateDefinitions) { ...@@ -175,19 +120,7 @@ function Enum (type, stateDefinitions) {
console.log('unable to retrieve decode info of ' + type) console.log('unable to retrieve decode info of ' + type)
return null return null
} }
var length = enumDef.children.length return new EnumType(enumDef)
var storageBytes = 0
while (length > 1) {
length = length / 256
storageBytes++
}
var decodeInfo = {
storageSlots: 1,
storageBytes: storageBytes,
typeName: 'enum',
enum: enumDef
}
return new EnumType(decodeInfo)
} }
/** /**
...@@ -203,13 +136,7 @@ function Struct (type, stateDefinitions) { ...@@ -203,13 +136,7 @@ function Struct (type, stateDefinitions) {
} }
var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition var memberDetails = getStructMembers(match[1], stateDefinitions) // type is used to extract the ast struct definition
if (!memberDetails) return null if (!memberDetails) return null
var decodeInfo = { return new StructType(memberDetails)
storageSlots: Math.ceil(memberDetails.storageBytes / 32),
storageBytes: 32,
typeName: 'struct',
members: memberDetails.members
}
return new StructType(decodeInfo)
} }
/** /**
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Address (decoder) { function Address () {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = 20
this.typeName = 'address'
} }
Address.prototype.decodeFromStorage = function (location, storageContent) { Address.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function ArrayType (decoder) { function ArrayType (underlyingType, arraySize) {
baseType(this, decoder) this.typeName = 'array'
this.arraySize = decoder.arraySize this.storageBytes = 32
this.subArray = decoder.subArray this.underlyingType = underlyingType
this.arraySize = arraySize
this.storageSlots = null
if (arraySize === 'dynamic') {
this.storageSlots = 1
} else {
if (underlyingType.storageBytes < 32) {
var itemPerSlot = Math.floor(32 / underlyingType.storageBytes)
this.storageSlots = Math.ceil(arraySize / itemPerSlot)
} else {
this.storageSlots = arraySize * underlyingType.storageSlots
}
}
} }
ArrayType.prototype.decodeFromStorage = function (location, storageContent) { ArrayType.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Bool (decoder) { function Bool () {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = 1
this.typeName = 'bool'
} }
Bool.prototype.decodeFromStorage = function (location, storageContent) { Bool.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function DynamicByteArray (decoder) { function DynamicByteArray () {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = 32
this.typeName = 'bytes'
} }
DynamicByteArray.prototype.decodeFromStorage = function (location, storageContent) { DynamicByteArray.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Enum (decoder) { function Enum (enumDef) {
baseType(this, decoder) this.enumDef = enumDef
this.enum = decoder.enum this.typeName = 'enum'
this.storageSlots = 1
var length = enumDef.children.length
this.storageBytes = 0
while (length > 1) {
length = length / 256
this.storageBytes++
}
} }
Enum.prototype.decodeFromStorage = function (location, storageContent) { Enum.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function FixedByteArray (decoder) { function FixedByteArray (storageBytes) {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'bytesX'
} }
FixedByteArray.prototype.decodeFromStorage = function (location, storageContent) { FixedByteArray.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Int (decoder) { function Int (storageBytes) {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'int'
} }
Int.prototype.decodeFromStorage = function (location, storageContent) { Int.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function StringType (decoder) { function StringType () {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = 32
this.typeName = 'string'
} }
StringType.prototype.decodeFromStorage = function (location, storageContent) { StringType.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Struct (decoder) { function Struct (memberDetails) {
baseType(this, decoder) this.storageSlots = Math.ceil(memberDetails.storageBytes / 32)
this.members = decoder.members this.storageBytes = 32
this.members = memberDetails.members
this.typeName = 'struct'
} }
Struct.prototype.decodeFromStorage = function (location, storageContent) { Struct.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict' 'use strict'
var baseType = require('./baseType')
function Uint (decoder) { function Uint (storageBytes) {
baseType(this, decoder) this.storageSlots = 1
this.storageBytes = storageBytes
this.typeName = 'uint'
} }
Uint.prototype.decodeFromStorage = function (location, storageContent) { Uint.prototype.decodeFromStorage = function (location, storageContent) {
......
'use strict'
module.exports = function (type, decoder) {
type.storageSlots = decoder.storageSlots
type.storageBytes = decoder.storageBytes
type.typeName = decoder.typeName
type.decoder = decoder.decoder
}
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