Skip to content

Commit 486413c

Browse files
committed
Make sure the line numbers sync up.
1 parent d680f5e commit 486413c

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tools/source-maps/sourcemapper.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
var fs = require('fs');
66
var path = require('path');
77

8+
var START_MARKER = '// EMSCRIPTEN_START_FUNCS\n';
9+
var END_MARKER = '// EMSCRIPTEN_END_FUNCS\n';
10+
811
function countLines(s) {
912
var count = 0;
1013
for (var i = 0, l = s.length; i < l; i ++) {
@@ -26,14 +29,13 @@ function countLines(s) {
2629
function extractComments(source, commentHandler) {
2730
var state = 'code';
2831
var commentContent = '';
29-
var functionStartIdx = source.indexOf('// EMSCRIPTEN_START_FUNCS');
30-
var functionEndIdx = source.lastIndexOf('// EMSCRIPTEN_END_FUNCS');
31-
var lineCount = countLines(source.slice(0, functionStartIdx));
32+
var functionStartIdx = source.indexOf(START_MARKER);
33+
var functionEndIdx = source.lastIndexOf(END_MARKER);
34+
var lineCount = countLines(source.slice(0, functionStartIdx)) + 2;
3235

33-
for (var i = functionStartIdx; i < functionEndIdx; i++) {
36+
for (var i = functionStartIdx + START_MARKER.length; i < functionEndIdx; i++) {
3437
var c = source[i];
3538
var nextC = source[i+1];
36-
if (c === '\n') lineCount++;
3739
switch (state) {
3840
case 'code':
3941
if (c === '/') {
@@ -62,7 +64,9 @@ function extractComments(source, commentHandler) {
6264
if (c === '\\') i++;
6365
else if (c === '"') state = 'code';
6466
break;
65-
}
67+
}
68+
69+
if (c === '\n') lineCount++;
6670
}
6771
}
6872

@@ -153,9 +157,9 @@ if (require.main === module) {
153157
var newMappings = {};
154158
// uglify processes the code between EMSCRIPTEN_START_FUNCS and
155159
// EMSCRIPTEN_END_FUNCS, so its line number maps are relative to those
156-
// markers. we correct for that here.
157-
var startFuncsLineNumber = countLines(
158-
source.slice(0, source.indexOf('// EMSCRIPTEN_START_FUNCS'))) + 2;
160+
// markers. we correct for that here. +2 = 1 for the newline in the marker
161+
// and 1 to make it a 1-based index.
162+
var startFuncsLineNumber = countLines(source.slice(0, source.indexOf(START_MARKER))) + 2;
159163
for (var line in optimizedMappings) {
160164
var originalLineNumber = optimizedMappings[line].originalLineNumber + startFuncsLineNumber;
161165
if (originalLineNumber in mappings) {

0 commit comments

Comments
 (0)