debugger_manager.js
1 var RemixDebug = require('remix-debug-debugtest'); 2 var CmdLine = RemixDebug.CmdLine; 3 const async = require('async'); 4 5 class DebuggerManager { 6 7 constructor(nodeUrl) { 8 this.nodeUrl = nodeUrl 9 this.outputJson = {} 10 this.inputJson = {} 11 } 12 13 setInputJson(inputJson) { 14 this.inputJson = inputJson 15 } 16 17 setOutputJson(outputJson) { 18 this.outputJson = outputJson 19 } 20 21 createDebuggerSession(txHash, filename, cb) { 22 return this.debug(txHash, filename, cb) 23 } 24 25 debug(txHash, filename, cb) { 26 console.dir("debugging tx " + txHash) 27 28 var cmd_line = new CmdLine() 29 this.cmd_line = cmd_line 30 cmd_line.connect("http", this.nodeUrl) 31 cmd_line.loadCompilationData(this.inputJson, this.outputJson) 32 33 cmd_line.initDebugger(() => { 34 this.isDebugging = true 35 36 cmd_line.startDebug(txHash, filename, () => { 37 if (cb) { 38 cmd_line.triggerSourceUpdate() 39 cb() 40 } 41 }) 42 }) 43 return cmd_line 44 } 45 46 getLastLine(txHash, filename, outputCb) { 47 const self = this; 48 let cmd_line = new CmdLine() 49 50 async.waterfall([ 51 function initDebugger(next) { 52 cmd_line = new CmdLine() 53 cmd_line.connect("http", self.nodeUrl) 54 cmd_line.loadCompilationData(self.inputJson, self.outputJson) 55 cmd_line.initDebugger(() => { 56 // self.isDebugging = true 57 next() 58 }) 59 }, 60 function startDebug(next) { 61 let debuggerData = {} 62 cmd_line.events.on("locals", (data) => { 63 debuggerData.locals = self.simplifyDebuggerVars(data) 64 }); 65 66 cmd_line.events.on("globals", (data) => { 67 debuggerData.contract = self.simplifyDebuggerVars(data) 68 }) 69 70 cmd_line.startDebug(txHash, filename, () => { 71 cmd_line.events.on("source", () => { 72 let lines = cmd_line.getSource() 73 // TODO: this is a bit of a hack 74 let line = lines.filter((x) => x.indexOf("=>") === 0)[0]; 75 outputCb(lines, line, debuggerData) 76 }) 77 78 let total_size = cmd_line.getTraceLength() 79 cmd_line.jumpTo(total_size - 1) 80 cmd_line.unload() 81 82 next() 83 }) 84 } 85 ], () => { 86 }) 87 } 88 89 // TODO: this is duplicated in debugger/index.js 90 simplifyDebuggerVars(data) { 91 let new_data = {}; 92 93 for (let key in data) { 94 let field = data[key]; 95 new_data[`${key} (${field.type})`] = field.value 96 } 97 98 return new_data 99 } 100 101 } 102 103 module.exports = DebuggerManager;