Eval String from Javascript with alignment of rows and columns?

Does anyone know a way to define a row so that if it (or the function it defines) generates an error, the row and column numbers indicated in the stack trace will be shifted by the amount specified in advance? As an alternative, suppose I want to break a long line of a source into pieces and evaluate them separately, but still get stack traces that look as if the entire line was evaluated at a time. Is there any way to achieve this effect, with the exception of using empty rows and columns? (I need a browser based solution, preferably a cross browser, but I can agree that it works in at least one of the main browsers.)

+7
javascript eval
source share
2 answers

The best solution I have found so far is the preliminary sourceURL directive for each line before its eval'ed, providing it with a marker in the form of a unique file name in the stack trace. Stack traces are then parsed (using the parser stacktracejs component) and adjusted by looking for line offsets associated with markers.

var evalCounter = 0; var lineCounter = 0; var lineOffsetTable = {}; function myEval(code) { lineOffsetTable[evalCounter] = lineCounter; lineCounter += countLines(code); return eval("//# sourceURL=" + (evalCounter++) + "\n" + code); } window.onerror = function(errorMsg, url, lineNumber, column, e) { var stackFrames = ErrorStackParser.parse(e); logStackTrace(stackFrames.map(function(f) { if(f.fileName in lineOffsetTable) f.lineNumber += lineOffsetTable[f.fileName]; return f; })); }; 

Unfortunately, this only works in Firefox at the moment. Chrome refuses to pass the error object to the onerror callback (a problem that only occurs with eval'ed code, oddly enough), and IE ignores the sourceURL directive.

0
source share

I do not think this is possible, because the basic mechanism that is considered working is actually out of date . For security reasons, browsers no longer pass the error object to Javascript.

However, since you are working with a custom programming language that is compiled in Javascript, you know what the structure of the resulting script will be. You can also enter operator counts as a result of Javascript, so you can always find out what has been done in the past. Something like:

 function(1); function(2); function(3); 

can be translated as:

 var __column=0; var __line=0; function(1); __column+=12; function(2); /*__column+=12;*/ __line++; __column=0; function(3); /*__column+=12;*/ __line++; __column=0; 

Where 12 is "function(n);".length . Of course, the resulting code is ugly, but you can enable this behavior with a debug flag or something.

0
source share

All Articles