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

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

Loading…
Cancel
Save