Skip to content

Commit

Permalink
Fixed Js preprocessor incorrect error line reporting bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebloss committed Mar 7, 2012
1 parent 5bd21be commit 7a6780f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
2 changes: 0 additions & 2 deletions win/gel.exe/Scripting/ActiveScript/ActiveScriptSite.cs
Expand Up @@ -117,8 +117,6 @@ void IActiveScriptSite.OnScriptError(IActiveScriptError scriptError)
uint errLine;
int errCol;
scriptError.GetSourcePosition(out errCtx, out errLine, out errCol);
errLine++;
errCol++;

var errNum = exInfo.scode;
var errLineText = SafeGetSourceLineText(scriptError, errNum);
Expand Down
54 changes: 30 additions & 24 deletions win/gel.exe/Scripting/JScript/JsEngine+JsPreprocSource.cs
Expand Up @@ -44,7 +44,7 @@ public int GetLineSource(int line, out IScriptSource source)
if (line >= item.First && line <= item.Last)
{
source = item.Source;
return line - item.First + item.Offset + 1;
return (line - item.First) + item.Offset;
}
}
source = _source;
Expand All @@ -63,8 +63,14 @@ public System.IO.TextReader GetReader()
class LineSource
{
public LineSource(IScriptSource source)
: this(source, 0) { }

public LineSource(IScriptSource source, int offset)
{
_Source = source;
First = -1;
Last = -1;
Offset = offset;
}

/// <summary>
Expand Down Expand Up @@ -113,19 +119,28 @@ string Process()
var map = new List<LineSource>();
int count = 0;

Process(_source, sb, map, 0, ref count);
Process(_source, sb, map, 0, ref count, null);
_lineSrcMap = map;
_processedCode = sb.ToString();

return _processedCode;
}

static void Process(IScriptSource source, StringBuilder sb, IList<LineSource> map, int level, ref int count)
static void Add(LineSource chunk, ref int count, ref int localCount)
{
var chunk = new LineSource(source);
chunk.First = count + 1;
if (chunk.First < 0)
chunk.First = count;
chunk.Last = count;

count++;
localCount++;
}

static void Process(IScriptSource source, StringBuilder sb, List<LineSource> map, int level, ref int count, string includeArg)
{
var chunk = new LineSource(source);
var localCount = 0;

var indent = (string)null;
var lines = source.AsEnumerableLines();

Expand All @@ -136,19 +151,17 @@ static void Process(IScriptSource source, StringBuilder sb, IList<LineSource> ma
if (line.Length < IncLexMinLen ||
!line.StartsWith(IncLex))
{
chunk.Last++;
count++;
sb.AppendLine(line);
Add(chunk, ref count, ref localCount);
continue;
}
/// Get the argument portion of the include statement.
/// If none can be found, just append the line.
var argEnd = line.IndexOfAny(new[] { '>', '"' }, IncLexLen + 1);
if (argEnd < IncLexLen)
{
chunk.Last++;
count++;
sb.AppendLine(line);
Add(chunk, ref count, ref localCount);
continue;
}
var arg = line.Substring(IncLexLen + 1, argEnd - IncLexLen - 1);
Expand All @@ -163,39 +176,32 @@ static void Process(IScriptSource source, StringBuilder sb, IList<LineSource> ma
case '<':
/// Embedded Path.
indent = indent ?? new String(' ', level);
chunk.Last++;
count++;
sb.AppendLine(String.Format("{0}// <{1}> include BEGIN", indent, arg));
Add(chunk, ref count, ref localCount);

map.Add(chunk);

// New chunk of source.
int offset = count - chunk.First + 1;
chunk = new LineSource(source);
chunk.Offset = offset;

var efileSrc = new ScriptEmbedded(arg);
Process(efileSrc, sb, map, level + 1, ref count);
sb.AppendLine(String.Format("{0}// <{1}> include END", indent, arg));
Process(efileSrc, sb, map, level + 1, ref count, arg);

chunk.First = count + 1;
chunk.Last = count;
// New chunk of source.
chunk = new LineSource(source, localCount);

break;
case '"':
/// TODO: Preprocess Include File Path.
throw new NotImplementedException();
default:
chunk.Last++;
count++;
sb.AppendLine(line);
Add(chunk, ref count, ref localCount);
break;
}
}
if (level > 0)
{
chunk.Last++;
count++;
indent = new String(' ', level - 1);
sb.AppendLine(String.Format("{0}// <{1}> include END", indent, includeArg));
Add(chunk, ref count, ref localCount);
}
map.Add(chunk);
}
Expand Down
16 changes: 13 additions & 3 deletions win/gel.exe/Scripting/JScript/JsEngine.cs
Expand Up @@ -115,21 +115,31 @@ static void DefaultExceptionHandler(object source, UnhandledExceptionEventArgs e

protected override int GetLineSource(int sourceId, int sourceLine, ref int sourceCol, ref string sourceText, ref string path)
{
// TODO: If sourceLine == 0, get line zero of source (via sourceId). If
// line zero starts with a "magic string" such as a call to a module
// loader function, adjust the sourceCol to hide the magic string.

// Change the reported column from zero to one based.
sourceCol++;

/// Find the <see cref="IScriptSource">source</see> for the given line,
/// set the path argument from it, change the reported line from zero
/// to one based and return.
if (sourceId == 0)
{
path = "(global)";
return sourceLine;
return sourceLine + 1;
}
IScriptSource src;
if (!_sourceById.TryGetValue(sourceId, out src))
{
return sourceLine;
return sourceLine + 1;
}
IScriptSource src2;
int srcLine = src.GetLineSource(sourceLine, out src2);
if (src2 != null && src2.Path != null)
path = src2.Path;
return srcLine;
return srcLine + 1;
}

#endregion
Expand Down

0 comments on commit 7a6780f

Please sign in to comment.