remove unnecessary if elses

refactor_remix_debug5_ab
Iuri Matias 4 years ago
parent fda09db1e6
commit 3ef7042558
  1. 55
      libs/remix-debug/src/source/astWalker.js
  2. 5
      libs/remix-debug/src/source/sourceLocationTracker.js
  3. 11
      libs/remix-debug/src/source/sourceMappingDecoder.js
  4. 7
      libs/remix-lib/src/storage.js
  5. 95
      libs/remix-lib/src/universalDapp.js
  6. 5
      libs/remix-lib/src/web3Provider/web3Providers.js
  7. 24
      libs/remix-lib/src/web3Provider/web3VmProvider.js

@ -0,0 +1,55 @@
'use strict'
/**
* Crawl the given AST through the function walk(ast, callback)
*/
function AstWalker () {} // eslint-disable-line
/**
* visit all the AST nodes
*
* @param {Object} ast - AST node
* @param {Object or Function} callback - if (Function) the function will be called for every node.
* - if (Object) callback[<Node Type>] will be called for
* every node of type <Node Type>. callback["*"] will be called for all other nodes.
* in each case, if the callback returns false it does not descend into children.
* If no callback for the current type, children are visited.
*/
AstWalker.prototype.walk = function (ast, callback) {
if (callback instanceof Function) {
callback = {'*': callback}
}
if (!('*' in callback)) {
callback['*'] = function () { return true }
}
const nodes = ast.nodes || (ast.body && ast.body.statements) || ast.declarations
if(ast.body && ast.initializationExpression) // 'for' loop handling
nodes.push(ast.initializationExpression)
if (manageCallBack(ast, callback) && nodes && nodes.length > 0) {
for (let k in nodes) {
const child = nodes[k]
this.walk(child, callback)
}
}
}
/**
* walk the given @astList
*
* @param {Object} sourcesList - sources list (containing root AST node)
* @param {Function} - callback used by AstWalker to compute response
*/
AstWalker.prototype.walkAstList = function (sourcesList, callback) {
const walker = new AstWalker()
for (let k in sourcesList) {
walker.walk(sourcesList[k].ast, callback)
}
}
function manageCallBack (node, callback) {
if (node.nodeType in callback) {
return callback[node.nodeType](node)
}
return callback['*'](node)
}
module.exports = AstWalker

@ -88,10 +88,9 @@ function extractSourceMap (self, codeManager, address, contracts) {
const sourceMap = getSourceMap(address, result.bytecode, contracts)
if (sourceMap) {
if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap
resolve(sourceMap)
} else {
reject('no sourcemap associated with the code ' + address)
return resolve(sourceMap)
}
reject('no sourcemap associated with the code ' + address)
}).catch(reject)
})
}

