build tree without while loop

pull/7/head
yann300 8 years ago
parent 1d56742b15
commit b989f1a453
  1. 61
      src/util/internalCallTree.js

@ -8,6 +8,7 @@ var util = require('../helpers/util')
/** /**
* Tree representing internal jump into function. * Tree representing internal jump into function.
* Trigger `callTreeReady` event when tree is ready * Trigger `callTreeReady` event when tree is ready
* Trigger `callTreeBuildFailed` event when tree fails to build
*/ */
class InternalCallTree { class InternalCallTree {
/** /**
@ -30,9 +31,14 @@ class InternalCallTree {
if (!this.solidityProxy.loaded()) { if (!this.solidityProxy.loaded()) {
console.log('compilation result not loaded. Cannot build internal call tree') console.log('compilation result not loaded. Cannot build internal call tree')
} else { } else {
buildTree(this, 0, '', trace) buildTree(this, 0, '', (error) => {
} if (!error) {
this.event.trigger('callTreeReady', [this.scopes, this.scopeStarts]) this.event.trigger('callTreeReady', [this.scopes, this.scopeStarts])
} else {
this.event.trigger('callTreeBuildFailed', [error])
}
})
}
}) })
} }
@ -76,40 +82,54 @@ class InternalCallTree {
} }
} }
/** /**
* build tree (called recursively) * build tree (called recursively)
* *
* @param {Object} tree - instance of InternalCallTree * @param {Object} tree - instance of InternalCallTree
* @param {Int} step - index on the vm trace * @param {Int} step - index on the vm trace
* @param {String} scopeId - deepness of the current scope * @param {String} scopeId - deepness of the current scope
* @param {Object} trace - vm trace * @param {Function} cb - callback
*/ */
function buildTree (tree, step, scopeId, trace) { function buildTree (tree, step, scopeId, cb) {
let subScope = 1 let subScope = 1
tree.scopeStarts[step] = scopeId tree.scopeStarts[step] = scopeId
tree.scopes[scopeId] = { firstStep: step } tree.scopes[scopeId] = { firstStep: step, locals: {} }
while (step < trace.length) { visitStep(tree, step, scopeId, subScope, function (error, result) {
var sourceLocation cb(error, result)
extractSourceLocation(tree, step, (error, src) => { })
}
/**
* visit a step (called recursively)
*
* @param {Object} tree - instance of InternalCallTree
* @param {Int} step - index on the vm trace
* @param {String} scopeId - deepness of the current scope
* @param {Int} subScope - index of the next scope from current scope
* @param {Function} cb - callback
*/
function visitStep (tree, step, scopeId, subScope, cb) {
extractSourceLocation(tree, step, (error, sourceLocation) => {
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
sourceLocation = src if (sourceLocation.jump === 'i') {
buildTree(tree, step + 1, scopeId === '' ? subScope.toString() : scopeId + '.' + subScope, function (error, outStep) {
if (!error) {
visitStep(tree, outStep, scopeId, subScope + 1, cb)
} else {
cb('error computing jump')
} }
}) })
if (sourceLocation.jump === 'i') { return
step = buildTree(tree, step + 1, scopeId === '' ? subScope.toString() : scopeId + '.' + subScope, trace)
subScope++
} else if (sourceLocation.jump === 'o') { } else if (sourceLocation.jump === 'o') {
tree.scopes[scopeId].lastStep = step tree.scopes[scopeId].lastStep = step
return step + 1 cb(null, step + 1)
return
} else { } else {
if (tree.includeLocalVariables) { if (tree.includeLocalVariables) {
var variableDeclaration = resolveVariableDeclaration(tree, step, sourceLocation) var variableDeclaration = resolveVariableDeclaration(tree, step, sourceLocation)
if (variableDeclaration) { if (variableDeclaration) {
if (!tree.scopes[scopeId].locals) {
tree.scopes[scopeId].locals = {}
}
tree.traceManager.getStackAt(step, (error, stack) => { tree.traceManager.getStackAt(step, (error, stack) => {
if (!error) { if (!error) {
tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached
@ -126,10 +146,15 @@ function buildTree (tree, step, scopeId, trace) {
}) })
} }
} }
step++ step++
} }
if (tree.traceManager.inRange(step)) {
visitStep(tree, step, scopeId, subScope, cb)
} else {
cb(null, step)
}
} }
})
} }
function extractSourceLocation (tree, step, cb) { function extractSourceLocation (tree, step, cb) {

Loading…
Cancel
Save