codeResolver and codeUtils updated

pull/5370/head
aniket-engg 4 years ago committed by Aniket
parent 763000ebaa
commit 18525f620d
  1. 25
      libs/remix-debug/src/code/codeResolver.ts
  2. 23
      libs/remix-debug/src/code/codeUtils.ts

@ -1,21 +1,27 @@
'use strict' 'use strict'
const codeUtils = require('./codeUtils') const codeUtils = require('./codeUtils')
function CodeResolver ({getCode}) { export class CodeResolver {
this.getCode = getCode
getCode
bytecodeByAddress
instructionsByAddress
instructionsIndexByBytesOffset
constructor({getCode}) {
this.getCode = getCode
this.bytecodeByAddress = {} // bytes code by contract addesses this.bytecodeByAddress = {} // bytes code by contract addesses
this.instructionsByAddress = {} // assembly items instructions list by contract addesses this.instructionsByAddress = {} // assembly items instructions list by contract addesses
this.instructionsIndexByBytesOffset = {} // mapping between bytes offset and instructions index. this.instructionsIndexByBytesOffset = {} // mapping between bytes offset and instructions index.
} }
CodeResolver.prototype.clear = function () { clear () {
this.bytecodeByAddress = {} this.bytecodeByAddress = {}
this.instructionsByAddress = {} this.instructionsByAddress = {}
this.instructionsIndexByBytesOffset = {} this.instructionsIndexByBytesOffset = {}
} }
CodeResolver.prototype.resolveCode = async function (address) { async resolveCode (address) {
const cache = this.getExecutingCodeFromCache(address) const cache = this.getExecutingCodeFromCache(address)
if (cache) { if (cache) {
return cache return cache
@ -25,7 +31,7 @@ CodeResolver.prototype.resolveCode = async function (address) {
return this.cacheExecutingCode(address, code) return this.cacheExecutingCode(address, code)
} }
CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) { cacheExecutingCode (address, hexCode) {
const codes = this.formatCode(hexCode) const codes = this.formatCode(hexCode)
this.bytecodeByAddress[address] = hexCode this.bytecodeByAddress[address] = hexCode
this.instructionsByAddress[address] = codes.code this.instructionsByAddress[address] = codes.code
@ -33,12 +39,12 @@ CodeResolver.prototype.cacheExecutingCode = function (address, hexCode) {
return this.getExecutingCodeFromCache(address) return this.getExecutingCodeFromCache(address)
} }
CodeResolver.prototype.formatCode = function (hexCode) { formatCode (hexCode) {
const [code, instructionsIndexByBytesOffset] = codeUtils.nameOpCodes(Buffer.from(hexCode.substring(2), 'hex')) const [code, instructionsIndexByBytesOffset] = codeUtils.nameOpCodes(Buffer.from(hexCode.substring(2), 'hex'))
return {code, instructionsIndexByBytesOffset} return {code, instructionsIndexByBytesOffset}
} }
CodeResolver.prototype.getExecutingCodeFromCache = function (address) { getExecutingCodeFromCache (address) {
if (!this.instructionsByAddress[address]) { if (!this.instructionsByAddress[address]) {
return null return null
} }
@ -49,8 +55,7 @@ CodeResolver.prototype.getExecutingCodeFromCache = function (address) {
} }
} }
CodeResolver.prototype.getInstructionIndex = function (address, pc) { getInstructionIndex (address, pc) {
return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc] return this.getExecutingCodeFromCache(address).instructionsIndexByBytesOffset[pc]
} }
}
module.exports = CodeResolver

@ -1,8 +1,7 @@
'use strict' 'use strict'
const opcodes = require('./opcodes') const opcodes = require('./opcodes')
module.exports = { export function nameOpCodes (raw) {
nameOpCodes: function (raw) {
let pushData = '' let pushData = ''
const codeMap = {} const codeMap = {}
const code = [] const code = []
@ -18,19 +17,19 @@ module.exports = {
i += jumpNum i += jumpNum
} }
const data = pushData.toString('hex') !== '' ? ' ' + pushData.toString('hex') : '' const data = pushData.toString() !== '' ? ' ' + pushData.toString() : ''
code.push(this.pad(pc, this.roundLog(raw.length, 10)) + ' ' + curOpCode + data) code.push(this.pad(pc, this.roundLog(raw.length, 10)) + ' ' + curOpCode + data)
pushData = '' pushData = ''
} }
return [ code, codeMap ] return [ code, codeMap ]
}, }
/** /**
* Parses code as a list of integers into a list of objects containing * Parses code as a list of integers into a list of objects containing
* information about the opcode. * information about the opcode.
*/ */
parseCode: function (raw) { export function parseCode (raw) {
const code = [] const code = []
for (let i = 0; i < raw.length; i++) { for (let i = 0; i < raw.length; i++) {
const opcode = opcodes(raw[i], true) const opcode = opcodes(raw[i], true)
@ -48,19 +47,19 @@ module.exports = {
code.push(opcode) code.push(opcode)
} }
return code return code
}, }
pad: function (num, size) { export function pad (num, size) {
let s = num + '' let s = num + ''
while (s.length < size) s = '0' + s while (s.length < size) s = '0' + s
return s return s
}, }
log: function (num, base) { export function log (num, base) {
return Math.log(num) / Math.log(base) return Math.log(num) / Math.log(base)
}, }
roundLog: function (num, base) { export function roundLog (num, base) {
return Math.ceil(this.log(num, base)) return Math.ceil(this.log(num, base))
} }
}

Loading…
Cancel
Save