@ -94,12 +94,8 @@ SourceMappingDecoder.prototype.convertOffsetToLineColumn = function (sourceLocat
start: convertFromCharPosition(sourceLocation.start, lineBreakPositions),
end: convertFromCharPosition(sourceLocation.start + sourceLocation.length, lineBreakPositions)
}
} else {
return {
start: null,
end: null
}
}
return {start: null, end: null}
}
/**
@ -119,10 +115,7 @@ function convertFromCharPosition (pos, lineBreakPositions) {
}
const beginColumn = line === 0 ? 0 : (lineBreakPositions[line - 1] + 1)
const column = pos - beginColumn
return {
line: line,
column: column
}
return {line, column}
}
function sourceLocationFromAstNode (astNode) {

@ -27,10 +27,8 @@ function Storage (prefix) {
this.remove = function (name) {
if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name)
return true
} else {
return true
}
return true
}
this.rename = function (originalName, newName) {
@ -46,9 +44,8 @@ function Storage (prefix) {
// NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
} else {
return []
}
return []
}
this.keys = function () {

@ -91,17 +91,16 @@ module.exports = class UniversalDApp {
if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode')
}
passwordPromptCb((passphrase) => {
return passwordPromptCb((passphrase) => {
this.executionContext.web3().personal.newAccount(passphrase, cb)
})
} else {
let privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
}
let privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
}
/** Add an account to the list of account (only for Javascript VM) */
@ -110,22 +109,23 @@ module.exports = class UniversalDApp {
throw new Error('_addAccount() cannot be called in non-VM mode')
}
if (this.accounts) {
privateKey = Buffer.from(privateKey, 'hex')
const address = privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) return console.log(error)
account.balance = balance || '0xf00000000000000001'
stateManager.putAccount(address, account, function cb (error) {
if (error) console.log(error)
})
if (!this.accounts) {
return
}
privateKey = Buffer.from(privateKey, 'hex')
const address = privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) return console.log(error)
account.balance = balance || '0xf00000000000000001'
stateManager.putAccount(address, account, function cb(error) {
if (error) console.log(error)
})
})
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
/** Return the list of accounts */
@ -171,40 +171,36 @@ module.exports = class UniversalDApp {
}
/** Get the balance of an address */
getBalance (address, cb) {
getBalance(address, cb) {
address = stripHexPrefix(address)
if (!this.executionContext.isVM()) {
this.executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) {
cb(err)
} else {
cb(null, res.toString(10))
}
})
} else {
if (!this.accounts) {
return cb('No accounts?')
}
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
return this.executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) {
cb('Account not found')
} else {
cb(null, new BN(res.balance).toString(10))
return cb(err)
}
cb(null, res.toString(10))
})
}
if (!this.accounts) {
return cb('No accounts?')
}
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) {
return cb('Account not found')
}
cb(null, new BN(res.balance).toString(10))
})
}
/** Get the balance of an address, and convert wei to ether */
getBalanceInEther (address, callback) {
this.getBalance(address, (error, balance) => {
if (error) {
callback(error)
} else {
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
return callback(error)
}
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
})
}
@ -219,10 +215,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback.
*/
createContract (data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
}
/**
@ -235,10 +228,7 @@ module.exports = class UniversalDApp {
*/
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) {
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure'
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, callback)
}
/**
@ -249,10 +239,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback.
*/
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
}
context () {

@ -19,10 +19,9 @@ Web3Providers.prototype.addProvider = function (type, obj) {
Web3Providers.prototype.get = function (type, cb) {
if (this.modes[type]) {
cb(null, this.modes[type])
} else {
cb('error: this provider has not been setup (' + type + ')', null)
return cb(null, this.modes[type])
}
cb('error: this provider has not been setup (' + type + ')', null)
}
Web3Providers.prototype.addWeb3 = function (type, web3) {

@ -211,10 +211,9 @@ web3VmProvider.prototype.traceTransaction = function (txHash, options, cb) {
cb(null, this.vmTraces[txHash])
}
return this.vmTraces[txHash]
} else {
if (cb) {
cb('unable to retrieve traces ' + txHash, null)
}
}
if (cb) {
cb('unable to retrieve traces ' + txHash, null)
}
}
@ -232,9 +231,8 @@ web3VmProvider.prototype.storageRangeAt = function (blockNumber, txIndex, addres
storage: JSON.parse(JSON.stringify(storage)),
nextKey: null
})
} else {
cb('unable to retrieve storage ' + txIndex + ' ' + address)
}
cb('unable to retrieve storage ' + txIndex + ' ' + address)
}
web3VmProvider.prototype.getBlockNumber = function (cb) { cb(null, 'vm provider') }
@ -245,10 +243,9 @@ web3VmProvider.prototype.getTransaction = function (txHash, cb) {
cb(null, this.txs[txHash])
}
return this.txs[txHash]
} else {
if (cb) {
cb('unable to retrieve tx ' + txHash, null)
}
}
if (cb) {
cb('unable to retrieve tx ' + txHash, null)
}
}
@ -259,10 +256,9 @@ web3VmProvider.prototype.getTransactionReceipt = function (txHash, cb) {
cb(null, this.txsReceipt[txHash])
}
return this.txsReceipt[txHash]
} else {
if (cb) {
cb('unable to retrieve txReceipt ' + txHash, null)
}
}
if (cb) {
cb('unable to retrieve txReceipt ' + txHash, null)
}
}

Loading…
Cancel
Save