- add comments
pull/7/head
yann300 8 years ago
parent 0e39252222
commit e3934b915e
  1. 24
      src/solidity/astHelper.js
  2. 53
      src/solidity/decodeInfo.js

@ -7,15 +7,15 @@ var AstWalker = require('../util/astWalker')
* @param {Object} sourcesList - sources list (containing root AST node)
* @return {Object} - returns a mapping from AST node ids to AST nodes for the contracts
*/
function extractContractsDefinition (sourcesList) {
function extractContractDefinitions (sourcesList) {
var ret = {
contractsIds: {},
contractsNames: {}
contractsById: {},
contractsByName: {}
}
var walker = new AstWalker()
walker.walkAstList(sourcesList, { 'ContractDefinition': function (node) {
ret.contractsIds[node.id] = node
ret.contractsNames[node.attributes.name] = node.id
ret.contractsById[node.id] = node
ret.contractsByName[node.attributes.name] = node
return false
}})
return ret
@ -28,8 +28,8 @@ function extractContractsDefinition (sourcesList) {
* @param {Map} contracts - all contracts defined in the current context
* @return {Array} - array of base contracts in derived to base order as AST nodes.
*/
function getLinearizedBaseContracts (id, contracts) {
return contracts[id].attributes.linearizedBaseContracts.map(function (id) { return contracts[id] })
function getLinearizedBaseContracts (id, contractsById) {
return contractsById[id].attributes.linearizedBaseContracts.map(function (id) { return contractsById[id] })
}
/**
@ -40,11 +40,11 @@ function getLinearizedBaseContracts (id, contracts) {
* @return {Array} - return an array of AST node of all state variables (including inherited) (this will include all enum/struct declarations)
*/
function extractStateVariables (contractName, sourcesList) {
var contracts = extractContractsDefinition(sourcesList)
var id = contracts.contractsNames[contractName]
if (id) {
var contracts = extractContractDefinitions(sourcesList)
var node = contracts.contractsByName[contractName]
if (node) {
var stateVar = []
var baseContracts = getLinearizedBaseContracts(id, contracts.contractsIds)
var baseContracts = getLinearizedBaseContracts(node.id, contracts.contractsById)
baseContracts.reverse()
for (var k in baseContracts) {
var ctr = baseContracts[k]
@ -59,6 +59,6 @@ function extractStateVariables (contractName, sourcesList) {
module.exports = {
extractStateVariables: extractStateVariables,
extractContractsDefinition: extractContractsDefinition,
extractContractDefinitions: extractContractDefinitions,
getLinearizedBaseContracts: getLinearizedBaseContracts
}

@ -1,15 +1,11 @@
'use strict'
/**
* Uint decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g uint256, uint32)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Uint (type) {
if (type.split(' ')) {
type = type.split(' ')[0]
}
return {
needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('uint', '')) / 8,
@ -17,10 +13,24 @@ function Uint (type) {
}
}
/**
* Int decode the given @arg type
*
* @param {String} type - type given by the AST (e.g int256, int32)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Int (type) {
return {
needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('int', '')) / 8,
typeName: type
}
}
/**
* Address decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g address)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Address (type) {
@ -34,7 +44,7 @@ function Address (type) {
/**
* Bool decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g bool)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Bool (type) {
@ -48,7 +58,7 @@ function Bool (type) {
/**
* DynamicByteArray decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g bytes storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function DynamicByteArray (type) {
@ -62,7 +72,7 @@ function DynamicByteArray (type) {
/**
* FixedByteArray decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g bytes16)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function FixedByteArray (type) {
@ -76,27 +86,10 @@ function FixedByteArray (type) {
}
}
/**
* Int decode the given @arg type
*
* @param {String} type - type given by the AST
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Int (type) {
if (type.split(' ')) {
type = type.split(' ')[0]
}
return {
needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('int', '')) / 8,
typeName: type
}
}
/**
* StringType decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g string storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function StringType (type) {
@ -110,7 +103,7 @@ function StringType (type) {
/**
* ArrayType decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g int256[] storage ref, int256[] storage ref[] storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder, arraySize, subArray}
*/
function ArrayType (type, stateDefinitions) {
@ -153,7 +146,7 @@ function ArrayType (type, stateDefinitions) {
/**
* Enum decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g enum enumDef)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder}
*/
function Enum (type, stateDefinitions) {
@ -169,7 +162,7 @@ function Enum (type, stateDefinitions) {
/**
* Struct decode the given @arg type
*
* @param {String} type - type given by the AST
* @param {String} type - type given by the AST (e.g struct structDef storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder, members}
*/
function Struct (type, stateDefinitions) {

Loading…
Cancel
Save